Skip to content

Commit 1ebad05

Browse files
author
brentru
committed
reorganize examples, 1st class native networking
1 parent db78907 commit 1ebad05

File tree

7 files changed

+63
-140
lines changed

7 files changed

+63
-140
lines changed

examples/adafruit_io_http/adafruit_io_feeds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
88
from adafruit_esp32spi import adafruit_esp32spi
99
import adafruit_requests as requests
10-
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
10+
from adafruit_io.adafruit_io import IO_HTTP
1111

1212
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
1313
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other

examples/adafruit_io_http/adafruit_io_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
88
from adafruit_esp32spi import adafruit_esp32spi
99
import adafruit_requests as requests
10-
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
10+
from adafruit_io.adafruit_io import IO_HTTP
1111

1212

1313
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and

examples/adafruit_io_http/adafruit_io_randomizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
88
from adafruit_esp32spi import adafruit_esp32spi
99
import adafruit_requests as requests
10-
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
10+
from adafruit_io.adafruit_io import IO_HTTP
1111

1212
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
1313
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,51 @@
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"])

examples/adafruit_io_http/adafruit_io_simpletest_wifi.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

examples/adafruit_io_http/adafruit_io_temperature.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
88
from adafruit_esp32spi import adafruit_esp32spi
99
import adafruit_requests as requests
10-
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1110
import adafruit_adt7410
11+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1212

1313
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
1414
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
@@ -67,17 +67,12 @@
6767
adt.high_resolution = True
6868

6969
while True:
70-
try:
71-
temperature = adt.temperature
72-
# set temperature value to two precision points
73-
temperature = "%0.2f" % (temperature)
70+
temperature = adt.temperature
71+
# set temperature value to two precision points
72+
temperature = "%0.2f" % (temperature)
7473

75-
print("Current Temperature: {0}*C".format(temperature))
76-
print("Sending to Adafruit IO...")
77-
io.send_data(temperature_feed["key"], temperature)
78-
print("Data sent!")
79-
except (ValueError, RuntimeError) as e:
80-
print("Failed to get data, retrying\n", e)
81-
wifi.reset()
82-
continue
74+
print("Current Temperature: {0}*C".format(temperature))
75+
print("Sending to Adafruit IO...")
76+
io.send_data(temperature_feed["key"], temperature)
77+
print("Data sent!")
8378
time.sleep(0.5)

examples/adafruit_io_http/adafruit_io_weather.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Example of using the Adafruit IO+ Weather Service
22
# adafruit_circuitpython_adafruitio with an esp32spi_socket
3-
import time
43
import board
54
import busio
65
from digitalio import DigitalInOut
76
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
87
from adafruit_esp32spi import adafruit_esp32spi
98
import adafruit_requests as requests
10-
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
9+
from adafruit_io.adafruit_io import IO_HTTP
1110

1211
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
1312
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other

0 commit comments

Comments
 (0)