IOT devices NodeMCU with ESP8266 part 2 | Botshop

IOT devices Part 2

Part 1 can be read here.
In this tutorial we will control electrical devices like fans, lights, etc., using esp8266 from a web browser.  To do that we need to get familiar with IOT devices. The Arduino sketch below shows how you can add HTML code (web page code) to the NodeMcu board so it will give you a web page when connecting to it. The web page the NodeMcu shows you will have buttons you can click on to control relays that in turn will control lights and so on.
What makes this cool is that you can connect to your NodeMcu from your wireless network from any device that got a web browser, including your phone.
Note: Make sure your browser enabled devices and the NodeMcu board is on the same wireless network. Depending on your networking knowledge you can take this a step further and configure your wireless router to do port forwarding and dynamic DNS to access your NodeMcu from anywhere on the Internet. Another good idea is to setup your router to always assign the same IP to your NodeMcu board in your router’s DHCP settings.
The components that you will need for completing this project are very simple, you need to have a NodeMcu module and a relay, make sure you buy a 5v relay, which are very easy to use with esp chips, it doesn’t require an external power supply.
We tested and sell this NodeMCU board.
You can also use this board with the easy and powerful LAU scripting language, more info can be found on the LAU website.

For this example project, I have used a 2 relay circuit. The actual program is written to connect four relays to IOT devices.

You can watch the above video on how it works and how to connect your browser to the IP address returned from esp, all the details are included in this video. The MCU IoT devices make it really easy these days.

Copy the below Arduino code and paste it into your Arduino IDE, then upload the program to your nodemcu or any other esp devices that you are using, make sure to choose the correct port and board. Also, don’t forget to change the SSID and password to your Wi-fi settings.
This program for the esp8266 also will return the status of the device, which will. in turn notify you when it’s connected to the specified network and it will also tell you what IP it received from the router.

#include <ESP8266WiFi.h>

const char* ssid = “Your Network Name”;
const char* password = “Your Password”;

; //
WiFiServer server(80);

void setup() {
Serial.begin(9600);
delay(10);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
digitalWrite(13, LOW);

// 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.print(“Use this URL to connect: “);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.println(“/”);

}

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 request = client.readStringUntil(‘\r’);
Serial.println(request);
client.flush();

// Match the request

if (request.indexOf(“/light1on”) > 0) {
digitalWrite(5, HIGH);

}
if (request.indexOf(“/light1off”) >0) {
digitalWrite(5, LOW);

}

if (request.indexOf(“/light2on”) > 0) {
digitalWrite(4, HIGH);

}
if (request.indexOf(“/light2off”) >0) {
digitalWrite(4, LOW);

}
if (request.indexOf(“/light3on”) >0) {
digitalWrite(0, HIGH);

}
if (request.indexOf(“/light3off”) > 0) {
digitalWrite(0, LOW);

}
if (request.indexOf(“/light4on”) > 0) {
digitalWrite(13, HIGH);

}
if (request.indexOf(“/light4off”) > 0) {
digitalWrite(13, LOW);

}
// Set ledPin according to the request
//digitalWrite(ledPin, value);

// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“”); // do not forget this one
client.println(“<!DOCTYPE HTML>”);
client.println(“<html>”);
client.println(“<head>”);
client.println(“<meta name=’apple-mobile-web-app-capable’ content=’yes’ />”);
client.println(“<meta name=’apple-mobile-web-app-status-bar-style’ content=’black-translucent’ />”);
client.println(“</head>”);
client.println(“<body bgcolor = \”#f7e6ec\”>”);
client.println(“<hr/><hr>”);
client.println(“<h4><center> Esp8266 Electrical Device Control </center></h4>”);
client.println(“<hr/><hr>”);
client.println(“<br><br>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 1”);
client.println(“<a href=\”/light1on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light1off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 2”);
client.println(“<a href=\”/light2on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light2off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 3”);
client.println(“<a href=\”/light3on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light3off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 4”);
client.println(“<a href=\”/light4on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light4off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“<table border=\”5\”>”);
client.println(“<tr>”);
if (digitalRead(5))
{
client.print(“<td>Light 1 is ON</td>”);

}
else
{
client.print(“<td>Light 1 is OFF</td>”);

}

client.println(“<br />”);

if (digitalRead(4))
{
client.print(“<td>Light 2 is ON</td>”);

}
else
{

client.print(“<td>Light 2 is OFF</td>”);

}
client.println(“</tr>”);
client.println(“<tr>”);

if (digitalRead(0))

{
client.print(“<td>Light 3 is ON</td>”);

}

else

{
client.print(“<td>Light 3 is OFF</td>”);
}
if (digitalRead(13))
{
client.print(“<td>Light 4 is ON</td>”);

}
else
{
client.print(“<td>Light 4 is OFF</td>”);
}

client.println(“</tr>”);
client.println(“</table>”);

client.println(“</center>”);
client.println(“</html>”);
delay(1);
Serial.println(“Client disonnected”);
Serial.println(“”);

}

Share This

Share this post with your friends!