IOT Light Control Over Internet _NodeMCU ESP8266

by Ashraf Minhaj in Circuits > Remote Control

20008 Views, 15 Favorites, 0 Comments

IOT Light Control Over Internet _NodeMCU ESP8266

Control Anything Over Internet / WiFi. IOT Light Switch_ NodeMCU ESP8266 .

This tutorial is all about how to control electronic devices using WiFi technology. You have heard the name of NodeMCU ESp8266 WiFi development Board. A lot of guys wrote introductions and tons of tutorials are present here on Instructables. So, Im going to focus on

  • NodeMCU Setup to upload Code using Arduino.ide
  • Connecting to WiFi network and getting IP Address
  • Controlling light using Android App over internet.

Let's proceed.

Parts:

  1. NodeMCU ESp8266 WiFi Dev. Board
  2. Micro usb cable to upload Code
  3. 330r resistor
  4. Android App

Buy electronic components at utsource.net

Pinout

nodemcu-12E-pinout.png

NodeMCU's pin out and written 'D' pins are different. Make sure to check them before you hook up anything.

We will connect an Led using with positive end on D4 and Gnd to Gnd of NodeMCU.

Setting Up Arduino.Ide

To Upload code to NodeMCU using Arduino.ide

  1. Update Arduino.ide (desktop app)start Arduino appgoto Files > Preferences Then paste the link then press ok. http://arduino.esp8266.com/stable/package_esp8266...
  2. goto Tools>Boards> Board Manager then wait untill it finds ESP8266 properties
  3. Scroll down and click install
  4. Then Restart the Arduino App.

Now you can upload Code in C / C++ to NodeMCU ESP8266 using Arduino.ide

Code:

To check IP Address (Internet Protocol) upload the code and open Serial Monitor.

/* Node MCU ESP8266 IOT wifi dev. Board wifi connectivity test code by Ashraf Minhaj.

Tutorial on blog www.youtube.com/c/fusebatti

for any quory mail at ashraf_minhaj@yahoo.com

Consider subscribing my youTube channel www.youtube.com/c/fusebatti */

#include //declare ESP8266 library
const char* ssid="Your wifi name"; //Put your wifi network name here

const char* password = "Password"; //Put your wifi password here

void setup() { Serial.begin(115200); //initial Serial communication for serial monitor Note:115200 depends on your board

Serial.println();

Serial.print("Wifi connecting to ");

Serial.println( ssid ); WiFi.begin(ssid,password); Serial.println(); Serial.print("Connecting"); while( WiFi.status() != WL_CONNECTED ) //while loop runs repeatedly unless condition is false { //it'll keep trying unless wifi is connected delay(500); Serial.print("."); } Serial.println(); Serial.println("Wifi Connected Success!");

Serial.print("NodeMCU IP Address : "); //Shows the IP (Internet Protocol) number of your NodeMcu

Serial.println(WiFi.localIP() ); //Gets the IP address of your Board } void loop() //In our case we don't need this but still it needs to be there.

{ // put your main code here, to run repeatedly: }

Now to Control our project light upload this Code....

The code is also avilable on arduino.examples. I've managed to build an app to make it work with it.

#include

const char* ssid = "Wifi name"; const char* password = PassWordl";

// 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.mode(WIFI_STA); 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\r\n \r\nGPIO is now "; s += (val) ? "high" : "low"; s += "

\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 }

Upload the Code .

Control Using App

After uploading the code Download the App and make Sure to Connect your Phone to Internet or WiFi.

Now Enter Your NodeMCU's IP Address and jus press ON the light will be on, to switch off press OFF.

Congrats,You have just made your 1st IOT project.