|
1 |
| -# adafruit_circuitpython_adafruitio usage with an esp32spi_socket |
2 |
| -from random import randint |
3 |
| -import board |
4 |
| -import busio |
5 |
| -from digitalio import DigitalInOut |
6 |
| -import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
7 |
| -from adafruit_esp32spi import adafruit_esp32spi |
8 |
| -import adafruit_requests as requests |
9 |
| -from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
10 |
| - |
11 |
| -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
12 |
| -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
13 |
| -# source control. |
14 |
| -# pylint: disable=no-name-in-module,wrong-import-order |
15 |
| -try: |
16 |
| - from secrets import secrets |
17 |
| -except ImportError: |
18 |
| - print("WiFi secrets are kept in secrets.py, please add them there!") |
19 |
| - raise |
20 |
| - |
21 |
| -# If you are using a board with pre-defined ESP32 Pins: |
22 |
| -esp32_cs = DigitalInOut(board.ESP_CS) |
23 |
| -esp32_ready = DigitalInOut(board.ESP_BUSY) |
24 |
| -esp32_reset = DigitalInOut(board.ESP_RESET) |
25 |
| - |
26 |
| -# If you have an externally connected ESP32: |
27 |
| -# esp32_cs = DigitalInOut(board.D9) |
28 |
| -# esp32_ready = DigitalInOut(board.D10) |
29 |
| -# esp32_reset = DigitalInOut(board.D5) |
30 |
| - |
31 |
| -spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
32 |
| -esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
33 |
| - |
34 |
| -print("Connecting to AP...") |
35 |
| -while not esp.is_connected: |
36 |
| - try: |
37 |
| - esp.connect_AP(secrets["ssid"], secrets["password"]) |
38 |
| - except RuntimeError as e: |
39 |
| - print("could not connect to AP, retrying: ", e) |
40 |
| - continue |
41 |
| -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
42 |
| - |
43 |
| -socket.set_interface(esp) |
44 |
| -requests.set_socket(socket, esp) |
45 |
| - |
46 |
| -# Set your Adafruit IO Username and Key in secrets.py |
47 |
| -# (visit io.adafruit.com if you need to create an account, |
48 |
| -# or if you need your Adafruit IO key.) |
49 |
| -aio_username = secrets["aio_username"] |
50 |
| -aio_key = secrets["aio_key"] |
51 |
| - |
52 |
| -# Initialize an Adafruit IO HTTP API object |
53 |
| -io = IO_HTTP(aio_username, aio_key, requests) |
54 |
| - |
55 |
| -try: |
56 |
| - # Get the 'temperature' feed from Adafruit IO |
57 |
| - temperature_feed = io.get_feed("temperature") |
58 |
| -except AdafruitIO_RequestError: |
59 |
| - # If no 'temperature' feed exists, create one |
60 |
| - temperature_feed = io.create_new_feed("temperature") |
61 |
| - |
62 |
| -# Send random integer values to the feed |
63 |
| -random_value = randint(0, 50) |
64 |
| -print("Sending {0} to temperature feed...".format(random_value)) |
65 |
| -io.send_data(temperature_feed["key"], random_value) |
66 |
| -print("Data sent!") |
67 |
| - |
68 |
| -# Retrieve data value from the feed |
69 |
| -print("Retrieving data from temperature feed...") |
70 |
| -received_data = io.receive_data(temperature_feed["key"]) |
71 |
| -print("Data from temperature feed: ", received_data["value"]) |
| 1 | +# adafruit_circuitpython_adafruitio usage with native wifi networking |
| 2 | +import ssl |
| 3 | +from random import randint |
| 4 | +import adafruit_requests |
| 5 | +import socketpool |
| 6 | +import wifi |
| 7 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
| 8 | + |
| 9 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 10 | +# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
| 11 | +# source control. |
| 12 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 13 | +try: |
| 14 | + from secrets import secrets |
| 15 | +except ImportError: |
| 16 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 17 | + raise |
| 18 | + |
| 19 | +# Set your Adafruit IO Username and Key in secrets.py |
| 20 | +# (visit io.adafruit.com if you need to create an account, |
| 21 | +# or if you need your Adafruit IO key.) |
| 22 | +aio_username = secrets["aio_username"] |
| 23 | +aio_key = secrets["aio_key"] |
| 24 | + |
| 25 | +print("Connecting to %s" % secrets["ssid"]) |
| 26 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 27 | +print("Connected to %s!" % secrets["ssid"]) |
| 28 | + |
| 29 | + |
| 30 | +pool = socketpool.SocketPool(wifi.radio) |
| 31 | +requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 32 | +# Initialize an Adafruit IO HTTP API object |
| 33 | +io = IO_HTTP(aio_username, aio_key, requests) |
| 34 | + |
| 35 | +try: |
| 36 | + # Get the 'temperature' feed from Adafruit IO |
| 37 | + temperature_feed = io.get_feed("temperature") |
| 38 | +except AdafruitIO_RequestError: |
| 39 | + # If no 'temperature' feed exists, create one |
| 40 | + temperature_feed = io.create_new_feed("temperature") |
| 41 | + |
| 42 | +# Send random integer values to the feed |
| 43 | +random_value = randint(0, 50) |
| 44 | +print("Sending {0} to temperature feed...".format(random_value)) |
| 45 | +io.send_data(temperature_feed["key"], random_value) |
| 46 | +print("Data sent!") |
| 47 | + |
| 48 | +# Retrieve data value from the feed |
| 49 | +print("Retrieving data from temperature feed...") |
| 50 | +received_data = io.receive_data(temperature_feed["key"]) |
| 51 | +print("Data from temperature feed: ", received_data["value"]) |
0 commit comments