Beginner's guide to MQTT & Sparkplug: Dive into Lua for exciting IoT adventures

Beginner's guide to MQTT & Sparkplug: Dive into Lua for exciting IoT adventures


The MQTT (Message Queue Telemetry Transport) protocol is a lightweight and efficient messaging protocol for IoT (Internet of Things) applications. It follows a publish-subscribe model, where clients can publish messages to topics or subscribe to topics to receive messages.

The protocol is built on top of the TCP/IP protocol, making it ideal for limited bandwidth or network connectivity instability. MQTT uses a small packet size and requires minimal overhead, making it highly suitable for resource-constrained devices.

The MQTT protocol consists of three main components: the broker, the publisher, and the subscriber. The broker acts as a central hub that receives messages from publishers and delivers them to subscribers based on topic subscriptions.

Publishers are clients that send messages to the broker, specifying a topic to which the message belongs. Subscribers are clients that express their interest in specific topics and receive messages published to those topics.

Implementing MQTT in Lua with the Barracuda App Server's MQTT Client

Lua is a lightweight, high-level scripting language known for its simplicity and flexibility. The Barracuda App Server provides a Lua MQTT client library, enabling you to easily integrate MQTT functionality into your Lua-based applications. Let's explore some examples of using the Barracuda App Server's MQTT client in Lua.

Note that the MQTT protocol is built on top of the TCP/IP protocol. The Lua MQTT client, which can be downloaded from GitHub, is implemented in Lua and uses the Barracuda App Server's TCP/IP API.

Establishing a Connection to the MQTT Broker

To establish a connection to an MQTT broker using the Barracuda App Server's MQTT client, you can use the following Lua code snippet:

local function onstatus(type,code,status)
   if "mqtt" == type and "connect" == code and 0 == status.reasoncode then
      print"Connected to broker"
      return true -- Accept connection
   end
   print("Disconnect or connect failed",type,code)
   return false -- Deny reconnect
end

local mqtt=require("mqttc").create("broker.emqx.io",onstatus)        

Publishing Messages to a Topic

To publish a message to a specific topic using the MQTT client, you can use the following Lua code snippet:

mqtt:publish("sensors/1/temperature","25.5")        

Subscribing to Topics and Receiving Messages

To subscribe to a topic and receive messages published to that topic, you can use the following Lua code snippet:

mqtt:subscribe("sensors/+/temperature", {
   onpub=function(topic,payload)
      print("Received message:", topic, payload)
   end
})        

In this example, the topic "sensors/+/temperature" is a wildcard subscription that matches any topic under the "sensors" namespace followed by "temperature." The callback function is invoked whenever a message is received on a subscribed topic.

Disconnecting from the MQTT Broker

To disconnect from the MQTT broker, you can use the following Lua code snippet:

mqtt:disconnect()        

Conclusion

The MQTT protocol offers a lightweight solution for communication in IoT and constrained network environments. By using the Barracuda App Server's MQTT client library in Lua, you can easily incorporate MQTT functionality into your Lua-based applications, enabling seamless communication with MQTT brokers and interaction with publish-subscribe messaging patterns.

How to get started

Click to run the above MQTT example on the Lua tutorial server.

To dive into designing MQTT clients using Lua, check out the official Lua MQTT documentation. The Lua MQTT client is pre-integrated in the Mako Server and the Xedge32 development environment for ESP32. Xedge32 simplifies the development of MQTT client applications on the versatile, easy-to-use, and low-cost ESP32 microcontroller.

No alt text provided for this image
The Xedge Development Environment

MQTT Sparkplug

The Sparkplug specification revolutionizes MQTT communication in the Industrial Internet of Things (IIoT) by establishing standardized guidelines for MQTT topic namespaces and Google's Protocol Buffer metric encoding. Ensuring interoperability across MQTT-based IIoT systems, the Sparkplug library simplifies integration with detailed structures, from state management using birth and last-will messages to efficient data encoding. Coupled with the Barracuda App Server's Sparkplug Lua library, developers can seamlessly craft Sparkplug-compliant Edge of Network Nodes (EoN), requiring only basic Lua knowledge, while the intricate details of MQTT and Protobuf are effortlessly managed behind the scenes.

For more information and practical examples, you can refer to the GitHub repository RealTimeLogic/LSP-Examples - Sparkplug, which offers code examples and resources demonstrating MQTT Sparkplug implementation and usage in industrial automation and control systems.

One of the included Sparkplug examples is a ready-to-run weather station written in the Lua language and designed for the ESP32 development environment Xedge32.

No alt text provided for this image

This ready-to-run example provides an excellent opportunity to experiment and learn more about the power and capabilities of the MQTT Sparkplug library when running on a microcontroller.

To view or add a comment, sign in

More articles by Real Time Logic LLC

Others also viewed

Explore content categories