It was three years ago. I started playing with MicroPython with camera support on an ESP32-Camera board with MicroPython version 1.11-XXX.
This time, the firmware support version 1.18-XXX. We may reach version 2.00-XXX by the end of 2022. The MicroPython team has been busy.
The build process and structure have changed since version 1.11-XXX.
This firmware is made especially for the ESP32-Camera board and was cleanly compiled with Espressif IDF version 4.4. It supports the OV2640 camera and PSRAM. To reduce size, webrepl, BLE, help modules were not included in the firmware.
I wish to reach a wider audience. So, I will try to make this as simple as possible. To join the fun, these are what you need to do:
- Download MicroPython firmware file firmware.bin
- Download script files help.py, html.py, site.py, webcam.py, and wifi.py
- Flash the firmware.bin to your ESP32-Camera board
- Save the five script files onto the flash file system of your ESP32-Camera board.
- Install thonny Python IDE from https://thonny.org
The thonny IDE helps beginners flash new firmware and upload script files to a development board.
To flash a new MicroPython firmware, these are what you need to:
- Connect GPIO-00 and GND pins.
- Connect your ESP32-Camera board to an FTDI USB UART adapter. Remember to cross TX and RX wires between the board and adapter.
- Connect the FTDI adapter to a USB port of your PC.
- Start thonny IDE
- Click Tools --> Options --> Interpreter, choose MicroPython (ESP32). For Port, choose "Try to detect port automatically"
- Click the "Install or update firmware" link. Click Reload and choose Port value, for example /dev/ttyUSB0". Click [Browse...] and choose the firmware file. Click Install. Click Close when Done!
- Remove the wire between GPIO-00 and GND pins and push the RST button on the development board.
Hopefully, you will get MicroPython REPL in the thonny IDE shell window.
MicroPython v1.18-610-gcf7d962cf-kaki5 on 2022-06-10; ESP32 CAMERA module (KAKI5) with ESP32
>>>
We can test the camera module in the REPL
>>> import camera
>>> camera.init()
True
>>> img=camera.capture()
>>> len(img)
58810
>>>
Congratulation. We now have a working MicroPython with the camera module.
We will now upload the five script files onto the flash file system using thonny IDE. Click [Open...] to load each script file into the IDE. You should have all five files in different TAB windows. You need to edit the 'wifi.py' file. Change the "YOUR-SSID" and "YOUR-PWD" to the values of your home WiFi.
Save all the five files to "MicroPython device". Do this for each script file. Click File --> Save as... and choose MicroPython device as destination. Make sure to write the correct file name.
We can check whether all five files are in the flash storage using the REPL.
>>> uos.listdir()
['boot.py', 'help.py', 'html.py', 'site.py', 'webcam.py', 'wifi.py']
>>>
We are now ready to try our webcam server. In the REPL:
>>> execfile('webcam.py')
Camera ready?: True
Camera ready
Waiting ...
Waiting ...
Connected to dlink-3530
network config: ('10.0.0.46', '255.255.255.0', '10.0.0.138', '148.122.164.253')
PWD: M1XDYKh3
Start server 80
Try - http://10.0.0.46/login/M1XDYKh3
Your output will differ from what is shown here. Open a browser and go to the login URL. You will get "OK!" if successful. Now, go to the 'root' page, for example, http://10.0.0.46. You will receive a 'help' page.
Once login, the server is locked to the IP of the PC. Requests from other PCs are not allowed. To remove the lock, you need to logout, for example, http://10.0.0.46/logout. The server will create a new random login password. You will see the new password in the REPL.
('10.0.0.72', 37146) ['GET', '/logout']
New PWD: IT0XfQOQ
You need to use the new password to login again. You can disable the authentication scheme by 'auth.on=False' in webcam.py.
After login, you can try:
- Rotate the image in the browser. http://10.0.0.46/rot/90
- Take a photo and show it in the browser. http://10.0.0.46/snap
- Live streaming to the browser. http://10.0.0.46/webcam
- Get help page. http://10.0.0.46
Please change the IP to your server IP. For more information, please read the help page.
Our webcam server provides us with microservices to control the camera and capture images. This means, on a linux PC, we can use wget or curl to request a service.
It is super easy to make timelapse video on a linux PC:
- login
wget http://10.0.0.46/login/M1XDYKh3
-OR-
curl http://10.0.0.46/login/M1XDYKh3
- get a still photo
wget http://10.0.0.46/foto -o foto1.jpeg
-OR-
curl http://10.0.0.46/foto --output foto2.jpeg
- a simple loop in a bash script will take an image every 30sec
#!/bin/bash
cnt=0
while :
do
let "cnt=cnt+1"
N=$( printf '%04d' $cnt )
curl http://10.0.0.87/foto --output foto$N.jpeg
sleep 30
done
- run the get_foto.sh script
/get_foto.sh 2>/dev/null &
[1] 34394
- kill it whenever you are done
kill -9 34394
[1]+ Killed ./get_foto.sh 2> /dev/null
- rotate the photos if needed using convert in a script rot_foto.sh, try different rotate value (-90, 90)
#!/bin/bash
for f in `ls foto*.jpeg`
do
echo "Rotate $f"
convert $f -rotate 90 $f
done
- from foto*.jpeg make timelapse tmlap.mp4 using ffmpeg
cat foto*.jpeg | ffmpeg -f image2pipe -r 1 -vcodec mjpeg -i - -vcodec libx264 tmlap.mp4
- you can use curl to record live video too
curl http://10.0.0.87/live --output live.avi
<ctrl>+c to kill curl
- use celluloid to show the videos
celluloid tmlap.mp4
celluloid live.avi
You can now live stream video from your ESP32-Camera board at your front gate.
You can also do other projects with your ESP32-Camera board now that you have camera-enabled MicroPython firmware.
Good luck.