IOT Home Automation using Blynk and NodeMCU
NodeMCU is very popular in Home Automation. It’s WiFi capabilities and Arduino IDE support making it easier for IoT Applications. It is very tiny and has many Digital I/O pins, Serial Communication and I2C Communication. NodeMCU has a micro USB port to program it using your existing mobile cable (no additional programmer needed). There is a successor called ESP32 Development board which has more Analog pins and Digital pins. You can use any one of them for this project according to your requirements. Here we will be using NodeMCU.
Blynk is a mobile application which has its own server to process user requests. It is an open source application and anybody can use it in their Home Automation to control devices, monitor sensor data and get a notification by some trigger actions. It has a nice GUI with Graphs, Timers, Slider, Joystick and even Video Streaming. You can also make your own app and publish it in Google play store.
Components required:
Hardware:
Software required:
Circuit Diagram:
Example Relay connection with AC Devices. Consider the relay as a switch:
Note: For example purpose, we have connected Relay at D0, D1, D2 and D3. You need to make sure there should not be alternate functions to that pins. Because the D0 pin has connected with Onboard LED (D4 has TXD1 function), whenever you Power ON the NodeMCU it blinks for a second. In case you have connected a Tube Light, it will flash during startup. So please refer the NodeMCU pinout and assign the proper pins without any conflictions.
P.S – I found that the D1, D2, D5, D6 are good to use for without any flickering of Relays.
Caution: If you are using AC Power supply, please connect it carefully as it is High Voltage and have Risk of electric shock or short circuit.
Check this tutorial for Voice Controlled Home Automation using Google Assistant – an upgraded version of this tutorial.
Check this tutorial for implementing this Relay with Two-Way Switches to control from both of them.
NodeMCU Pinout:
Download Blynk Application from Google Play store:
Download and install the Blynk App Here:
Create a Blynk App:
Step1: For new users Sign Up and old users just Log in.
Step2: Create a New Project and Choose NodeMCU board in the Choose device drop-down and then click create.
Step 3: Now you will receive an email with the Authentication Token. You need to place it in the Arduino Code. Before that, we will finish the App Creation.
Step 4: Click on the ‘+’ Icon on the top right corner. Now Widget box will open, We need 4 buttons to control the relay. So add 4 buttons by choosing it here.
Step 5: Now tap on the button from the Homepage to configure it. Choose the Output Pin D0 and Select the Mode as Switch. Then go back. Configure the remaining buttons in the same way.
Step 6: Now we have the App ready.
Step 7: Now connect the NodeMCU to the PC and open Arduino IDE.
Install the Blynk library from Sketch>Include Library> Manage Library and search for blynk and install it. Or you can install using Git Method from here.
In case, you are using ESP8266 or NodeMCU for the first time you need to add NodeMCU on boards manager and the ESP8266Wifi.h library to be installed. Otherwise, skip to step 8.
Installing ESP8266 Boards:
Navigate to Tools>Board>Boards Manager. Search for esp8266 and install esp8266 boards.
Installing Arduino Master (or Arduino Core) library:
The ESP8266Wifi.h library is included in Arduino Master(Core Library). You need to download and extract the file. Then navigate to libraries in the extracted file (Example: C:\Users\YourPCName\Downloads\Arduino-master\Arduino-master\libraries). Then copy all ESP Related folders or all the folders (depending upon your choice) and paste in inside C:\Program Files (x86)\Arduino\libraries.
Source Path (Extracted File location of Arduino Core library)
Destination Path (Arduino Libraries location)
Now we have set up everything.
Step 8: Copy the following code or Open File->Examples and navigate to Blynk->WiFi->NodeMCU.
You need to modify only three things in this code.
Paste the Authentication Token that you received in your email at ‘YourAuthToken’. Then add your SSID and Password in the code as ‘YourNetworkName’ and ‘YourPassword’.
Note: In some cases, it might show you warnings for ‘Multiple Libraries found’. It is just a warning to acknowledge which library it is using. Because the Arduino and esp8266 core library have the same libraries. Example Servo.h library. Simply ignore it and upload the program.
/************************************************************* Download latest Blynk library here: https://github.com/blynkkk/blynk-library/releases/latest Downloads, docs, tutorials: http://www.blynk.cc Sketch generator: http://examples.blynk.cc Blynk community: http://community.blynk.cc ************************************************************* This example runs directly on NodeMCU. Note: This requires ESP8266 support package: https://github.com/esp8266/Arduino Change WiFi ssid, pass, and Blynk auth token to run :) *************************************************************/ #define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "YourAuthToken"; // Your WiFi credentials. // Set password to "" for open networks. char ssid[] = "YourNetworkName"; char pass[] = "YourPassword"; void setup() { /*To avoid relays chatters when connected to blynk, set the pin to HIGH (for Relays that turns OFF at HIGH) before blynk.begin function. Eg. for D3 Given Below */ //pinMode(D3, OUTPUT); //digitalWrite(D3, HIGH); Serial.begin(9600); Blynk.begin(auth, ssid, pass); Blynk.syncAll(); //This will sync the last state of your device } void loop() { Blynk.run(); }
Once it completes, Choose the right port and Board then upload the code. That’s it! Your Home Automation Project is ready. You can test it in your Blynk app by turning ON and OFF. It can work from anywhere in the world with an internet connection.
The advanced version of the app is continued in the next tutorial. It can control the devices using your Voice Commands using Google Assistant on your Phone or Google Home. Check out the tutorial, if you want to upgrade your project with Voice Control – Voice Controlled Home Automation | Google Assistant | NodeMCU
Comments (82)
Thanks for the tutorial. Here is my challenge, I uses Node MCU V3 but here I have the failure report when uploading.
Sketch uses 235849 bytes (22%) of program storage space. Maximum is 1044464 bytes.
Global variables use 34312 bytes (41%) of dynamic memory, leaving 47608 bytes for local variables. Maximum is 81920 bytes.
Uploading 240000 bytes from C:\Users\ISAACB~1\AppData\Local\Temp\arduino_build_96048/NodeMCU.ino.bin to flash at 0x00000000
…………………………………………………………………….. [ 34% ]
…………………………………………………………………….. [ 68% ]
………………………………………………………………… [ 100% ]
Invalid library found in C:\Users\Isaac Bello\Documents\Arduino\libraries\NODE_MCU: C:\Users\Isaac Bello\Documents\Arduino\libraries\NODE_MCU
Invalid library found in C:\Users\Isaac Bello\Documents\Arduino\libraries\NODE_MCU: C:\Users\Isaac Bello\Documents\Arduino\libraries\NODE_MCU
Please help me ASAP.
1. The program needs the ESP8266 WiFi library. It is available in the Arduino Master library. You can download it here – https://github.com/esp8266/Arduino
Note: You need to extract this file and navigate to the libraries folder (inside this you find all the library folders), then copy paste the ESP libraries that you needed or copy-paste all libraries according to your choice into this path – C:\Program Files (x86)\Arduino\libraries\
2. It seems like you are using a wrong library file folder. By default, There is no library named as ‘NODE_MCU’. Maybe you extracted the downloaded filename itself instead of the library name (which contents might be available inside that folder).
The libraries are installed in two places –
The main Arduino IDE Location – C:\Program Files (x86)\Arduino\libraries\
User Work Space – C:\Users\John_Doe\Documents\Arduino\libraries\
Inside these two locations, all the libraries will be available. And it will start with the Library names like Servo. ESP8266, Blynk, LiquidCrystal, Wire and more. Inside that library names the src, examples folders exist.
3. If the library is available in both locations, delete any one of it.
We are not getting to download the Arduino master app
It’s not an app actually. It is a core library file that Arduino needs. You can able to see clone and download option on GitHub and then extract and navigate to libraries folder (refer the above mentioned steps)
sarahth your work is excellent can you modify the code for reading 4 switch data which will light up led on blynk app
Try This. It is modified for a single button and LED Widget. Mentioned places to add by yourself. Didn’t tested but it might work.
#define BLYNK_PRINT Serial
#include
#include
const int btnPin1 = 1; // Your Physical Button Pin
//Add similar way for other buttons too like prev
WidgetLED led1(V1); //Virtual Pins that u selected on BlynkApp
//Add the similar way for other virtual buttons too like prev line
BlynkTimer timer;
// Your WiFi credentials.
// Set password to "" for open networks.
char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
//Button Function
boolean btnState1 = false;
void buttonLedWidget1()
{
// Read button
boolean isPressed = (digitalRead(btnPin1) == LOW);
// If state has changed...
if (isPressed != btnState1) {
if (isPressed) {
led1.on();
} else {
led1.off();
}
btnState1 = isPressed;
}
//Create other functions for other LED Widgets here (Name accordingly)
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
Blynk.syncAll(); //This will sync the last state of your device
pinMode(btnPin1, INPUT_PULLUP); //Always High - To be grounded (LOW) when button pressed.
timer.setInterval(500L, buttonLedWidget1); //buttonLedWidget1 function called every 500ms
}
void loop()
{
Blynk.run();
}
I actually did all required and it works perfectly, I can now control my home appliances from anywhere. Many thanks for your time Sharat
Awesome! Can you tell what problem that you faced and how did you solved. It will help others too.
Sir ,when the current off the all lights are off and another case when current off all lights on .you can charge the code pls
Sir i want to ask
Which WIFI password i have to enter in the CODE
Please tell me.
I dont have h wifi router installed at my home so please tell me .
How to connect nodemcu to my blynk app.
Help me please…..
Thank you
You can turn on ‘Wi-Fi Hotspot’ on your mobile phone and enter that credentials and use it. Anyway you are going to use the Blynk App on your smartphone, so you can use the same mobile’s hotspot. But the connected hotspot should be in its Wi-Fi Range with NodeMCU.
Thank you
Hello,
I have done all the above connections and have successfully turned on a LED.
But, when I try to use a relay, the output bulb doesn’t glow.
The LED on the relay board does turn ON, but the bulb doesn’t show any changes.
The bulb stays ON when I connect to the NC port, and stays OFF when I connect to the NO port.
I’ve done the exact some connection as above.
Please help me, I’m losing my mind over this.
The relay has 3 terminals,
1 NO (Normally Open. Eg. a disconnected wire)
1 COM (Common. Eg a terminal of the power supply, say Negative)
1 NC (Normally Closed, Eg The COM (Negative) and this terminal are contacted together in idle state).
Now positive end of the power supply to be connected directly to the light.
The other end (-ve) from light should be connected to the NO Terminal of relay.
The negative from the battery should be connected to COM of Relay.
Think of relay as a switch that connects two broken wires together. In place of negative terminal you cut it and join it in COM and NO.
Try switching it in both NO and NC Connections even if the light is ON, try to turn off.
Check you can hear the relay switching sound happens.
Check the Bulb is working properly in direct power supply.
Hu ,,I am a basic learner of nodemcu so pls kindly explain me how to get aur token and network name and ssid
On the Blynk app on your android phone, click the ‘Gear Icon’ on the top. Inside that, you can see ‘Auth tokens’, either you can send that tokens to your email or copy it to the clipboard and then send it via WhatsApp or SMS. Sending it to email and copy-pasting to the code seems to be a preferred method.
Regarding the network name,ssid – It is just your Wi-Fi Username (SSID) and Password. If you don’t have a Wi-Fi Router, then you can turn on Wi-Fi hotspot on your mobile and put its username and password.
Sir ,when the current off the all lights are off and another case when current off all lights on .you can charge the code pls
Hello Dear:
I want to ask you how to increase Blynk battery in free and one more thing need your help I am working on it to control the all devices actually we make a model and then perform action . its really help me but how to control the fan speed and motors too
We can’t increase the battery for free. Before it was cheaper actually now it is a little higher. I think that 1000+1000 is more sufficient that you already got some free points. For speed control, there is no existing controller right now for AC appliances. Either you need to use a stepper/servo on the Fan speed control knob with a belt and program it or some switching circuit (it might be bulky and costlier too).
How to bypass the automation circuit if I need to manually control the appliances through the switchboard.
You need to use two-way switches (staircase). That is covered in this tutorial – https://www.factoryforward.com/wireless-rf-remote-control-2way-switches/
I will post the picture of it if needed.
How to connect “phase via common terminal ” and “neutral”?
can you tel me how use slider to control fan using bylnk app ? please .
You can’t control an AC Fan speed using existing hardware. Only smaller DC Motors can be controlled using a PWM Controller.
hello…..
i need to control the appliances using blynk and also with the google commands, please can u provide the program for this…..
It’s already covered in this tutorial – https://www.factoryforward.com/voice-controlled-home-automation-google-assistant-nodemcu/
thank u for ur response…. but i need the program….
In step 8 it is given. Check that.
Could not write build preferences file
java.io.FileNotFoundException: C:\Users\ASMITA\AppData\Local\Temp\build924115131595931435.tmp\NodeMCU.cpp (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.(FileOutputStream.java:213)
at java.io.FileOutputStream.(FileOutputStream.java:162)
at processing.app.debug.Compiler.preprocess(Compiler.java:1268)
at processing.app.debug.Compiler.preprocess(Compiler.java:1228)
at processing.app.debug.Compiler.compile(Compiler.java:358)
at processing.app.debug.Compiler.build(Compiler.java:121)
at processing.app.Sketch.build(Sketch.java:1109)
at processing.app.Sketch.build(Sketch.java:1084)
at processing.app.Editor$BuildHandler.run(Editor.java:1955)
at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
at processing.app.debug.Compiler.adviseDuplicateLibraries(Compiler.java:477)
at processing.app.debug.Compiler.build(Compiler.java:128)
at processing.app.Sketch.build(Sketch.java:1109)
at processing.app.Sketch.build(Sketch.java:1084)
at processing.app.Editor$BuildHandler.run(Editor.java:1955)
at java.lang.Thread.run(Thread.java:745)
sir its showing this error…what to do for this
Just change the board from current to any other board and compile once. Then select again NodeMCU and try upload. It seems your chached file is cleared. So if u change it to some other board and back to NodeMCU it will rebuild the file again.
I’m doing the same as you have shown in your article but I’m getting “wasn’t online yet” problem.
Are you using the correct SSID, Password and Authentication Token? To check if you are connected to Wi-Fi use IP Scanner for Android (Advanced IP Scanner for PC) and search for networks. If you can find the nodeMCU related manufacturer name, then the problem is with Blynk Server (it might have down).
Also note that, you have a proper Internet Connection.
Arduino: 1.8.8 (Windows 7), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200”
new_mar04a:3:9: error: #include expects “FILENAME” or
#include
^
exit status 1
#include expects “FILENAME” or
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
You must have ESP8266 library and Blynk library properly installed.
The header file should have mentioned on the top with proper open and closing symbols.
#include
#include
Also check the double quotes are in text editor format (“”). In MS Office or Formatted webpages it adds stylings like curved quotes. It is a different unicode character that the programmable one. Try to check that quotes too
i need to on two bulb using relay i need that digital read program
You don’t need any program changes for this, you can create a button widget and mention the pin (Slide to Switch for ON/OFF Switch) on the Blynk App.
can you teach me how to programing blynk local server to board. i’ved done try to program my local server to board but it cant connect to local server ( IP address). can you help me? ive put my program below for your reference.
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, “blynk-cloud.com”, 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
https://youtu.be/33ynNkvfvWU
I’ve seen that video but I tried to use it in window 10. I managed to make blynk server but when I tried to program to board, my blynk server can not connect to board. why? can you teach me?
In the above code you mentioned. You are using the Wi-Fi mode only. You have to comment it and uncomment the IPAddress line with your IP Address.
Modified code here (change it to your IP that was set in Blynk app)
void setup()
{
// Debug console
Serial.begin(9600);
//Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, “blynk-cloud.com”, 80);
Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080); //uncommented code
}
okay, thank you for your information. i still have a question. how to use local server to ifttt. im new to this …could you explain me in detail. how to make webhook request to my local server and which url should i use …my current local server url is 192.168……….
It won’t work with IFTTT probably. Because, when you trigger with local IP it actually triggers the local IP of that server only. So it won’t work. You need to find an alternative solution like ngrok, and port forward (not safe) your router with ngrok with its given IP.
THANK YOU SO MUCH sir
i followed all the above steps….
without any error i got a good result….
but i have some doubts sir so can u send your contact number to my mail…..
sir if i want to control this with more than one mobile then how it is possible
In somewhere in blynk website they told you can use the same username and password for multiple devices. Also, they have some pricing plans to make it as an app as well as publish in Playstore. Kindly check blynk official website and forum for more details.
We are not getting software how to do…
Which software? Are you asking about the Blynk Software?
i got problem for my coding. our buzzer still on if we dont on the buzzer and our notfucation we ddnt receive.
You can just alter the terminals of the buzzer and check.
For notifications, you have to use this function
Blynk.notify(“Hey, You have a notification!”); //Display on Blynk app
Other Notifications:
Blynk.email(“[email protected]”, “Subject”, “Your message goes here”); //For Email Notification
Blynk.tweet(“Hey! My NodeMCU can tweet now!”); //For twitter notification
I new to software and I have downloaded blynk application with mobile from apple store but unable to join with wifi network. what to do ?
Blynk can work with your mobile internet also?
The Wi-Fi needed for here is for NodeMCU. So we are creating a portable hotspot (Wi-Fi Tethering). Normally you can use your Home network too.
Thanks dear Sharath for your guidence. I am learner and would like to ask you, How to confirm blynk application is working ok and giving command to NodeMCU ? ( communication between blynk and NodeMCU).When I touch button on screen it goes into setting of pin and no changes in output of NodeMCU. ( program uploading and wi-fi network name,password with token inserted in program). Also tell me how to check functioning of uploaded program with NodeMCU?
In step 6, you can able to see the Cog Wheel,+ icon and Play Icon on the top right. It denotes it is in editor mode. If you touch any pins it goes to edit the pin numbers.
You have to click the Play icon to make it Live Mode. This screenshot you can see at the last step. On this, a chip symbol with red mark denotes the NodeMCU is not connected to the Server. If you click on that chip icon, it shows you the last connected date, time and status of the NodeMCU as well as ONLINE/OFFLINE.
If you want to check the function of the program open serial monitor in the Arduino IDE, It shows the status and IP address and all.
Thanks sir,
After making play button on, red dot appears and message displays ” wasn’t online yet”. what is the problem ?
Also serial monitor after uploading successfully remains blank. please guide.
regards,
In the code, you have to change your Wi-Fi network name its password, and the Authentication token you got in email for that project dashboard properly. Wi-Fi must have an internet connection. If the NodeMCU is connected to the blynk server, the serial monitor displays a big blynk logo and shows as connected.
Sir,
I have already done what you told, but not getting result. Also no logo at serial monitor. what to check and how to overcome ?
Just verify all the steps once. I tested it in the evening nly.
Thanks sir. I am having two problems. One after downloading Blynk application in ios system after red dot appears on top with ” wasn’t online yet ” means not able to join online with wifi. what may be problem? Other is after successful uploading code with NODEmcu, serial monitor remains blank with box on top and curser.( right top corner shows “SEND”. what can be done sir ?
sir I tried Blynk application with both ios and Android based mobile but not succeeded. How to test both separately i.e blynk application with phone and NodeMcu function working ?
regards,
Sir, can you show me how to insert my coding in coding blynk? i want it to run this coding using push button in blynk. Below are my coding.
Regards,
#define FLOAT_SENSOR 2 // the number of the pushbutton pin
int solenoid = 22; //pin solenoid valve to drain water out
int Liquid_level = 0;
int waterlevel = 5;
int pump = 21;
int perismatic = 23;
void setup()
{
pinMode(solenoid,OUTPUT);
pinMode(waterlevel,INPUT);
pinMode(pump,OUTPUT);
pinMode(FLOAT_SENSOR, INPUT_PULLUP);
pinMode(perismatic,OUTPUT);
}
void loop()
{
do{
digitalWrite(solenoid, HIGH); // Turn on the solenoid valve
}while(digitalRead(5) == HIGH);
digitalWrite(solenoid, LOW);
do{
digitalWrite(solenoid, LOW);
digitalWrite(pump, HIGH);
}while(digitalRead(FLOAT_SENSOR) == LOW && digitalRead(FLOAT_SENSOR)==LOW);
digitalWrite(solenoid, LOW);
digitalWrite(pump, LOW);
digitalWrite(perismatic, HIGH);
delay(5000);
digitalWrite(perismatic, LOW);
while(1){}
}
Fine but I didn’t understand what you are saying. Does it need to display sensor values or you need to control solenoid or any other?
Hello,
I have done all the above connections and have successfully turned on a LED.
But, when I try to use a relay, the output bulb doesn’t glow.
How to fix it pls reply