Friday, November 22, 2024
HomeOpinions & PeopleTutorial on Controlling Your Home Appliances : Part I

Tutorial on Controlling Your Home Appliances : Part I

- Advertisement -
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:

- Advertisement -

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

- Advertisement -
Related Articles

1 COMMENT

  1. showing error
    Arduino: 1.6.9 (Windows 7), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)”

    Warning: Board arduino:avr:fio doesn’t define a ‘build.board’ preference. Auto-set to: AVR_FIO
    Warning: Board arduino:avr:pro328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_PRO328
    Warning: Board arduino:avr:atmega328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_ATMEGA328
    Warning: Board arduino:avr:bt doesn’t define a ‘build.board’ preference. Auto-set to: AVR_BT
    Warning: Board arduino:avr:mega doesn’t define a ‘build.board’ preference. Auto-set to: AVR_MEGA
    Warning: Board arduino:avr:pro5v doesn’t define a ‘build.board’ preference. Auto-set to: AVR_PRO5V
    Warning: Board arduino:avr:atmega8 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_ATMEGA8
    Warning: Board arduino:avr:bt328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_BT328
    Warning: Board arduino:avr:diecimila doesn’t define a ‘build.board’ preference. Auto-set to: AVR_DIECIMILA
    Warning: Board arduino:avr:atmega168 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_ATMEGA168
    Warning: Board arduino:avr:leonardo doesn’t define a ‘build.board’ preference. Auto-set to: AVR_LEONARDO
    Warning: Board arduino:avr:uno328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_UNO328
    Warning: Board arduino:avr:uno doesn’t define a ‘build.board’ preference. Auto-set to: AVR_UNO
    Warning: Board arduino:avr:mini328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_MINI328
    Warning: Board arduino:avr:mega2560 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_MEGA2560
    Warning: Board arduino:avr:nano328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_NANO328
    Warning: Board arduino:avr:lilypad328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_LILYPAD328
    Warning: Board arduino:avr:pro doesn’t define a ‘build.board’ preference. Auto-set to: AVR_PRO
    Warning: Board arduino:avr:nano doesn’t define a ‘build.board’ preference. Auto-set to: AVR_NANO
    Warning: Board arduino:avr:pro5v328 doesn’t define a ‘build.board’ preference. Auto-set to: AVR_PRO5V328
    Warning: Board arduino:avr:mini doesn’t define a ‘build.board’ preference. Auto-set to: AVR_MINI
    Warning: Board arduino:avr:ethernet doesn’t define a ‘build.board’ preference. Auto-set to: AVR_ETHERNET
    Warning: Board arduino:avr:lilypad doesn’t define a ‘build.board’ preference. Auto-set to: AVR_LILYPAD
    Blink:12: error: stray ‘\223’ in program

    const char* ssid = “myHomeWifiName”;

    ^

    Blink:12: error: stray ‘\224’ in program

    Blink:13: error: stray ‘\223’ in program

    const char* password = “MyWifiPassword”;

    ^

    Blink:13: error: stray ‘\224’ in program

    Blink:30: error: stray ‘\223’ in program

    Serial.print(“Connecting to “);

    ^

    Blink:30: error: stray ‘\223’ in program

    Blink:37: error: stray ‘\223’ in program

    Serial.print(“.”);

    ^

    Blink:37: error: stray ‘\224’ in program

    Blink:39: error: stray ‘\223’ in program

    Serial.println(“”);

    ^

    Blink:39: error: stray ‘\224’ in program

    Blink:40: error: stray ‘\223’ in program

    Serial.println(“WiFi connected”);

    ^

    Blink:40: error: stray ‘\224’ in program

    Blink:44: error: stray ‘\223’ in program

    Serial.println(“Server started”);

    ^

    Blink:44: error: stray ‘\224’ in program

    Blink:58: error: stray ‘\223’ in program

    Serial.println(“new client”);

    ^

    Blink:58: error: stray ‘\224’ in program

    Blink:64: error: stray ‘\221’ in program

    String req = client.readStringUntil(‘\r’);

    ^

    Blink:64: error: stray ‘\’ in program

    Blink:64: error: stray ‘\222’ in program

    Blink:70: error: stray ‘\223’ in program

    if (req.indexOf(“/gpio/0”) != –1)

    ^

    Blink:70: error: stray ‘\224’ in program

    Blink:70: error: stray ‘\226’ in program

    Blink:72: error: stray ‘\223’ in program

    else if (req.indexOf(“/gpio/1”) != –1)

    ^

    Blink:72: error: stray ‘\224’ in program

    Blink:72: error: stray ‘\226’ in program

    Blink:75: error: stray ‘\223’ in program

    Serial.println(“invalid request”);

    ^

    Blink:75: error: stray ‘\224’ in program

    Blink:86: error: stray ‘\223’ in program

    String s = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now “;

    ^

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\’ in program

    Blink:86: error: stray ‘\223’ in program

    Blink:87: error: stray ‘\223’ in program

    s += (val)?“high”:“low”;

    ^

    Blink:87: error: stray ‘\224’ in program

    Blink:87: error: stray ‘\223’ in program

    Blink:87: error: stray ‘\224’ in program

    Blink:88: error: stray ‘\223’ in program

    s += “\n”;

    ^

    Blink:88: error: stray ‘\’ in program

    Blink:88: error: stray ‘\224’ in program

    Blink:12: error: ‘myHomeWifiName’ was not declared in this scope

    const char* ssid = “myHomeWifiName”;

    ^

    Blink:13: error: ‘MyWifiPassword’ was not declared in this scope

    const char* password = “MyWifiPassword”;

    ^

    C:\Users\SHAFIQUE\AppData\Local\Temp\arduino_modified_sketch_876674\Blink.ino: In function ‘void setup()’:

    Blink:30: error: ‘Connecting’ was not declared in this scope

    Serial.print(“Connecting to “);

    ^

    Blink:37: error: expected primary-expression before ‘.’ token

    Serial.print(“.”);

    ^

    Blink:37: error: expected unqualified-id before ‘)’ token

    Serial.print(“.”);

    ^

    Blink:40: error: expected ‘)’ before ‘connected’

    Serial.println(“WiFi connected”);

    ^

    Blink:40: error: no matching function for call to ‘HardwareSerial::println(ESP8266WiFiClass&)’

    Serial.println(“WiFi connected”);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Temp\arduino_modified_sketch_876674\Blink.ino:40:34: note: candidates are:

    In file included from C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Stream.h:26:0,

    from C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/HardwareSerial.h:31,

    from C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Arduino.h:245,

    from sketch\Blink.ino.cpp:1:

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:79:16: note: size_t Print::println(const __FlashStringHelper*)

    size_t println(const __FlashStringHelper *);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:79:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘const __FlashStringHelper*’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:80:16: note: size_t Print::println(const String&)

    size_t println(const String &s);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:80:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘const String&’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:81:16: note: size_t Print::println(const char*)

    size_t println(const char[]);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:81:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘const char*’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:82:16: note: size_t Print::println(char)

    size_t println(char);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:82:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘char’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:83:16: note: size_t Print::println(unsigned char, int)

    size_t println(unsigned char, int = DEC);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:83:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘unsigned char’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:84:16: note: size_t Print::println(int, int)

    size_t println(int, int = DEC);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:84:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘int’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:85:16: note: size_t Print::println(unsigned int, int)

    size_t println(unsigned int, int = DEC);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:85:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘unsigned int’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:86:16: note: size_t Print::println(long int, int)

    size_t println(long, int = DEC);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:86:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘long int’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:87:16: note: size_t Print::println(long unsigned int, int)

    size_t println(unsigned long, int = DEC);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:87:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘long unsigned int’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:88:16: note: size_t Print::println(double, int)

    size_t println(double, int = 2);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:88:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘double’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:89:16: note: size_t Print::println(const Printable&)

    size_t println(const Printable&);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:89:16: note: no known conversion for argument 1 from ‘ESP8266WiFiClass’ to ‘const Printable&’

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:90:16: note: size_t Print::println()

    size_t println(void);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.h:90:16: note: candidate expects 0 arguments, 1 provided

    Blink:44: error: expected primary-expression before ‘started’

    Serial.println(“Server started”);

    ^

    C:\Users\SHAFIQUE\AppData\Local\Temp\arduino_modified_sketch_876674\Blink.ino: In function ‘void loop()’:

    Blink:58: error: expected type-specifier before ‘client’

    Serial.println(“new client”);

    ^

    Blink:64: error: ‘r’ was not declared in this scope

    String req = client.readStringUntil(‘\r’);

    ^

    Blink:70: error: expected primary-expression before ‘/’ token

    if (req.indexOf(“/gpio/0”) != –1)

    ^

    Blink:70: error: ‘gpio’ was not declared in this scope

    if (req.indexOf(“/gpio/0”) != –1)

    ^

    Blink:72: error: expected primary-expression before ‘/’ token

    else if (req.indexOf(“/gpio/1”) != –1)

    ^

    Blink:75: error: ‘invalid’ was not declared in this scope

    Serial.println(“invalid request”);

    ^

    Blink:86: error: ‘HTTP’ was not declared in this scope

    String s = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now “;

    ^

    Blink:87: error: ‘high’ was not declared in this scope

    s += (val)?“high”:“low”;

    ^

    Blink:87: error: ‘low’ was not declared in this scope

    s += (val)?“high”:“low”;

    ^

    Blink:88: error: expected primary-expression before ‘<' token

    s += “\n”;

    ^

    Blink:88: error: expected primary-expression before ‘/’ token

    s += “\n”;

    ^

    Blink:88: error: ‘html’ was not declared in this scope

    s += “\n”;

    ^

    Blink:88: error: ‘n’ was not declared in this scope

    s += “\n”;

    ^

    exit status 1
    stray ‘\223’ in program

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!

News

Solutions

Most Popular

×