Add Portenta WiFi examples.

This commit is contained in:
iabdalkader 2021-02-13 21:24:33 +02:00
parent 5e024e22db
commit cc3e4f1522
10 changed files with 358 additions and 0 deletions

View File

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

View File

@ -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])

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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]))

View File

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

View File

@ -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]))