deep sleep mode, remember the variable
Posted: Thu Sep 02, 2021 4:22 pm
Hi,
I use this simple example program with esp 01 and dht11 sensor. The esp controller send the measured data via MQTT protocol to a raspberry pi.
If i use deep-sleep mode the esp module use a few uA current, I want to do a battery power. I want to wake up and measure sensor data in about every 2-3 minutes, but i only want to send it on mqtt protocol if the data has changed compared to the previous data. Because the esp module consumes a lot of power while a wifi connection.
So how can I compare the two measured data with each other if the esp module is deep asleep? It should be a variable that remember even after a deep sleep.
I use this simple example program with esp 01 and dht11 sensor. The esp controller send the measured data via MQTT protocol to a raspberry pi.
Code: Select all
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to NodeMCU (D6)
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#define wifi_ssid "dlink"
#define wifi_password "uazuazuaz"
#define mqtt_server "192.168.1.30"
//#define mqtt_user "user"
//#define mqtt_password "password"
#define humidity_topic "hum"
#define temperature_celsius_topic "cels"
#define temperature_fahrenheit_topic "fahr"
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
Serial.print("Connecting to ");
Serial.print(mqtt_server);
Serial.print(" as ");
Serial.println(clientName);
// Attempt to connect
// If you do not want to use a username and password, change next line to
if (client.connect((char*) clientName.c_str())) {
//if (client.connect((char*) clientName.c_str()), mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(2000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Wait a few seconds between measurements.
delay(100);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
// Serial.print(f);
// Serial.print(" *F\t");
// Serial.print("Heat index: ");
// Serial.print(hic);
// Serial.print(" *C ");
// Serial.print(hif);
// Serial.println(" *F");
Serial.print("Temperature in Celsius:");
Serial.println(String(t).c_str());
client.publish(temperature_celsius_topic, String(t).c_str(), true);
// Serial.print("Temperature in Fahrenheit:");
// Serial.println(String(f).c_str());
// client.publish(temperature_fahrenheit_topic, String(f).c_str(), true);
Serial.print("Humidity:");
Serial.println(String(h).c_str());
client.publish(humidity_topic, String(h).c_str(), true);
}
So how can I compare the two measured data with each other if the esp module is deep asleep? It should be a variable that remember even after a deep sleep.