homeostasis/src/main.cpp

38 lines
1000 B
C++

#include "wlan.h"
#include <Arduino.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 4
DHT dht(DHTPIN, DHTTYPE);
const int fc28_pin = A0;
const int max_connection_attempts = 60;
const char *topic = "homeostasis/room";
const char *device_id = "homeostasis";
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_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[3] = {temperature, humidity, static_cast<float>(soil_percentage)};
if (check_valid_value(temperature) && check_valid_value(humidity)) {
mqtt_transfer(mqtt_client, device_id, topic, data);
}
disconnect_mqtt(mqtt_client, topic);
enter_deep_sleep(false);
}