homeostasis/src/main.cpp

38 lines
1000 B
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
const int max_connection_attempts = 60;
const char *topic = "homeostasis/room";
2023-04-03 03:26:12 +02:00
const char *device_id = "homeostasis";
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
2023-04-03 03:26:12 +02:00
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);
float data[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
2023-04-03 03:26:12 +02:00
if (check_valid_value(temperature) && check_valid_value(humidity)) {
mqtt_transfer(mqtt_client, device_id, topic, data);
2023-04-03 03:26:12 +02:00
}
disconnect_mqtt(mqtt_client, topic);
enter_deep_sleep(false);
2023-03-15 01:53:25 +01:00
}