Monday, October 10, 2016

Get Temperature and Humidity Data on Phone using Nodemcu (Tutorial-2)

In one of my previous post, we learn how to get started with NodeMCU/ESP8266 using Arduino IDE. Click here to read the post.

And in my last post i used Arduino UNO to get the DHT11 Sensor Data, and then used this data to plot the graph on LabVIEW. Click Here to read the post.
In this post i will use sketch from my last post and use it to measure the temperature and humidity using Nodemcu/ESP8266.

But here is a catch, due to some reason pin mapping doesn't work for Nodemcu/ESP8266 in Arduino IDE, what i mean to say is that, suppose you had connected the sensor on pin "D5" of Nodemcu/ESP8266, and when you write your code you will simply use the following statement in your code.
 DHT11.attach(5);  
Trust me, this will not work, so the workaround for this is to add the following few lines of the code for mapping the pins whenever using Nodemcu/ESP8266 with Arduino IDE.
 static const uint8_t D0  = 16;  
 static const uint8_t D1  = 5;  
 static const uint8_t D2  = 4;  
 static const uint8_t D3  = 0;  
 static const uint8_t D4  = 2;  
 static const uint8_t D5  = 14;  
 static const uint8_t D6  = 12;  
 static const uint8_t D7  = 13;  
 static const uint8_t D8  = 15;  
 static const uint8_t D9  = 3;  
 static const uint8_t D10 = 1;  

The complete code for getting the temperature and humidity value is as follow:
 #include <dht11.h>  
 static const uint8_t D0  = 16;  
 static const uint8_t D1  = 5;  
 static const uint8_t D2  = 4;  
 static const uint8_t D3  = 0;  
 static const uint8_t D4  = 2;  
 static const uint8_t D5  = 14;  
 static const uint8_t D6  = 12;  
 static const uint8_t D7  = 13;  
 static const uint8_t D8  = 15;  
 static const uint8_t D9  = 3;  
 static const uint8_t D10 = 1;  
 dht11 DHT11;  
 void setup()  
 {  
  DHT11.attach(D5);  
  Serial.begin(9600);  
  Serial.println("DHT11 TEST PROGRAM ");  
  Serial.print("LIBRARY VERSION: ");  
  Serial.println(DHT11LIB_VERSION);  
 }  
 void loop()  
 {  
  Serial.println("\n");  
  int chk = DHT11.read();  
  switch (chk)  
  {  
   case 0:  
   Serial.print("Humidity (%): ");  
   Serial.println(DHT11.humidity, DEC);  
   Serial.print("Temperature (C): ");  
   Serial.println(DHT11.temperature, DEC);  
   break;  
   case -1: Serial.println("Checksum error"); break;  
   case -2: Serial.println("Time out error"); break;  
   default: Serial.println("Unknown error"); break;  
  }  
  delay(2000);  
 }  
Output of the above Program
But this is not what i want to show in this post, the main attraction of this post is WiFi capabilities of Nodemcu/ESP8266.
Block Diagram
So in this post i will sense the temperature and humidity using Nodemcu/ESP8266 and then make it as web server (IP Address to it will be dynamically assigned by my router, but is fixed always), and then Android PC running DHT11 Application, will receive the temperature and humidity data over WiFi from Nodemcu/ESP8266.

The Nodemcu/ESP8266 program is as follow:

  #include <DHT.h>   
  #include <ESP8266WiFi.h>   
  #define DHTPIN D5   
  #define DHTTYPE DHT11   
  DHT dht(DHTPIN, DHTTYPE);   
  const char* ssid = "Wifi_Name";   
  const char* password = "your_password";   
  WiFiServer server(5000);   
  void setup()   
  {   
  Serial.begin(115200);   
  dht.begin();   
  delay(10);   
  // Connect to WiFi network   
  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");   
  // Print the IP address   
  Serial.println(WiFi.localIP());    
  // Start the server   
  server.begin();   
  Serial.println("Server started");   
  }   
  void loop()    
  {    
  // Check if a client has connected   
  WiFiClient client = server.available();   
  if (!client)    
  {   
   return;   
  }   
  // Wait until the client sends some data   
  Serial.println("new client");   
  while( client.connected() )   
  {   
   uint8_t h = dht.readHumidity();   
   uint8_t t = dht.readTemperature();   
   char buffer[6];   
   snprintf(buffer,6,"%d,%d",t,h);   
   Serial.println(buffer);   
   client.print(buffer);   
   delay(2000);   
  }   
  client.stop();   
  }   

