一、取得固件
办法1:克隆源码,自己编译固件
clone
git clone https://github.com/tsaarni/micropython-with-esp32-cam.git micropython
build
cd micropython/mpy-cross
make
cd ../ports/esp32
git submodule update --init
make V=1 SDKCONFIG=boards/sdkconfig.esp32cam -j
办法2:直接下载编译好的固件。
注:micropython的官方固件 并不支撑cam
二、usbttl与esp32 cam连接:
USB UART | ESP32-CAM |
---|---|
TX | U0R |
RX | U0T |
DTR | IO0 |
5V | 5V |
GND | GND |
三、使用thonny烧录:
烧录结束后,记得把链接帽(IO0 GNO)去掉。
这样,就进入了调试界面:
接下来,新建文件,保存在esp32cam设备中,文件名为main.py
import usocket as socket
import network
import esp
import camera
import gc
esp.osdebug(None)
gc.collect()
ssid = 'wifi'
password = '123456'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
print('Content = %s' % request)
on = request.find('/capture')
if on == 6:
camera.init()
buf = camera.capture()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: image/jpg\n')
conn.send('Connection: close\n\n')
conn.sendall(buf)
camera.deinit()
conn.close()
else:
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall("hello")
conn.close()
翻开浏览器,输入该设备的ip:
访问:
http://192.168.101.197/capture
把 192.168.101.197 替换成你的 esp32cam地址
更新版:添加闪光灯
import usocket as socket
import network
import esp
import camera
import gc
from machine import Pin
esp.osdebug(None)
gc.collect()
ssid = 'wifi'
password = '123456'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
light = Pin(4,Pin.OUT)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
print('Content = %s' % request)
on = request.find('/capture')
on_light = request.find('/light')
buf = []
out = "hello"
if on == 6:
camera.init()
buf = camera.capture()
camera.deinit()
if on_light == 6:
camera.init()
light.on()
buf = camera.capture()
camera.deinit()
conn.send('HTTP/1.1 200 OK\n')
if len(buf)>0:
conn.send('Content-Type: image/jpg\n')
out = buf
else:
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(out)
conn.close()
light.off()
gc.collect()
闪光灯摄影
http://192.168.101.197/light
把 192.168.101.197 替换成你的 esp32cam地址
参阅: lemariva.com/blog/2019/0…
github.com/tsaarni/esp…
github.com/tsaarni/esp…
randomnerdtutorials.com/esp32-esp82…