Skip to content

File mqtt.h

Location: include/mqtt.h

Includes

graph LR
1["include/mqtt.h"]
click 1 "mqtt_8h.md#mqtt_8h"
1 --> 2

2["platform.h"]
click 2 "platform_8h.md#platform_8h"
2 --> 3
2 --> 4
2 --> 5
2 --> 6
2 --> 7
2 --> 8
2 --> 9

7["Adafruit_ADT7410.h"]

3["Arduino.h"]

8["ArduinoJson.h"]

9["ArduinoMqttClient.h"]

6["RTClib.h"]

5["SdFat.h"]

4["Wire.h"]

Included by

graph RL
1["include/mqtt.h"]
click 1 "mqtt_8h.md#mqtt_8h"
2 --> 1
3 --> 1
4 --> 1

2["src/core.cpp"]
click 2 "core_8cpp.md#core_8cpp"

3["src/mqtt.cpp"]
click 3 "mqtt_8cpp.md#mqtt_8cpp"

4["src/network.cpp"]
click 4 "network_8cpp.md#network_8cpp"

Variables

Variable mqttClient

Definition: include/mqtt.h (line 5)

MqttClient mqttClient

Type: MqttClient

Functions

Function SendTempToMqtt

bool SendTempToMqtt(MqttClient &mqttClient, const char *topicPrefix, const char *sensorType, const char *sensorId, float celsius, const DateTime &now, int sequence)

Publishes real-time sensor data to the MQTT broker with QoS 1 delivery.

This function builds a JSON payload from the provided sensor data and publishes it to the specified MQTT topic. After publishing, it waits briefly for a PUBACK handshake from the broker to confirm delivery. If no acknowledgment is received within the timeout window, the data is saved to a CSV file for later recovery.

Parameters:

  • mqttClient: Reference to the MQTT client instance
  • topicPrefix: Topic prefix for MQTT publishing (e.g., "dhbw/ai/si2023/2/")
  • sensorType: Sensor type string (e.g., "temp")
  • sensorId: Unique sensor identifier
  • celsius: Measured temperature value in Celsius
  • now: Current timestamp (DateTime)
  • sequence: Sequence number for the measurement

Returns:

true if published and acknowledged by broker, false if fallback to CSV

?> Uses QoS 1 for reliable delivery. If broker does not echo/PUBACK within the timeout, data is persisted for later transmission.

Parameters:

  • MqttClient & mqttClient
  • const char * topicPrefix
  • const char * sensorType
  • const char * sensorId
  • float celsius
  • const DateTime & now
  • int sequence

Return type: bool

Function SendPendingDataToMqtt

bool SendPendingDataToMqtt(MqttClient &mqttClient, const char *topicPrefix, const char *sensorType, const char *sensorId, const DateTime &now)

Processes and transmits pending CSV files from offline periods to the MQTT broker.

This function scans the SD card for CSV files containing unsent sensor data from previous offline periods. Each file is converted to a JSON payload and published to the MQTT topic /recovered with QoS 1. After publishing, it waits briefly for a PUBACK handshake from the broker to confirm delivery. If the PUBACK is not received within the timeout period, the file is saved for later transmission. Files are only deleted if the publish operation succeeds. Files older than 24 hours or with invalid data are skipped.

Parameters:

  • mqttClient: Reference to the MQTT client instance
  • topicPrefix: Topic prefix for MQTT publishing (e.g., "dhbw/ai/si2023/2/")
  • sensorType: Sensor type string (e.g., "temp")
  • sensorId: Unique sensor identifier
  • now: Current timestamp (DateTime)

Returns:

true if all valid files were published and deleted, false if any files remain or errors occurred

?> Uses QoS 1 for reliable delivery. Skips files older than 24 hours or with invalid content. Aborts if recovery exceeds time limit.

Parameters:

  • MqttClient & mqttClient
  • const char * topicPrefix
  • const char * sensorType
  • const char * sensorId
  • const DateTime & now

Return type: bool

Function CreateFullTopic

void CreateFullTopic(char *buffer, size_t bufferSize, const char *topicPrefix, const char *sensorType, const char *sensorId, const char *suffix="")

Parameters:

  • char * buffer
  • size_t bufferSize
  • const char * topicPrefix
  • const char * sensorType
  • const char * sensorId
  • const char * suffix = ""

Return type: void

Source

#pragma once

#include "platform.h"

extern MqttClient mqttClient;

bool SendTempToMqtt(MqttClient& mqttClient, const char* topicPrefix, const char* sensorType,
                const char* sensorId, float celsius, const DateTime& now, int sequence);

bool SendPendingDataToMqtt(MqttClient& mqttClient, const char* topicPrefix, const char* sensorType,
                     const char* sensorId, const DateTime& now);

void CreateFullTopic(char* buffer, size_t bufferSize, const char* topicPrefix, const char* sensorType,
                     const char* sensorId, const char* suffix = "");