The library used for getting DHT11 sensor data in above program is the updated version of library we used in our last post, and this library support DHT11, DHT21 and DHT22 sensors, and can be downloaded by clicking here.
Note: This updated version of library doesn't work with Arduino UNO and that's why i have to use the older library.

The Android Application which receives data from Nodemcu/ESP8266 is written in Basic4Android
B4A includes all the features needed to quickly develop any type of Android application. B4A is used by tens of thousands of developers from all over the world, including companies such as NASA, HP, IBM and others. Together with B4i you can now easily develop applications for both Android and iOS.
Android Application
Description

  • Nodemcu/ESP8266 will connect with the WiFi router, then WiFi router will assign IP Address to the Nodemcu/ESP8266, you can configure the router to always assign fixed IP address to the Nodemcu/ESP8266 board.
  • Connect your Phone with the WiFi Router.
  • After connecting, open the DHT11 application on your Android Phone and click on connect button, if the connection is successful, then you will get connected message and if not then you will get server not available.
  • Now you can see the temperature and humidity values on your Android Phone.
Have a look at this video to see how the complete setup works.


Click Here to Download the Arduino IDE and Android Code.

15 comments:

  1. hello sir..please help me i am getting this error"Arduino: 1.8.1 (Windows 7), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

    In file included from C:\Program Files\Arduino\libraries\DHT-sensor-library-master\DHT_U.cpp:22:0:

    C:\Program Files\Arduino\libraries\DHT-sensor-library-master\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1
    Error compiling for board NodeMCU 1.0 (ESP-12E Module).

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences."

    ReplyDelete
    Replies
    1. IMPORTANT: As of version 1.3.0 of the DHT library usefull you will need to install the library Adafruit_Sensor, All which is available from the Arduino library manager:

      So in short download this library from the link given below
      https://github.com/adafruit/Adafruit_Sensor

      Add this to Arduino Libraries folder and you are done.
      To be honest this is the latest changes to the DHT library, which is causing this error, earlier it was not present that's why my program runs without any problem.

      Delete
  2. Hello Sir, It is perfect solution. Works fine. but i want to add extra Switches to control. May i have your android source code project please. I've downloaded the code from the link you mentioned but some files are missing. Can you give me the complete android project. That would be great help. Thank you.

    ReplyDelete
    Replies
    1. I am glad it works for you.
      Please contact us at matlab.academy@gmail.com for Android Source code.

      Delete
    2. Hello Sir, Thank you for your reply...I have mailed you.

      Delete
    3. Hi Embedded Laboratory, did you program in Android Studio?

      Delete
  3. hiii, is it possible to make a call using nodemcu with android, if yes then please let me know what should i do, i am new into this field...
    thanks

    ReplyDelete
  4. Please contact the developer at matlab.academy@gma.com

    ReplyDelete
  5. Hello sir, I have gone through your article and its great.
    I working on some thing similar to this. Can i get the Android source code from you. thanks

    ReplyDelete
    Replies
    1. Thanks
      Contact developer at matlab.academy@gmail.com

      Delete
    2. Sir thank you for your reply. I have mailed you . Please Check

      Delete
  6. Hello, You are sharing valuable & unique data for monitoring environmental conditions at phone. It improves our knowledge. Keep sharing!

    ReplyDelete