|
| 1 | +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +""" |
| 4 | +Example using create_and_get_feed. Creates a new feed if it does not exist and sends to it, or |
| 5 | +sends to an existing feed once it has been created. |
| 6 | +""" |
| 7 | +import ssl |
| 8 | +import adafruit_requests |
| 9 | +import socketpool |
| 10 | +import wifi |
| 11 | +import microcontroller |
| 12 | +from adafruit_io.adafruit_io import IO_HTTP |
| 13 | + |
| 14 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 15 | +# "password" keys with your WiFi credentials, and "aio_username" and "aio_key" keys with your |
| 16 | +# Adafruit IO credentials, DO NOT share that file or commit it into Git or other |
| 17 | +# source control. |
| 18 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 19 | +try: |
| 20 | + from secrets import secrets |
| 21 | +except ImportError: |
| 22 | + print( |
| 23 | + "WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!" |
| 24 | + ) |
| 25 | + raise |
| 26 | + |
| 27 | +# Connect to Wi-Fi using credentials from secrets.py |
| 28 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 29 | +print("Connected to {}!".format(secrets["ssid"])) |
| 30 | +print("IP:", wifi.radio.ipv4_address) |
| 31 | + |
| 32 | +pool = socketpool.SocketPool(wifi.radio) |
| 33 | +requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
| 34 | + |
| 35 | +# Obtain Adafruit IO credentials from secrets.py |
| 36 | +aio_username = secrets["aio_username"] |
| 37 | +aio_key = secrets["aio_key"] |
| 38 | + |
| 39 | +# Initialize an Adafruit IO HTTP API object |
| 40 | +io = IO_HTTP(aio_username, aio_key, requests) |
| 41 | + |
| 42 | +# Create temperature variable using the CPU temperature and print the current value. |
| 43 | +temperature = microcontroller.cpu.temperature |
| 44 | +print("Current CPU temperature: {0} C".format(temperature)) |
| 45 | + |
| 46 | +# Create and get feed. |
| 47 | +io.send_data(io.create_and_get_feed("cpu-temperature-feed")["key"], temperature) |
0 commit comments