Skip to content

Commit da3b838

Browse files
committed
feather s2 bme280 deepsleep aio example
1 parent 5df1fe0 commit da3b838

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
# On MagTag, enable power to NeoPixels.
23+
# Remove these two lines on boards without board.NEOPIXEL_POWER.
24+
np_power = digitalio.DigitalInOut(board.NEOPIXEL_POWER)
25+
np_power.switch_to_output(value=True)
26+
27+
builtin_led = digitalio.DigitalInOut(board.LED)
28+
builtin_led.switch_to_output(value=True)
29+
30+
status_pixel = neopixel.NeoPixel(
31+
board.NEOPIXEL, 1, brightness=0.1, pixel_order=neopixel.GRB, auto_write=True
32+
)
33+
status_pixel[0] = 0xFF0000
34+
35+
# Create sensor object, using the board's default I2C bus.
36+
i2c = board.I2C() # uses board.SCL and board.SDA
37+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
38+
print("Found BME280")
39+
# change this to match the location's pressure (hPa) at sea level
40+
bme280.sea_level_pressure = 1013.25
41+
42+
temperature = bme280.temperature * 9 / 5 + 32
43+
humidity = bme280.relative_humidity
44+
pressure = bme280.pressure
45+
print("\nTemperature: %0.1f F" % temperature)
46+
print("Humidity: %0.1f %%" % humidity)
47+
print("Pressure: %0.1f hPa" % pressure)
48+
49+
bme280.mode = adafruit_bme280.MODE_SLEEP
50+
bme280.overscan_temperature = adafruit_bme280.OVERSCAN_X16
51+
bme280.overscan_humidity = adafruit_bme280.OVERSCAN_X16
52+
bme280.overscan_pressure = adafruit_bme280.OVERSCAN_X16
53+
bme280.iir_filter = adafruit_bme280.IIR_FILTER_DISABLE
54+
bme280.standby_period = adafruit_bme280.STANDBY_TC_1000
55+
56+
57+
status_pixel[0] = 0xFFFF00
58+
59+
print("Connecting to AdafruitIO")
60+
61+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
62+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
63+
ssid = getenv("WIFI_SSID")
64+
password = getenv("WIFI_PASSWORD")
65+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
66+
aio_key = getenv("ADAFRUIT_AIO_KEY")
67+
68+
print("Connecting to %s" % ssid)
69+
wifi.radio.connect(ssid, password)
70+
print("Connected to %s!" % ssid)
71+
72+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
73+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
74+
requests = adafruit_requests.Session(pool, ssl_context)
75+
# Initialize an Adafruit IO HTTP API object
76+
io = IO_HTTP(aio_username, aio_key, requests)
77+
78+
status_pixel[0] = 0x00FF00
79+
80+
try:
81+
# Get the feeds from Adafruit IO
82+
temperature_feed = io.get_feed("temperature")
83+
humidity_feed = io.get_feed("humidity")
84+
pressure_feed = io.get_feed("pressure")
85+
86+
# send data to the feeds
87+
io.send_data(temperature_feed["key"], temperature)
88+
io.send_data(humidity_feed["key"], humidity)
89+
io.send_data(pressure_feed["key"], pressure)
90+
91+
except AdafruitIO_RequestError as e:
92+
print(e)
93+
print(
94+
"You must create feeds on AdafruitIO for: temperature, humidity, and pressure"
95+
)
96+
97+
np_power.value = False
98+
builtin_led.value = False
99+
100+
# Create an alarm that will trigger 5 minutes from now.
101+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + (5 * 60))
102+
# Exit the program, and then deep sleep until the alarm wakes us.
103+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
104+
# Does not return, so we never get here.

0 commit comments

Comments
 (0)