Easily Automate Your Life with NodeMCU(ESP8266) and MQTT

maxresdefault 6 NODEMCU
0
0

Introduction

In today’s smart home era, controlling home appliances remotely has become a popular trend. In this tutorial, we will explore how to use the NodeMCU board, a 4-channel relay module, and MQTT (Message Queuing Telemetry Transport) protocol to control your home appliances wirelessly. We will also provide the necessary Arduino code to help you get started.

Prerequisites

Before diving into this tutorial, you should have a basic understanding of Arduino programming, MQTT, and how to set up the NodeMCU board. Make sure you have the following components:

  1. NodeMCU board
  2. 4-channel relay module
  3. Jumper wires
  4. Arduino IDE (Integrated Development Environment)
  5. MQTT broker (such as Mosquitto or Cloud-based MQTT services like Adafruit IO or AWS IoT)

Step 1: Setting up the Hardware

  1. Connect the VCC and GND pins of the relay module to the 5V and GND pins on the NodeMCU board, respectively.
  2. Connect the input pins (IN1, IN2, IN3, and IN4) of the relay module to the NodeMCU’s digital pins (D1, D2, D3, and D4) using jumper wires.

Step 2: Install Required Libraries

  1. Open the Arduino IDE and navigate to “Sketch” -> “Include Library” -> “Manage Libraries.”
  2. Search for and install the following libraries:
  • PubSubClient by Nick O’Leary (for MQTT communication)
  • ESP8266WiFi by ESP8266 Community (for WiFi connectivity)

Step 3: Connect to Wi-Fi Network

  1. Include the required libraries at the beginning of your Arduino sketch:
   #include <ESP8266WiFi.h>
   #include <PubSubClient.h>
  1. Configure your Wi-Fi credentials:
   const char* ssid = "Your WiFi SSID";
   const char* password = "Your WiFi Password";
  1. Set up the MQTT broker details:
   const char* mqttServer = "MQTT Broker IP";
   const int mqttPort = 1883;
  1. Connect to the Wi-Fi network:
   WiFiClient espClient;
   PubSubClient client(espClient);

   void setup_wifi() {
     delay(10);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) {
       delay(500);
     }
   }
  1. Add the following code inside the void setup() function to connect to the MQTT broker:
   client.setServer(mqttServer, mqttPort);
   client.setCallback(callback);

   void reconnect() {
     while (!client.connected()) {
       if (client.connect("ESP8266Client", mqttUser, mqttPassword)) {
         client.subscribe("device/led");
       }
     }
   }

Step 4: Control Home Appliances via MQTT & NodeMCU

  1. Add the following code inside the void loop() function to listen for MQTT messages and control the relay module accordingly:
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character

  if ((char)payload[0] == '0') {
    digitalWrite(D6, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else if ((char)payload[0] == '1') {
    digitalWrite(D6, HIGH);  // Turn the LED off by making the voltage HIGH
  }
  else if ((char)payload[0] == '2') {
    digitalWrite(D7, HIGH);  // Turn the LED off by making the voltage HIGH
  }
  else if ((char)payload[0] == '3') {
    digitalWrite(D7, LOW);  // Turn the LED off by making the voltage HIGH
  }
  else if ((char)payload[0] == '4') {
    digitalWrite(D8, HIGH);  // Turn the LED off by making the voltage HIGH
  }
  else if ((char)payload[0] == '5') {
    digitalWrite(D8, LOW);  // Turn the LED off by making the voltage HIGH
  }
  else if ((char)payload[0] == '6') {
    digitalWrite(D4, HIGH);  // Turn the LED off by making the voltage HIGH
  }
  else if ((char)payload[0] == '7') {
    digitalWrite(D4, LOW);  // Turn the LED off by making the voltage HIGH
  }
}
  1. Add the following code inside the void setup() function to initialize the relay module pins as output:
   pinMode(D4, OUTPUT);
   pinMode(D6, OUTPUT);
   pinMode(D7, OUTPUT);
   pinMode(D8, OUTPUT);

Step 5: Upload the Sketch and Test

  1. Connect your NodeMCU board to your computer using a USB cable.
  2. Select the appropriate board and port in the Arduino IDE.
  3. Click on the “Upload” button to compile and upload the sketch to the NodeMCU board.
  4. Open the serial monitor to monitor the connection status.
  5. Once connected, use an MQTT client (e.g., MQTT.fx, MQTT Explorer) or your own application to publish messages to the topic “device/led” with payloads “1” or “0” for ON and OFF states of the respective relays.

DOWNLOAD FULL CODE

From Zero to Smart Home Hero: Automate Your Life with NodeMCU and MQTT

Conclusion

Congratulations! You have successfully learned how to control home appliances using the NodeMCU board, a 4-channel relay module, and MQTT. With this setup, you can remotely control your devices and create an intelligent home automation system. Feel free to explore further and expand the functionalities according to your needs.

Remember to ensure safety precautions when dealing with electrical devices and to comply with local regulations. Happy tinkering!

Also, check out our other playlist Rasa ChatbotInternet of thingsDockerPython ProgrammingMQTT, etc.
Become a member of our social family on youtube here.

Stay tuned and Happy Learning. ✌🏻😃

Leave a Reply