|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +CircuitPython example for deep sleep and BME280 sensor sending data |
| 6 | +to Adafruit IO. |
| 7 | +""" |
| 8 | +from os import getenv |
| 9 | +import time |
| 10 | +import alarm |
| 11 | +import board |
| 12 | +import digitalio |
| 13 | +import neopixel |
| 14 | +import wifi |
| 15 | + |
| 16 | +from adafruit_bme280 import advanced as adafruit_bme280 |
| 17 | +import adafruit_connection_manager |
| 18 | +import adafruit_requests |
| 19 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
| 20 | + |
| 21 | + |
| 22 | +# enable power to NeoPixels. |
| 23 | +np_power = digitalio.DigitalInOut(board.NEOPIXEL_POWER) |
| 24 | +np_power.switch_to_output(value=True) |
| 25 | + |
| 26 | +# standard LED |
| 27 | +builtin_led = digitalio.DigitalInOut(board.LED) |
| 28 | +builtin_led.switch_to_output(value=True) |
| 29 | + |
| 30 | +# neopixel to use for status |
| 31 | +status_pixel = neopixel.NeoPixel( |
| 32 | + board.NEOPIXEL, 1, brightness=0.1, pixel_order=neopixel.GRB, auto_write=True |
| 33 | +) |
| 34 | +status_pixel[0] = 0xFF0000 |
| 35 | + |
| 36 | +# Create sensor object, using the board's default I2C bus. |
| 37 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 38 | +bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c) |
| 39 | +print("Found BME280") |
| 40 | +# change this to match the location's pressure (hPa) at sea level |
| 41 | +bme280.sea_level_pressure = 1013.25 |
| 42 | + |
| 43 | +# temperature converted to F |
| 44 | +temperature = bme280.temperature * 9 / 5 + 32 |
| 45 | +humidity = bme280.relative_humidity |
| 46 | +pressure = bme280.pressure |
| 47 | +print("\nTemperature: %0.1f F" % temperature) |
| 48 | +print("Humidity: %0.1f %%" % humidity) |
| 49 | +print("Pressure: %0.1f hPa" % pressure) |
| 50 | + |
| 51 | +bme280.mode = adafruit_bme280.MODE_SLEEP |
| 52 | +bme280.overscan_temperature = adafruit_bme280.OVERSCAN_X16 |
| 53 | +bme280.overscan_humidity = adafruit_bme280.OVERSCAN_X16 |
| 54 | +bme280.overscan_pressure = adafruit_bme280.OVERSCAN_X16 |
| 55 | +bme280.iir_filter = adafruit_bme280.IIR_FILTER_DISABLE |
| 56 | +bme280.standby_period = adafruit_bme280.STANDBY_TC_1000 |
| 57 | + |
| 58 | +# set status pixel to yellow |
| 59 | +status_pixel[0] = 0xFFFF00 |
| 60 | + |
| 61 | +print("Connecting to AdafruitIO") |
| 62 | + |
| 63 | +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml |
| 64 | +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) |
| 65 | +ssid = getenv("WIFI_SSID") |
| 66 | +password = getenv("WIFI_PASSWORD") |
| 67 | +aio_username = getenv("ADAFRUIT_AIO_USERNAME") |
| 68 | +aio_key = getenv("ADAFRUIT_AIO_KEY") |
| 69 | + |
| 70 | +print("Connecting to %s" % ssid) |
| 71 | +wifi.radio.connect(ssid, password) |
| 72 | +print("Connected to %s!" % ssid) |
| 73 | + |
| 74 | +# setup socket pool and requests session |
| 75 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 76 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 77 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 78 | + |
| 79 | +# Initialize an Adafruit IO HTTP API object |
| 80 | +io = IO_HTTP(aio_username, aio_key, requests) |
| 81 | + |
| 82 | +# set status pixel to green |
| 83 | +status_pixel[0] = 0x00FF00 |
| 84 | + |
| 85 | +try: |
| 86 | + # Get the feeds from Adafruit IO |
| 87 | + temperature_feed = io.get_feed("temperature") |
| 88 | + humidity_feed = io.get_feed("humidity") |
| 89 | + pressure_feed = io.get_feed("pressure") |
| 90 | + |
| 91 | + # send data to the feeds |
| 92 | + io.send_data(temperature_feed["key"], temperature) |
| 93 | + io.send_data(humidity_feed["key"], humidity) |
| 94 | + io.send_data(pressure_feed["key"], pressure) |
| 95 | + |
| 96 | +except AdafruitIO_RequestError as e: |
| 97 | + print(e) |
| 98 | + print( |
| 99 | + "You must create feeds on AdafruitIO for: temperature, humidity, and pressure" |
| 100 | + ) |
| 101 | + |
| 102 | +# turn off the neopixel and builtin LED |
| 103 | +np_power.value = False |
| 104 | +builtin_led.value = False |
| 105 | + |
| 106 | +# Create an alarm that will trigger 5 minutes from now. |
| 107 | +time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + (5 * 60)) |
| 108 | +# Exit the program, and then deep sleep until the alarm wakes us. |
| 109 | +alarm.exit_and_deep_sleep_until_alarms(time_alarm) |
| 110 | +# Does not return, so we never get here. |
0 commit comments