From cc3e4f1522a7d7211173005dbca65939fb12ab6f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sat, 13 Feb 2021 21:24:33 +0200 Subject: [PATCH] Add Portenta WiFi examples. --- .../Arduino/Portenta-H7/40-WiFi/connect.py | 19 +++++ .../Arduino/Portenta-H7/40-WiFi/dns.py | 20 +++++ .../Portenta-H7/40-WiFi/http_client.py | 39 ++++++++++ .../Portenta-H7/40-WiFi/http_client_ssl.py | 49 ++++++++++++ .../Portenta-H7/40-WiFi/mjpeg_streamer.py | 78 +++++++++++++++++++ .../Arduino/Portenta-H7/40-WiFi/mqtt_pub.py | 32 ++++++++ .../Arduino/Portenta-H7/40-WiFi/mqtt_sub.py | 39 ++++++++++ .../Arduino/Portenta-H7/40-WiFi/ntp.py | 33 ++++++++ .../Arduino/Portenta-H7/40-WiFi/scan.py | 16 ++++ .../Arduino/Portenta-H7/40-WiFi/static_ip.py | 33 ++++++++ 10 files changed, 358 insertions(+) create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/connect.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/dns.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client_ssl.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/mjpeg_streamer.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_pub.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_sub.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/ntp.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/scan.py create mode 100644 scripts/examples/Arduino/Portenta-H7/40-WiFi/static_ip.py diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/connect.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/connect.py new file mode 100644 index 000000000..64620226a --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/connect.py @@ -0,0 +1,19 @@ +# Connect Example +# +# This example shows how to connect your OpenMV Cam with a WiFi shield to the net. + +import network + +SSID='' # Network SSID +KEY='' # Network key + +# Init wlan module and connect to network +print("Trying to connect. Note this may take a while...") + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/dns.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/dns.py new file mode 100644 index 000000000..ffd489207 --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/dns.py @@ -0,0 +1,20 @@ +# DNS Example +# +# This example shows how to get the IP address for websites via DNS. + +import network, usocket + +SSID='' # Network SSID +KEY='' # Network key + +# Init wlan module and connect to network +print("Trying to connect. Note this may take a while...") + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) +print(usocket.getaddrinfo("www.google.com", 80)[0][4]) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client.py new file mode 100644 index 000000000..7fbd536fd --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client.py @@ -0,0 +1,39 @@ +# Simple HTTP client example. + +import network, socket + +# AP info +SSID='' # Network SSID +KEY='' # Network key + +PORT = 80 +HOST = "www.google.com" + +# Init wlan module and connect to network +print("Trying to connect. Note this may take a while...") + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) + +# Get addr info via DNS +addr = socket.getaddrinfo(HOST, PORT)[0][4] +print(addr) + +# Create a new socket and connect to addr +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect(addr) + +# Set timeout +client.settimeout(3.0) + +# Send HTTP request and recv response +client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST)) +print(client.recv(1024)) + +# Close socket +client.close() diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client_ssl.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client_ssl.py new file mode 100644 index 000000000..98e0f903e --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/http_client_ssl.py @@ -0,0 +1,49 @@ +# Simple HTTPS client example. +import network, socket, ussl + +# AP info +SSID='' # Network SSID +KEY='' # Network key + +PORT = 443 +HOST = "www.google.com" + +# Init wlan module and connect to network +print("Trying to connect. Note this may take a while...") + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) + +# Get addr info via DNS +addr = socket.getaddrinfo(HOST, PORT)[0][4] +print(addr) + +# Create a new socket and connect to addr +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +client.connect(addr) + +# Set timeout +client.settimeout(3.0) + +client = ussl.wrap_socket(client, server_hostname=HOST) + +# Send HTTP request and recv response +request = "GET / HTTP/1.1\r\n" +request += "HOST: %s\r\n" +request += "User-Agent: Mozilla/5.0\r\n" +request += "Connection: keep-alive\r\n\r\n" +# Add more headers if needed. +client.write(request%(HOST)+"\r\n") + +response = client.read(1024) +for l in response.split(b"\r\n"): + print(l.decode()) + +# Close socket +client.close() diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/mjpeg_streamer.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/mjpeg_streamer.py new file mode 100644 index 000000000..597710881 --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/mjpeg_streamer.py @@ -0,0 +1,78 @@ +# MJPEG Streaming +# +# This example shows off how to do MJPEG streaming to a FIREFOX webrowser +# Chrome, Firefox and MJpegViewer App on Android have been tested. +# Connect to the IP address/port printed out from ifconfig to view the stream. +import sensor, image, time, network, socket, sys + +SSID='' # Network SSID +KEY='' # Network key +HOST ='' # Use first available interface +PORT = 8080 # Arbitrary non-privileged port + +# Init sensor +sensor.reset() +sensor.set_framesize(sensor.QVGA) +sensor.set_pixformat(sensor.GRAYSCALE) + +# Init wlan module and connect to network +print("Trying to connect... (This may take a while)...") +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) + +# Create server socket +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) + +# Bind and listen +s.bind([HOST, PORT]) +s.listen(5) + +# Set server socket to blocking +s.setblocking(True) + +def start_streaming(s): + print ('Waiting for connections..') + client, addr = s.accept() + # set client socket timeout to 5s + client.settimeout(5.0) + 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.sendall("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 + # NOTE: Disable IDE preview to increase streaming FPS. + while (True): + clock.tick() # Track elapsed milliseconds between snapshots(). + frame = sensor.snapshot() + cframe = frame.compressed(quality=35) + header = "\r\n--openmv\r\n" \ + "Content-Type: image/jpeg\r\n"\ + "Content-Length:"+str(cframe.size())+"\r\n\r\n" + client.sendall(header) + client.sendall(cframe) + print(clock.fps()) + +while (True): + try: + start_streaming(s) + except OSError as e: + print("socket error: ", e) + #sys.print_exception(e) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_pub.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_pub.py new file mode 100644 index 000000000..a43443534 --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_pub.py @@ -0,0 +1,32 @@ +# MQTT Example. +# This example shows how to use the MQTT library to publish to a topic. +# +# 1) Copy the mqtt.py library to OpenMV storage. +# 2) Run this script on the OpenMV camera. +# 3) Install the mosquitto client on PC and run the following command: +# mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v +# +# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com) +import time, network +from mqtt import MQTTClient + +SSID='' # Network SSID +KEY='' # Network key + +# Init wlan module and connect to network +print("Trying to connect. Note this may take a while...") + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) + +client = MQTTClient("openmv", "test.mosquitto.org", port=1883) +client.connect() + +while (True): + client.publish("openmv/test", "Hello World!") + time.sleep_ms(1000) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_sub.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_sub.py new file mode 100644 index 000000000..2de69aa2d --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/mqtt_sub.py @@ -0,0 +1,39 @@ +# MQTT Example. +# This example shows how to use the MQTT library to subscribe to a topic. +# +# 1) Copy the mqtt.py library to OpenMV storage. +# 2) Run this script on the OpenMV camera. +# 3) Install the mosquitto client on PC and run the following command: +# mosquitto_pub -t "openmv/test" -m "Hello World!" -h test.mosquitto.org -p 1883 +# +# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com) +import time, network +from mqtt_update import MQTTClient + +SSID='' # Network SSID +KEY='' # Network key + +# Init wlan module and connect to network +print("Trying to connect. Note this may take a while...") + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) + +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) + +client = MQTTClient("openmv", "test.mosquitto.org", port=1883) +client.connect() + +def callback(topic, msg): + print(topic, msg) + +# must set callback first +client.set_callback(callback) +client.subscribe("openmv/test") + +while (True): + client.check_msg() # poll for messages. + time.sleep_ms(1000) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/ntp.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/ntp.py new file mode 100644 index 000000000..11c854fe5 --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/ntp.py @@ -0,0 +1,33 @@ +# NTP Example +# +# This example shows how to get the current time using NTP with the WiFi shield. + +import network, socket, ustruct, utime + +SSID='' # Network SSID +KEY='' # Network key + +TIMESTAMP = 2208988800+946684800 + +# Init wlan module and connect to network +print("Trying to connect... (This may take a while)...") +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +wlan.connect(SSID, KEY, timeout=30000) +# We should have a valid IP now via DHCP +print("WiFi Connected ", wlan.ifconfig()) + +# Create new socket +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +# Get addr info via DNS +addr = socket.getaddrinfo("pool.ntp.org", 123)[0][4] + +# Send query +client.sendto('\x1b' + 47 * '\0', addr) +data, address = client.recvfrom(1024) + +# Print time +t = ustruct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print ("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (utime.localtime(t)[0:6])) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/scan.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/scan.py new file mode 100644 index 000000000..3c4bab355 --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/scan.py @@ -0,0 +1,16 @@ +# Scan Example +# +# This example shows how to scan for networks with the WiFi shield. + +import time, network + +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) + +while (True): + scan_result = wlan.scan() + for ap in scan_result: + print(ap) + print() + time.sleep_ms(1000) diff --git a/scripts/examples/Arduino/Portenta-H7/40-WiFi/static_ip.py b/scripts/examples/Arduino/Portenta-H7/40-WiFi/static_ip.py new file mode 100644 index 000000000..cc040b297 --- /dev/null +++ b/scripts/examples/Arduino/Portenta-H7/40-WiFi/static_ip.py @@ -0,0 +1,33 @@ +# NTP Example using static IP. +# +# This example shows how to get the current time using NTP with the WiFi shield. + +import network, socket, ustruct, utime + +SSID='' # Network SSID +KEY='' # Network key + +TIMESTAMP = 2208988800+946684800 + +# Init wlan module and connect to network +print("Trying to connect... (This may take a while)...") +wlan = network.WLAN(network.STA_IF) +wlan.deinit() +wlan.active(True) +# ifconfig must be called before connect() +wlan.ifconfig(('192.168.1.200', '255.255.255.0', '192.168.1.1', '192.168.1.1')) +wlan.connect(SSID, KEY, timeout=30000) + +# Create new socket +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +# Get addr info via DNS +addr = socket.getaddrinfo("pool.ntp.org", 123)[0][4] + +# Send query +client.sendto('\x1b' + 47 * '\0', addr) +data, address = client.recvfrom(1024) + +# Print time +t = ustruct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print ("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (utime.localtime(t)[0:6]))