|
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 | 8 | import board
|
8 | 9 | import busio
|
9 | 10 | from digitalio import DigitalInOut
|
|
50 | 51 | ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
|
51 | 52 | requests = adafruit_requests.Session(pool, ssl_context)
|
52 | 53 |
|
| 54 | +# If you are testing on python with blinka, use real requests below and comment out above: |
| 55 | +# import os, datetime, requests as real_requests |
| 56 | +# from adafruit_io.adafruit_io import IO_HTTP |
| 57 | +# secrets = {"aio_username": os.getenv("AIO_USERNAME"), "aio_key": os.getenv("AIO_KEY")} |
| 58 | +# requests = real_requests.Session() |
| 59 | + |
| 60 | + |
53 | 61 | # Set your Adafruit IO Username and Key in secrets.py
|
54 | 62 | # (visit io.adafruit.com if you need to create an account,
|
55 | 63 | # or if you need your Adafruit IO key.)
|
|
72 | 80 | humidity_feed = io.create_new_feed("humidity", "a feed for humidity data")
|
73 | 81 | io.add_feed_to_group(sensor_group["key"], humidity_feed["key"])
|
74 | 82 |
|
| 83 | +# show humidity feed is in two groups |
| 84 | +print("Getting fresh humidity feed info... (notice groups)") |
| 85 | +print(io.get_feed(humidity_feed["key"])) |
| 86 | + |
| 87 | +# Publish data for multiple feeds to a group, use different timestamps for no reason |
| 88 | +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) |
| 90 | +io.send_group_data( |
| 91 | + group_key=sensor_group["key"], |
| 92 | + feeds_and_data=[ |
| 93 | + {"key": "temperature", "value": 20.0}, |
| 94 | + {"key": "humidity", "value": 40.0}, |
| 95 | + ], |
| 96 | + metadata={ |
| 97 | + "lat": 50.1858942, |
| 98 | + "lon": -4.9677478, |
| 99 | + "ele": 4, |
| 100 | + "created_at": thetime.isoformat(), |
| 101 | + }, |
| 102 | +) |
| 103 | + |
75 | 104 | # Get info from the group
|
76 |
| -print("Getting fresh group info...") |
| 105 | +print("Getting fresh group info... (notice created_at vs updated_at)") |
77 | 106 | sensor_group = io.get_group("envsensors") # refresh data via HTTP API
|
78 | 107 | print(sensor_group)
|
79 | 108 |
|
|
0 commit comments