Économies d'énergie pour pile CR2032 (v0.6)

- une seule mesure AHT par cycle, suppression des sleep(2000)
- envoi temp/humidité sur seuil (0,2 °C / 1 %), envoi forcé toutes les heures
- batterie une fois par jour, unités une seule fois après démarrage
- MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS 2000 -> 500
- plus de blocage si l'AHT est absent
- compatible 1 MHz (MY_BAUD_RATE 9600, I2C 50 kHz)
- PA_MAX

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-29 14:21:26 +01:00
co-authored by Claude Opus 5.5
parent af6e7c224a
commit 62bc7859c4
+89 -55
View File
@@ -8,6 +8,9 @@
#define MY_RF24_CHANNEL 14 #define MY_RF24_CHANNEL 14
// At 1 MHz (clock=1MHz_internal), 115200 baud is impossible: debug serial at 9600
#define MY_BAUD_RATE 9600
// INFO coincell optimization // INFO coincell optimization
// https://www.openhardware.io/view/398/NModule-Temperature-Humidity-Light-Door-sensor-shield#tabs-source // https://www.openhardware.io/view/398/NModule-Temperature-Humidity-Light-Door-sensor-shield#tabs-source
@@ -18,11 +21,9 @@
// 5s is long enough to connect to gateway so you should connect your node back to your computer and check the log if your node cannot connect // 5s is long enough to connect to gateway so you should connect your node back to your computer and check the log if your node cannot connect
#define MY_TRANSPORT_WAIT_READY_MS 5000 // no more than 5 seconds of waiting for transport to be ready #define MY_TRANSPORT_WAIT_READY_MS 5000 // no more than 5 seconds of waiting for transport to be ready
// Reduce maximum delay before going to sleep, as we do not want to kill the battery if gateway is not available // Before each sleep(), if the link to the gateway is lost, MySensors searches for it with the receiver on (~13 mA).
// Before going to sleep MySensors will make sure we don't have a connection problem to MySensors network // 500 ms instead of 2000: if the link is lost, give up quickly and retry at the next wake-up.
// If this value is too high and you have connection problem, MySensors will never go to sleep and bye bye battery ... #define MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS 500
// If using AA/AAA batteries you can set a longer duration like 5000ms
#define MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS 2000
// #define MY_SIGNING_SIMPLE_PASSWD "C@KptBi1L@5op?8!" // #define MY_SIGNING_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
// #define MY_SECURITY_SIMPLE_PASSWD "C@KptBi1L@5op?8!" // #define MY_SECURITY_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
@@ -35,19 +36,36 @@
static const uint64_t UPDATE_INTERVAL = 900000; // (15min * 60sec = 900 seconds * 1000 milliseconds) static const uint64_t UPDATE_INTERVAL = 900000; // (15min * 60sec = 900 seconds * 1000 milliseconds)
// Send temperature/humidity only when they change by more than these thresholds...
#define TEMP_THRESHOLD 0.2 // °C
#define HUM_THRESHOLD 1.0 // %
// ...but force a send every FORCE_SEND_CYCLES cycles so the controller knows the node is alive
#define FORCE_SEND_CYCLES 4 // 4 x 15 min = 1 h
// Battery reported once a day (hwCPUVoltage() keeps the CPU awake 70 ms)
#define BATTERY_REPORT_CYCLES 96 // 96 x 15 min = 24 h
// AHTxx sets the I2C clock with Wire.setClock(): 100 kHz cannot be reached at 1 MHz
// (TWBR would wrap around and the bus would crawl at ~2 kHz), 50 kHz is fine.
#if F_CPU >= 4000000L
#define AHT_I2C_SPEED_HZ 100000
#else
#define AHT_I2C_SPEED_HZ 50000
#endif
#define CHILD_ID_HUM 0 #define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1 #define CHILD_ID_TEMP 1
#define CHILD_ID_BAT 2 #define CHILD_ID_BAT 2
#define CHILD_ID_BATV 3 #define CHILD_ID_BATV 3
bool metric = true; bool metric = true;
float ahtTemp; //to store T/RH result float last_ahtTemp = 0;
float last_ahtTemp; //to store T/RH result float last_ahtHumi = 0;
float ahtHumi; bool ahtOk = false;
float last_ahtHumi; bool unitsSent = false;
uint8_t cyclesSinceForcedSend = FORCE_SEND_CYCLES; // forces a send on the first cycle
uint8_t cyclesSinceBattery = BATTERY_REPORT_CYCLES; // battery reported on the first cycle
int oldBatteryPcnt = 0; #define FULL_BATTERY 3 // 3V for a fresh CR2032
#define FULL_BATTERY 3 // 3V for 2xAA alkaline. Adjust if you use a different battery setup.
MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgHum(CHILD_ID_HUM, V_HUM);
@@ -59,9 +77,7 @@ MyMessage msgBatVPrefix(CHILD_ID_BATV, V_UNIT_PREFIX); // Custom unit message.
// AHT10 // AHT10
// #define AHT1X_POWER_ON_DELAY 100 //wait for AHT1x to initialize after power-on, in milliseconds // #define AHT1X_POWER_ON_DELAY 100 //wait for AHT1x to initialize after power-on, in milliseconds
// #define AHTXX_I2C_SPEED_HZ 400000
AHTxx aht10(0x38, AHT1x_SENSOR); //sensor address, sensor type AHTxx aht10(0x38, AHT1x_SENSOR); //sensor address, sensor type
// AHTxx aht10(0x00, 0x38);
// Sleep between sendings to preserve coin cell // Sleep between sendings to preserve coin cell
void sleepForCoinCell() { void sleepForCoinCell() {
@@ -75,7 +91,7 @@ void presentation()
#endif #endif
// Send the sketch version information to the gateway // Send the sketch version information to the gateway
sendSketchInfo("aht10", "0.5"); sendSketchInfo("aht10", "0.6");
sleepForCoinCell(); sleepForCoinCell();
// Register all sensors to gw (they will be created as child devices) // Register all sensors to gw (they will be created as child devices)
@@ -89,8 +105,6 @@ void presentation()
sleepForCoinCell(); sleepForCoinCell();
metric = getControllerConfig().isMetric; metric = getControllerConfig().isMetric;
sleepForCoinCell();
} }
@@ -98,18 +112,14 @@ void setup()
{ {
#ifdef MY_DEBUG #ifdef MY_DEBUG
Serial.println("- - - - - setup - - - - -"); Serial.println("- - - - - setup - - - - -");
// Serial.println(AHT1x_SENSOR);
// aht10.setType(AHT1x_SENSOR);
#endif #endif
while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2); // Never block here: if the AHT is missing, an awake CPU (~3 mA) would drain the cell in a few days.
{ // loop() will retry at every wake-up.
Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free ahtOk = aht10.begin(AHT_I2C_SPEED_HZ);
delay(5000);
}
#ifdef MY_DEBUG #ifdef MY_DEBUG
Serial.println(F("AHT10 OK")); Serial.println(ahtOk ? F("AHT10 OK") : F("AHT10 not found"));
#endif #endif
} }
@@ -118,64 +128,88 @@ void loop()
{ {
#ifdef MY_DEBUG #ifdef MY_DEBUG
Serial.println("- - - - - loop - - - - -"); Serial.println("- - - - - loop - - - - -");
Serial.println("getting humidity & temperature");
#endif #endif
ahtTemp = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds // Battery voltage is measured first, after the long sleep: resting voltage, not sagged by the radio
bool reportBattery = (++cyclesSinceBattery >= BATTERY_REPORT_CYCLES);
uint16_t batteryMillivolts = 0;
if (reportBattery) {
cyclesSinceBattery = 0;
batteryMillivolts = hwCPUVoltage();
}
bool sent = false;
if (!ahtOk) {
ahtOk = aht10.begin(AHT_I2C_SPEED_HZ);
}
if (ahtOk) {
// A single measurement (~80 ms): humidity is read from the same data
float ahtTemp = aht10.readTemperature();
float ahtHumi = aht10.readHumidity(AHTXX_USE_READ_DATA);
if (ahtTemp == AHTXX_ERROR || ahtHumi == AHTXX_ERROR) {
ahtOk = false; // will call begin() again at the next wake-up
#ifdef MY_DEBUG
Serial.println(F("AHT10 read error"));
#endif
} else {
#ifdef MY_DEBUG #ifdef MY_DEBUG
Serial.print(F("Temperature: ")); Serial.print(F("Temperature: "));
Serial.print(ahtTemp); Serial.print(ahtTemp);
Serial.println(F(" +-0.3C")); Serial.print(F(" C, Humidity: "));
Serial.print(ahtHumi);
Serial.println(F(" %"));
#endif #endif
// static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
if (ahtTemp != last_ahtTemp) bool force = (++cyclesSinceForcedSend >= FORCE_SEND_CYCLES);
{ if (force) {
cyclesSinceForcedSend = 0;
}
if (force || fabs(ahtTemp - last_ahtTemp) >= TEMP_THRESHOLD) {
send(msgTemp.set(ahtTemp, 1)); send(msgTemp.set(ahtTemp, 1));
last_ahtTemp = ahtTemp; last_ahtTemp = ahtTemp;
sent = true;
} }
if (force || fabs(ahtHumi - last_ahtHumi) >= HUM_THRESHOLD) {
sleep(2000); //measurement with high frequency leads to heating of the sensor, see NOTE if (sent) sleepForCoinCell();
ahtHumi = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
#ifdef MY_DEBUG
Serial.print(F("Humidity: "));
Serial.print(ahtHumi);
Serial.println(F(" +-2%"));
#endif
// static MyMessage msgHum(CHILD_ID_HUM, V_HUM);
if (ahtHumi != last_ahtHumi)
{
send(msgHum.set(ahtHumi, 1)); send(msgHum.set(ahtHumi, 1));
last_ahtHumi = ahtHumi; last_ahtHumi = ahtHumi;
sent = true;
}
}
} }
if (reportBattery) {
sleep(2000);
// get the battery Voltage
long batteryMillivolts = hwCPUVoltage();
float batteryVolts = batteryMillivolts / 1000.0; float batteryVolts = batteryMillivolts / 1000.0;
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5; int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
if (batteryPcnt > 100) batteryPcnt = 100;
#ifdef MY_DEBUG #ifdef MY_DEBUG
Serial.print("Battery voltage: "); Serial.print("Battery voltage: ");
Serial.print(batteryVolts); Serial.print(batteryVolts);
Serial.println("V"); Serial.print("V, percent: ");
Serial.print("Battery percent: ");
Serial.print(batteryPcnt); Serial.print(batteryPcnt);
Serial.println(" %"); Serial.println(" %");
#endif #endif
if (oldBatteryPcnt != batteryPcnt) { // Units only once, after boot
if (!unitsSent) {
if (sent) sleepForCoinCell();
send(msgBatPrefix.set("%"));
sleepForCoinCell();
send(msgBatVPrefix.set("V"));
unitsSent = true;
sent = true;
}
if (sent) sleepForCoinCell();
sendBatteryLevel(batteryPcnt); sendBatteryLevel(batteryPcnt);
sleepForCoinCell(); sleepForCoinCell();
oldBatteryPcnt = batteryPcnt;
send(msgBatPrefix.set("%")); // Set custom unit.
sleepForCoinCell();
send(msgBat.set(batteryPcnt, 1)); send(msgBat.set(batteryPcnt, 1));
sleepForCoinCell(); sleepForCoinCell();
send(msgBatVPrefix.set("V")); // Set custom unit.
sleepForCoinCell();
send(msgBatV.set(batteryVolts, 1)); send(msgBatV.set(batteryVolts, 1));
} }