Getting started with MQTT using nodeMCU
|In this blog we will see how to connect to a MQTT broker from nodeMCU and exchange messages.
We will just subscribe to the channel and display the data published to the channel on the console. whenever message is posted in the subscribed channel , we will post the status of the IO’s to the specified channel.
First we need a MQTT broker you can install the broker on your local machine and connect to it or use a cloud provider. I will use cluodmqtt. coludmqtt provides a free instance which is good for exploring purpose.
Setting up cloudmqtt :
- Go to plans , and select the Cute Cat plan, signup if not already done
- Go to control panel
- Create a instance
- Click on details of the created instance and note down server address and port, we need them to connect to the broker.
- Create a user with name “nodemcu” and give a password
- Save the new user, now we need to provide the ACL to the new user. Please note that I am giving full access to the “nodemcu” user, in production you should use more find grained ACLs
- Now your MQTT instance is ready to accept connections.
Loading Scripts:
There are four files that you need to flash to the module.
config.lua : this file contains the information about the MQTT server ,WiFi details. You need to place your wifi name password,MQTT server name,MQTT user name (that you just created in step 5) and password in this file
setup.lua: This file contains the code to connect to your WiFi
app.lua : This is where you will place your application logic, like controlling the IO pins based on the information received , and sending the messages to the desired channels
init.lua : NodeMCU will execute this file on startup, we will bootstrap the application using this file.
All the files are available on github.
Once you changed the parameters in config.lua , you need to flash all the four files to nodeMCU and reset the board.
You should see something like this on your console.
Note down the chip id, you need it to send messages to the nodeMCU.
You need a MQTT client application to send messages to nodeMCU form your laptop, I am using mqtt-spy.
First you need to subscribe to the nodeMCU publishing channel to receive the messages from nodeMCU.
Channel name: /nodemcu/10652610/fromnode
10652610 is my node chip id, replace it with yours. To send messages to the nodeMCU you can use the following channel,
Channel Name: /nodemcu/10652610/tonode
Again you need to replace the chip id with yours, what ever you post will appear in your console.
Wonderful post.
Im not able to get the “Successfully subscribed to data endpoint” from my Node MCU . My NodeMCU was able to publish the data to the Cloud but I cant get to subscribe ! ( I see the data in the “Received messages” in the MQTT console. )
Hi,
which broker you are using ?, make sure that the user has the read and write access to the channel
Ignore my note above – I got where I made the mistake !
I had not set the username /pwd in the config.lua – now node is able to publish the message.
Sankar, when does the function send_ping get activated ?
in app.lua ?
mqtt_start -> consume_data -> send_ping () but in that function I see a publish ( … gpio.read(1) .. and so on )
What does this do ?
Currently my nodemcu is talking to the CloudMQTT and published the message “Published the status ” – I see that in the CoolTerm Monitor ( i.e, the serial monitor)
Sankar, seems like
m:publish(config.ENDPOINT ..”/fromnode”,gpio.read(1)..gpio.read(2)..gpio.read(3)..gpio.read(4),0,1)
is reading the values on this pin and sending out ?
because I see a data =1110 on my MQTT-SPY client dashboard.
What is happening here ?
Appreciate a reply.
The sendping() function sends the status of digital IO (1-4), when ever it receives a message
Sankar, this is really awesome. Everything worked first time. MQTT-Spy is great, CloudMQTT is excellent. Thank you so much for putting all this together, you’ve made my day!
Thank you for this code. It’s just what I wanted.
However, I get the below error message
PANIC: unprotected error in call to Lua API (app.lua:22: attempt to index global ‘mqtt’ (a nil value))
Can you please help.
seems your nodemcu doesn’t have latest firmware, update frimware that has mqtt, it should work
http://icircuit.net/setting-up-node-mcu/743
Yes I have just tumbled.
Reflashed and just tested all ok.
Sorry to trouble you, just got my first ESP8266 yesterday.
Thank you again . .
SIr, thanks for the tutorial. But i am not able to run the init.lua file. It says “cannot open init.lua”. I downloaded the files from the link provided but still not able to get it done correctly. Would you please help me with this. Thanks in advance!
Hello,
It could because the init.lua is not present on the board. See the following blog to know how to load code to nodemcu
http://icircuit.net/setting-up-node-mcu/743
Thanks for the such a great help but,
Can i flash the Nodemcu through Arduino IDE?
Yes, you can use Arduino IDE to program nodemcu
http://icircuit.net/using-nodemcu-arduino-ide/1396
Hi Sankar,
I need to send data from arduino to my mqtt account. I created the account and tried to verify my nodemcu sending data to broker account using mqtt spy. But my connection is not going green. It is remaining red. Could you help me with this? Appreciate a reply.
Hi,
Make sure you got the broker url and port right from the cloudmqtt console.
You can also test using public mqtt broker provided by eclipse , it doesn’t require any authentication.
If you want to use arduino program nodemcu and connect to mqtt broker , you can follow this post
http://icircuit.net/arduino-connecting-nodemcu-mqtt-broker/2342
Thank you sankar. I will try this one. And one more thing, Is it possible to send data coming from RFID readers to a database using nodemcu? If so, could you suggest me a post which i can refer to publish data which is available at Rx pins of arduino- mega (data from RFID readers) to a created a database. I tried many methods using arduino- IDE but am not able to send this data to the database.
yes you can use arduino along with nodemcu to send RFID data to server.
You can use ESP as network processor with AT firmware or you can run a small app on nodemcu.
The second approch is followed in the following article (nodemcu will
just out the serial data from arduino)
http://icircuit.net/interfacing-arduino-uno-wtih-nodemcu/1474
Thanks for the post. Where do I find this ‘Console’ you talk about?
Hi, I am referring to the serial console.
Hi sankar…
I am able to send my nodemcu data to public broker iot.eclipse.org. But now iam having troubles in sending the same data to a database created in xampp. I really dont know what should i change in the program, like the serial and publish channels. Could you suggest me a post for this. Appreciate a reply.
sir can u please help me in flashing node mcu with those config.lua etc 4 files