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)
Wonder if you can help. I am controlling several 2 way lighting circuits using an arduino and Blynk. I am using 2 way so I can turn lights on in a garden room by a switch but also remotely with Blynk(via an arduino controlled 2 way relay) from the comfort of my house. Problem I have is that if I turn the lights on via the manual switch I get no update on Blynk to say lights are on and vice versa ie if I turn lights on with blynk and then off with the manual switch, Blynk doesn’t change state of switch to off. You can imagine with 8 channels of this it gets very messy on the Blynk app actually knowing which is on and off!!
So my question is, is there any way to easily do this ?
The problem here is the Relay we are using. It actually isolate the lower voltage side (Arduino) with an Higher voltage side. There is no acknowledgement for that physical switch states.
If there exist a 8 channel current sensor, we can connect the devices through that and monitor current flow. But again, there needs to be a room for another 8 GPIO Pins for that sensor.
If we make the relay as the main controller, then there is a chance to control both ways via the Relay (using touch kind of switches that sonoff have. It also need more GPIO Pins).
okay ….thank you
Arduino: 1.8.9 (Windows 10), Board: “Generic ESP8266 Module, 80 MHz, Flash, Disabled, All SSL ciphers (most compatible), nodemcu, 26 MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, nonos-sdk 2.2.1 (legacy), v2 Lower Memory, Disabled, None, Only Sketch, 9600”
Build options changed, rebuilding all
Sketch uses 279848 bytes (56%) of program storage space. Maximum is 499696 bytes.
Global variables use 28856 bytes (35%) of dynamic memory, leaving 53064 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.6
2.6
esptool.py v2.6
Serial port COM5
Connecting….
Chip is ESP8266EX
Features: WiFi
MAC: cc:50:e3:df:bb:8f
Uploading stub…
Running stub…
Stub running…
Configuring flash size…
Auto-detected Flash size: 4MB
Flash params set to 0x0340
Compressed 284000 bytes to 204321…
Writing at 0x00000000… (7 %)Traceback (most recent call last):
File “C:\Users\sande\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2/tools/upload.py”, line 25, in
esptool.main(fakeargs)
File “C:/Users/sande/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 2698, in main
operation_func(esp, args)
File “C:/Users/sande/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 2158, in write_flash
esp.flash_defl_block(block, seq, timeout=DEFAULT_TIMEOUT * ratio * 2)
File “C:/Users/sande/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 104, in inner
return func(*args, **kwargs)
File “C:/Users/sande/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py”, line 632, in flash_defl_block
self.ESP_FLASH_DEFL_DATA, struct.pack(‘ Preferences.
Every time upload the program hard resetting via RTS pin and all relay led on but no outpuy
In NodeMCU the pins are having different functionalities. When some pins are HIGH/LOW on boot up it will reset. Have a look at this guy’s video, as he explain it clear and in-depth – https://youtu.be/c0tMGlJVmkw
Thanks for your reply but still facing same problem can you send your what’s no or any contact details or view through team viewer
sir please provide circuit diagram for 4 relay switch
Same connection as shown your site pin using esp8266 is d1 de de dr supply 3.3 v esp8266 realy board 5 v supply
Pin using d1 d2 d3 d4 supply esp8266 3.3 v really board same in picture on your web site
supply 5 v blank power is 200
can give me full coding for smart home using nodeMCU, i have problem about coding, this is for my final year project
hii I want to interface four p6 led module to node MCU to send data using wifi and i have tried to Connect but the data was not displaying properly, and i used serial data and also used dmd library but i don’t receive any data to p6led module ( indore )
sir i am facing a problem , i have uploaded the code on the nodemcu and i used a arduino board to power the 5V relay module , when i connect the nodemcu to the wifi everything is working fine but the relay module is not working . solution to this is ?
The relay need more current, If you are powering it via USB, then atleast 2Amps it should provide to power the relay. Otherwise, You need to power relay externally with another 5V Power Supply.
CAN U HELP MY ONLY ONE SWITCH IS WORKING, REST TWO ARE NOT PERFORMING ANY FUNCTION.
Have you chosen NodeMCU Board on the Blynk App?
can it work without internet too? with just router in offline mode.
Yes, But you need to install blynk local server on a Raspberry Pi or on Your PC. All devices need to be in the same network
after power cut all devices are powering on their own. can you suggest me the code to store the previous state.