From a276f7bf9f0236d1f149461348b9cd76d776ee52 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 9 Oct 2023 16:27:45 +0200 Subject: [PATCH] scripts/examples: Update WiFi and Bluetooth examples. --- .../Nicla-Vision/03-WiFi/connect.py | 1 + .../Nicla-Vision/03-WiFi/dns.py | 2 +- .../Nicla-Vision/03-WiFi/http_client.py | 1 + .../Nicla-Vision/03-WiFi/http_client_ssl.py | 5 +- .../Nicla-Vision/03-WiFi/ntp.py | 8 +-- .../Nicla-Vision/03-WiFi/static_ip.py | 8 +-- .../Nicla-Vision/04-Bluetooth/ble_blinky.py | 61 ++++++++++++++++++ .../04-Bluetooth/ble_temperature.py | 13 ++-- .../04-Bluetooth/temp_sensor_aioble.py | 63 +++++++++++++++++++ .../Portenta-H7/02-WiFi/connect.py | 1 + .../Portenta-H7/02-WiFi/dns.py | 2 +- .../Portenta-H7/02-WiFi/http_client.py | 1 + .../Portenta-H7/02-WiFi/http_client_ssl.py | 5 +- .../Portenta-H7/02-WiFi/ntp.py | 9 +-- .../Portenta-H7/02-WiFi/static_ip.py | 8 +-- .../Portenta-H7/03-Bluetooth/ble_blinky.py | 61 ++++++++++++++++++ .../03-Bluetooth/ble_temperature.py | 13 ++-- .../03-Bluetooth/temp_sensor_aioble.py | 63 +++++++++++++++++++ 18 files changed, 289 insertions(+), 36 deletions(-) create mode 100644 scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_blinky.py create mode 100644 scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/temp_sensor_aioble.py create mode 100644 scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_blinky.py create mode 100644 scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/temp_sensor_aioble.py diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/connect.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/connect.py index 443784736..89dac8231 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/connect.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/connect.py @@ -3,6 +3,7 @@ # This example shows how to connect your OpenMV Cam with a WiFi shield to the net. import network +import time SSID = "" # Network SSID KEY = "" # Network key diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/dns.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/dns.py index 13d1c2864..c4d27c5d4 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/dns.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/dns.py @@ -3,7 +3,7 @@ # This example shows how to get the IP address for websites via DNS. import network -import usocket +import time SSID = "" # Network SSID KEY = "" # Network key diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client.py index 63986d875..f444527ec 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client.py @@ -2,6 +2,7 @@ import network import socket +import time # AP info SSID = "" # Network SSID diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client_ssl.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client_ssl.py index 74287c961..59c666bc9 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client_ssl.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/http_client_ssl.py @@ -1,7 +1,8 @@ # Simple HTTPS client example. import network import socket -import ussl +import ssl +import time # AP info SSID = "" # Network SSID @@ -34,7 +35,7 @@ client.connect(addr) # Set timeout client.settimeout(3.0) -client = ussl.wrap_socket(client, server_hostname=HOST) +client = ssl.wrap_socket(client, server_hostname=HOST) # Send HTTP request and recv response request = "GET / HTTP/1.1\r\n" diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/ntp.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/ntp.py index 65edea931..37a16ffc0 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/ntp.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/ntp.py @@ -4,8 +4,8 @@ import network import socket -import ustruct -import utime +import struct +import time SSID = "" # Network SSID KEY = "" # Network key @@ -36,5 +36,5 @@ 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])) +t = struct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (time.localtime(t)[0:6])) diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/static_ip.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/static_ip.py index 20730398a..5ae1bb4d5 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/static_ip.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/03-WiFi/static_ip.py @@ -4,8 +4,8 @@ import network import socket -import ustruct -import utime +import struct +import time SSID = "" # Network SSID KEY = "" # Network key @@ -35,5 +35,5 @@ 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])) +t = struct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (time.localtime(t)[0:6])) diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_blinky.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_blinky.py new file mode 100644 index 000000000..02846702c --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_blinky.py @@ -0,0 +1,61 @@ +# Bluetooth Blinky Example +# +# Use nRFConnect app from the App store, connect to the Nano and write 1/0 to control the LED. + +import bluetooth +import time +from ble_advertising import advertising_payload +from machine import LED +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) + +_FLAG_READ = const(0x0002) +_FLAG_WRITE = const(0x0008) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) + +_SERVICE_UUID = bluetooth.UUID(0x1523) +_LED_CHAR_UUID = (bluetooth.UUID(0x1525), _FLAG_WRITE) +_LED_SERVICE = ( + _SERVICE_UUID, + (_LED_CHAR_UUID,), +) + + +class BLETemperature: + def __init__(self, ble, name="Nicla-Vision"): + self._ble = ble + self._ble.active(True) + self._ble.irq(self._irq) + ((self._handle,),) = self._ble.gatts_register_services((_LED_SERVICE,)) + self._connections = set() + self._payload = advertising_payload(name=name, services=[_SERVICE_UUID]) + self._advertise() + self.led = LED("LED_BLUE") + + def _irq(self, event, data): + # Track connections so we can send notifications. + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, _, _ = data + self._connections.add(conn_handle) + elif event == _IRQ_CENTRAL_DISCONNECT: + conn_handle, _, _ = data + self._connections.remove(conn_handle) + # Start advertising again to allow a new connection. + self._advertise() + elif event == _IRQ_GATTS_WRITE: + self.led.value(self._ble.gatts_read(data[-1])[0]) + + def _advertise(self, interval_us=500000): + self._ble.gap_advertise(interval_us, adv_data=self._payload) + + +if __name__ == "__main__": + ble = bluetooth.BLE() + temp = BLETemperature(ble) + + while True: + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_temperature.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_temperature.py index 90d0b3372..c9fbbd32d 100644 --- a/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_temperature.py +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/ble_temperature.py @@ -8,7 +8,7 @@ import random import struct import time from ble_advertising import advertising_payload - +from machine import LED from micropython import const _IRQ_CENTRAL_CONNECT = const(1) @@ -36,7 +36,7 @@ _ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) class BLETemperature: - def __init__(self, ble, name="PORTENTA_BLE"): + def __init__(self, ble, name="Nicla-Vision"): self._ble = ble self._ble.active(True) self._ble.irq(self._irq) @@ -48,17 +48,20 @@ class BLETemperature: appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER, ) self._advertise() + self.led = LED("LED_BLUE") def _irq(self, event, data): # Track connections so we can send notifications. if event == _IRQ_CENTRAL_CONNECT: conn_handle, _, _ = data self._connections.add(conn_handle) + self.led.on() elif event == _IRQ_CENTRAL_DISCONNECT: conn_handle, _, _ = data self._connections.remove(conn_handle) # Start advertising again to allow a new connection. self._advertise() + self.led.off() elif event == _IRQ_GATTS_INDICATE_DONE: conn_handle, value_handle, status = data @@ -79,7 +82,7 @@ class BLETemperature: self._ble.gap_advertise(interval_us, adv_data=self._payload) -def demo(): +if __name__ == "__main__": ble = bluetooth.BLE() temp = BLETemperature(ble) @@ -93,7 +96,3 @@ def demo(): # Random walk the temperature. t += random.uniform(-0.5, 0.5) time.sleep_ms(1000) - - -if __name__ == "__main__": - demo() diff --git a/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/temp_sensor_aioble.py b/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/temp_sensor_aioble.py new file mode 100644 index 000000000..fedf843ec --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Nicla-Vision/04-Bluetooth/temp_sensor_aioble.py @@ -0,0 +1,63 @@ +from micropython import const + +import uasyncio as asyncio +import aioble +import bluetooth + +import random +import struct + +# org.bluetooth.service.environmental_sensing +_ENV_SENSE_UUID = bluetooth.UUID(0x181A) +# org.bluetooth.characteristic.temperature +_ENV_SENSE_TEMP_UUID = bluetooth.UUID(0x2A6E) +# org.bluetooth.characteristic.gap.appearance.xml +_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) + +# How frequently to send advertising beacons. +_ADV_INTERVAL_MS = 250_000 + +# Register GATT server. +temp_service = aioble.Service(_ENV_SENSE_UUID) +temp_characteristic = aioble.Characteristic( + temp_service, _ENV_SENSE_TEMP_UUID, read=True, notify=True +) +aioble.register_services(temp_service) + + +# Helper to encode the temperature characteristic encoding (sint16, hundredths of a degree). +def _encode_temperature(temp_deg_c): + return struct.pack("IIIIIIIIIIII", data)[10] - TIMESTAMP -print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (utime.localtime(t)[0:6])) +t = struct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (time.localtime(t)[0:6])) diff --git a/scripts/examples/10-Arduino-Boards/Portenta-H7/02-WiFi/static_ip.py b/scripts/examples/10-Arduino-Boards/Portenta-H7/02-WiFi/static_ip.py index 20730398a..5ae1bb4d5 100644 --- a/scripts/examples/10-Arduino-Boards/Portenta-H7/02-WiFi/static_ip.py +++ b/scripts/examples/10-Arduino-Boards/Portenta-H7/02-WiFi/static_ip.py @@ -4,8 +4,8 @@ import network import socket -import ustruct -import utime +import struct +import time SSID = "" # Network SSID KEY = "" # Network key @@ -35,5 +35,5 @@ 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])) +t = struct.unpack(">IIIIIIIIIIII", data)[10] - TIMESTAMP +print("Year:%d Month:%d Day:%d Time: %d:%d:%d" % (time.localtime(t)[0:6])) diff --git a/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_blinky.py b/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_blinky.py new file mode 100644 index 000000000..80d9ba402 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_blinky.py @@ -0,0 +1,61 @@ +# Bluetooth Blinky Example +# +# Use nRFConnect app from the App store, connect to the Nano and write 1/0 to control the LED. + +import bluetooth +import time +from ble_advertising import advertising_payload +from machine import LED +from micropython import const + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) + +_FLAG_READ = const(0x0002) +_FLAG_WRITE = const(0x0008) +_FLAG_NOTIFY = const(0x0010) +_FLAG_INDICATE = const(0x0020) + +_SERVICE_UUID = bluetooth.UUID(0x1523) +_LED_CHAR_UUID = (bluetooth.UUID(0x1525), _FLAG_WRITE) +_LED_SERVICE = ( + _SERVICE_UUID, + (_LED_CHAR_UUID,), +) + + +class BLETemperature: + def __init__(self, ble, name="Portenta-H7"): + self._ble = ble + self._ble.active(True) + self._ble.irq(self._irq) + ((self._handle,),) = self._ble.gatts_register_services((_LED_SERVICE,)) + self._connections = set() + self._payload = advertising_payload(name=name, services=[_SERVICE_UUID]) + self._advertise() + self.led = LED("LED_BLUE") + + def _irq(self, event, data): + # Track connections so we can send notifications. + if event == _IRQ_CENTRAL_CONNECT: + conn_handle, _, _ = data + self._connections.add(conn_handle) + elif event == _IRQ_CENTRAL_DISCONNECT: + conn_handle, _, _ = data + self._connections.remove(conn_handle) + # Start advertising again to allow a new connection. + self._advertise() + elif event == _IRQ_GATTS_WRITE: + self.led.value(self._ble.gatts_read(data[-1])[0]) + + def _advertise(self, interval_us=500000): + self._ble.gap_advertise(interval_us, adv_data=self._payload) + + +if __name__ == "__main__": + ble = bluetooth.BLE() + temp = BLETemperature(ble) + + while True: + time.sleep_ms(1000) diff --git a/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_temperature.py b/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_temperature.py index 90d0b3372..d6e0663fe 100644 --- a/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_temperature.py +++ b/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/ble_temperature.py @@ -8,7 +8,7 @@ import random import struct import time from ble_advertising import advertising_payload - +from machine import LED from micropython import const _IRQ_CENTRAL_CONNECT = const(1) @@ -36,7 +36,7 @@ _ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) class BLETemperature: - def __init__(self, ble, name="PORTENTA_BLE"): + def __init__(self, ble, name="Portenta-H7"): self._ble = ble self._ble.active(True) self._ble.irq(self._irq) @@ -48,17 +48,20 @@ class BLETemperature: appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER, ) self._advertise() + self.led = LED("LED_BLUE") def _irq(self, event, data): # Track connections so we can send notifications. if event == _IRQ_CENTRAL_CONNECT: conn_handle, _, _ = data self._connections.add(conn_handle) + self.led.on() elif event == _IRQ_CENTRAL_DISCONNECT: conn_handle, _, _ = data self._connections.remove(conn_handle) # Start advertising again to allow a new connection. self._advertise() + self.led.off() elif event == _IRQ_GATTS_INDICATE_DONE: conn_handle, value_handle, status = data @@ -79,7 +82,7 @@ class BLETemperature: self._ble.gap_advertise(interval_us, adv_data=self._payload) -def demo(): +if __name__ == "__main__": ble = bluetooth.BLE() temp = BLETemperature(ble) @@ -93,7 +96,3 @@ def demo(): # Random walk the temperature. t += random.uniform(-0.5, 0.5) time.sleep_ms(1000) - - -if __name__ == "__main__": - demo() diff --git a/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/temp_sensor_aioble.py b/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/temp_sensor_aioble.py new file mode 100644 index 000000000..c6b40d1f7 --- /dev/null +++ b/scripts/examples/10-Arduino-Boards/Portenta-H7/03-Bluetooth/temp_sensor_aioble.py @@ -0,0 +1,63 @@ +from micropython import const + +import uasyncio as asyncio +import aioble +import bluetooth + +import random +import struct + +# org.bluetooth.service.environmental_sensing +_ENV_SENSE_UUID = bluetooth.UUID(0x181A) +# org.bluetooth.characteristic.temperature +_ENV_SENSE_TEMP_UUID = bluetooth.UUID(0x2A6E) +# org.bluetooth.characteristic.gap.appearance.xml +_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) + +# How frequently to send advertising beacons. +_ADV_INTERVAL_MS = 250_000 + +# Register GATT server. +temp_service = aioble.Service(_ENV_SENSE_UUID) +temp_characteristic = aioble.Characteristic( + temp_service, _ENV_SENSE_TEMP_UUID, read=True, notify=True +) +aioble.register_services(temp_service) + + +# Helper to encode the temperature characteristic encoding (sint16, hundredths of a degree). +def _encode_temperature(temp_deg_c): + return struct.pack("