homeostasis/src/main.cpp

42 lines
1.2 KiB
C++
Raw Normal View History

#include "wlan.h"
2023-03-15 01:53:25 +01:00
#include <Arduino.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 4
DHT dht(DHTPIN, DHTTYPE);
const int fc28_pin = A0;
2023-04-03 03:26:12 +02:00
// TODO Tweak threshold to get good value
const int soil_threshold = 40;
2023-04-03 03:26:12 +02:00
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();
2023-04-03 03:26:12 +02:00
connect_wlan(max_connection_attempts);
}
2023-03-15 01:53:25 +01:00
void loop() {
2023-04-03 03:26:12 +02:00
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int analog_val = analogRead(fc28_pin);
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
2023-04-03 03:26:12 +02:00
float data[] = {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);
}
disconnect_mqtt(mqtt_client, topics, num_topics);
enter_deep_sleep(false);
2023-03-15 01:53:25 +01:00
}