Tuesday, August 8, 2017

IOT- weather station with NodeMCU and deep sleep mode

In this tutorial, we are going to monitor the temperature and humidity level and send to thingspeak interface for analysing. In this process, we are going to use NodeMCU as a hardware and thingspeak interface for data collection with addition of the DeepSleep feature. 
(Deepsleep feature puts controller(ESP8266) in the sleep mode to reduce the power consumption)
Interface with Thingspeak is easy to work with, You need thingspeak API key to get an access of server and data transfer. On the hardware side, you required DTH11 connected to NodeMCU.  

Basic you need to know,
A) NodeMCU integration with Arduino IDE
B) Basic hardware
a.      NodeMCU
b.      Breadboard
c.       DTH11 sensor
d.      Battery (for standalone device)
e.      Internet connection 😉
       C) Thingspeak – open source IOT platform to store and retrieve data over HTTP protocol. It enables the creation of sensor logging applications, location tracking applications and a social network of things with status updates.

To start with thingspeak, visit: https://thingspeak.com/, and creat your account to start with.


Create an account and log in,


Go to create a channel from top left menu and click on new channel.



Add a channel name and description, field1 is temperature and field2 is humidity (It will be defined by Arduino code, so please make sure we keep a same field name as above).
Save this setting and make a channel.



After you finish, Go to channel name and channel setting.


You will see a channel ID, Percentage of complete (Used) and other information as defined.





Click on API key to collect your write API key number, It will allow NodeMCU to write on this page.

Hardware design


Connect NodeMCU to the breadboard with DTH sensor and resistor as shown above.

DTH11 sensor: Pin number D1 with 10K resistor to VCC.

Breadboard connection:

Software:
ESP8266 chip provides 3 configurable sleep modes. We can choose and configure the sleep mode as required.
The 3 sleep modes are:
Modem-sleep
Light-sleep
Deep-sleep



To enable a deepsleep mode, We can call the interface function system_deep_sleep to immediately enable Deep-sleep. In this mode, the chip will turn off Wi-Fi connectivity and data connection; only the RTC module is still working, responsible for periodic wake-ups. but to awake a chip from deepsleep we need to do some modification in a hardware like.



You need to connect D0 pin (Ping number 16) to reset, so whenever you counter reach out deepsleep time, it will enable a reset. Meaning it will reset a chip and code will work in normal mode.

Code:
Before you start, make sure your Arduino IDE has installed ESP8266 board with thingspeak library.
Thingspeak library for Arduino and ESP compatible:

Arduino Code:
#include <DHT.h>
#include <ESP8266WiFi.h>

// replace with your channel’s thingspeak API key,
String apiKey = "your API";
const char* ssid = "Wifi network name";
const char* password = "password";

const char* server = "api.thingspeak.com";
#define DHTPIN 5 // what pin we’re connected to
const int sleepTimeS = 900; // second 15min (60*15) connect reset with 16 number pin for wake upcall

DHT dht(DHTPIN, DHT11,15);
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();

WiFi.begin(ssid, password);

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");

}

void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if (client.connect(server,80)) { 
String postStr = apiKey;
postStr +="&field1="; // As field1 is temperature at thingspeak site
postStr += String(t);
postStr +="&field2="; // As field2 is humidity at thingspeak site
postStr += String(h);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Thingspeak");

Serial.println("deepsleep");
ESP.deepSleep(sleepTimeS * 1000000);  //deepsleep defination 

}
client.stop();
Serial.println("Waiting…");

}

.................................................................................................................................................................................


After some days. A screen shot from Thingspeak.



Test results 
Before Deepsleep feature, device was taking current around  84.1 mA (Including NodeMCU circuits with DTH11)


After DeepSleep mode (almost 5.5 time less than normal mode)


Final Circuit with battery connection, As a stand alone device.





0 comments:

Post a Comment