#23 NodeMCU: MQTT Publish-Subscribe communication using Lua Program

IoT Tutorial #23 [ NodeMCU Tutorials #8 ]
We have already seen how to flash Lua firmware on NodeMCU and Rum first Lua program for blinking LED on NodeMCU ESP8266.

In this post, we will write a Lua program for MQTT Publish-Subscribe communication over the internet.


MQTT Publish-Subscribe using LUA Program:

Board: NodeMCU (ESP8266)

MQTT Publish-Subscribe:
Publisher  : Lua Program on NodeMCU
Subscriber : Lua Program on NodeMCU
Broker     : iot.eclipse.org


    1. Flash Fresh Lua firmware using NodeMCU Flasher (Click here to Check how) > Close Flasher

    2. Open Chrome and Install and Launch "MQTT Box" (Chrome App)
      Click on > "Create MQTT Client" > and Set Following things:
      MQTT Client Name :  Any (As per your choice)
      Protocol         :  mqtt/tcp
      Host             :  iot.eclipse.org
      Will - Topic     :  mytopic/iot
      Port             :  1883             
      Click on Save

      Topic to publish   : mytopic/iot
      Topic to subscribe : mytopic/iot
      Click on Subscribe
      then Type whatever msg you want to send/publish, write it in the "Payload" textbox and hit "Publish" to send.
    3. Keep NodeMCU connected > Open ESPlorer > A new ESPlorer window will open.
      "Port" must be automatically detected there. Set Baud_rate = 9600
      If PORT is not getitng detected then you need to install drivers:

      Click on "Open" to Check the firmware version of nodemcu.
      then click "RST" button on NodeMCU.

      It will show the Firmware version...

      NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4

      If some program is already uploaded on the nodeMCU then It won't detect the firmware version.

      In that case,
      Flash default (Lua) firware (shown in step 3) of previous post and then try to detect it using above steps.

    4. Copy following code and Place it in the left black window of ESPlorer and
      Change wifi_name and wifi_password in the below code.
      save it with .lua extention:
      --wifi connection
      wifi.sta.config("SuiteUp","welcome@10",true) --Change Wifi_Name & Password Here
      --callback on connect and disconnects
      c:on("connect", function(con) print("connected")end)
      c:on("offline", function(con) print("offline")end)
      c:on("message", function(conn,topic,data)
          if data~=nil then
       c:publish("mytopic/iot","Hello from LUA - AKSHAY!!!",0,0,function(conn)

    5. Then Click on "Send to ESP"

    6. Above Program will Subscribe to topic "mytopic/iot" and Publish following msg to the same topic
      "Hello from LUA - AKSHAY!!!"
      You can observe the same msg received by MQTT Box Subscriber (From step 2)
      Similarlly, you can publish some messages from MQTT Box Publisher (From step 2) and
      Observe those messages received by NodeMCU (In the right Black window of the ESPlorer)

      Using this You can send message from any place to other place.
      I mean, It is NOT necessary to keep NodeMCU, MQTT Box in the same network.

For Better understanding you can watch demonstration video given below:


Download link is given in the Description of the YouTube video shown below.



Click here to see more codes for Raspberry Pi 3 and similar Family.
Click here to see more codes for NodeMCU ESP8266 and similar Family.
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,
-Akshay P. Daga


Post a Comment
Previous Post Next Post