Sunday, October 23, 2016

Control Devices Over WiFi Using Phone without Router (Tutorial-4)

Nodemcu is a great WiFi chip, that can be used in several wireless applications. In my previous post, i showed you guys how to control devices using Nodemcu and Android Phone, and i received overwhelming response from you guys, and all due to this, my last post "Controlling Devices Using Nodemcu", is now one of the popular posts of my blog.
But there was one dependency in that project, the involvement of WiFi router. In the last post, first you have to connect Nodemcu with the Router and then you have to connect the Android Phone with the same router, but as Nodemcu can behave as Access Point, so we can bypass the router and directly connect our Android Phone with Nodemcu, by doing some minor changes in the Nodemcu Code.
The Block Diagram is as follow:

Block Diagram
The Updated Arduino IDE Code is as follow:

#include <DHT.h>  
#include <ESP8266WiFi.h>  
#include <WiFiClient.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 = "Nodemcu";  
const char* password = "12345678";  
WiFiServer server(5000);  
#define OUT_BUFFER_SIZE  6u  
char Out_Buffer[OUT_BUFFER_SIZE] = { 0 };  
char In_Buffer;  
void setup()  
{  
  delay(100);  
  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);  
  // Create Access Point  
  WiFi.softAP("Configuting Access Point..");  
  WiFi.softAP(ssid, password);  
  IPAddress myIP = WiFi.softAPIP();  
  Serial.println("Access Point IP is: ");  
  Serial.println(myIP);  
  // 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();  
}

You can compare this code, with the code from the last post, and you will see, the changes are very less, see the following comparison snapshot.
Changes
After flashing the above code in Nodemcu, first of all you have to connect your Android Phone with the Nodemcu.
In code the following lines are written, which is your WiFi name and Password.
 const char* ssid = "Nodemcu";  
 const char* password = "12345678";  

Just follow the procedure as given in images below.
Search For the Access Point Name "Nodemcu".
Enter the Nodemcu Password.
Enter the Server IP which in most case will be 192.168.4.1
In most of the cases, the server IP will remain 192.168.4.1, but still its better to check the IP address of Nodemcu, by sending it over the serial port. Rest all of the things are same and I don't want to post the same content again and again, you can read from that post.


Arduino code can be copied and pasted from the links given above, while Android APK can be downloaded from this link.

25 comments:

  1. Dear Sir,
    I am new in this field.Please provide me source code for this.
    "Control Devices Over WiFi Using Phone without Router (Tutorial-4)"

    E-mail : amityadav8679@gmail.com

    Thanks in advance.

    ReplyDelete
  2. Dear Sir,
    I am new in this field.Please provide me source code for this.
    "Control Devices Over WiFi Using Phone without Router (Tutorial-4)"

    E-mail : amityadav8679@gmail.com

    Thanks in advance.

    ReplyDelete
    Replies
    1. You can download the Arduino Source code and Android Apk from this post only.

      Delete
    2. where can I download it?

      Thanks.

      Delete
    3. Arduino Code can be copied and pasted from the blog post.
      Android APK can be downloaded from the link given at the bottom of the post.
      https://drive.google.com/open?id=0B0ppa9IHkik0Q3BtTGlCbHc4X0k

      Delete
    4. https://github.com/xpress-embedo/HomeAutomation

      Delete
  3. Thanks for this great post! if you are facing any troubleshoot or any problem like
    routerlogin
    netgear setup

    ReplyDelete
    Replies
    1. Sorry I didn't understand the problem.
      This post doesn't have any use of router.

      Delete
  4. Hi
    I need B4A source code.
    Help me please

    ReplyDelete
  5. Could you please send e androdi source code for single button

    ReplyDelete
  6. Hi,
    Thanks for your code, it is so helpful.
    I've used you're code to connect raspberry to esp.
    but I'am Countering an issue that my esp after number of loop it log out from the client.connected loop although, raspberry still connect and receive none instead of values.
    and for some reason esp don't log on the condition client.available and do what it is inside, just log on to client.connected and only for a short time.
    what do you think the problem is?
    looking forward your reply.
    Regards.

    ReplyDelete
    Replies
    1. Hi! In my opinion, Nodemcu is a great WiFi chip, that can be used in several wireless applications. It's really cool to control devices using Nodemcu and Android Phone and I am wondering what I have to change in your code to work in ESP8266-07, because seems to not function at all in ESP8266-01.

      Delete
  7. Dear Sir,
    thank you so much, your tutorial is so helpful.
    I am wondering what I have to change in your code to be worked in ESP8266-07, it is work with my ESP-01 but not 07.
    I am looking forward to hearing your suggestion.
    Regards.

    ReplyDelete
  8. Dear Sir,
    thank you so much, your tutorial is so helpful.
    I am wondering what I have to change in your code to be worked in ESP8266-07, it is work with my ESP-01 but not 07.
    I am looking forward to hearing your suggestion.
    Regards.

    ReplyDelete
  9. How can I reread/refresh temp or humidity without disconnect and connect on application ?

    ReplyDelete
  10. How can I reread/refresh temp or humidity without disconnect and connect on application ?

    ReplyDelete
  11. Amazing nice work. This is very useful article. Thank you. For more

    ReplyDelete
  12. how to connect it with internet and name the buttons
    instead of relays

    ReplyDelete
  13. Hi,
    I was trying DL HomeAutomation.zip from Google Drive, but the Google say that the file is infected with virus and only the owner is allowed to download infected files!?

    ReplyDelete
    Replies
    1. Updated the project on GitHub.
      Similar to this one.
      https://github.com/xpress-embedo/HomeAutomation

      Delete
    2. I'm wondering if you can update the Google Drive link to get the app. It's not working for me. Cheers.

      Delete
    3. I really don't know the problem, even I can't access them, the complete code is available on GitHub now
      https://github.com/xpress-embedo/HomeAutomation

      Delete