|
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 adafruit_datetime as datetime |
7 | 8 | import board
|
8 | 9 | import busio
|
9 | 10 | from digitalio import DigitalInOut
|
|
14 | 15 |
|
15 | 16 |
|
16 | 17 | # Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
|
17 |
| -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
18 |
| -# 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. |
19 | 20 | # pylint: disable=no-name-in-module,wrong-import-order
|
20 | 21 | try:
|
21 | 22 | from secrets import secrets
|
22 | 23 | except ImportError:
|
23 |
| - print("WiFi secrets are kept in secrets.py, please add them there!") |
24 |
| - raise |
| 24 | + import os |
| 25 | + |
| 26 | + if os.getenv("ADAFRUIT_AIO_USERNAME") and os.getenv("ADAFRUIT_AIO_KEY"): |
| 27 | + secrets = { |
| 28 | + "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"), |
| 29 | + "aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"), |
| 30 | + "ssid": os.getenv("CIRCUITPY_WIFI_SSID", ""), |
| 31 | + "password": os.getenv("CIRCUITPY_WIFI_PASSWORD", ""), |
| 32 | + } |
| 33 | + else: |
| 34 | + print( |
| 35 | + "WiFi + Adafruit IO secrets are kept in secrets.py, please add them there!" |
| 36 | + ) |
| 37 | + raise |
25 | 38 |
|
26 | 39 | # If you are using a board with pre-defined ESP32 Pins:
|
27 | 40 | esp32_cs = DigitalInOut(board.ESP_CS)
|
|
45 | 58 | continue
|
46 | 59 | print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
|
47 | 60 |
|
| 61 | +# If you are using a wifi based mcu use this instead of esp code above, remove the from |
| 62 | +# adafruit_esp32spi import line, optionally esp.connect(secrets["ssid"], secrets["password"]) |
| 63 | +# import wifi |
| 64 | +# esp = wifi.radio |
| 65 | + |
48 | 66 | # Initialize a requests session
|
49 | 67 | pool = adafruit_connection_manager.get_radio_socketpool(esp)
|
50 | 68 | ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
|
51 | 69 | requests = adafruit_requests.Session(pool, ssl_context)
|
52 | 70 |
|
| 71 | +# If you are testing on python with blinka, use real requests below and comment out above: |
| 72 | +# import os, datetime, requests as real_requests |
| 73 | +# from adafruit_io.adafruit_io import IO_HTTP |
| 74 | +# secrets = { |
| 75 | +# "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME"), |
| 76 | +# "aio_key": os.getenv("ADAFRUIT_AIO_KEY"), |
| 77 | +# } |
| 78 | +# requests = real_requests.Session() |
| 79 | + |
| 80 | + |
53 | 81 | # Set your Adafruit IO Username and Key in secrets.py
|
54 | 82 | # (visit io.adafruit.com if you need to create an account,
|
55 | 83 | # or if you need your Adafruit IO key.)
|
|
72 | 100 | humidity_feed = io.create_new_feed("humidity", "a feed for humidity data")
|
73 | 101 | io.add_feed_to_group(sensor_group["key"], humidity_feed["key"])
|
74 | 102 |
|
| 103 | +# show humidity feed is in two groups |
| 104 | +print("Getting fresh humidity feed info... (notice groups)") |
| 105 | +print(io.get_feed(humidity_feed["key"])) |
| 106 | + |
| 107 | +# fetch current time |
| 108 | +print("Fetching current time from IO... ", end="") |
| 109 | +year, month, day, hour, minute, second, *_ = io.receive_time(timezone="UTC") |
| 110 | +old_time = datetime.datetime(year, month, day, hour, minute, second) |
| 111 | +print(old_time.isoformat()) |
| 112 | + |
| 113 | +# Publish data for multiple feeds to a group, use different timestamps for no reason |
| 114 | +print("Publishing batch data to group feeds with created_at set 99minutes ago...") |
| 115 | +thetime = old_time - datetime.timedelta(minutes=99) |
| 116 | +print(thetime) |
| 117 | + |
| 118 | +io.send_group_data( |
| 119 | + group_key=sensor_group["key"], |
| 120 | + feeds_and_data=[ |
| 121 | + {"key": "temperature", "value": 20.0}, |
| 122 | + {"key": "humidity", "value": 40.0}, |
| 123 | + ], |
| 124 | + metadata={ |
| 125 | + "lat": 50.1858942, |
| 126 | + "lon": -4.9677478, |
| 127 | + "ele": 4, |
| 128 | + "created_at": thetime.isoformat(), |
| 129 | + }, |
| 130 | +) |
| 131 | + |
75 | 132 | # Get info from the group
|
76 |
| -print("Getting fresh group info...") |
| 133 | +print("Getting fresh group info... (notice created_at vs updated_at)") |
77 | 134 | sensor_group = io.get_group("envsensors") # refresh data via HTTP API
|
78 | 135 | print(sensor_group)
|
79 | 136 |
|
|
0 commit comments