#include "wlan.h" #include #include #define DHTTYPE DHT11 #define DHTPIN 4 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(); 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); 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); }