Switch to ESP 32
[manu/arduino-maison.git] / cave / cave.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <HCSR04.h>
4
5 #include "cave-config.h"
6
7 const char*    wifi_ssid = WIFI_SSID;
8 const char*    wifi_pass = WIFI_PASS;
9
10 // Grafana
11 IPAddress      influxdb_ip(INFLUXDB_IP);
12 long           influxdb_port = INFLUXDB_PORT;
13 unsigned int   localPort = 2390;
14 // MilliSeconds
15 long unsigned  poll_int = POLL_INT;
16 String city = CITY;
17 String location = LOCATION;
18 WiFiUDP Udp;
19
20 // TRIGGER, ECHO
21 UltraSonicDistanceSensor distanceSensor(14, 12);
22
23 void setup() {
24
25   #if defined(DEBUG)
26   Serial.begin(115200);
27   Serial.print("Connecting to ");
28   Serial.println(wifi_ssid);
29   #endif
30
31   WiFi.mode(WIFI_STA);
32   WiFi.begin(WIFI_SSID, WIFI_PASS);
33
34   while (WiFi.status() != WL_CONNECTED) {
35     delay(500);
36     #if defined(DEBUG)
37     Serial.print(".");
38     #endif
39   }
40
41   #if defined(DEBUG)
42   Serial.println("WiFi connected");
43   Serial.println("IP address: ");
44   Serial.println(WiFi.localIP());
45   #endif
46 }
47
48 void loop() {
49   // FIXME: get temperature from sensor and use it here
50   float temp = 20.0;
51   double water_dist = distanceSensor.measureDistanceCm(temp);
52   #if defined(DEBUG)
53   Serial.println("Distance: "+String(water_dist));
54   #endif
55   sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
56   delay(POLL_INT); 
57 }
58
59 void sendToInfluxDB(String measure, String key, String value) {
60   String line = measure+" "+key+"="+value;
61   Udp.begin(localPort);
62   Udp.beginPacket(influxdb_ip, influxdb_port);
63   Udp.print(line);
64   Udp.endPacket();
65 }