#include <WiFi.h>
#include <WiFiUdp.h>
-#include <SimpleDHT.h>
+#include <Adafruit_Sensor.h>
+#include <DHT.h>
+#include <DHT_U.h>
#include "salon-config.h"
String location = LOCATION;
WiFiUDP Udp;
-int pinDHT22 = 13;
-SimpleDHT22 dht22(pinDHT22);
+// Temp
+#define DHTPIN 13
+#define DHTTYPE DHT22
+DHT_Unified dht(DHTPIN, DHTTYPE);
void setup() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
+
+ dht.begin();
+ sensor_t sensor;
+ dht.temperature().getSensor(&sensor);
+ dht.humidity().getSensor(&sensor);
+
}
void loop() {
- float temperature = 0;
- float humidity = 0;
- int err = SimpleDHTErrSuccess;
- if ((err = dht22.read2(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) {
- sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(temperature));
- sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(humidity));
- }
-
delay(POLL_INT);
+
+ // Check wifi connexion
+ if ( WiFi.status() != WL_CONNECTED ) {
+ int retry = 0;
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(WIFI_SSID, WIFI_PASS);
+ while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+ retry++;
+ delay(500);
+ }
+ }
+
+ if ( WiFi.status() == WL_CONNECTED ) {
+ sensors_event_t event;
+
+ dht.temperature().getEvent(&event);
+ if (isnan(event.temperature)) {
+ // Error
+ } else {
+ sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+ }
+ dht.humidity().getEvent(&event);
+ if (isnan(event.relative_humidity)) {
+ // Error
+ } else {
+ sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+ }
+ }
}
void sendToInfluxDB(String measure, String key, String value) {