Skip to content

Commit 8dae729

Browse files
authored
Merge pull request #3051 from FoamyGuy/deepsleep_bme280_aio
feather s2 bme280 deepsleep aio example
2 parents 0434974 + 6994ee0 commit 8dae729

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

Adafruit_Feather_ESP32-S2/Arduino_BME280_DeepSleep_AdafruitIO/.feather_esp32s2.test.only

Whitespace-only changes.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-FileCopyrightText: 2025 Limor Fried for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
#include "config.h"
5+
#include <Adafruit_BME280.h>
6+
#include <Adafruit_NeoPixel.h>
7+
8+
Adafruit_BME280 bme; // I2C
9+
10+
AdafruitIO_Feed *temperature = io.feed("temperature");
11+
AdafruitIO_Feed *humidity = io.feed("humidity");
12+
AdafruitIO_Feed *pressure = io.feed("pressure");
13+
float temp, humid, pres;
14+
15+
Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
16+
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
pinMode(LED_BUILTIN, OUTPUT);
21+
digitalWrite(LED_BUILTIN, HIGH);
22+
23+
// wait for serial monitor to open
24+
//while(! Serial);
25+
26+
// turn on neopixel
27+
pinMode(NEOPIXEL_POWER, OUTPUT);
28+
digitalWrite(NEOPIXEL_POWER, HIGH);
29+
pixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
30+
pixel.setBrightness(10); // not so bright
31+
32+
pixel.setPixelColor(0, 0xFF0000); // red
33+
pixel.show();
34+
35+
if (! bme.begin()) {
36+
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
37+
deepSleep();
38+
}
39+
Serial.println("Found BME280");
40+
float temp = bme.readTemperature();
41+
float pres = bme.readPressure() / 100.0F;
42+
float hum = bme.readHumidity();
43+
// shhh time to close your eyes
44+
bme.setSampling(Adafruit_BME280::MODE_SLEEP,
45+
Adafruit_BME280::SAMPLING_X16, Adafruit_BME280::SAMPLING_X16, Adafruit_BME280::SAMPLING_X16,
46+
Adafruit_BME280::FILTER_OFF,
47+
Adafruit_BME280::STANDBY_MS_1000);
48+
49+
Serial.print("Connecting to Adafruit IO");
50+
51+
pixel.setPixelColor(0, 0xFFFF00); // yellow
52+
pixel.show();
53+
54+
// connect to io.adafruit.com
55+
io.connect();
56+
57+
// wait for a connection
58+
while(io.status() < AIO_CONNECTED) {
59+
Serial.print(".");
60+
delay(100);
61+
}
62+
63+
// we are connected
64+
pixel.setPixelColor(0, 0x00FF00); // green
65+
pixel.show();
66+
Serial.println();
67+
Serial.println(io.statusText());
68+
69+
io.run();
70+
71+
temp = temp * 9.0 / 5.0 + 32;
72+
Serial.print("Temperature = ");
73+
Serial.print(temp);
74+
Serial.println(" *F");
75+
temperature->save(temp);
76+
77+
Serial.print("Pressure = ");
78+
Serial.print(pres);
79+
Serial.println(" hPa");
80+
pressure->save(pres);
81+
82+
Serial.print("Humidity = ");
83+
Serial.print(hum);
84+
Serial.println(" %");
85+
humidity->save(hum);
86+
87+
Serial.println();
88+
89+
deepSleep();
90+
}
91+
92+
void loop() {
93+
// we never get here!
94+
}
95+
96+
97+
void deepSleep() {
98+
pinMode(NEOPIXEL_POWER, OUTPUT);
99+
digitalWrite(NEOPIXEL_POWER, LOW); // off
100+
pinMode(LED_BUILTIN, OUTPUT);
101+
digitalWrite(LED_BUILTIN, LOW);
102+
103+
esp_sleep_enable_timer_wakeup(300000000); // 5 minutes
104+
esp_deep_sleep_start();
105+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: 2025 Limor Fried for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
#define IO_USERNAME "your-aio-username"
5+
#define IO_KEY "your-aio-token"
6+
#define WIFI_SSID "your-wifi-ssid"
7+
#define WIFI_PASS "your-wifi-pass"
8+
9+
#include "AdafruitIO_WiFi.h"
10+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)