Add wifi auto-reconnect
[manu/arduino-maison.git] / cave / cave.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <HCSR04.h>
4 #include <Adafruit_Sensor.h>
5 #include <DHT.h>
6 #include <DHT_U.h>
7
8 #include "cave-config.h"
9
10 const char*    wifi_ssid = WIFI_SSID;
11 const char*    wifi_pass = WIFI_PASS;
12
13 // Grafana
14 IPAddress      influxdb_ip(INFLUXDB_IP);
15 long           influxdb_port = INFLUXDB_PORT;
16 unsigned int   localPort = 2390;
17 // MilliSeconds
18 long unsigned  poll_int = POLL_INT;
19 String city = CITY;
20 String location = LOCATION;
21 WiFiUDP Udp;
22
23 // TRIGGER, ECHO
24 UltraSonicDistanceSensor distanceSensor(14, 12);
25
26 // Temp
27 #define DHTPIN 27
28 #define DHTTYPE DHT22
29 DHT_Unified dht(DHTPIN, DHTTYPE);
30
31 void setup() {
32
33   #if defined(DEBUG)
34   Serial.begin(115200);
35   Serial.print("Connecting to ");
36   Serial.println(wifi_ssid);
37   #endif
38
39   WiFi.mode(WIFI_STA);
40   WiFi.begin(WIFI_SSID, WIFI_PASS);
41
42   while (WiFi.status() != WL_CONNECTED) {
43     delay(500);
44     #if defined(DEBUG)
45     Serial.print(".");
46     #endif
47   }
48
49   #if defined(DEBUG)
50   Serial.println("WiFi connected");
51   Serial.println("IP address: ");
52   Serial.println(WiFi.localIP());
53   #endif
54
55   dht.begin();
56   sensor_t sensor;
57   dht.temperature().getSensor(&sensor);
58   dht.humidity().getSensor(&sensor);
59
60 }
61
62 void loop() {
63   delay(POLL_INT);
64
65   // Check wifi connexion
66   if ( WiFi.status() != WL_CONNECTED ) {
67       int retry = 0;
68       WiFi.mode(WIFI_STA);
69       WiFi.begin(WIFI_SSID, WIFI_PASS);
70       while (retry < 10 || WiFi.status() != WL_CONNECTED) {
71           retry++;
72           delay(500);
73       }
74   }
75
76   if ( WiFi.status() != WL_CONNECTED ) {
77       sensors_event_t event;
78       
79       dht.temperature().getEvent(&event);
80       if (isnan(event.temperature)) {
81           #if defined(DEBUG)
82           Serial.println("DHT 22 temperature error");
83           #endif
84       } else {
85           #if defined(DEBUG)
86           Serial.println("Temperature: "+String(event.temperature));
87           #endif
88           sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
89           double water_dist = distanceSensor.measureDistanceCm(event.temperature);
90           #if defined(DEBUG)
91           Serial.println("Distance: "+String(water_dist));
92           #endif
93           sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
94       }
95       dht.humidity().getEvent(&event);
96       if (isnan(event.relative_humidity)) {
97           #if defined(DEBUG)
98           Serial.println("DHT 22 humidity error");
99           #endif
100       } else {
101           #if defined(DEBUG)
102           Serial.println("Humidity: "+String(event.relative_humidity));
103           #endif
104           sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
105       }
106   }
107 }
108
109 void sendToInfluxDB(String measure, String key, String value) {
110   String line = measure+" "+key+"="+value;
111   Udp.begin(localPort);
112   Udp.beginPacket(influxdb_ip, influxdb_port);
113   Udp.print(line);
114   Udp.endPacket();
115 }