Skip to content

Commit db78907

Browse files
author
brentru
committed
add exampel for native wifi networking
1 parent cb346e6 commit db78907

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)