Getting started with IOT Project (NODE MCU ESP8266)

on Monday, April 30, 2018








Hello everyone. In this article we are going to see the basic project to get started with IOT (Internet of things). If you are already familiar with arduino projects then this articles helps you to step into IOT projects. Giving short introduction to IOT. Internet of things means  connecting electronic devices to internet or network through WiFi or Ethernet. So we can control them or getting data from the devices over internet obviously any where from the world. But today here we are going to see WiFi network level controlling of devices. To do that we are going to use ESP8266 wifi module. ESP8266 is a WiFi module which consists programmable I/O pins like arduino and also it can be programmable using Arduino IDE. Lets get started with the practical. We are going to see two things in this practical.

  1. Configure Arduino IDE to program ESP8266 wifi module.
  2. Program mini server using ESP8266 wifi module in our network to control LED.

1.Configure Arduino IDE to program ESP8266 WiFi module:

 The first step in configuration, install ESP8266 board manager in  Arduino IDE. For that copy the below link:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Open ArduinoIDE and goto file --> Preferences and paste the copied link in the "Additional Board Manager URLs:" then click ok.

Then goto Tools -- > Board -- > Boards Manager then a new window will open. Search ESP8266 Then install the search result as shown in video. Then restart the arduino IDE. goto Tools -- > Board -- > Boards Manager and scroll down then select the option NodeMCU 1.0.

ESP8266 board manager detail  :  https://github.com/esp8266/Arduino

Now connect the ESP8266 to computer through  USB Data cable.

Now we need the install the driver for ESP8266 (It may vary based on the board. For most case you can see the driver name in the back side of the ESP8266 board).

For my board i need to install driver  CH340G. it is available in below link:

https://wiki.wemos.cc/downloads


Some other board require CP210X drivers. To get that visit link below:

https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

After install the driver. Let us upload the sample blink code given below into ESP8266 module using Arduino IDE

Blink Code :

int LED = D2;
void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  digitalWrite(LED, HIGH);  
  delay(1000);                      
  digitalWrite(LED, LOW);   
  delay(1000);                     
}

2.Program mini server using ESP8266 WiFi module in our network to control LED.

In this part we are going to make ESP8266 to act as a mini server in our WiFi network. The ESP8266 has simple html page host in it. If it connected to our WiFi or mobile hotspot then it host that html page in particular IP address which can accessible with in our network. That html page consist buttons to control LED connected in ESP8266 module D2 pin as shown in video. Code for this functionality given below. Upload the code to ESP8266 and open any mobile or computer (which connected in the same WiFi network or Mobile hotspot that ESP8266 connected.)browser go to the IP address which will shown in serial monitor of arduino IDE after uploading the code.

Mini Server code to control LED :

#include <ESP8266WiFi.h>
#define LED D2

unsigned char led_status=0;
const char* ssid = "Wifi or mobile hotspot username";
const char* password = "Wifi or mobile hotspot password";

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  Serial.print("CONNECTING TO");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(400);
    Serial.print("-");
  }
  Serial.println("");
  Serial.println("WIFI CONNECTED!");

  server.begin();
  Serial.println("MINI SERVER STARTS AT : ");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("CLIENT FOUND");
  while (! client.available())
  {
    delay (1);
  }
 
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
 
  if (req.indexOf("/lightoff") != -1)  {
    led_status=0;
    digitalWrite(LED, LOW);
    Serial.println("LED OFF");
  }
  else if(req.indexOf("/lighton") != -1)
  {
    led_status=1;
    digitalWrite(LED,HIGH);
    Serial.println("LED ON");
  }

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("");

client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>IOT</title>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
client.println("</head>");
client.println("<body style=\"height:100%;margin:0;background:#f6f6f6\">");
client.println("<div style=\"height:6%;text-align:center;padding:3px;background:#4caf50;color:white;\">");
client.println("<h3>IOT</h3>");
client.println("</div>");
client.println("<br/>");
if(led_status == 1)
client.println("<div style=\"color:#f57c00;text-align:center;font-size:20px;font-weight:bold;\">LIGHT ON</div>");
else
client.println("<div style=\"color:#f57c00;text-align:center;font-size:20px;font-weight:bold;\">LIGHT OFF</div>");

client.println("<div style=\"color:#212121;text-align:center;\">status</div>");
client.println("<br/>");
client.println("<br/>");
client.println("<div style=\"text-align:center;\">");
client.println("<a href=\"/lighton\"\" style=\"color:white;background:#607d8b;padding:10px;border-radius:10px;text-decoration: none;\">LIGHT ON </a>");
client.println("</div>");
client.println("<br/>");
client.println("<br/>");
client.println("<div style=\"text-align:center;\">");
client.println("<a href=\"/lightoff\"\" style=\"color:white;background:#607d8b;padding:10px;border-radius:10px;text-decoration: none;\">LIGHT OFF </a>");
client.println("</div>");
client.println("</body>");
client.println("</html>");


  delay(1);
  Serial.println("RESPONSE SENT");
  Serial.println("");

}



Play with the project. Thank you for reading.














0 comments:

Post a Comment