Send the data to one MQTT topic using JSON
This commit is contained in:
parent
1ad08b06ff
commit
ba6d31c798
|
@ -16,3 +16,4 @@ lib_deps =
|
|||
adafruit/DHT sensor library@^1.4.4
|
||||
adafruit/Adafruit Unified Sensor@^1.1.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
bblanchon/ArduinoJson@^6.21.1
|
||||
|
|
16
src/main.cpp
16
src/main.cpp
|
@ -7,15 +7,11 @@
|
|||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
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 char *topics[] = {"homeostasis/temperature", "homeostasis/humidity",
|
||||
"homeostasis/soil_humidity"};
|
||||
const char *topic = "homeostasis/room";
|
||||
const char *device_id = "homeostasis";
|
||||
WiFiClient wlan_client;
|
||||
PubSubClient mqtt_client(wlan_client);
|
||||
WiFiClient wifi_client;
|
||||
PubSubClient mqtt_client(wifi_client);
|
||||
|
||||
bool check_valid_value(float value) {
|
||||
return (!isnan(value) && value >= 0 && value <= 100);
|
||||
|
@ -32,10 +28,10 @@ void loop() {
|
|||
float humidity = dht.readHumidity();
|
||||
int analog_val = analogRead(fc28_pin);
|
||||
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)) {
|
||||
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);
|
||||
}
|
||||
|
|
33
src/wlan.cpp
33
src/wlan.cpp
|
@ -1,5 +1,6 @@
|
|||
#include "wlan.h"
|
||||
#include "credentials.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void initial_connection() {
|
||||
WiFi.begin(SSID, PSK);
|
||||
|
@ -23,32 +24,36 @@ void connect_wlan(const int max_retries) {
|
|||
}
|
||||
|
||||
void connect_mqtt(PubSubClient &client, const char *device_id,
|
||||
const char **topics, int num_topics) {
|
||||
const char *topic) {
|
||||
if (!client.connected())
|
||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||
if (client.connect(device_id, MQTT_USER, MQTT_PASSWORD)) {
|
||||
Serial.println("MQTT connected");
|
||||
for (int i = 0; i < num_topics; i++)
|
||||
client.subscribe(topics[i]);
|
||||
client.subscribe(topic);
|
||||
}
|
||||
}
|
||||
|
||||
void disconnect_mqtt(PubSubClient &client, const char **topics,
|
||||
int num_topics) {
|
||||
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
||||
Serial.println("Disconnecting MQTT");
|
||||
for (int i = 0; i < num_topics; i++)
|
||||
client.unsubscribe(topics[i]);
|
||||
client.unsubscribe(topic);
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
void mqtt_transfer(PubSubClient &client, const char *device_id,
|
||||
const char **topics, float *data, int num_topics) {
|
||||
connect_mqtt(client, device_id, topics, num_topics);
|
||||
for (int i = 0; i < num_topics; i++) {
|
||||
char buffer[50];
|
||||
sprintf(buffer, "%f", data[i]);
|
||||
client.publish(topics[i], buffer);
|
||||
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,
|
||||
const char *topic, float *data) {
|
||||
char buffer[100];
|
||||
connect_mqtt(client, device_id, topic);
|
||||
size_t payload_size = construct_json(data, buffer, 100);
|
||||
client.publish(topic, buffer, payload_size);
|
||||
Serial.println("Data transferred successfully");
|
||||
}
|
||||
|
||||
|
|
|
@ -10,10 +10,11 @@ const int WIFI_TIMEOUT = 10000;
|
|||
void initial_connection();
|
||||
void connect_wlan(const int max_retries);
|
||||
void connect_mqtt(PubSubClient &client, const char *device_id,
|
||||
const char **topics, int num_topics);
|
||||
void disconnect_mqtt(PubSubClient &client, const char **topics, int num_topics);
|
||||
const char *topic);
|
||||
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,
|
||||
const char **topics, float *data, int num_topics);
|
||||
const char *topic, float *data);
|
||||
void enter_deep_sleep(bool wifi_timeout);
|
||||
|
||||
#endif /* WLAN_H */
|
||||
|
|
Loading…
Reference in New Issue