2023-03-30 01:28:31 +02:00
|
|
|
#include "wlan.h"
|
2023-03-15 01:53:25 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <DHT.h>
|
|
|
|
|
2023-03-15 02:47:49 +01:00
|
|
|
#define DHTTYPE DHT11
|
|
|
|
#define DHTPIN 4
|
|
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
|
2023-03-30 02:10:49 +02:00
|
|
|
const int fc28_pin = A0;
|
2023-04-03 03:26:12 +02:00
|
|
|
const int max_connection_attempts = 60;
|
2023-04-03 06:23:03 +02:00
|
|
|
const char *topic = "homeostasis/room";
|
2023-04-03 03:26:12 +02:00
|
|
|
const char *device_id = "homeostasis";
|
2023-04-03 06:23:03 +02:00
|
|
|
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);
|
|
|
|
}
|
2023-03-15 04:03:22 +01:00
|
|
|
|
2023-03-15 02:47:49 +01:00
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
|
|
dht.begin();
|
2023-04-03 03:26:12 +02:00
|
|
|
connect_wlan(max_connection_attempts);
|
2023-03-15 02:47:49 +01:00
|
|
|
}
|
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();
|
2023-03-15 04:03:22 +01:00
|
|
|
int analog_val = analogRead(fc28_pin);
|
|
|
|
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
|
2023-04-03 06:23:03 +02:00
|
|
|
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)) {
|
2023-04-03 06:23:03 +02:00
|
|
|
mqtt_transfer(mqtt_client, device_id, topic, data);
|
2023-04-03 03:26:12 +02:00
|
|
|
}
|
2023-04-03 06:23:03 +02:00
|
|
|
disconnect_mqtt(mqtt_client, topic);
|
2023-03-30 02:10:49 +02:00
|
|
|
enter_deep_sleep(false);
|
2023-03-15 01:53:25 +01:00
|
|
|
}
|