Tutorial on Controlling Your Home Appliances : Part I

30357
IoT
IoT
Satya Sankar Sahoo, Embedded software developer

Automate your house hold appliances with this simple tutorial. With open source softwares and components available in plenty ,  create your own creative DIY‘s.

The tutorial provides you with step by step process of creating android app and controlling home appliances over internet using ESP8266 wifi chip.

Requirements

Hardware Needed:

  • Node MCU V1.0 or ESP8166-12E
  • USB Data Cable

Software Needed:

Miscellaneous:

  • Wifi Network Acess(Home wifi router)

While going through this tutorial a little familiarity with Arduino is required. The programming code is be written for ESP8266 wifi chip using Arduino IDE, for which installation of  ESP8266 library is required.  Click here to setup library for ESP.

Inthis tutorial NodeMCU V1.0 has been used, which is ESP8266 breakout board with usb-ttl option. This provides for easy connection to your PC and arduino IDE. If you are connecting Node MCU board for the first time you would require usb drivers. These drivers could be download by clicking here.

Step 1:

After Connecting Node MCU(Multi-point Control unit) to PC , open Arduino IDE, set com port and choose NODE MCU 1.0 from board as circled in the image below.

Board and Port Selection
Board and Port Selection Fig. 1 Board and Port Selection

Step 2:

Copy the below code to IDE.

/*
*  This sketch demonstrates how to set up a simple HTTP-like server.
*  The server will set a GPIO pin depending on the request
*    http://server_ip/gpio/0 will set the GPIO2 low,
*    http://server_ip/gpio/1 will set the GPIO2 high
*  server_ip is the IP address of the ESP8266 module, will be
*  printed to Serial when the module is connected.
*/
 
#include <ESP8266WiFi.h>
 
const char* ssid = “myHomeWifiName”;
const char* password = “MyWifiPassword”;
 
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
  
  // 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”);
  
  // Start the server
  server.begin();
  Serial.println(“Server started”);
 
  // Print the IP address
  Serial.println(WiFi.localIP());
}
 
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.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil(‘\r’);
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf(“/gpio/0”) != 1)
    val = 0;
  else if (req.indexOf(“/gpio/1”) != 1)
    val = 1;
  else {
    Serial.println(“invalid request”);
    client.stop();
    return;
  }
 
  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();
 
  // Prepare the response
  String s = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now “;
  s += (val)?“high”:“low”;
  s += “</html>\n”;
 
  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println(“Client disonnected”);
 
  // The client will actually be disconnected
  // when the function returns and ‘client’ object is detroyed
}

Step 3:

In the above code mention the wifi network SSID and Password as shown below:

const char* ssid = “You WIFI SSID Name”;

const char* password = “your wifi password”;

Step 4:

Compile and Upload code to Node MCU(Multi-point Control unit) and this brings us to the end of coding part.

Step 5:

Now, Open Serial Terminal of Arduino IDE and press reset button on ESP8266. Now it will connect to your wifi network and show you an IP address in terminal.

Step 6:

Go to your browser enter URL like below

http://YourESP8266IPAddress/gpio/0 will set the GPIO2 low,

http://YourESP8266IPAddress/gpio/1 will set the GPIO2 High,

You can observe that the Node MCU(Multi-point Control unit) LED toggles between on and off when you press the above URL.

Stay tuned to the Part II of this tutorial soon.


Courtsey: Control home appliances Part I