Arduino: ESP32 Home automation (Simple On/Off control using MQTT)

We have built a home automation system using NodeMCU here. In this post we will try to replicate same system using ESP32. We will connect couple of LEDs to ESP32 and control those LEDs from a WebApp (The WebApp is integrated into this blog, scroll down to see!!).

Environment requirements:

  • ESP32
  • Arduino IDE environment to program ESP32
  • Arduino libraries : PubSubClient,ArduinoJson 5.13.1 (if you haven’t already installed it , you can install it from Sketch->Include library->Manage Libraries)
  • mqtt broker , if you don’t have one you can use eclipse Paho broker for experimentation (host: iot.eclipse.org , port : 1883, it is a free and open broker)
  • couple of LEDs and breadboard to see the output

Solution Approach :

MQTT will be used to transport messages between ESP32 and client application. All the messages are JSON encoded.We already know how to connect ESP32 to Access point and MQTT broker from this post. You can check this post by Nuno Santos to know more about parsing JSON messages using ArduinoJson library.

ESP32 MQTT LED Control
ESP32 MQTT LED Control

ESP32 will listen on the following channel for messages

/ic/ha/to/esp32/gpio/

Expected JSON message is format is as follows

{
  "switches": [
    {
      "status": false,
      "id": 1,
      "lable": "GPIO 4"
    },
    {
      "status": false,
      "id": 2,
      "lable": "GPIO 5"
    },
    {
      "status": false,
      "id": 3,
      "lable": "GPIO 16"
    },
    {
      "status": true,
      "id": 4,
      "lable": "GPIO 17"
    }
  ]
}

we have an array called “switches”, each item in this array is a switch with three fields. status indicates whether IO is high(true) or low(false). id is used to map each switch to GPIOs of the ESP32. We need to manipulate the status filed of respective switches to control it for example to make GPIO 4 HIGH and remaining all GPIOs low, we need to send following message on the MQTT channel

{
  "switches": [
    {
      "status": true,
      "id": 1,
      "lable": "GPIO 4"
    },
    {
      "status": false,
      "id": 2,
      "lable": "GPIO 5"
    },
    {
      "status": false,
      "id": 3,
      "lable": "GPIO 16"
    },
    {
      "status": false,
      "id": 4,
      "lable": "GPIO 17"
    }
  ]
}

PubSub client default max message length is 128, since our message much bigger than that, we need to increase it to 1024. You can find the setting in PubSubClinet.h file (~\Documents\Arduino\libraries\pubsubclient\src). Remember to change it back to 128 once you are done with the testing (otherwise other arduino boards with lesser RAM may not work properly)

Increasing MAX PACKET size of PUBSUB client
Increasing MAX PACKET size of PUBSUB client

 

Code:

First we will connect to given access point (change the ssid, password to connect to your network). By default ESP32 will connect to eclipse open mqtt broker. if you have MQTT broker change the mqtt_server,mqtt_port (if the broker is protected by user name and password you need to mention them in MQTT_USER, MQTT_PASSWORD). Once ESP is connected to network it will try to initiate connection with broker and subscribe to the channel “/ic/ha/to/esp32/gpio/“.  In main loop we have nothing much to do , we simply call loop method of pubsub client.

When a new message is received pubsub will call you callback method with channel name and message. we need to parse the received JSON and switch the GPIO accordingly.

 

How To Test:

Connect LEDs to GPIO4,5,16,17 with a 4k7 resistor

ESP32 MQTT LED On/Off
ESP32 MQTT LED On/Off

Copy the above code to Arduino IDE and Upload the code to ESP32 and open serial console. Console will show connected message if it is connected to MQTT broker (if it prints any error code, check you network, MQTT settings in the code)

Now ESP32 is ready to receive messages from MQTT broker, click on “Connect” below (if you are using a different MQTT broker provide those details in the respective fields), you will see four buttons (if the button is in blue means OFF,green means ON). Click the button to control the LEDs.

———————————————————–

 
 

———————————————————–

Tags:,
10 Comments

Add a Comment

Your email address will not be published. Required fields are marked *