Example explanation 14-WiFi-Shield->mjpeg_streamer video stream
import sensor
import time
import network
import socket
SSID = ""
KEY = ""
HOST = ""
PORT = 8080
sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)
while not wlan.isconnected():
print('Trying to connect to "{:s}"...'.format(SSID))
time.sleep_ms(1000)
print("WiFi Connected ", wlan.ifconfig())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
s.bind([HOST, PORT])
s.listen(5)
s.setblocking(True)
def start_streaming(s):
print("Waiting for connections..")
client, addr = s.accept()
client.settimeout(5.0)
print("Connected to " + addr[0] + ":" + str(addr[1]))
data = client.recv(1024)
client.sendall(
"HTTP/1.1 200 OK\r\n"
"Server: OpenMV\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n"
"Cache-Control: no-cache\r\n"
"Pragma: no-cache\r\n\r\n"
)
clock = time.clock()
while True:
clock.tick()
frame = sensor.snapshot()
cframe = frame.compressed(quality=35)
header = (
"\r\n--openmv\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length:" + str(cframe.size()) + "\r\n\r\n"
)
client.sendall(header)
client.sendall(cframe)
print(clock.fps())
while True:
try:
start_streaming(s)
except OSError as e:
print("socket error: ", e)