|
4 | 4 | # Adafruit IO HTTP API - Group Interactions
|
5 | 5 | # Documentation: https://io.adafruit.com/api/docs/#groups
|
6 | 6 | # adafruit_circuitpython_adafruitio with an esp32spi_socket
|
7 |
| -import datetime |
| 7 | +import adafruit_datetime as datetime |
8 | 8 | import board
|
9 | 9 | import busio
|
10 | 10 | from digitalio import DigitalInOut
|
|
15 | 15 |
|
16 | 16 |
|
17 | 17 | # 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. |
20 | 20 | # pylint: disable=no-name-in-module,wrong-import-order
|
21 | 21 | try:
|
22 | 22 | from secrets import secrets
|
23 | 23 | 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 |
26 | 37 |
|
27 | 38 | # If you are using a board with pre-defined ESP32 Pins:
|
28 | 39 | esp32_cs = DigitalInOut(board.ESP_CS)
|
|
46 | 57 | continue
|
47 | 58 | print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
|
48 | 59 |
|
| 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 | + |
49 | 65 | # Initialize a requests session
|
50 | 66 | pool = adafruit_connection_manager.get_radio_socketpool(esp)
|
51 | 67 | ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
|
|
84 | 100 | print("Getting fresh humidity feed info... (notice groups)")
|
85 | 101 | print(io.get_feed(humidity_feed["key"]))
|
86 | 102 |
|
| 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 | + |
87 | 109 | # Publish data for multiple feeds to a group, use different timestamps for no reason
|
88 | 110 | 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 | + |
90 | 114 | io.send_group_data(
|
91 | 115 | group_key=sensor_group["key"],
|
92 | 116 | feeds_and_data=[
|
|
0 commit comments