ESP8266 & DHT22 Lowpower MQTT Sensor
For my project where I control my remote via pnps I want to create a low power sensor which will be able to send humidity & temperature data to my openhab server which is running mosquitto
Since I have some esp8266 chips laying around, this would be great to setup as a wifi mqtt sensor with the dht22, which will periodically (every 10 minutes) send new sensor data to my mqtt broker.
A quick video how to get a basic breadboard setup:
I will need it to be pretty energy efficient, so whenever the esp is done sending data, it will go into deepsleep for 10 minutes, and then wakeup and send again…
My Test setup:
- in order to be able to upload a sketch, you need to connect GPIO0 to gnd
- In order to be able to wakeup the esp you need to :
- connect GPIO16 <-> RESET
- connect GPIO0 & GPIO2 To high (with a resistor, if your esp didn’t come with a breakout board with resistors.
Needed libraries:
My test Code:
/* To install the ESP8266 board, (using Arduino 1.6.4+): - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": http://arduino.esp8266.com/stable/package_esp8266com_index.json - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" - Select your ESP8266 in "Tools -> Board" */ #include <ESP8266WiFi.h> #include <PubSubClient.h> #include "sensor.h" /** * Wifi & MQTT Settings */ const char* ssid = "YOURSSID"; const char* password = "YOURPASS"; const char* mqtt_server = "YOURMQTTIP"; WiFiClient espClient; PubSubClient client(espClient); char msg[50]; float tmp = 0; float humidity = 0; void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); setupSensor(); } /** * Connect to wifi */ void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, 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 loop() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); client.loop(); }else{ delay(2000); } } delay(dht.getMinimumSamplingPeriod()); tmp = getTemperature(); humidity = getHumidity(); dtostrf(tmp, 6, 2, msg); client.publish("temp/bathroom", msg); dtostrf(humidity, 6, 2, msg); client.publish("humidity/bathroom", msg); //Sleep for 120 seconds, then wakeup and send data again ESP.deepSleep(120 * 1000000, WAKE_RF_DEFAULT); }
Because I like to seperate things, my Sensor.h file (which is included in above file)
#include "DHT.h" DHT dht; void setupSensor() { dht.setup(14,dht.DHT22); // data pin 2 } float getHumidity() { return dht.getHumidity(); } float getTemperature() { return dht.getTemperature(); }
At this point I’m measuring 88uA with my multimeter, when the esp8266 is sleeping.
Based on the above combination (DHT22 + ESP8266), may I know the battery life?
Batterylife greatly depends on how many measurements you need, if you need a measurement every 2 minutes as in the example, using 2 aaa batteries:
A 1000mA battery would have 1000mA * 3600 = 3.600.000 mAseconds
with 2 minutes sleeptime:
Usage of battery:
8 seconds of 80mA
120 seconds of 88uA (= 0.088mA)
0.088 * 120 = 10.56mAsec
80mA * 8 seconds = 640 mAsec
Usage per 2 minute cycle = 650.56mAsec
3.600.000 / 650.56mAsec = 5538 cycles
divided by 30 cycles in an hour = 184.60 hours = +/- 7.7 days
Compared to running the measurement once every hour:
0.088 * 3600 = 316.80mAsec
80mA * 8 seconds = 640 mAsec
Usage per cycle = 956.80mAsec
3.600.000 / 650.56mAsec = 5533 cycles
divided by 1 cycles in an hour = 5533 hours = +/- 230 days
I have changed my approach to this, to Furthermore improve batterylife, I dont really need to send the measured data every time, In my case I want the ventilation to turn when taling a shower, then humidity is > 75%
and then turn of when its 75% to <70% or the other way around.
You could also choose to keep track of the latest sent value and only send if the next value has a difference larger than 5%
Based on the above combination (DHT22 + ESP8266), may I know the battery life?
Batterylife greatly depends on how many measurements you need, if you need a measurement every 2 minutes as in the example, using 2 aaa batteries:
A 1000mA battery would have 1000mA * 3600 = 3.600.000 mAseconds
with 2 minutes sleeptime:
Usage of battery:
8 seconds of 80mA
120 seconds of 88uA (= 0.088mA)
0.088 * 120 = 10.56mAsec
80mA * 8 seconds = 640 mAsec
Usage per 2 minute cycle = 650.56mAsec
3.600.000 / 650.56mAsec = 5538 cycles
divided by 30 cycles in an hour = 184.60 hours = +/- 7.7 days
Compared to running the measurement once every hour:
0.088 * 3600 = 316.80mAsec
80mA * 8 seconds = 640 mAsec
Usage per cycle = 956.80mAsec
3.600.000 / 650.56mAsec = 5533 cycles
divided by 1 cycles in an hour = 5533 hours = +/- 230 days
I have changed my approach to this, to Furthermore improve batterylife, I dont really need to send the measured data every time, In my case I want the ventilation to turn when taling a shower, then humidity is > 75%
and then turn of when its 75% to <70% or the other way around.
You could also choose to keep track of the latest sent value and only send if the next value has a difference larger than 5%