Advertisement
Guest User

MY SOURCE CODE

a guest
Mar 7th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. SSID=''      # Network SSID
  2. KEY=''       # Network key
  3. HOST =''     # Use first available interface
  4. PORT = 8080  # Arbitrary non-privileged port
  5.  
  6. # Init sensor
  7. sensor.reset()
  8. sensor.set_framesize(sensor.VGA)
  9. sensor.set_pixformat(sensor.RGB565)
  10.  
  11. # Init wlan module and connect to network
  12. print("Trying to connect... (This may take a while)...")
  13. wlan = network.WLAN(network.STA_IF)
  14. wlan.deinit()
  15. wlan.active(True)
  16. wlan.connect(SSID, KEY, timeout=30000)
  17.  
  18. # We should have a valid IP now via DHCP
  19. print("WiFi Connected ", wlan.ifconfig())
  20.  
  21. # Create server socket
  22. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
  24.  
  25. # Bind and listen
  26. s.bind([HOST, PORT])
  27. s.listen(5)
  28.  
  29. # Set server socket to blocking
  30. s.setblocking(True)
  31.  
  32. def start_streaming(s):
  33.     print ('Waiting for connections..')
  34.     client, addr = s.accept()
  35.     # set client socket timeout to 5s
  36.     client.settimeout(5.0)
  37.     print ('Connected to ' + addr[0] + ':' + str(addr[1]))
  38.  
  39.     # Read request from client
  40.     data = client.recv(1024)
  41.     # Should parse client request here
  42.  
  43.     # Send multipart header
  44.     client.sendall("HTTP/1.1 200 OK\r\n" \
  45.                 "Server: OpenMV\r\n" \
  46.                 "Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \
  47.                 "Cache-Control: no-cache\r\n" \
  48.                 "Pragma: no-cache\r\n\r\n")
  49.  
  50.     # FPS clock
  51.     clock = time.clock()
  52.  
  53.     # Start streaming images
  54.     # NOTE: Disable IDE preview to increase streaming FPS.
  55.     while (True):
  56.         clock.tick() # Track elapsed milliseconds between snapshots().
  57.         frame = sensor.snapshot()
  58.         cframe = frame.compressed(quality=35)
  59.         header = "\r\n--openmv\r\n" \
  60.                  "Content-Type: image/jpeg\r\n"\
  61.                  "Content-Length:"+str(cframe.size())+"\r\n\r\n"
  62.         client.sendall(header)
  63.         client.sendall(cframe)
  64.         print(clock.fps())
  65.  
  66. while (True):
  67.     try:
  68.         start_streaming(s)
  69.     except OSError as e:
  70.         print("socket error: ", e)
  71.         #sys.print_exception(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement