NodeMCU IoT Project

NodeMCU IoT Project with Save Last GPIO State

Make a NodeMCU IoT project using sensors with the New Blynk Automation. It can save the last GPIO state after power failure. (Circuit + Code).

In this article, I have explained how to make a NodeMCU IoT project using sensors with the New Blynk Automation.

In the project, the ESP8266 NodeMCU can also remember the last GPIO state after the power failure. So the after restart the appliances will automatically turn on according to the previous state.

NodeMCU IoT Project

You can control the relays with IR remote and manual switches without WiFi. The NodeMCU will automatically connect with the Blynk cloud if Wi-Fi is available.

I have used all the FREE tools for this NodeMCU IoT Project project using the Blynk IoT app.

So if you follow all the steps, you can easily make this home automation system with ESP8266 NodeMCU and Blynk IoT.

Required Components for the NodeMCU IoT project

Required Components for Blynk IoT projects
  • ESP8266 NodeMCU.
  • 4-channel 5V SPDT Relay Module Amazon
  • DHT11 Sensor Amazon
  • TSOP1838 IR Receiver (with metallic case) Amazon
  • Switches or Pushbuttons Amazon
  • Any IR Remote

Circuit of the IoT Project using NodeMCU

Circuit of the IoT Project using ESP8266

The circuit is very simple, I have used D1, D2, D5 & D6 GPIO pins to control the 4-channel relay module.

And the GPIO SD3, D3, D7 & RX are connected with the switches to control the relay module manually.

I have used the INPUT_PULLUP function in Arduino IDE instead of using the pull-up resistors with each push button.

As per the source code, when the control pins of the relay module receive a LOW signal the relay will turn on and the relay will turn off for the HIGH signal in the control pin.

IR remote receiver (TSOP1838) connected with SD2. And the DHT11 sensor is connected with D4.

If you want to use the pushbuttons instead of latched switches, then just connect the pushbuttons instead of the latched switches across GPIO pins and GND.

Circuit of the IoT Project using ESP8266

I have used a 5V 5Amp mobile charger to supply the circuit.

Please take proper safety precautions while connecting the AC appliances.

Tutorial video on IoT project using ESP8266 & Blynk

In the Internet of things tutorial video, I have covered the following steps in detail.

  • Control appliances with Blynk, IR Remote, DHT11 Sensor, and Timer.
  • How to save LAST GPIO state in ESP8266 flash memory.
  • Circuit of the ESP8266 IoT projects with sensors.
  • How to set up Blynk IoT cloud for ESP8266.
  • Source Code for the Blynk ESP8266 NodeMCU IoT project.

Set up Blynk IoT Cloud for the IoT-based Project

You can refer to the following article to set up the new Blynk cloud account

Getting started with New Blynk 2.0 IoT platform

Create Blynk Template

During creating the template, I selected ESP8266 as hardware and connection type as WiFi.

Create Datastreams in the Blynk cloud

Datastreams for this Blynk Project

For demonstration, I have created 4 Datastreams (V1 to V4) to control 4 relays, 1 Datastream (V5) to turn off all the relays, and 2 Datastreams (V6 & V7) to get the DHT11 sensor readings.

Define Datastreams for Automations

Define Datastreams for Automation

Next, you have to define which Datastreams will be available in Automation actions and conditions.

Only Virtual Pin, Enumerable, and Location Datastreams are supported.

Create Web Dashboard

After that, click and drag Switch widgets according to the number of relays and 2 Level widgets for DHT11 sensors.

Then click on “Save“.

Add Device in Blynk Cloud using Template

You can refer to the following article to add a device to the Blynk IoT cloud.

Add Device in Blynk Cloud from Template

Program NodeMCU with Arduino IDE

For this IoT-based home automation project, I have used the Arduino IDE to program ESP8266 NodeMCU.

First update the Preferences –> Aditional boards Manager URLs: https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json

  • Then install the ESP8266 board from the Board manager or Click Here to download the ESP8266 board.
  • Download the required libraries from the following links:
Preferences Library

In the code, I have used the Preferences library to remember the last states of all the GPIO connected with relays. Please refer to the following article on the Preferences library tutorial.

Source Codes for Blynk IoT Project with NodeMCU

Click on the following buttons to download the source codes for this ESP32 project.

First, you have to upload the Code for Getting HEX codes to NodeMCU and connect the IR receiver with SD2 / GPIO-9 (Please refer to the below circuit).

After that, open the serial monitor, and select the Baud Rate at 115200.

Now, you have to press all the remote buttons (one by one) which you want to use to control the relays.

Now, save all the HEX codes. You have to update the main code with these HEX codes. You need 5 HEX codes.

Modify the main code for this IoT-based project

Modify the main code

In the code, you have to update only the BLYNK_TEMPLATE_ID, BLYNK_DEVICE_NAME, and Auth Token.

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""

Then enter the WiFi Credentials.

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "WiFi Name";
char pass[] = "WiFi Password";

Update the HEX code of the IR Remote buttons (as shown in the tutorial video).

//Update the HEX code of IR Remote buttons 0x<HEX CODE>
#define IR_Button_1   0x80BF49B6
#define IR_Button_2   0x80BFC936
#define IR_Button_3   0x80BF33CC
#define IR_Button_4   0x80BF718E
#define IR_All_Off    0x80BF3BC4

After doing these changes, go to Tools and select the board as “NodeMCU 1.0” and the proper PORT in Arduino IDE.

Then click on the upload button to program the ESP8266 NodeMCU.

PCB for this NodeMCU Home Automation system

To make the circuit compact, I have designed a PCB for this ESP8266 IoT project.

If you want, you can also use this PCB to make the circuit compact and give the project a professional look. This PCB can be used for any ESP8266 Home Automation project.

About PCBWay and their services

You can order any custom design PCBs from PCBWay at very reasonable prices.

This image has an empty alt attribute; its file name is PCBWay-750x161-Banner.jpg

PCBWay produces FR-4 and Aluminum boards and advanced PCBs like Rogers, HDI, Flexible and Rigid-Flex boards at very affordable prices.
For the online instant quote page please visit – pcbway.com/orderonline

You can also explore different PCB projects from their Open-source community pcbway.com/project/.

For more details please visit the following articles.
Why PCBway
PCB Capabilities
High-Quality PCB

Steps to order PCB from PCBWay

To order the PCB first visit PCBWay.com.

Step 2 to order from PCBWay

Then enter the following details:

  1. PCB Size (Length & Width) in mm & PCB quantity
  2. Select masking color for the PCB
  3. Select country and shipping method
  4. Click on the “Save to Cart” button
Step-3 to order from PCBWay

Now click on the “Add Gerber Files” to upload the PCB Gerber file.

Then click on the “Submit Order Now” to place the order.

After that, they will review the Gerber file and accordingly confirm the order.

I have used their services for my different home automation projects, I always received the PCB on time and the quality is very good in this price range.

Add Automations to the Blynk Project

Add Automations to the Blynk IoT

In Blynk cloud, you add Automations to control the relays automatically with sensor redings and timer.

In this automation project, I controlled the 3rd relay with the DHT11 temperature reading and added a timer for the 4th relay.

Control relays with the Blynk IoT, IR Remote & Switch

NodeMCU control relay with Blynk IoT app
control relays with Blynk IoT app

You can control appliances with the New Blynk IoT app from anywhere in the world through the internet.

If the NodeMCU is connected with WiFi, then you can also monitor the real-time feedback and room temperature in the Blynk IoT app.

Control relays with IR Remote
Control relays with IR Remote

You can use any IR remote to control the appliances.

If the Wi-Fi is not connected, still you can control relays with the IR remote.

Control relays with switches
Control relays with switches

You can also control the appliances from manual switches or push buttons.

If the NodeMCU is connected to Wi-Fi, it will send real-time feedback to the Blynk IoT server.

I hope you like this Smart Home IoT project idea with the new Blynk 2.0 IoT Cloud and NodeMCU ESP8266.

Click Here for more such ESP8266 projects.

Please do share your feedback on this IoT project. Thank you for your time.