Switch to DallasTemperature for DS18B20
authorEmmanuel Lacour <elacour@easter-eggs.com>
Tue, 5 Apr 2022 16:26:43 +0000 (18:26 +0200)
committerEmmanuel Lacour <elacour@easter-eggs.com>
Tue, 5 Apr 2022 16:26:43 +0000 (18:26 +0200)
garage/garage.ino

index 2467f07..a6c20cc 100644 (file)
@@ -1,9 +1,8 @@
 #include <WiFi.h>
 #include <WiFiUdp.h>
 #include <LibTeleinfo2Std.h>
-#include <Adafruit_Sensor.h>
-#include <DHT.h>
-#include <DHT_U.h>
+#include <OneWire.h>
+#include <DallasTemperature.h>
 #include <HardwareSerial.h>
 
 #include "garage-config.h"
@@ -25,9 +24,12 @@ String location = LOCATION;
 WiFiUDP Udp;
 
 // Temp
-#define DHTPIN 19
-#define DHTTYPE DHT22
-DHT_Unified dht(DHTPIN, DHTTYPE);
+// GPIO where the DS18B20 is connected to
+const int oneWireBus = 19;
+// Setup a oneWire instance to communicate with any OneWire devices
+OneWire oneWire(oneWireBus);
+// Pass our oneWire reference to Dallas Temperature sensor 
+DallasTemperature sensors(&oneWire);
 
 // Uptime timer
 boolean tick1sec=0;// one for interrupt, don't mess with 
@@ -66,10 +68,7 @@ void setup() {
     delay(500);
   }
 
-  dht.begin();
-  sensor_t sensor;
-  dht.temperature().getSensor(&sensor);
-  dht.humidity().getSensor(&sensor);
+  sensors.begin();
 
   tinfo.init(TINFO_MODE_STD);
   tinfo.attachDataStd(DataCallback);
@@ -96,20 +95,16 @@ void loop() {
 
     if ( WiFi.status() == WL_CONNECTED ) {
         Serial.println("DEBUG wifi connected, start temp measure");
-        sensors_event_t event;
+
+        sensors.requestTemperatures(); 
+        float tempC = sensors.getTempCByIndex(0);
         
-        dht.temperature().getEvent(&event);
-        if (isnan(event.temperature)) {
+        if(tempC == DEVICE_DISCONNECTED_C) 
           // 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));
+            sendToInfluxDB("temperature,city="+city+",location=serre_semis", "value", String(tempC));
         }
+        
         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS", tinfo_SINSTS);
         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS1", tinfo_SINSTS1);
         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS2", tinfo_SINSTS2);