Skip to content

Commit 6e630a8

Browse files
committed
Update groups http example
1 parent ac84b37 commit 6e630a8

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

examples/adafruit_io_http/adafruit_io_groups.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Adafruit IO HTTP API - Group Interactions
55
# Documentation: https://io.adafruit.com/api/docs/#groups
66
# adafruit_circuitpython_adafruitio with an esp32spi_socket
7-
import datetime
7+
import adafruit_datetime as datetime
88
import board
99
import busio
1010
from digitalio import DigitalInOut
@@ -15,14 +15,25 @@
1515

1616

1717
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
18-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
19-
# source control.
18+
# "password" keys with your WiFi credentials, along with "aio_username" and "aio_key" for
19+
# your Adafruit IO user/key. DO NOT share that file or commit it into Git or other source control.
2020
# pylint: disable=no-name-in-module,wrong-import-order
2121
try:
2222
from secrets import secrets
2323
except ImportError:
24-
print("WiFi secrets are kept in secrets.py, please add them there!")
25-
raise
24+
import os
25+
if os.getenv("ADAFRUIT_AIO_USERNAME") and os.getenv("ADAFRUIT_AIO_KEY"):
26+
secrets = {
27+
"aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"),
28+
"aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"),
29+
"ssid": os.getenv("CIRCUITPY_WIFI_SSID", ""),
30+
"password": os.getenv("CIRCUITPY_WIFI_PASSWORD", ""),
31+
}
32+
else:
33+
print(
34+
"WiFi + Adafruit IO secrets are kept in secrets.py, please add them there!"
35+
)
36+
raise
2637

2738
# If you are using a board with pre-defined ESP32 Pins:
2839
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -46,6 +57,11 @@
4657
continue
4758
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
4859

60+
# If you are using a wifi based mcu use this instead of esp code above, remove the from
61+
# adafruit_esp32spi import line, optionally esp.connect(secrets["ssid"], secrets["password"])
62+
# import wifi
63+
# esp = wifi.radio
64+
4965
# Initialize a requests session
5066
pool = adafruit_connection_manager.get_radio_socketpool(esp)
5167
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
@@ -84,9 +100,17 @@
84100
print("Getting fresh humidity feed info... (notice groups)")
85101
print(io.get_feed(humidity_feed["key"]))
86102

103+
# fetch current time
104+
print("Fetching current time from IO... ", end="")
105+
year, month, day, hour, minute, second, *_ = io.receive_time()
106+
old_time = datetime.datetime(year, month, day, hour, minute, second)
107+
print(old_time.isoformat())
108+
87109
# Publish data for multiple feeds to a group, use different timestamps for no reason
88110
print("Publishing batch data to group feeds with created_at set 99minutes ago...")
89-
thetime = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(minutes=99)
111+
thetime = old_time - datetime.timedelta(minutes=99)
112+
print(thetime)
113+
90114
io.send_group_data(
91115
group_key=sensor_group["key"],
92116
feeds_and_data=[

0 commit comments

Comments
 (0)