We have seen how to use MQTT on nodeMCU in previous post, But in that post we didn’t do any thing useful. In this post we will build a home load controlling system using nodemcu (Home Automation using nodemcu) and on client side we will use android app. We will be using MQTT to transfer the messages between nodemcu and android app.
Setting up MQTT
First we need a MQTT broker. If you already have a broker then you can skip this step.
1. Goto https://www.cloudmqtt.com/plans.html, try the free plan.
2. Create a user and set the ACL’s to the newly created user
Please refer this blog on how to create the MQTT broker.
Now we have the MQTT broker if you want play with the the MQTT , use the mqttspy app to publish/subscribe to the channels.
Loading Scripts :
Now we will load the scripts to nodeMCU. We have four lua scripts that needs to be loaded to the nodeMCU.
config.lua -> In this file we will store the wifi details , mqtt user details setup.lua -> This script is responsible for making the wifi connection app.lua -> This file contains the actual application logic init.lua -> NodeMCU will execute this file on startup, we will use this script to bootstrap our application
The Only file you need make changes is the config.lua, you need add your wifi details and mqtt account details.