From 1ad08b06ff3733b2aefec30cfb370dcbe447931b Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 3 Apr 2023 03:26:12 +0200 Subject: [PATCH] Transfer the data via MQTT --- platformio.ini | 3 +-- src/main.cpp | 26 ++++++++++++++++++++------ src/wlan.cpp | 38 +++++++++++++++++++++++++++++++------- src/wlan.h | 14 +++++++++++--- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/platformio.ini b/platformio.ini index 0382bdc..7fe0869 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,5 +15,4 @@ framework = arduino lib_deps = adafruit/DHT sensor library@^1.4.4 adafruit/Adafruit Unified Sensor@^1.1.9 -build_type=debug -monitor_filters = esp8266_exception_decoder + knolleary/PubSubClient@^2.8 diff --git a/src/main.cpp b/src/main.cpp index d200f45..bad4c58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,21 +7,35 @@ 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 *device_id = "homeostasis"; +WiFiClient wlan_client; +PubSubClient mqtt_client(wlan_client); + +bool check_valid_value(float value) { + return (!isnan(value) && value >= 0 && value <= 100); +} void setup() { Serial.begin(9600); dht.begin(); - wlan_connection(60); + connect_wlan(max_connection_attempts); } void loop() { + float temperature = dht.readTemperature(); + float humidity = dht.readHumidity(); int analog_val = analogRead(fc28_pin); int soil_percentage = map(analog_val, 0, 1023, 0, 100); - char buffer[200]; - sprintf(buffer, "Temperature: %.2f°C Humidity: %.2f%% Soil humidity: %i%%", - dht.readTemperature(), dht.readHumidity(), soil_percentage); - Serial.println(buffer); - delay(30000); + float data[] = {temperature, humidity, static_cast(soil_percentage)}; + if (check_valid_value(temperature) && check_valid_value(humidity)) { + mqtt_transfer(mqtt_client, device_id, topics, data, num_topics); + } + disconnect_mqtt(mqtt_client, topics, num_topics); enter_deep_sleep(false); } diff --git a/src/wlan.cpp b/src/wlan.cpp index e218be3..e76f355 100644 --- a/src/wlan.cpp +++ b/src/wlan.cpp @@ -1,6 +1,5 @@ #include "wlan.h" #include "credentials.h" -#include void initial_connection() { WiFi.begin(SSID, PSK); @@ -9,27 +8,52 @@ void initial_connection() { WiFi.setAutoReconnect(true); } -void wlan_connection(int max_retries) { +void connect_wlan(const int max_retries) { if (WiFi.SSID() != SSID) initial_connection(); int retries = 0; while (WiFi.status() != WL_CONNECTED) { - retries++; if (retries == max_retries) enter_deep_sleep(true); + retries++; delay(1000); Serial.print("."); } Serial.println("WiFi connected"); - Serial.println(WiFi.localIP()); } -void mqtt_connection(char *server, int port, char *fingerprint) { - WiFiClientSecure client; - client.connect(server, port); +void connect_mqtt(PubSubClient &client, const char *device_id, + const char **topics, int num_topics) { + 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]); + } +} + +void disconnect_mqtt(PubSubClient &client, const char **topics, + int num_topics) { + Serial.println("Disconnecting MQTT"); + for (int i = 0; i < num_topics; i++) + client.unsubscribe(topics[i]); + 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); + } + Serial.println("Data transferred successfully"); } void enter_deep_sleep(bool wifi_timeout) { + Serial.println("Entering deep sleep"); if (wifi_timeout) WiFi.disconnect(); ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT); diff --git a/src/wlan.h b/src/wlan.h index 7014394..2d2bd49 100644 --- a/src/wlan.h +++ b/src/wlan.h @@ -1,11 +1,19 @@ #ifndef WLAN_H #define WLAN_H -const int SLEEP_TIME = 480000000; +#include +#include + +const int SLEEP_TIME = 900000000; const int WIFI_TIMEOUT = 10000; -void wlan_connection(int max_retries); -void prometheus_connection(char *server, int port, char *fingerprint); +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); +void mqtt_transfer(PubSubClient &client, const char *device_id, + const char **topics, float *data, int num_topics); void enter_deep_sleep(bool wifi_timeout); #endif /* WLAN_H */