openmv/usr/examples/14-WiFi-Shield/mjpeg_streamer.py
Kwabena W. Agyeman 94bc225542 Moved examples arround.
Tried to emulate Arduino's 11 folders... I'd perfer to have all the
shield scripts in one folder... but, that might not make sense. I don't
really want one script per folder however. So, I might merge some more
stuff in the future. I have a grand idea here that will become evident as
I work though the examples.

Anyway, the current structure is not final. It will be in flux for a
little while.

As for Git History, folder history is the best we're going to get. Git
and GitHub don't seem to deal with moves too well.
2016-03-30 21:30:11 -04:00

77 lines
1.8 KiB
Python

'''
Simple MJPEG streaming server
'''
import wlan, socket
import led,time,sensor
SSID='' # Network SSID
KEY='' # Network key
HOST = '' # Use first available interface
PORT = 8000 # Arbitrary non-privileged port
led.off(led.RED)
led.off(led.BLUE)
led.on(led.GREEN)
# Init sensor and set sensor parameters
sensor.reset()
sensor.set_brightness(-2)
sensor.set_contrast(1)
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.JPEG)
# Init wlan module and connect to network
wlan.init()
wlan.connect(SSID, sec=wlan.WPA2, key=KEY)
led.off(led.GREEN)
# Wait for connection to be established
while (True):
led.toggle(led.BLUE)
time.sleep(250)
led.toggle(led.BLUE)
time.sleep(250)
if wlan.connected():
led.on(led.BLUE)
break;
# We should have a valid IP now via DHCP
wlan.ifconfig()
# Create server socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
# Set socket in blocking mode
s.setblocking(True)
# Bind and listen
s.bind((HOST, PORT))
s.listen(5)
print ('Waiting for connections..')
client, addr = s.accept()
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
# Read request from client
data = client.recv(1024)
# Should parse client request here
# Send multipart header
client.send("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")
# Start streaming images
while (True):
frame = sensor.snapshot()
client.send("\r\n--openmv\r\n" \
"Content-Type: image/jpeg\r\n"\
"Content-Length:"+str(frame.size())+"\r\n\r\n")
client.send(frame)
time.sleep(30)
client.close()