Arduino: MQTT to Serial Bridge Using ESP32

Most of the applications involved with ESP32 requires some sort of data transport, means the device should be able to send data over network to some other device/system. There are several methods available to do that. You can find brief overview of the available protocols here .  In this blog we will see who to connect ESP32 to a MQTT broker using pubsub client.

The system we are designing in this blog will be capable of sending the text received on serial line to MQTT broker. It will also subscribe to a specified channel and send any message received on channel to the serial line. It works as a serial to MQTT bridge.

ESP32 based Serial to MQTT bridge
ESP32 based Serial to MQTT bridge

Environment requirements:

  • you need to have a ESP32
  • setup your Arduino IDE to program ESP32
  • PubSubClient arduino library (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).

 

ESP32 MQTT Code:

On setup function we will try to connect to access point. Once we are connected to access point we will connect to given MQTT broker and subscribe to “/icircuit/ESP32/serialdata/rx” channel. Messages published to this channel will be printed on our serial console. All the messages sent from serial console will be published to “/icircuit/ESP32/serialdata/tx’.

Upload the following code to ESP32. Don’t forget to change the network and MQTT broker details before uploading.if you are using eclipse mqtt broker, no need of any username/password.

Note: Some times, Arduino IDE throws error ”

A fatal error occurred: Timed out waiting for packet content
In such cases, while uploading the code, hold “Boot” button of ESP32 to rectify this issue. (looks like newer Cp210X drivers cause this issue)

 

Test the connection straight from this page:

In order to make the testing easier,I built the code using react.js. Thus you can test the MQTT and ESP32  connectivity from the below form element itself without opening another browser console.

Step 1 :connect ESP32 to system and load the code.

Step2 :Open serial console, if every thing goes well it will show some thing like following. (pressing reset will bring this values, if nothing comes on serial monitor)

Connecting ESP32 to MQTT broker
Connecting ESP32 to MQTT broker

Step 3: you can send and receive messages to ESP32 right from this page, click on “Connect”in the below form

(if you are using broker other than eclipse, provide the details in the respective fields).

Step4 : If the connection is successful you will see a text field, enter your message and click on “Send” it should appear in the serial console. If you send any message from the serial console , the message should appear here
——————————————————————–

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

Tags:,
One Comment

Add a Comment

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