'''
    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()

@@ -46,43 +46,43 @@
 ort 
-wlan, socket%0Aimport led,time,sensor
+time, sensor, pyb, network, usocket
 %0A%0ASS
@@ -244,72 +244,77 @@
 %0Aled
-.off(led.RED)%0Aled.off(led.BLUE)%0Aled.on(led.GREEN
+_r = pyb.LED(1)%0Aled_b = pyb.LED(2)%0Aled_g = pyb.LED(3
 )%0A%0A# 
-Ini
+Rese
 t sensor
  and
@@ -313,48 +313,68 @@
 nsor
- and set sensor parameters%0Asensor.rese
+%0Asensor.reset()%0A%0A# Set sensor settings%0Asensor.set_contras
 t(
+1
 )%0Ase
@@ -397,10 +397,9 @@
 ess(
--2
+1
 )%0Ase
@@ -403,34 +403,63 @@
 %0Asensor.set_
-contrast
+saturation(1)%0Asensor.set_gainceiling
 (1
+6
 )%0Asensor.set
@@ -510,20 +510,22 @@
 (sensor.
-JPEG
+RGB565
 )%0A%0A# Ini
@@ -569,13 +569,23 @@
 wlan
-.init
+ = network.WINC
 ()%0Aw
@@ -606,260 +606,39 @@
 ID, 
-sec=wlan.WPA2, key=KEY)%0Aled.off(led.GREEN)%0A%0A# Wait for connection to be established%0Awhile (True):%0A    led.toggle(led.BLUE)%0A    time.sleep(250)%0A    led.toggle(led.BLUE)%0A    time.sleep(250)%0A    if wlan.connected():%0A        led.on(led.BLUE)%0A        break;
+key=KEY, security=wlan.WPA_PSK)
 %0A%0A# 
@@ -676,16 +676,22 @@
 ia DHCP%0A
+print(
 wlan.ifc
@@ -697,16 +697,17 @@
 config()
+)
 %0A%0A# Crea
@@ -727,16 +727,17 @@
 ket%0As = 
+u
 socket.s
@@ -742,16 +742,17 @@
 .socket(
+u
 socket.A
@@ -759,16 +759,17 @@
 F_INET, 
+u
 socket.S
@@ -782,79 +782,8 @@
 REAM
-, socket.IPPROTO_TCP)%0A%0A# Set socket in blocking mode%0As.setblocking(True
 )%0A%0A#
@@ -833,16 +833,55 @@
 ten(5)%0A%0A
+# Set timeout to 1s%0As.settimeout(1.0)%0A%0A
 print ('
@@ -1580,16 +1580,29 @@
 nd(frame
+.compress(50)
 )%0A    ti
@@ -1610,17 +1610,17 @@
 e.sleep(
-3
+1
 0)%0A%0Aclie

Update MJPEG streaming examples.