Creating first IoT project using ESP32-Node MCU
There has been a lot of development in the field of ioT and smart homes. We live in a era where most of our devices are connected to the internet. Raspberry Pi was launched in 2012 and with it there was a rise in several IoT Smart home applications. But the problem was that raspberry pi was a bit costly. Here in India it was available for around 3000 to 4000 rupees. We had Arduino uno before but it lacked Wi-Fi and adding external wifi module was a headache. But in 2015 a new device surfaced in the market with built in wifi. It was the node mcu. It was not as powerful as raspberry pi and it didn’t even run linux os but it was powerful enough to compute and control other devices. It was the perfect cheap alternative to develop iot projects. Later ESP32 was developed with more features and processing power.
That’s enough history now let me explain how I managed to develop a smart home project.
The goal :
We should be able to control the lights using wifi.
The Idea :
ESP32 / Node MCU will act as a server
Phone / PC / Browser will be the client
Client will send the request to control the lights
Server will read the request and control the lights
Stuff you will need :
ESP 32 / Node MCU
Relay (channel will depend on the devices you want to control)
5v Mobile charger
If you don't like reading i have got a video for you that i made explaining all this stuff you can come back for code on this blog
Creating server on node mcu
1. The following code will create a server on esp32 and control the lights. Ill break and explain the code after the snippet
2. These are reqired libraries
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
3. If you are using node mcu you need the following libraries
#include "ESP8266WiFi.h"
#include "ESP8266WebServer.h"
#include "ESP8266WebServer.h"
4. Define your wifi username and password
const char *ssid = "WiFi_Name";
const char *password = "WiFi_Password;
const char *password = "WiFi_Password;
5. Define server port
WebServer server(80);
6. Define what should appear on root (eg. http://192.168.0.103/). This is like index.html file.
void handleRoot() {
server.send(200, "text/html", "Hi");
}
server.send(200, "text/html", "Hi");
}
7. Define what should happen in case of 404 fine not fond error
void handleNotFound() {
server.send(404, "text/plain", "404");
}
server.send(404, "text/plain", "404");
}
8. In first part of void setup we are just setting pin mode of esp/node pins and connecting to wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(14, OUTPUT);
pinMode(27, OUTPUT);
pinMode(26, OUTPUT);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
WiFi.begin(ssid, password);
Serial.println("");
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(14, OUTPUT);
pinMode(27, OUTPUT);
pinMode(26, OUTPUT);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
9. Reading/Handeling parameters : We want to let esp32 process and act on parameters like 192.168.0.103/allup to turn on all lights and 192.168.0.103/alldown to turn off all lights or 192.168.0.103/adja to turn on adjacent lights. So this is the code:
server.on("/alldown", []() {
digitalWrite(12, 1);
digitalWrite(13, 1);
digitalWrite(14, 1);
digitalWrite(27, 1);
server.send(200, "text/plain", "done");
});
digitalWrite(12, 1);
digitalWrite(13, 1);
digitalWrite(14, 1);
digitalWrite(27, 1);
server.send(200, "text/plain", "done");
});
10. To Read Dynamic parameters like 192.168.0.103/pin//state/ This code will work
String rawpin = server.pathArg(0);
String rawstate = server.pathArg(1);
int upin = rawpin.toInt();
uint8_t pin = (uint8_t)upin;
int ustate = rawstate.toInt();
uint8_t state = (uint8_t)ustate;
digitalWrite(pin, state);
server.send(200, "text/plain", "done");
});
String colorip = server.arg("val");
long hexcol = strtoul(colorip.c_str(), NULL, 16);
Serial.println(hexcol);
for (int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = hexcol;
FastLED.show();
delay(30);
}
server.send(200, "text/plain", String(hexcol));
}
server.handleClient();
Hardware connection :
ESP32 to relay :
On relay connect vcc gnd and signal/signals depending on relay channel
Mains connection :
Take a common ground and connect it to ground of bulb
Router config :
You need to set a static IP for esp32/node MCU in your router.
Now that everything is in place the project should work.
In my case I have created a application to convert voice to text and execute the URL. I will create a separate blog explaining how to convert voice to text and controlling the lights but for this tutorial we will do it using a browser.
We can enter the IP address of ESP32 in browser along with the parameters. I will consider that the IP of ESP32 is 192.168.0.103.
So to turn on the lights we need to execute the following url
192.168.0.103/allup
To turn off all the lights :
192.168.0.103/alldown
I guess you got my point. So that’s how you can control IoT device. If this was helpful subscribe to my youtube channel at youtube.com/encryptedguy.

