#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"
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
+// ROM = 28 C6 C9 81 E3 6E 3C C ROM = 28 31 A7 1D 13 21 1 F6 ROM = 28 15 8E 4B 13 21 1 46 No more addresses.
+DeviceAddress sensor_heater_out = {0x28, 0x31, 0xA7, 0x1D, 0x13, 0x21, 0x01, 0xF6};
+DeviceAddress sensor_serre = {0x28, 0x15, 0x8E, 0x4B, 0x13, 0x21, 0x01, 0x46};
+DeviceAddress sensor_heater_in = {0x28, 0xC6, 0xC9, 0x81, 0xE3, 0x6E, 0x3C ,0x0C};
+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
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
-
+ delay(500);
+
while (WiFi.status() != WL_CONNECTED) {
+ WiFi.begin(wifi_ssid, wifi_pass);
delay(500);
+ if (WiFi.status() != WL_CONNECTED) {
+ delay(2000);
+ }
}
- dht.begin();
- sensor_t sensor;
- dht.temperature().getSensor(&sensor);
- dht.humidity().getSensor(&sensor);
+ sensors.begin();
tinfo.init(TINFO_MODE_STD);
tinfo.attachDataStd(DataCallback);
fulldata = true;
previousMillis = millis();
- // Check wifi connexion
- if ( WiFi.status() != WL_CONNECTED ) {
- int retry = 0;
- WiFi.mode(WIFI_STA);
+ // Reconnect if needed
+ while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASS);
- while (retry < 10 || WiFi.status() != WL_CONNECTED) {
- retry++;
- delay(500);
+ delay(500);
+ if (WiFi.status() != WL_CONNECTED) {
+ delay(2000);
}
}
if ( WiFi.status() == WL_CONNECTED ) {
Serial.println("DEBUG wifi connected, start temp measure");
- sensors_event_t event;
+
+ sensors.requestTemperatures();
+ float serre_tempC = sensors.getTempC(sensor_serre);
+ float heater_out_tempC = sensors.getTempC(sensor_heater_out);
+ float heater_in_tempC = sensors.getTempC(sensor_heater_in);
+
+ if(serre_tempC == DEVICE_DISCONNECTED_C) {
+ // Error
+ } else {
+ sendToInfluxDB("temperature,city="+city+",location=serre-semis", "value", String(serre_tempC));
+ }
- dht.temperature().getEvent(&event);
- if (isnan(event.temperature)) {
+ if(heater_out_tempC == DEVICE_DISCONNECTED_C) {
// Error
} else {
- sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+ sendToInfluxDB("temperature,city="+city+",location=heater,direction=out", "value", String(heater_out_tempC));
}
- dht.humidity().getEvent(&event);
- if (isnan(event.relative_humidity)) {
+
+ if(heater_in_tempC == DEVICE_DISCONNECTED_C) {
// Error
} else {
- sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+ sendToInfluxDB("temperature,city="+city+",location=heater,direction=in", "value", String(heater_in_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);
--- /dev/null
+// Board ESP8266 - NodeMCU 1.0 (ESP-12E module)
+#include <Arduino.h>
+#include <ESP8266WiFi.h>
+#include <WiFiUdp.h>
+#include <Adafruit_Sensor.h>
+#include <DHT.h>
+#include <DHT_U.h>
+
+#include "grande-chambre-config.h"
+
+const char* wifi_ssid = WIFI_SSID;
+const char* wifi_pass = WIFI_PASS;
+
+// Grafana
+IPAddress influxdb_ip(INFLUXDB_IP);
+long influxdb_port = INFLUXDB_PORT;
+unsigned int localPort = 2390;
+// MilliSeconds
+long unsigned poll_int = POLL_INT;
+String city = CITY;
+String location = LOCATION;
+WiFiUDP Udp;
+
+// Temp
+// 5 = D1 on esp8266
+#define DHTPIN 5
+#define DHTTYPE DHT22
+DHT_Unified dht(DHTPIN, DHTTYPE);
+
+void setup() {
+
+ Serial.begin(9600);
+ // Not for esp8266 WiFi.mode(WIFI_STA);
+ WiFi.begin(WIFI_SSID, WIFI_PASS);
+ while (WiFi.status() != WL_CONNECTED) {
+ delay(500);
+ }
+
+ dht.begin();
+ sensor_t sensor;
+ dht.temperature().getSensor(&sensor);
+ dht.humidity().getSensor(&sensor);
+
+}
+
+void loop() {
+ delay(POLL_INT);
+ // Check wifi connexion
+ if ( WiFi.status() != WL_CONNECTED ) {
+ int retry = 0;
+ // Not for esp8266 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) {
+ String line = measure+" "+key+"="+value;
+ Udp.begin(localPort);
+ Udp.beginPacket(influxdb_ip, influxdb_port);
+ Udp.print(line);
+ Udp.endPacket();
+}
int rainfall_sensor_pin = 2;
float rain_incr = 0.2794; // this is mm/m2 for each sensor tick
int rainfall_ticks = 0;
-
-// Timing
-unsigned long previousMillis= 0;
-unsigned long previousMillis2= 0;
+long debouncing_time = 30;
+volatile unsigned long last_micros;
// Initiate udp client
WiFiUDP udp;
Serial.println(wifi_ssid);
// Connect to WPA/WPA2 network
WiFi.begin(wifi_ssid, wifi_pass);
+ delay(500);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
+ WiFi.begin(wifi_ssid, wifi_pass);
delay(500);
+ if (WiFi.status() != WL_CONNECTED) {
+ delay(2000);
+ }
}
Serial.println("Connected to wifi");
{
delay(POLL_INT);
- // Check wifi connexion
- if ( WiFi.status() != WL_CONNECTED ) {
- int retry = 0;
+ // Reconnect if needed
+ while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(wifi_ssid, wifi_pass);
- while (retry < 10 || WiFi.status() != WL_CONNECTED) {
- retry++;
- delay(500);
+ delay(500);
+ if (WiFi.status() != WL_CONNECTED) {
+ delay(2000);
}
}
void getRain ()
{
- rainfall_ticks ++ ;
+ if((long)(micros() - last_micros) >= debouncing_time * 1000) {
+ rainfall_ticks ++ ;
+ last_micros = micros();
+ }
}