From da3b8383148b3accb9b87709c0d4544f8d24d5af Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 3 Jun 2025 12:26:46 -0500 Subject: [PATCH 1/3] feather s2 bme280 deepsleep aio example --- .../.feather_esp32s2.test.only | 0 .../Arduino_BME280_DeepSleep_AdafruitIO.ino | 105 ++++++++++++++++++ .../config.h | 10 ++ .../code.py | 104 +++++++++++++++++ 4 files changed, 219 insertions(+) create mode 100644 Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/.feather_esp32s2.test.only create mode 100644 Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/Arduino_BME280_DeepSleep_AdafruitIO.ino create mode 100644 Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/config.h create mode 100644 Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py diff --git a/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/.feather_esp32s2.test.only b/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/.feather_esp32s2.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/Arduino_BME280_DeepSleep_AdafruitIO.ino b/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/Arduino_BME280_DeepSleep_AdafruitIO.ino new file mode 100644 index 000000000..aab5557ac --- /dev/null +++ b/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/Arduino_BME280_DeepSleep_AdafruitIO.ino @@ -0,0 +1,105 @@ +// SPDX-FileCopyrightText: 2025 Limor Fried for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#include "config.h" +#include +#include + +Adafruit_BME280 bme; // I2C + +AdafruitIO_Feed *temperature = io.feed("temperature"); +AdafruitIO_Feed *humidity = io.feed("humidity"); +AdafruitIO_Feed *pressure = io.feed("pressure"); +float temp, humid, pres; + +Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); + + +void setup() { + Serial.begin(115200); + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, HIGH); + + // wait for serial monitor to open + //while(! Serial); + + // turn on neopixel + pinMode(NEOPIXEL_POWER, OUTPUT); + digitalWrite(NEOPIXEL_POWER, HIGH); + pixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) + pixel.setBrightness(10); // not so bright + + pixel.setPixelColor(0, 0xFF0000); // red + pixel.show(); + + if (! bme.begin()) { + Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); + deepSleep(); + } + Serial.println("Found BME280"); + float temp = bme.readTemperature(); + float pres = bme.readPressure() / 100.0F; + float hum = bme.readHumidity(); + // shhh time to close your eyes + bme.setSampling(Adafruit_BME280::MODE_SLEEP, + Adafruit_BME280::SAMPLING_X16, Adafruit_BME280::SAMPLING_X16, Adafruit_BME280::SAMPLING_X16, + Adafruit_BME280::FILTER_OFF, + Adafruit_BME280::STANDBY_MS_1000); + + Serial.print("Connecting to Adafruit IO"); + + pixel.setPixelColor(0, 0xFFFF00); // yellow + pixel.show(); + + // connect to io.adafruit.com + io.connect(); + + // wait for a connection + while(io.status() < AIO_CONNECTED) { + Serial.print("."); + delay(100); + } + + // we are connected + pixel.setPixelColor(0, 0x00FF00); // green + pixel.show(); + Serial.println(); + Serial.println(io.statusText()); + + io.run(); + + temp = temp * 9.0 / 5.0 + 32; + Serial.print("Temperature = "); + Serial.print(temp); + Serial.println(" *F"); + temperature->save(temp); + + Serial.print("Pressure = "); + Serial.print(pres); + Serial.println(" hPa"); + pressure->save(pres); + + Serial.print("Humidity = "); + Serial.print(hum); + Serial.println(" %"); + humidity->save(hum); + + Serial.println(); + + deepSleep(); +} + +void loop() { + // we never get here! +} + + +void deepSleep() { + pinMode(NEOPIXEL_POWER, OUTPUT); + digitalWrite(NEOPIXEL_POWER, LOW); // off + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + esp_sleep_enable_timer_wakeup(300000000); // 5 minutes + esp_deep_sleep_start(); +} \ No newline at end of file diff --git a/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/config.h b/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/config.h new file mode 100644 index 000000000..3585e138e --- /dev/null +++ b/Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/config.h @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2025 Limor Fried for Adafruit Industries +// +// SPDX-License-Identifier: MIT +#define IO_USERNAME "your-aio-username" +#define IO_KEY "your-aio-token" +#define WIFI_SSID "your-wifi-ssid" +#define WIFI_PASS "your-wifi-pass" + +#include "AdafruitIO_WiFi.h" +AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS); \ No newline at end of file diff --git a/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py b/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py new file mode 100644 index 000000000..ea9bd8d7b --- /dev/null +++ b/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py @@ -0,0 +1,104 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +CircuitPython example for deep sleep and BME280 sensor sending data +to Adafruit IO. +""" +from os import getenv +import time +import alarm +import board +import digitalio +import neopixel +import wifi + +from adafruit_bme280 import advanced as adafruit_bme280 +import adafruit_connection_manager +import adafruit_requests +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError + + +# On MagTag, enable power to NeoPixels. +# Remove these two lines on boards without board.NEOPIXEL_POWER. +np_power = digitalio.DigitalInOut(board.NEOPIXEL_POWER) +np_power.switch_to_output(value=True) + +builtin_led = digitalio.DigitalInOut(board.LED) +builtin_led.switch_to_output(value=True) + +status_pixel = neopixel.NeoPixel( + board.NEOPIXEL, 1, brightness=0.1, pixel_order=neopixel.GRB, auto_write=True +) +status_pixel[0] = 0xFF0000 + +# Create sensor object, using the board's default I2C bus. +i2c = board.I2C() # uses board.SCL and board.SDA +bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c) +print("Found BME280") +# change this to match the location's pressure (hPa) at sea level +bme280.sea_level_pressure = 1013.25 + +temperature = bme280.temperature * 9 / 5 + 32 +humidity = bme280.relative_humidity +pressure = bme280.pressure +print("\nTemperature: %0.1f F" % temperature) +print("Humidity: %0.1f %%" % humidity) +print("Pressure: %0.1f hPa" % pressure) + +bme280.mode = adafruit_bme280.MODE_SLEEP +bme280.overscan_temperature = adafruit_bme280.OVERSCAN_X16 +bme280.overscan_humidity = adafruit_bme280.OVERSCAN_X16 +bme280.overscan_pressure = adafruit_bme280.OVERSCAN_X16 +bme280.iir_filter = adafruit_bme280.IIR_FILTER_DISABLE +bme280.standby_period = adafruit_bme280.STANDBY_TC_1000 + + +status_pixel[0] = 0xFFFF00 + +print("Connecting to AdafruitIO") + +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("WIFI_SSID") +password = getenv("WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") + +print("Connecting to %s" % ssid) +wifi.radio.connect(ssid, password) +print("Connected to %s!" % ssid) + +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) +# Initialize an Adafruit IO HTTP API object +io = IO_HTTP(aio_username, aio_key, requests) + +status_pixel[0] = 0x00FF00 + +try: + # Get the feeds from Adafruit IO + temperature_feed = io.get_feed("temperature") + humidity_feed = io.get_feed("humidity") + pressure_feed = io.get_feed("pressure") + + # send data to the feeds + io.send_data(temperature_feed["key"], temperature) + io.send_data(humidity_feed["key"], humidity) + io.send_data(pressure_feed["key"], pressure) + +except AdafruitIO_RequestError as e: + print(e) + print( + "You must create feeds on AdafruitIO for: temperature, humidity, and pressure" + ) + +np_power.value = False +builtin_led.value = False + +# Create an alarm that will trigger 5 minutes from now. +time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + (5 * 60)) +# Exit the program, and then deep sleep until the alarm wakes us. +alarm.exit_and_deep_sleep_until_alarms(time_alarm) +# Does not return, so we never get here. From d7cdb81494699b781a529712cfa9ed554b46c4e2 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 13 Jun 2025 14:57:43 -0500 Subject: [PATCH 2/3] additional comments --- .../CircuitPython_BME280_DeepSleep_AdafruitIO/code.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py b/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py index ea9bd8d7b..7c702b74a 100644 --- a/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py +++ b/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py @@ -53,7 +53,7 @@ bme280.iir_filter = adafruit_bme280.IIR_FILTER_DISABLE bme280.standby_period = adafruit_bme280.STANDBY_TC_1000 - +# set status pixel to yellow status_pixel[0] = 0xFFFF00 print("Connecting to AdafruitIO") @@ -69,12 +69,15 @@ wifi.radio.connect(ssid, password) print("Connected to %s!" % ssid) +# setup socket pool and requests session pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) requests = adafruit_requests.Session(pool, ssl_context) + # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) +# set status pixel to green status_pixel[0] = 0x00FF00 try: @@ -94,6 +97,7 @@ "You must create feeds on AdafruitIO for: temperature, humidity, and pressure" ) +# turn off the neopixel and builtin LED np_power.value = False builtin_led.value = False From 6994ee09d3844d91e2c7f67dd4cad7bd5432e16e Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 16 Jun 2025 09:07:06 -0500 Subject: [PATCH 3/3] remove magtag reference, update comments --- .../CircuitPython_BME280_DeepSleep_AdafruitIO/code.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py b/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py index 7c702b74a..ffa8a970b 100644 --- a/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py +++ b/Adafruit_Feather_ESP32-S2/CircuitPython_BME280_DeepSleep_AdafruitIO/code.py @@ -19,14 +19,15 @@ from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -# On MagTag, enable power to NeoPixels. -# Remove these two lines on boards without board.NEOPIXEL_POWER. +# enable power to NeoPixels. np_power = digitalio.DigitalInOut(board.NEOPIXEL_POWER) np_power.switch_to_output(value=True) +# standard LED builtin_led = digitalio.DigitalInOut(board.LED) builtin_led.switch_to_output(value=True) +# neopixel to use for status status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.1, pixel_order=neopixel.GRB, auto_write=True ) @@ -39,6 +40,7 @@ # change this to match the location's pressure (hPa) at sea level bme280.sea_level_pressure = 1013.25 +# temperature converted to F temperature = bme280.temperature * 9 / 5 + 32 humidity = bme280.relative_humidity pressure = bme280.pressure