NetBurner 3.5.6
PDF Version
MQTT Simple Object Pub/Sub Example

MQTT Simple Object Pub/Sub Application

Overview

This application demonstrates a simple MQTT publish/subscribe implementation using the NetBurner NNDK (NetBurner Network Development Kit). The application showcases automatic MQTT object synchronization, where a local variable can be both published to and updated from an MQTT topic.

Application Description

The MQTT Simple Object Pub/Sub Example is a console-based application that manages an integer variable (myInt) through MQTT messaging. The application performs the following key functions:

Core Functionality

  • Automatic Publishing: The myInt variable is automatically published to the MQTT topic testTopic/HelloWorld whenever its value changes
  • Periodic Publishing: Additionally publishes the current value every 3 minutes, regardless of whether it has changed
  • Automatic Subscription: The same variable automatically subscribes to the MQTT topic, allowing remote updates
  • Interactive Control: Users can increment the variable by typing any character in the serial console
  • Real-time Monitoring: Continuously displays the current value of myInt on the console

Technical Implementation

  • MQTT Client: Uses a configured MQTT client that gets its settings (broker URI, credentials, etc.) from the system configuration
  • MQTT Object: Utilizes the MQTT::mqtt_int class for seamless integration between local variables and MQTT topics
  • Network Stack: Initializes the full network stack and waits for active network connectivity
  • File system Support: Includes conditional file system initialization for platforms that support it (used for storing MQTT certificates and keys)

Key Components

Variables and Objects

  • myInt: An MQTT-enabled integer that automatically syncs with the MQTT topic
  • MqttClient: Configured MQTT client instance
  • MQTT_TOPIC_NAME: Defines the topic as "testTopic/HelloWorld"

Main Operations

  1. Network Initialization: Establishes network connectivity and waits up to 5 seconds for an active connection
  2. MQTT Configuration: Sets up automatic publishing every 3 minutes (180 seconds)
  3. Interactive Loop: Continuously monitors for user input and displays current values

Usage

Running the Application

  1. Ensure your NetBurner device is properly configured with network settings
  2. Configure MQTT broker settings through the system configuration
  3. Flash and run the application on your NetBurner device
  4. Monitor the serial console for output

Interacting with the Application

  • Local Control: Type any character in the serial console to increment myInt
  • Remote Control: Send a numeric MQTT message to topic testTopic/HelloWorld to update myInt
  • Monitoring: Watch the console output to see the current value of myInt

Expected Behavior

  • Each keypress increments the variable and triggers an MQTT publish
  • The current value is displayed on the console after each update
  • Every 3 minutes, the current value is published automatically
  • Remote MQTT messages to the topic will update the local variable

Configuration Requirements

Before running this application, ensure the following are configured:

  • Network Settings: IP address, subnet mask, gateway, DNS
  • MQTT Broker: Broker URI/IP address and port
  • Authentication: Username and password (if required by broker)
  • Security: TLS certificates and keys (if using secure connection)

Development Notes

  • The application includes system diagnostics that should be removed for production code
  • Filesystem initialization is conditional based on platform capabilities
  • The code uses NetBurner's RTOS and network stack APIs
  • MQTT objects provide automatic serialization and deserialization

Platform Requirements

  • NetBurner NNDK-compatible hardware
  • Network connectivity (Ethernet or Wi-Fi)
  • Access to an MQTT broker
  • Serial console for interaction and monitoring

This example serves as a foundation for more complex MQTT applications and demonstrates the ease of integrating MQTT messaging into NetBurner applications using the provided MQTT object classes.