Wednesday, October 19, 2016

Control Devices Using Phone and Nodemcu (Tutorial-3)

Nodemcu is a great WiFi chip, that can be used in several wireless applications. In my previous post, I showed you guys, that how we can program the Nodemcu using Arduino IDE and then we learn how to use Nodemcu to measure the temperature and humidity data from DHT11 sensor, after that we used Nodemcu WiFi capabilities to send the temperature and humidity data over WiFi to the Android Phone, and the Android Application running on phone will displays the temperature and humidity data on the phone display.

In this post I will use the old code base of Nodemcu Arduino IDE and Android Code to update my project for controlling the devices connected to the Nodemcu, in short we can say that, this post is closely mapped with the Home Automation Project.

In this post, an Application running on Android Phone will get the temperature and humidity values from the Nodemcu and it will also control the 4 Relays, which are connected to the Nodemcu, all over WiFi channel, so using this setup, you can control your appliances using Android Phone over WiFi.

The Block Diagram is as follow:
Block Diagram
For driving loads/appliances you can make your own boards or you can buy the ready-made board online.
For making your own custom boards using Relays, you can take help from the following post:
Relay: A Electromagnetic Switch.
The connection Diagram is as follow:
Connection Diagram
Observation from the above diagram:
  • DHT11 is connected to D5 pin of Nodemcu
  • Relay1 is controlled using D1
  • Relay2 is controlled using D2
  • Relay3 is controlled using D6
  • Relay4 is controlled using D7
The Arduino IDE Code is as follow:
 #include <DHT.h>  
 #include <ESP8266WiFi.h>  
 #define DHTPIN D5  
 #define DHTTYPE DHT11  
 DHT dht(DHTPIN, DHTTYPE);  
 #define RELAY1 D1  
 #define RELAY2 D2  
 #define RELAY3 D6  
 #define RELAY4 D7  
 const char* ssid = "name";  
 const char* password = "password";  
 WiFiServer server(5000);  
 #define OUT_BUFFER_SIZE  6u  
 char Out_Buffer[OUT_BUFFER_SIZE] = { 0 };  
 char In_Buffer;  
 void setup()  
 {  
  Serial.begin(115200);  
  pinMode(RELAY1, OUTPUT);  
  pinMode(RELAY2, OUTPUT);  
  pinMode(RELAY3, OUTPUT);  
  pinMode(RELAY4, OUTPUT);  
  digitalWrite(RELAY1, LOW);  
  digitalWrite(RELAY2, LOW);  
  digitalWrite(RELAY3, LOW);  
  digitalWrite(RELAY4, LOW);  
  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()   
 {  
  static uint32_t dht_timestamp = 0;  
  // 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() )  
  {  
   if( client.available() )  
   {  
    // Read Data  
    In_Buffer = client.read();  
    In_Buffer = 0x0F & In_Buffer;  
    // Relay 1  
    if( In_Buffer & 0x01 )  
    {  
     digitalWrite(RELAY1, HIGH);  
     Serial.println("Relay1 On");  
    }  
    else  
    {  
     digitalWrite(RELAY1, LOW);  
     Serial.println("Relay1 Off");  
    }  
    // Relay 2  
    if( In_Buffer & 0x02 )  
    {  
     digitalWrite(RELAY2, HIGH);  
     Serial.println("Relay2 On");  
    }  
    else  
    {  
     digitalWrite(RELAY2, LOW);  
     Serial.println("Relay2 Off");  
    }  
    // Relay 3  
    if( In_Buffer & 0x04 )  
    {  
     digitalWrite(RELAY3, HIGH);  
     Serial.println("Relay3 On");  
    }  
    else  
    {  
     digitalWrite(RELAY3, LOW);  
     Serial.println("Relay3 Off");  
    }  
    // Relay 4  
    if( In_Buffer & 0x08 )  
    {  
     digitalWrite(RELAY4, HIGH);  
     Serial.println("Relay4 On");  
    }  
    else  
    {  
     digitalWrite(RELAY4, LOW);  
     Serial.println("Relay4 Off");  
    }  
   }  
   if ( millis() - dht_timestamp > 2000ul )  
   {  
    uint8_t h = dht.readHumidity();  
    uint8_t t = dht.readTemperature();  
    snprintf(Out_Buffer,OUT_BUFFER_SIZE,"%d,%d",t,h);  
    // Serial.println(Out_Buffer);  
    client.print(Out_Buffer);  
    dht_timestamp = millis();  
   }  
  }  
  client.stop();  
 }  
The Android Application:
Application Running on Android Phone
Features of Android Application
  • You can see the IP Address assigned to your Android Phone.
  • You can edit the Server IP Address according to the Server Address, which you want to connect with your Android Phone.
  • You can edit the Port Number on which you want your Android Phone and Server to communicate.
  • Temperature and Humidity Labels, where you can see the Temperature and Humidity Values.
  • Four Relay Buttons with Relay On Off status, to control the Relays connected with Nodemcu.
  • Connect button, to connect with server if not connected.
  • Disconnect button, to dis-connect with server if connected.
  • Exit Button to exit the application.
Working:
The Relays are connected with Nodemcu and based on the command received by Nodemcu from the Android Application, relays will turn-on or off, which in turn will turn-on or off your appliances.
Nodemcu is configured as server and will measure the temperature and humidity data every two seconds and stored them in a variable. If the client and server (client-Android Phone and server-Nodemcu) are connected, then Nodemcu will broadcast the temperature and humidity values every two seconds to the client.
Nodemcu’s IP address is assigned by the WiFi Router (In this next post, WiFi Router will be removed and Nodemcu will directly connect with the Android Phone over WiFi), this assigned IP address is the Server IP Address, and for connecting the Android Phone with the Nodemcu, you need to enter this Server IP Address, in the Android Application.

For connection, press the Connect button on Android Application, if everything is fine the Android Application and Nodemcu will get connected, and you will see a message for "Connected", if there is some problem, then you will see “Server Not Available” message.
After connection is successful, you will see the temperature and humidity values on Android Phone Application, and now you can Turn-On or Off the relay by pressing the respective button.
Have a look at this video, to see how the complete setup works.



Click here to download the Firmware and Android APK.

7 comments:

  1. Hey how to full ready configure after I'm pay with you my co.0814988629

    ReplyDelete
    Replies
    1. First of all i didn't understand that properly.
      Why you want to pay?
      Source code for this project and Android apk is available for download.
      Only Android source code is not present which most if the guys don't require.

      Delete
  2. Hey, great tutorial. Are there applications available to get this working on iPhone?

    ReplyDelete
    Replies
    1. Thanks.
      No applications for ios as I don't have iPhone.
      In future, if i buy a iPhone i will definitely post the app here.

      Delete
  3. The requirement of my project is to have two nodeMCU connected to same router. I want to give the command to both nodeMCU with same APK without changing IP address. Is it possible? and what kind of changes should I make in code as well as APK? can I get the APK code for the same?

    ReplyDelete
    Replies
    1. As per my understanding two nodemcu devices on the network can't have the same ip address, there must be two ip address for each nodemcu.

      You can also look at mqtt, but again you need two ip addresses for both nodemcu, but mqtt will handle that things so you need just one ip address of the server running mqtt, but right now i don't have example for that with me.

      For apk code contact at matlab.academy@gmail.com

      Delete
  4. This comment has been removed by the author.

    ReplyDelete