Skip to content

Commit 69b0501

Browse files
committed
Add example for get_user_rate_info
1 parent 6d7abbc commit 69b0501

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: 2024 Tyeth Gundry for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# retrieve user rate info via adafruit_circuitpython_adafruitio with native wifi networking
5+
import ssl
6+
import time # pylint: disable=unused-import
7+
import adafruit_requests
8+
import socketpool
9+
import wifi
10+
from adafruit_io.adafruit_io import IO_HTTP
11+
12+
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
13+
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
14+
# source control.
15+
16+
# pylint: disable=no-name-in-module,wrong-import-order
17+
try:
18+
from secrets import secrets
19+
except ImportError:
20+
print("WiFi secrets are kept in secrets.py, please add them there!")
21+
raise
22+
23+
# Set your Adafruit IO Username and Key in secrets.py
24+
# (visit io.adafruit.com if you need to create an account,
25+
# or if you need your Adafruit IO key.)
26+
aio_username = secrets["aio_username"]
27+
aio_key = secrets["aio_key"]
28+
29+
print("Connecting to %s" % secrets["ssid"])
30+
wifi.radio.connect(secrets["ssid"], secrets["password"])
31+
print("Connected to %s!" % secrets["ssid"])
32+
33+
34+
pool = socketpool.SocketPool(wifi.radio)
35+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
36+
# Initialize an Adafruit IO HTTP API object
37+
io = IO_HTTP(aio_username, aio_key, requests)
38+
39+
print("===============\nUser Rate info:\n===============")
40+
print("\n".join([f"{k:<30}\t=\t{v}" for (k, v) in io.get_user_rate_info().items()]))
41+
42+
print(f"Throttle limit: {io.get_throttle_limit()}")
43+
print(f"Remaining throttle limit: {io.get_remaining_throttle_limit()}")
44+
45+
46+
# # Uncomment these lines to retrieve all user info as one big json object:
47+
# print("Waiting 5seconds before fetching full user info (a lot of JSON output)")
48+
# time.sleep(5)
49+
# try:
50+
# print("\n\nFull User info:")
51+
# print(io.get_user_info())
52+
# except MemoryError as me:
53+
# print(
54+
# "Board ran out of memory when processing all that user info json."
55+
# + "This is expected on most boards (ESP32-S3 should work)"
56+
# )
57+
# raise me
58+
# except Exception as e:
59+
# print("Unexpected error!")
60+
# raise e
61+
62+
print("\n\nDone!")

0 commit comments

Comments
 (0)