|
| 1 | +# SPDX-FileCopyrightText: 2024 Tyeth Gundry for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# adafruit_circuitpython_adafruitio usage for batch data with a CPython socket. |
| 5 | +import datetime |
| 6 | +import socket |
| 7 | +import ssl |
| 8 | +from random import randint |
| 9 | +import adafruit_requests |
| 10 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
| 11 | + |
| 12 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "aio_username" |
| 13 | +# and "aio_key" entries with your IO credentials, or set environment variables/defaults below. |
| 14 | +# *** DO NOT share that file or commit it into Git or other source control. *** |
| 15 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 16 | +try: |
| 17 | + from secrets import secrets |
| 18 | +except ImportError: |
| 19 | + import os |
| 20 | + |
| 21 | + secrets = { |
| 22 | + "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"), |
| 23 | + "aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"), |
| 24 | + } |
| 25 | + if ( |
| 26 | + secrets["aio_key"] == "Your_Adafruit_IO_Key_Here" |
| 27 | + or secrets["aio_username"] == "Your_Username_Here" |
| 28 | + ): |
| 29 | + print("Adafruit IO secrets are kept in secrets.py, please add them there!") |
| 30 | + raise |
| 31 | + |
| 32 | +# Set your Adafruit IO Username and Key in secrets.py |
| 33 | +# (visit io.adafruit.com if you need to create an account, |
| 34 | +# or if you need your Adafruit IO key.) |
| 35 | +aio_username = secrets["aio_username"] |
| 36 | +aio_key = secrets["aio_key"] |
| 37 | + |
| 38 | + |
| 39 | +requests = adafruit_requests.Session(socket, ssl.create_default_context()) |
| 40 | +# Initialize an Adafruit IO HTTP API object |
| 41 | +io = IO_HTTP(aio_username, aio_key, requests) |
| 42 | + |
| 43 | +try: |
| 44 | + # Get the 'temperature' feed from Adafruit IO |
| 45 | + temperature_feed = io.get_feed("batch-temperature") |
| 46 | +except AdafruitIO_RequestError: |
| 47 | + # If no 'temperature' feed exists, create one |
| 48 | + temperature_feed = io.create_new_feed("batch-temperature") |
| 49 | + |
| 50 | +# Get current time from Adafruit IO time service (in UTC) |
| 51 | +years, months, days, hours, minutes, seconds, *_ = io.receive_time("UTC") |
| 52 | +current_time = datetime.datetime(years, months, days, hours, minutes, seconds) |
| 53 | +print("Current time from Adafruit IO: ", current_time) |
| 54 | + |
| 55 | +# Create random values at different timestamps to send to the feed |
| 56 | +data = [] |
| 57 | +for i in range(5): |
| 58 | + random_value = randint(0, 50) |
| 59 | + time_offset = i - 5 |
| 60 | + created_at = current_time + datetime.timedelta(seconds=time_offset) |
| 61 | + print( |
| 62 | + "Adding datapoint {0} (at T:{1}) to collection for batch-temperature feed...".format( |
| 63 | + random_value, time_offset |
| 64 | + ) |
| 65 | + ) |
| 66 | + data.append( |
| 67 | + { |
| 68 | + "value": random_value, |
| 69 | + "created_at": created_at.isoformat(), # optional metadata like lat, lon, ele, etc |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | +# Send the data to the feed as a single batch |
| 74 | +io.send_batch_data(temperature_feed["key"], data) |
| 75 | +print("Data sent!") |
| 76 | +print() |
| 77 | +print( |
| 78 | + "View your feed graph at: https://io.adafruit.com/{0}/feeds/{1}".format( |
| 79 | + aio_username, temperature_feed["key"] |
| 80 | + ) |
| 81 | +) |
| 82 | +print() |
| 83 | + |
| 84 | +# Retrieve data value from the feed |
| 85 | +print("Retrieving data from batch-temperature feed...") |
| 86 | +received_data = io.receive_data(temperature_feed["key"]) |
| 87 | +print("Data from temperature feed: ", received_data["value"]) |
0 commit comments