https://github.com/maximilianfeldthusen/mqtt
Simple C Code – MQTT with TLS, JSON, and Reconnect
https://github.com/maximilianfeldthusen/mqtt
c json linux mqtt mqtt-broker reconnect tls ubuntu
Last synced: 2 months ago
JSON representation
Simple C Code – MQTT with TLS, JSON, and Reconnect
- Host: GitHub
- URL: https://github.com/maximilianfeldthusen/mqtt
- Owner: maximilianfeldthusen
- License: mit
- Created: 2025-06-22T06:14:57.000Z (4 months ago)
- Default Branch: TFD
- Last Pushed: 2025-06-22T06:29:59.000Z (4 months ago)
- Last Synced: 2025-06-22T07:23:40.899Z (4 months ago)
- Topics: c, json, linux, mqtt, mqtt-broker, reconnect, tls, ubuntu
- Language: C
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Documentation
### mqtt
MQTT is quietly powering a huge chunk of the connected world e.g.
### 1. **Smart Homes**
MQTT is the backbone of many home automation systems. Devices like thermostats, lights, security cameras, and smart plugs use it to communicate efficiently. Platforms like Home Assistant and OpenHAB rely on MQTT for seamless device integration.### 2. **Wearables and Health Monitoring**
Fitness trackers and medical devices use MQTT to send real-time data—like heart rate or glucose levels—to mobile apps or cloud servers. Its low power usage makes it ideal for battery-powered wearables.### 3. **Industrial Automation**
Factories use MQTT to monitor and control machinery. It helps unify data from different machines and vendors, enabling predictive maintenance and real-time analytics on the factory floor.### 4. **Automotive and Fleet Management**
Car manufacturers like BMW use MQTT for services like car-sharing, remote diagnostics, and over-the-air updates. It’s also used in fleet tracking systems to monitor vehicle location, fuel usage, and maintenance needs.### 5. **Energy and Utilities**
MQTT is used in smart grids and energy monitoring systems to collect data from meters and sensors, helping optimize energy distribution and detect outages quickly.### 6. **Transportation and Logistics**
Airlines and railways use MQTT to improve operational efficiency and passenger experience. Logistics companies use it for real-time asset tracking and route optimization.---
Here’s a breakdown of what this simple MQTT program in C is doing:
### Includes & Macros
```c
#include <...>
```
Standard headers for I/O, memory, signal handling, timing, etc.
`#include "MQTTClient.h"` brings in the Eclipse Paho MQTT C API.```c
#define ADDRESS "ssl://your-mqtt-broker.com:8883"
```
Defines the MQTT broker endpoint using secure TLS (`ssl://` over port `8883`), along with constants like client ID, topic, and quality of service (QoS).---
### Graceful Exit Handling
```c
volatile int running = 1;void handle_sigint(int sig) {
running = 0;
}
```
Allows the app to shut down cleanly when interrupted (`Ctrl+C`). The `volatile` keyword ensures `running` is safely read in a multithreaded or signal-driven context.---
### Simulated Sensors
```c
float read_temp_sensor() { ... }
float read_hum_sensor() { ... }
```
Dummy functions generating semi-random temperature and humidity data. In real use, you’d connect to hardware like a DHT22 or BME280.---
### MQTT Setup
```c
MQTTClient_create(...)
conn_opts.keepAliveInterval = 20;
conn_opts.ssl = &ssl_opts;
```
Initializes the MQTT client and sets up connection options:
- 20s keep-alive ping
- Clean session (no retained session state)
- SSL certificate validation using the specified CA file---
### Reconnect Loop
```c
while ((rc = MQTTClient_connect(...)) != MQTTCLIENT_SUCCESS) { ... }
```
If the broker is unreachable, it retries the connection every 3 seconds. Prevents crashing on startup if the network is down.---
### Main Publish Loop
```c
while (running) {
// create payload
// publish message
// wait and repeat
}
```
Every 5 seconds, it:
1. Reads simulated sensor values.
2. Builds a JSON message.
3. Publishes to the broker.
4. Waits for acknowledgment (`MQTTClient_waitForCompletion`).You get structured data like:
```json
{"temperature": 23.45, "humidity": 55.20, "timestamp": 1726886494}
```---
### Cleanup
On exit, it disconnects and destroys the client:
```c
MQTTClient_disconnect(...);
MQTTClient_destroy(...);
```---
Using MQTT with C can be powerful but comes with its own set of quirks. Here are some common challenges:
### 1. **Memory Management**
C doesn’t have garbage collection, so you need to manually allocate and free memory for MQTT messages, payloads, and client structures. A missed `free()` can lead to memory leaks, while a premature one can crash your app.### 2. **Threading and Concurrency**
MQTT clients often run in separate threads to handle incoming messages. Managing thread safety—especially when publishing or modifying shared data—can be tricky without proper synchronization.### 3. **Error Handling**
The MQTT C libraries (like Paho or libmosquitto) return error codes, but interpreting them and recovering gracefully (e.g. reconnecting after a dropped connection) requires careful design.### 4. **TLS/SSL Configuration**
Setting up secure connections involves configuring certificates, trust stores, and sometimes dealing with platform-specific quirks. It’s easy to misconfigure and hard to debug.### 5. **Limited Debugging Tools**
Unlike higher-level languages, C lacks built-in debugging or logging frameworks. You’ll often need to roll your own logging or use tools like `gdb` to trace issues.### 6. **Cross-Compilation for Embedded Targets**
If you're deploying to ARM-based boards or microcontrollers, cross-compiling the MQTT library and your app can be a challenge—especially when dealing with dependencies like OpenSSL.### 7. **Library Differences**
Different MQTT C libraries (e.g. Paho vs. libmosquitto) have different APIs and capabilities. Choosing the right one and understanding its limitations is key.---
If you're using a Linux system (like Ubuntu or Debian-based distros), here’s how to get everything set up:
### Prerequisites
**Install Eclipse Paho MQTT C client:**
```bash
sudo apt update
sudo apt install libpaho-mqtt-dev
```**Optional — For TLS support:**
```bash
sudo apt install libssl-dev
```---
### Compile the Code
Assuming your file is named `mqtt_sensor.c`, compile with:
```bash
gcc mqtt_sensor.c -o mqtt_sensor \
-lpaho-mqtt3cs -lssl -lcrypto
```- `-lpaho-mqtt3cs`: for the Paho MQTT C synchronous library with SSL
- `-lssl -lcrypto`: for TLS (OpenSSL)---
### Run It
You’ll need to provide your CA certificate if using TLS:
```bash
./mqtt_sensor
```If it can’t connect to your broker or authenticate the certificate, make sure you:
- Point to a valid `ca.crt` file (like from Let’s Encrypt or your broker)
- Match the broker hostname in the certificate’s CN/SAN fields
- Open port `8883` on your firewall or broker side---
To run your MQTT sensor app as a background service on Linux, we can set it up as a **systemd** service. This lets it start automatically on boot, restart on failure, and run in the background cleanly.
Here’s how to do it:
---
### Step 1: Move the Binary
Assuming you compiled your code to `mqtt_sensor`:
```bash
sudo cp mqtt_sensor /usr/local/bin/
```---
### Step 2: Create a systemd Service File
Create a new file at `/etc/systemd/system/mqtt_sensor.service`:
```bash
sudo nano /etc/systemd/system/mqtt_sensor.service
```Add this content:
```ini
[Unit]
Description=MQTT Sensor Publisher Service
After=network.target[Service]
ExecStart=/usr/local/bin/mqtt_sensor
Restart=always
RestartSec=5
User=pi
Group=pi[Install]
WantedBy=multi-user.target
```Make sure to change `User` and `Group` to whoever should run the service (like `ubuntu` or `yourusername`).
---
### Step 3: Enable and Start the Service
```bash
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable mqtt_sensor.service
sudo systemctl start mqtt_sensor.service
```---
### Check Status and Logs
```bash
sudo systemctl status mqtt_sensor.service
journalctl -u mqtt_sensor.service -f
```---
Now your sensor app runs quietly in the background—even after reboot!