wifi communication

Video tutorial 12 - WIFI wireless image transmission: https://singtown.com/learn/50005/

Video Tutorial 39 - WIFI Programming for OpenMV Remote Debugging: https://singtown.com/learn/50899/

Video tutorial 30 - Wired image transmission expansion board: https://singtown.com/learn/50521/

Video tutorial 31 - Wireless image transmission expansion board: https://singtown.com/learn/50535/

OpenMV official expansion board uses ATWINC1500 module, which can transmit images.

Detailed information:http://singtown.cc/product/openmv-wifi扩展板/\ If tend to use ESP8266, use serial communication is okay.

wifi scanning

wireless transmission of images

wireless transmission of small ball coordinates

import sensor
import time
import network
import socket
import json

SSID ='OPENMV_AP'    # Network SSID
KEY  ='1234567890'    # Network key (must be 10 chars)
HOST = ''           # Use first available interface
PORT = 8080         # Arbitrary non-privileged port

green_threshold   = (   0,   80,  -70,   -10,   -0,   30)

# Reset sensor
sensor.reset()
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

# Init wlan module in AP mode.
wlan = network.WLAN(network.AP_IF)
wlan.config(ssid=SSID, key=KEY, channel=2)
wlan.active(True)


print("AP mode started. SSID: {} IP: {}".format(SSID, wlan.ifconfig()[0]))

# You can block waiting for client to connect
# print(wlan.wait_for_sta(100000))

def response(client):
    # 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: application/json\r\n" \
                "Cache-Control: no-cache\r\n" \
                "Pragma: no-cache\r\n\r\n")

    # FPS clock
    clock = time.clock()

    # Start streaming images
    # NOTE: Disable IDE preview to increase streaming FPS.

    img = sensor.snapshot()
    blobs = img.find_blobs([green_threshold])
    if blobs:
        for b in blobs:
            img.draw_rectangle(b[0:4]) # rect
            img.draw_cross(b[5], b[6]) # cx, cy

    client.send(json.dumps(blobs))
    client.close()

server = None

while True:
    if server is None:
        # Create server socket
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
        # Bind and listen
        server.bind([HOST, PORT])
        server.listen(5)
        # Set server socket to blocking
        server.setblocking(True)

    try:
        print("Waiting for connections..")
        client, addr = server.accept()
    except OSError as e:
        server.close()
        server = None
        print("server socket error:", e)
        continue

    try:
        # set client socket timeout to 2s
        client.settimeout(5.0)
        print("Connected to " + addr[0] + ":" + str(addr[1]))
        response(client)
    except OSError as e:
        client.close()
        print("client socket error:", e)
        # sys.print_exception(e)

Similar to transferring images, enter IP:PORT in the browser, and the IP can be seen in the terminal. If it is a Wi-Fi expansion board: 192.168.1.1:8080 If it is OpenMV RT: 192.168.4.1:8080, you can get json data

If you want to get the data in your own code, just GET the URL and you will get JSON.

For example, python:

import requests
r = requests.get('192.168.1.1:8080')

transmit only one image

import sensor
import time
import network
import socket
import json

SSID ='OPENMV_AP'    # Network SSID
KEY  ='1234567890'    # Network key (must be 10 chars)
HOST = ''           # Use first available interface
PORT = 8080         # Arbitrary non-privileged port

green_threshold   = (   0,   80,  -70,   -10,   -0,   30)

# Reset sensor
sensor.reset()
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

# Init wlan module in AP mode.
wlan = network.WLAN(network.AP_IF)
wlan.config(ssid=SSID, key=KEY, channel=2)
wlan.active(True)


print("AP mode started. SSID: {} IP: {}".format(SSID, wlan.ifconfig()[0]))

# You can block waiting for client to connect
# print(wlan.wait_for_sta(100000))

def response(client):
    # Read request from client
    data = client.recv(1024)
    # Should parse client request here

    # Send jpeg header
    client.send("HTTP/1.1 200 OK\r\n" \
                    "Server: OpenMV\r\n" \
                    "Content-Type: image/jpeg\r\n\r\n")

    frame = sensor.snapshot()
    cframe = frame.compressed(quality=35)
    client.send(cframe)
    client.close()

server = None

while True:
    if server is None:
        # Create server socket
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
        # Bind and listen
        server.bind([HOST, PORT])
        server.listen(5)
        # Set server socket to blocking
        server.setblocking(True)

    try:
        print("Waiting for connections..")
        client, addr = server.accept()
    except OSError as e:
        server.close()
        server = None
        print("server socket error:", e)
        continue

    try:
        # set client socket timeout to 2s
        client.settimeout(5.0)
        print("Connected to " + addr[0] + ":" + str(addr[1]))
        response(client)
    except OSError as e:
        client.close()
        print("client socket error:", e)
        # sys.print_exception(e)

If it is a Wi-Fi expansion board: enter 192.168.1.1:8080 in the browser. If it is an OpenMV RT: enter 192.168.4.1:8080 in the browser. You can view a picture.

results matching ""

    No results matching ""