# MJPEG Streaming
#
# This example shows off how to do MJPEG streaming to a FIREFOX webrowser
# (IE and Chrome do not work). Just input your network SSID and KEY and then
# connect to the IP address/port printed out from ifconfig.

import sensor, image, time, network, usocket

SSID=''     # Network SSID
KEY=''      # Network key
HOST = ''   # Use first available interface
PORT = 8000 # Arbitrary non-privileged port

# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)
sensor.set_brightness(1)
sensor.set_saturation(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)

# We should have a valid IP now via DHCP
print(wlan.ifconfig())

# Create server socket
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)

# Bind and listen
s.bind([HOST, PORT])
s.listen(5)

# Set timeout to 1s
s.settimeout(1.0)

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")

# FPS clock
clock = time.clock()
# Start streaming images
while (True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    frame = sensor.snapshot()
    cframe = frame.compressed(quality=90)
    client.send("\r\n--openmv\r\n" \
                "Content-Type: image/jpeg\r\n"\
                "Content-Length:"+str(cframe.size())+"\r\n\r\n")
    client.send(cframe)
    print(clock.fps())
client.close()

@@ -269,16 +269,21 @@
  usocket
+, sys
 %0A%0ASSID='
@@ -382,17 +382,17 @@
 ORT = 80
-0
+8
 0 # Arbi
@@ -599,16 +599,17 @@
 (sensor.
+Q
 QVGA)%0Ase
@@ -1051,16 +1051,44 @@
 t(1.0)%0A%0A
+def start_streaming(s):%0A    
 print ('
@@ -1115,16 +1115,20 @@
 ons..')%0A
+    
 client, 
@@ -1145,16 +1145,20 @@
 ccept()%0A
+    
 print ('
@@ -1205,16 +1205,20 @@
 r%5B1%5D))%0A%0A
+    
 # Read r
@@ -1236,16 +1236,20 @@
  client%0A
+    
 data = c
@@ -1265,17 +1265,20 @@
 v(1024)%0A
-%0A
+    
 # Should
@@ -1305,16 +1305,20 @@
 t here%0A%0A
+    
 # Send m
@@ -1333,16 +1333,20 @@
  header%0A
+    
 client.s
@@ -1385,16 +1385,20 @@
         
+    
 %22Server:
@@ -1416,32 +1416,36 @@
 %22 %5C%0A            
+    
 %22Content-Type: m
@@ -1504,16 +1504,20 @@
         
+    
 %22Cache-C
@@ -1532,32 +1532,36 @@
 no-cache%5Cr%5Cn%22 %5C%0A
+    
             %22Pra
@@ -1585,16 +1585,20 @@
 %5Cr%5Cn%22)%0A%0A
+    
 # FPS cl
@@ -1601,16 +1601,20 @@
 S clock%0A
+    
 clock = 
@@ -1626,16 +1626,21 @@
 clock()%0A
+%0A    
 # Start 
@@ -1656,16 +1656,79 @@
  images%0A
+    # NOTE: Disable IDE preview to increase streaming FPS.%0A    
 while (T
@@ -1733,16 +1733,20 @@
 (True):%0A
+    
     cloc
@@ -1808,16 +1808,20 @@
 ().%0A    
+    
 frame = 
@@ -1834,24 +1834,28 @@
 .snapshot()%0A
+    
     cframe =
@@ -1884,28 +1884,29 @@
 ity=
-90
+35
 )%0A    
-client.send(
+    header = 
 %22%5Cr%5C
@@ -1930,32 +1930,33 @@
                 
+ 
 %22Content-Type: i
@@ -1971,16 +1971,17 @@
 g%5Cr%5Cn%22%5C%0A
+ 
         
@@ -2039,34 +2039,145 @@
 r%5Cn%22
-)%0A    client.send(cframe)%0A
+%0A        client.send(header)%0A        client.send(cframe)%0A%0Awhile (True):%0A    try:%0A        start_streaming(s)%0A    except OSError as e:%0A    
     
@@ -2186,32 +2186,57 @@
 int(
-clock.fps())%0Aclient.close(
+%22socket error: %22, e)%0A        #sys.print_exception(e
 )%0A

Update mjpeg streaming example.