Add software debouncing
[manu/arduino-maison.git] / station-meteo / station-meteo.ino
1 // D4: temperature sensor
2 // D2: rain sensor
3
4 // Include the libraries we need
5
6 #include <SPI.h>
7 #include <WiFiNINA.h>
8 #include <WiFiUdp.h>
9 #include <OneWire.h>
10 #include <DallasTemperature.h>
11 #include "station-meteo-config.h"
12
13 // Wifi configuration
14 char wifi_ssid[] = WIFI_SSID; //  your network SSID (name)
15 char wifi_pass[] = WIFI_PASS;    // your network password (use for WPA, or use as key for WEP)
16 int wifi_status = WL_IDLE_STATUS;     // the Wifi radio's status
17
18 // Influxdb configuration
19 byte influxdb_host[] = INFLUXDB_IP;
20 int influxdb_port = INFLUXDB_PORT;
21 unsigned int udp_localport = 2390;
22 String city = CITY;
23 String location = LOCATION;
24
25 // Temperature sensor
26 #define ONE_WIRE_BUS 4
27
28 // Rain fall sensor
29 int rainfall_sensor_pin = 2;
30 float rain_incr = 0.2794; // this is mm/m2 for each sensor tick 
31 int rainfall_ticks = 0;
32 long debouncing_time = 30;
33 volatile unsigned long last_micros;
34
35 // Initiate udp client
36 WiFiUDP udp;
37
38 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
39 OneWire oneWire(ONE_WIRE_BUS);
40
41 // Pass our oneWire reference to Dallas Temperature. 
42 DallasTemperature sensors(&oneWire);
43
44 /*
45  * The setup function. We only start the sensors here
46  */
47 void setup(void)
48 {
49   // start serial port
50   Serial.begin(9600);
51
52   // Start up the onewire library for temp sensor
53   sensors.begin();
54   
55   pinMode(rainfall_sensor_pin, INPUT);
56   attachInterrupt(digitalPinToInterrupt(rainfall_sensor_pin), getRain, RISING);
57
58   // check for the WiFi module:
59   if (WiFi.status() == WL_NO_MODULE) {
60     Serial.println("Communication with WiFi module failed!");
61     // don't continue
62     while (true);
63   }
64
65   String fv = WiFi.firmwareVersion();
66   if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
67     Serial.println("Please upgrade the firmware");
68   }
69
70   Serial.print("Attempting to connect to SSID: ");
71   Serial.println(wifi_ssid);
72   // Connect to WPA/WPA2 network
73   WiFi.begin(wifi_ssid, wifi_pass);
74   delay(500);
75
76   // attempt to connect to Wifi network:
77   while (WiFi.status() != WL_CONNECTED) {
78     WiFi.begin(wifi_ssid, wifi_pass);
79     delay(500);
80     if (WiFi.status() != WL_CONNECTED) {
81         delay(2000);
82     }
83   }
84
85   Serial.println("Connected to wifi");
86   printWifiStatus();
87
88   udp.begin(udp_localport);
89 }
90
91 /*
92  * Main function, get and show the temperature
93  */
94 void loop(void)
95
96   delay(POLL_INT);
97   
98   // Reconnect if needed
99   while (WiFi.status() != WL_CONNECTED) {
100       WiFi.begin(wifi_ssid, wifi_pass);
101       delay(500);
102       if (WiFi.status() != WL_CONNECTED) {
103           delay(2000);
104       }
105   }
106
107   if ( WiFi.status() == WL_CONNECTED ) {
108       String temp_influx_line;
109       String rain_influx_line;
110       float rainfall_1min(0);
111       float tempC(0);
112       
113       // Get rainfall height
114       rainfall_1min = rainfall_ticks * rain_incr;
115       rainfall_ticks = 0;
116       Serial.print("Rain mm/m2: ");
117       Serial.println(rainfall_1min);
118       Serial.println("Sending rain height to influxdb...");
119       rain_influx_line = String("rain,city="+city+",location="+location+" value=" + String(rainfall_1min, 2));
120       udp.beginPacket(influxdb_host, influxdb_port);
121       udp.print(rain_influx_line);
122       udp.endPacket();
123
124       // Temperature
125       tempC = get_temperature();
126       // Check if reading was successful
127       if(tempC != DEVICE_DISCONNECTED_C) 
128       {
129         Serial.print("Temperature for the device 1 (index 0) is: ");
130         Serial.println(tempC);
131         temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
132         // send the packet
133         Serial.println("Sending UDP packet...");
134         udp.beginPacket(influxdb_host, influxdb_port);
135         udp.print(temp_influx_line);
136         udp.endPacket();
137       } 
138       else
139       {
140         Serial.println("Error: Could not read temperature data");
141       }
142   }
143 }
144
145
146 float get_temperature() {
147   float tempC(0);
148   // call sensors.requestTemperatures() to issue a global temperature 
149   // request to all devices on the bus
150   Serial.print("Requesting temperatures...");
151   sensors.requestTemperatures(); // Send the command to get temperatures
152   Serial.println("DONE");
153   // After we got the temperatures, we can print them here.
154   // We use the function ByIndex, we have only one sensor.
155   tempC = sensors.getTempCByIndex(0);
156   return tempC;
157 }
158
159
160 void printWifiStatus() {
161   // print the SSID of the network you're attached to:
162   Serial.print("SSID: ");
163   Serial.println(WiFi.SSID());
164
165   // print your board's IP address:
166   IPAddress ip = WiFi.localIP();
167   Serial.print("IP Address: ");
168   Serial.println(ip);
169
170   // print the received signal strength:
171   long rssi = WiFi.RSSI();
172   Serial.print("signal strength (RSSI):");
173   Serial.print(rssi);
174   Serial.println(" dBm");
175 }
176
177 void getRain ()
178 {
179   if((long)(micros() - last_micros) >= debouncing_time * 1000) {
180     rainfall_ticks ++ ; 
181     last_micros = micros();
182   }
183 }