Add wifi auto-reconnect
[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
33 // Timing
34 unsigned long previousMillis=   0;
35 unsigned long previousMillis2=  0;
36
37 // Initiate udp client
38 WiFiUDP udp;
39
40 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
41 OneWire oneWire(ONE_WIRE_BUS);
42
43 // Pass our oneWire reference to Dallas Temperature. 
44 DallasTemperature sensors(&oneWire);
45
46 /*
47  * The setup function. We only start the sensors here
48  */
49 void setup(void)
50 {
51   // start serial port
52   Serial.begin(9600);
53
54   // Start up the onewire library for temp sensor
55   sensors.begin();
56   
57   pinMode(rainfall_sensor_pin, INPUT);
58   attachInterrupt(digitalPinToInterrupt(rainfall_sensor_pin), getRain, RISING);
59
60   // check for the WiFi module:
61   if (WiFi.status() == WL_NO_MODULE) {
62     Serial.println("Communication with WiFi module failed!");
63     // don't continue
64     while (true);
65   }
66
67   String fv = WiFi.firmwareVersion();
68   if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
69     Serial.println("Please upgrade the firmware");
70   }
71
72   Serial.print("Attempting to connect to SSID: ");
73   Serial.println(wifi_ssid);
74   // Connect to WPA/WPA2 network
75   WiFi.begin(wifi_ssid, wifi_pass);
76
77   // attempt to connect to Wifi network:
78   while (WiFi.status() != WL_CONNECTED) {
79     delay(500);
80   }
81
82   Serial.println("Connected to wifi");
83   printWifiStatus();
84
85   udp.begin(udp_localport);
86 }
87
88 /*
89  * Main function, get and show the temperature
90  */
91 void loop(void)
92
93   delay(POLL_INT);
94   
95   // Check wifi connexion
96   if ( WiFi.status() != WL_CONNECTED ) {
97       int retry = 0;
98       WiFi.begin(wifi_ssid, wifi_pass);
99       while (retry < 10 || WiFi.status() != WL_CONNECTED) {
100           retry++;
101           delay(500);
102       }
103   }
104
105   if ( WiFi.status() != WL_CONNECTED ) {
106       String temp_influx_line;
107       String rain_influx_line;
108       float rainfall_1min(0);
109       float tempC(0);
110       
111       // Get rainfall height
112       rainfall_1min = rainfall_ticks * rain_incr;
113       rainfall_ticks = 0;
114       Serial.print("Rain mm/m2: ");
115       Serial.println(rainfall_1min);
116       Serial.println("Sending rain height to influxdb...");
117       rain_influx_line = String("rain,city="+city+"location="+location+" value=" + String(rainfall_1min, 2));
118       udp.beginPacket(influxdb_host, influxdb_port);
119       udp.print(rain_influx_line);
120       udp.endPacket();
121
122       // Temperature
123       tempC = get_temperature();
124       // Check if reading was successful
125       if(tempC != DEVICE_DISCONNECTED_C) 
126       {
127         Serial.print("Temperature for the device 1 (index 0) is: ");
128         Serial.println(tempC);
129         temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
130         // send the packet
131         Serial.println("Sending UDP packet...");
132         udp.beginPacket(influxdb_host, influxdb_port);
133         udp.print(temp_influx_line);
134         udp.endPacket();
135       } 
136       else
137       {
138         Serial.println("Error: Could not read temperature data");
139       }
140   }
141 }
142
143
144 float get_temperature() {
145   float tempC(0);
146   // call sensors.requestTemperatures() to issue a global temperature 
147   // request to all devices on the bus
148   Serial.print("Requesting temperatures...");
149   sensors.requestTemperatures(); // Send the command to get temperatures
150   Serial.println("DONE");
151   // After we got the temperatures, we can print them here.
152   // We use the function ByIndex, we have only one sensor.
153   tempC = sensors.getTempCByIndex(0);
154   return tempC;
155 }
156
157
158 void printWifiStatus() {
159   // print the SSID of the network you're attached to:
160   Serial.print("SSID: ");
161   Serial.println(WiFi.SSID());
162
163   // print your board's IP address:
164   IPAddress ip = WiFi.localIP();
165   Serial.print("IP Address: ");
166   Serial.println(ip);
167
168   // print the received signal strength:
169   long rssi = WiFi.RSSI();
170   Serial.print("signal strength (RSSI):");
171   Serial.print(rssi);
172   Serial.println(" dBm");
173 }
174
175 void getRain ()
176 {
177   rainfall_ticks ++ ; 
178 }