Use deep sleep to save battery
This commit is contained in:
parent
4646a78a93
commit
9205ef1ef5
@ -12,7 +12,8 @@ EspMQTTClient client(
|
|||||||
"espmqtt" // Client name that uniquely identify your device
|
"espmqtt" // Client name that uniquely identify your device
|
||||||
);
|
);
|
||||||
|
|
||||||
#define reportingInterval 10000 // how often to sample/publish, in milliseconds
|
#define maxUptime 60*1000 // how long to stay awake to measure and report before sleeping
|
||||||
|
#define reportingInterval 10*60*1000 // how often to sample/publish, in milliseconds
|
||||||
#define PIN_RESET 4
|
#define PIN_RESET 4
|
||||||
#define LED_BUILTIN 0 // ESP32
|
#define LED_BUILTIN 0 // ESP32
|
||||||
//#define LED_BUILTIN 13 // Teensy
|
//#define LED_BUILTIN 13 // Teensy
|
||||||
@ -28,6 +29,7 @@ int pin_SDA = 15;
|
|||||||
int pin_SCL = 5;
|
int pin_SCL = 5;
|
||||||
|
|
||||||
unsigned long last_run = 0;
|
unsigned long last_run = 0;
|
||||||
|
bool useOled = true;
|
||||||
|
|
||||||
void onConnectionEstablished() {
|
void onConnectionEstablished() {
|
||||||
client.publish("esp/status", "{\"status\": \"connected\"}");
|
client.publish("esp/status", "{\"status\": \"connected\"}");
|
||||||
@ -51,9 +53,12 @@ void setup() {
|
|||||||
Wire.begin(pin_SDA, pin_SCL); //Qwiic Pro Mini
|
Wire.begin(pin_SDA, pin_SCL); //Qwiic Pro Mini
|
||||||
if (!oled.begin()){
|
if (!oled.begin()){
|
||||||
Serial.println("Failed to initialize OLED");
|
Serial.println("Failed to initialize OLED");
|
||||||
|
useOled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(1000); // Delay 1000 ms
|
delay(1000); // Delay 1000 ms
|
||||||
|
|
||||||
|
esp_sleep_enable_timer_wakeup(10*60*1000*1000); // 10 minutes
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0-based index read
|
// 0-based index read
|
||||||
@ -78,9 +83,9 @@ bool readAndPublishSensor(int i) {
|
|||||||
Serial.print(" - ");
|
Serial.print(" - ");
|
||||||
Serial.println(sensorText);
|
Serial.println(sensorText);
|
||||||
|
|
||||||
// show on LED with 1-based plant number and 0-100%
|
// show on OLED with 1-based plant number and 0-100%
|
||||||
snprintf(sensorText, 31, "%d: %d%%", n, sensorPercent);
|
snprintf(sensorText, 31, "%d: %d%%", n, sensorPercent);
|
||||||
oled.text(0, n*8, sensorText);
|
if (useOled) oled.text(0, n*8, sensorText);
|
||||||
|
|
||||||
return sensorPercent > 50; // over 50% is true/good, under 50% is false/bad
|
return sensorPercent > 50; // over 50% is true/good, under 50% is false/bad
|
||||||
}
|
}
|
||||||
@ -88,9 +93,12 @@ bool readAndPublishSensor(int i) {
|
|||||||
// the loop function runs over and over again forever
|
// the loop function runs over and over again forever
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long time_trigger = millis();
|
unsigned long time_trigger = millis();
|
||||||
if (last_run == 0 || time_trigger-last_run > reportingInterval) {
|
// report on first run
|
||||||
oled.erase();
|
if (last_run == 0) {
|
||||||
oled.text(0, 0, "Water:");
|
if (useOled) {
|
||||||
|
oled.erase();
|
||||||
|
oled.text(0, 0, "Water:");
|
||||||
|
}
|
||||||
bool showLed = false;
|
bool showLed = false;
|
||||||
// read + output plant sensors
|
// read + output plant sensors
|
||||||
for(int i=0;i<(sizeof(plantPin)/sizeof(plantPin[0]));i++){
|
for(int i=0;i<(sizeof(plantPin)/sizeof(plantPin[0]));i++){
|
||||||
@ -105,9 +113,19 @@ void loop() {
|
|||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
oled.display();
|
if (useOled) oled.display();
|
||||||
last_run = time_trigger;
|
last_run = time_trigger;
|
||||||
}
|
}
|
||||||
client.loop();
|
if (time_trigger > maxUptime) {
|
||||||
|
Serial.print("Sleeping now.");
|
||||||
|
delay(1000);
|
||||||
|
Serial.flush();
|
||||||
|
esp_deep_sleep_start();
|
||||||
|
Serial.print("You can't see this because I'm asleep.");
|
||||||
|
}
|
||||||
|
if (time_trigger-last_run > reportingInterval) {
|
||||||
|
Serial.print("Stayed online for a full reporting interval without sleeping or resetting, something is wrong");
|
||||||
|
}
|
||||||
|
client.loop(); // Do MQTT work
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user