IoT Bytes

Bits and Bytes of IoT

MQTT with NodeMCU

Pradeep Singh | 1st April 2016

mqttorg-glow

MQTT is the protocol of choice for M2M communication and ESP8266 s a wonderful compact prototyping module. When MQTT and NodeMCU Firmware come together they open an ocean of possibilities. You can use this combination for many IoT Applications.

Lua based NodeMCU Firmware natively supports MQTT Protocol so we need not to install any additional component. For our testing we shall use “paho” MQTT Broker (iot.eclipse.org).

Prerequisites:

  1. NodeMCU must be configured in Client/Station Mode and should be able to access internet connection (Refer Link).
  2. Any NodeMCU IDE. For Ex. ESPlorer.
  3. Port# 1883 should be open.

Initiate MQTT Client:

MQTT_CLIENT_ID = (wifi.sta.getmac())
MQTT_CLIENT_USER = "username"
MQTT_CLIENT_PASSWORD = "password"
MQTT_CLIENT_KEEPALIVE_TIME = 45

mqtt_Client = mqtt.Client(MQTT_CLIENT_ID, MQTT_CLIENT_KEEPALIVE_TIME, MQTT_CLIENT_USER, MQTT_CLIENT_PASSWORD)

Connect with MQTT Broker:

mqtt_Client:connect(MQTT_BROKER, MQTT_BROKER_PORT, MQTT_BROKER_SECURE, MQTT_RECONNECT, function(conn) end)

Subscribe to a Topic:

MQTT_PUBLISH_TOPIC = "testTopic"
MQTT_PUBLISH_TOPIC_QoS = 0

mqtt_Client:subscribe(MQTT_PUBLISH_TOPIC,MQTT_PUBLISH_TOPIC_QoS, function(conn) end)

Publish on a Topic:

MQTT_PUBLISH_MESSAGE = "Hello MQTT"
MQTT_PUBLISH_TOPIC_RETAIN = 0

mqtt_Client:publish(MQTT_PUBLISH_TOPIC, MQTT_PUBLISH_MESSAGE, MQTT_PUBLISH_TOPIC_QoS, MQTT_PUBLISH_TOPIC_RETAIN, function(conn) end)

Read Data from a Topic:

mqtt_Client:on("message", function(conn, topic, data)
 print data
end)

Precaution:

After booting ESP8266 may take some time to get IP from DHCP and connect to WiFi Network. If you try to connect to MQTT Broker too early, it may cause errors in your code. To prevent any such situation always check for Network Connectivity before attempting to connect to MQTT Broker.

Putting Things Together:

Following is the sample code for MQTT Publisher and Subscriber (GitHub Link for Code File) –

------------------------------------------
--- Set Variables ---
------------------------------------------
MQTT_BROKER = "iot.eclipse.org"
MQTT_BROKER_PORT = 1883
MQTT_BROKER_SECURE = 0

MQTT_PUBLISH_TOPIC = "my_topic"
MQTT_PUBLISH_TOPIC_QoS = 0
MQTT_PUBLISH_TOPIC_RETAIN = 0
MQTT_PUBLISH_MESSAGE = "Hello MQTT"

MQTT_SUBSCRIBE_TOPIC = "my_topic"
MQTT_SUBSCRIBE_TOPIC_QoS = 0

MQTT_CLIENT_ID = "client1"
MQTT_CLIENT_USER = "username"
MQTT_CLIENT_PASSWORD = "password"
MQTT_CLIENT_KEEPALIVE_TIME = 120
------------------------------------------

--- Initiate MQTT Client ---
MQTT_Client = mqtt.Client(MQTT_CLIENT_ID, MQTT_CLIENT_KEEPALIVE_TIME, MQTT_CLIENT_USER, MQTT_CLIENT_PASSWORD)

--- On Connect Event ---
MQTT_Client:on("connect", function(con) 
 print ("connected") 
end)

--- Offline Event ---
MQTT_Client:on("offline", function(con) 
 print ("offline") 
end)

-- On Message Recieve Event ---
-- This Function will be invoked everytime Broker sends message to subscribed Topic ---
MQTT_Client:on("message", function(conn, topic, data)
 print("MQTT Message Received...")
 print("Topic: " .. topic)
 if data ~= nil then
 print("Message: " .. data)
 end
end)

--- Connect to MQTT Broker ----
MQTT_Client:connect(MQTT_BROKER, MQTT_BROKER_PORT, MQTT_BROKER_SECURE, function(conn) 
 print("connected")
end)

-- Subscribe to MQTT Topic ---
MQTT_Client:subscribe(MQTT_SUBSCRIBE_TOPIC,MQTT_SUBSCRIBE_TOPIC_QoS, function(conn) 
end)

-- Publish to MQTT Topic ---
-- Use this Function to publish message on MQTT Topic ---
MQTT_Client:publish(MQTT_PUBLISH_TOPIC, MQTT_PUBLISH_MESSAGE, MQTT_PUBLISH_TOPIC_QoS, MQTT_PUBLISH_TOPIC_RETAIN, function(conn) 
 print("Message Published on MQTT Topic...") 
end)

You can use ESPlorer to push this code into NodeMCU.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: