Send the data to one MQTT topic using JSON

This commit is contained in:
coolneng 2023-04-03 06:23:03 +02:00
parent 1ad08b06ff
commit ba6d31c798
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
4 changed files with 30 additions and 27 deletions

View File

@ -16,3 +16,4 @@ lib_deps =
adafruit/DHT sensor library@^1.4.4 adafruit/DHT sensor library@^1.4.4
adafruit/Adafruit Unified Sensor@^1.1.9 adafruit/Adafruit Unified Sensor@^1.1.9
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
bblanchon/ArduinoJson@^6.21.1

View File

@ -7,15 +7,11 @@
DHT dht(DHTPIN, DHTTYPE); DHT dht(DHTPIN, DHTTYPE);
const int fc28_pin = A0; const int fc28_pin = A0;
// TODO Tweak threshold to get good value
const int soil_threshold = 40;
const int num_topics = 3;
const int max_connection_attempts = 60; const int max_connection_attempts = 60;
const char *topics[] = {"homeostasis/temperature", "homeostasis/humidity", const char *topic = "homeostasis/room";
"homeostasis/soil_humidity"};
const char *device_id = "homeostasis"; const char *device_id = "homeostasis";
WiFiClient wlan_client; WiFiClient wifi_client;
PubSubClient mqtt_client(wlan_client); PubSubClient mqtt_client(wifi_client);
bool check_valid_value(float value) { bool check_valid_value(float value) {
return (!isnan(value) && value >= 0 && value <= 100); return (!isnan(value) && value >= 0 && value <= 100);
@ -32,10 +28,10 @@ void loop() {
float humidity = dht.readHumidity(); float humidity = dht.readHumidity();
int analog_val = analogRead(fc28_pin); int analog_val = analogRead(fc28_pin);
int soil_percentage = map(analog_val, 0, 1023, 0, 100); int soil_percentage = map(analog_val, 0, 1023, 0, 100);
float data[] = {temperature, humidity, static_cast<float>(soil_percentage)}; float data[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
if (check_valid_value(temperature) && check_valid_value(humidity)) { if (check_valid_value(temperature) && check_valid_value(humidity)) {
mqtt_transfer(mqtt_client, device_id, topics, data, num_topics); mqtt_transfer(mqtt_client, device_id, topic, data);
} }
disconnect_mqtt(mqtt_client, topics, num_topics); disconnect_mqtt(mqtt_client, topic);
enter_deep_sleep(false); enter_deep_sleep(false);
} }

View File

@ -1,5 +1,6 @@
#include "wlan.h" #include "wlan.h"
#include "credentials.h" #include "credentials.h"
#include <ArduinoJson.h>
void initial_connection() { void initial_connection() {
WiFi.begin(SSID, PSK); WiFi.begin(SSID, PSK);
@ -23,32 +24,36 @@ void connect_wlan(const int max_retries) {
} }
void connect_mqtt(PubSubClient &client, const char *device_id, void connect_mqtt(PubSubClient &client, const char *device_id,
const char **topics, int num_topics) { const char *topic) {
if (!client.connected()) if (!client.connected())
client.setServer(MQTT_HOST, MQTT_PORT); client.setServer(MQTT_HOST, MQTT_PORT);
if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) { if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("MQTT connected"); Serial.println("MQTT connected");
for (int i = 0; i < num_topics; i++) client.subscribe(topic);
client.subscribe(topics[i]);
} }
} }
void disconnect_mqtt(PubSubClient &client, const char **topics, void disconnect_mqtt(PubSubClient &client, const char *topic) {
int num_topics) {
Serial.println("Disconnecting MQTT"); Serial.println("Disconnecting MQTT");
for (int i = 0; i < num_topics; i++) client.unsubscribe(topic);
client.unsubscribe(topics[i]);
client.disconnect(); client.disconnect();
} }
size_t construct_json(float *data, char *buffer, int buffer_size) {
StaticJsonDocument<100> json;
json["temperature"] = data[0];
json["humidity"] = data[1];
json["soil_humidity"] = data[2];
size_t payload_size = serializeJson(json, buffer, buffer_size);
return payload_size;
}
void mqtt_transfer(PubSubClient &client, const char *device_id, void mqtt_transfer(PubSubClient &client, const char *device_id,
const char **topics, float *data, int num_topics) { const char *topic, float *data) {
connect_mqtt(client, device_id, topics, num_topics); char buffer[100];
for (int i = 0; i < num_topics; i++) { connect_mqtt(client, device_id, topic);
char buffer[50]; size_t payload_size = construct_json(data, buffer, 100);
sprintf(buffer, "%f", data[i]); client.publish(topic, buffer, payload_size);
client.publish(topics[i], buffer);
}
Serial.println("Data transferred successfully"); Serial.println("Data transferred successfully");
} }

View File

@ -10,10 +10,11 @@ const int WIFI_TIMEOUT = 10000;
void initial_connection(); void initial_connection();
void connect_wlan(const int max_retries); void connect_wlan(const int max_retries);
void connect_mqtt(PubSubClient &client, const char *device_id, void connect_mqtt(PubSubClient &client, const char *device_id,
const char **topics, int num_topics); const char *topic);
void disconnect_mqtt(PubSubClient &client, const char **topics, int num_topics); void disconnect_mqtt(PubSubClient &client, const char *topic);
size_t construct_json(float *data, char *buffer, int buffer_size);
void mqtt_transfer(PubSubClient &client, const char *device_id, void mqtt_transfer(PubSubClient &client, const char *device_id,
const char **topics, float *data, int num_topics); const char *topic, float *data);
void enter_deep_sleep(bool wifi_timeout); void enter_deep_sleep(bool wifi_timeout);
#endif /* WLAN_H */ #endif /* WLAN_H */