MQTT Custom Object Example
Overview
This application demonstrates an industrial control system that exposes thermostat and PID controller data over MQTT. It provides a practical example of how to implement bidirectional MQTT communication for industrial automation, allowing both local and remote monitoring and control of temperature systems and PID control loops.
The application creates custom MQTT objects that automatically serialize to JSON and can be controlled through MQTT topic subscriptions, making it ideal for IoT industrial applications.
Features
Thermostat Control
- Temperature Range Management: Configurable minimum, maximum, and set point temperatures
- Location Tracking: Configurable location string (default: "Warehouse 1")
- Active State Control: Boolean flag to enable/disable thermostat operation
- Real-time Monitoring: Continuous JSON publication of thermostat status
PID Controller Sets
- Dual PID Controllers: Separate horizontal and vertical PID control sets
- Tunable Parameters: Individual P (Proportional), I (Integral), and D (Differential) terms
- Industrial Ready: Designed for precision control applications
MQTT Integration
- Automatic JSON Serialization: All objects automatically convert to JSON format
- Bidirectional Communication: Read current values and write new configurations
- Topic-based Organization: Clean topic structure for easy integration
- Configurable Client: Uses MQTT ConfiguredClient for flexible broker connections
Architecture
Class Hierarchy
mqtt_obj (base class)
TempRange
MaxTemp (mqtt_int)
SetTemp (mqtt_int)
MinTemp (mqtt_int)
Thermostat (extends TempRange)
Active (mqtt_bool)
Location (mqtt_string)
PIDSet
P (mqtt_float) - Proportional term
I (mqtt_float) - Integral term
D (mqtt_float) - Differential term
ControlSet
Hset (PIDSet) - Horizontal controller
Vset (PIDSet) - Vertical controller
MQTT Topics
| Topic | Purpose | Data Format |
Thermostat | Thermostat status and control | JSON with temperature ranges, active state, and location |
ctrl_set | PID controller parameters | JSON with horizontal and vertical PID settings |
Default Configuration
Thermostat Defaults
- Maximum Temperature: 100 deg
- Set Temperature: 30 deg
- Minimum Temperature: 10 deg
- Active State: true
- Location: "Warehouse 1"
PID Controller Defaults
- Proportional (P): 1.0
- Integral (I): 0.0001
- Differential (D): 0.0
Usage
Running the Application
- The application initializes the network stack and waits for network connectivity
- Connects to the configured MQTT broker
- Publishes current object states as JSON to their respective topics
- Subscribes to the same topics for remote configuration updates
- Continuously displays current status to console
Interactive Mode
- Press any key during operation to toggle between display and control modes
- The application will show real-time JSON representations of both the thermostat and control sets
- System diagnostics are enabled for monitoring and debugging
MQTT Communication Examples
Thermostat JSON Structure:**
{
"MaxTemp": 100,
"SetTemp": 30,
"MinTemp": 10,
"Active": true,
"Thermostat Location": "Warehouse 1"
}
Control Set JSON Structure:**
{
"Hset": {
"P": 1.0,
"I": 0.0001,
"D": 0.0
},
"Vset": {
"P": 1.0,
"I": 0.0001,
"D": 0.0
}
}
Configuration
The MQTT client configuration (broker URI, username, password, certificates) is managed through the NetBurner configuration system. Ensure proper MQTT broker settings are configured before running the application.
Prerequisites
- NetBurner RTOS platform
- Network connectivity
- MQTT broker access
- Filesystem support (for certificate storage)
Technical Implementation
Object Behavior
- Auto-Publication: Objects automatically publish to MQTT when values change (
eObj_Flag_PubOnWrite)
- Subscription: Objects subscribe to their topics for remote updates (
eObj_Flag_Subscribe)
- JSON Serialization: Built-in JSON rendering for all object types
- Type Safety: Strongly typed MQTT objects (int, float, bool, string)
Network Handling
- Automatic re-connection logic
- Connection status monitoring
- Graceful handling of network interruptions
- Configurable timeout and retry mechanisms
Use Cases
This example is ideal for:
- Industrial temperature control systems
- Remote monitoring of manufacturing processes
- IoT-enabled HVAC systems
- Process control applications requiring PID tuning
- Educational demonstrations of MQTT in industrial contexts
- Prototyping industrial automation systems
Development Notes
- The application uses NetBurner's MQTT library with custom object extensions
- Includes comprehensive error handling and connection management
- Designed for continuous operation in industrial environments
- Supports both local debugging and remote operation
- Extensible architecture for adding additional control objects