
Wemos D1 R2 (ESP8266)
I am try to build a sensor with low power consumption, low enough for be battery operated.
This sensor will acquire temperature and humidity readings and transmit them to my Raspberry Pi controller via HTTP requests.
The Raspberry Pi will utilize a webpy service running on port 8080 to collect data from various sensors deployed throughout the home. I have previously used webpy for similar sensor projects (see my previous post here), although Node.js could have been a suitable alternative.
The primary objective is to develop a battery-powered sensor. At the time, the Wemos D1 R2 offered the lowest power consumption. However, I have since discovered alternative components capable of broadcasting radio information with significantly lower power consumption, making them more suitable for battery-powered applications. Additionally, it is believed that removing the two on-board LEDs could further reduce power consumption, potentially extending battery life for several months. I plan to explore this hypothesis in future posts.
To optimize power consumption, the sensor will enter a sleep mode for 5 minutes, periodically waking up to acquire and transmit measurement data.
The solution below last 4 to 5 days on a 18650 battery.
#include #include // replace with your SSID and password const char* ssid = "xxxxxx"; const char* password = "xxxxxx"; const char* host = "xxx.xxx.xxx.xxx"; //Server IP const int port = 8080; //Server Port const int sleepSeconds = 300; //to save power i send the ESP to sleep #define DHTPIN D2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); WiFiClient client; void setup() { Serial.begin(115200); delay(10); dht.begin(); WiFi.begin(ssid, password); Serial.println(); 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()); pinMode(D0, WAKEUP_PULLUP); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } WiFiClient client; Serial.println("Start http client"); if (!client.connect(host, port)) { Serial.println("connection failed"); return; } Serial.println("temp call"); String url ="/insert/?device=main_bedroom"; url +="&type=Temp"; url +="&value="; url += String(t); Serial.println(url); //This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(100); Serial.println("Response"); // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.println(line); Serial.print(" temp http req"); } Serial.println("Stop http client"); client.stop(); Serial.println("Start http client"); if (!client.connect(host, port)) { Serial.println("connection failed"); return; } Serial.println("humidity call"); url ="/insert/?device=main_bedroom"; url +="&type=Humidity"; url +="&value="; url += String(h); Serial.println(url); //This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(100); Serial.println("Response"); // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.println(line); Serial.print(" humidity http req"); } Serial.println("Stop http client"); client.stop(); Serial.print("Temperature: "); Serial.print(t); Serial.print(" degrees Celsius Humidity: "); Serial.print(h); Serial.println(" Sending data to sql pi"); Serial.println("Waiting..."); // convert to microseconds ESP.deepSleep(sleepSeconds * 1000000); }
Below the blogs that mostly helped me to build my code:
http://www.esp8266learning.com/dht11-sensor-data-to-thingspeak-using-a-wemos-d1.php
https://www.losant.com/blog/making-the-esp8266-low-powered-with-deep-sleep