Add more debugging
[manu/arduino-maison.git] / cave / cave.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <HCSR04.h>
4 #include <SimpleDHT.h>
5
6 #include "cave-config.h"
7
8 const char*    wifi_ssid = WIFI_SSID;
9 const char*    wifi_pass = WIFI_PASS;
10
11 // Grafana
12 IPAddress      influxdb_ip(INFLUXDB_IP);
13 long           influxdb_port = INFLUXDB_PORT;
14 unsigned int   localPort = 2390;
15 // MilliSeconds
16 long unsigned  poll_int = POLL_INT;
17 String city = CITY;
18 String location = LOCATION;
19 WiFiUDP Udp;
20
21 // TRIGGER, ECHO
22 UltraSonicDistanceSensor distanceSensor(14, 12);
23
24 // Temp
25 int pinDHT22 = 27;
26 SimpleDHT22 dht22(pinDHT22);
27
28 void setup() {
29
30   #if defined(DEBUG)
31   Serial.begin(115200);
32   Serial.print("Connecting to ");
33   Serial.println(wifi_ssid);
34   #endif
35
36   WiFi.mode(WIFI_STA);
37   WiFi.begin(WIFI_SSID, WIFI_PASS);
38
39   while (WiFi.status() != WL_CONNECTED) {
40     delay(500);
41     #if defined(DEBUG)
42     Serial.print(".");
43     #endif
44   }
45
46   #if defined(DEBUG)
47   Serial.println("WiFi connected");
48   Serial.println("IP address: ");
49   Serial.println(WiFi.localIP());
50   #endif
51 }
52
53 void loop() {
54   float temperature = 0;
55   float humidity = 0;
56   int err = SimpleDHTErrSuccess;
57   if ((err = dht22.read2(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) {
58       #if defined(DEBUG)
59       Serial.println("Temperature: "+String(temperature));
60       Serial.println("Humidity: "+String(humidity));
61       #endif
62     sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(temperature));
63     sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(humidity));
64   } else {
65       #if defined(DEBUG)
66       Serial.println("DHT 22 error");
67       #endif
68   }
69   double water_dist = distanceSensor.measureDistanceCm(temperature);
70   #if defined(DEBUG)
71   Serial.println("Distance: "+String(water_dist));
72   #endif
73   sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
74   delay(POLL_INT); 
75 }
76
77 void sendToInfluxDB(String measure, String key, String value) {
78   String line = measure+" "+key+"="+value;
79   Udp.begin(localPort);
80   Udp.beginPacket(influxdb_ip, influxdb_port);
81   Udp.print(line);
82   Udp.endPacket();
83 }