Import initial sketches with sample configuration files
[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   // attempt to connect to Wifi network:
73   while (wifi_status != WL_CONNECTED) {
74     Serial.print("Attempting to connect to SSID: ");
75     Serial.println(wifi_ssid);
76     // Connect to WPA/WPA2 network
77     wifi_status = WiFi.begin(wifi_ssid, wifi_pass);
78
79     // wait 10 seconds for connection:
80     delay(10000);
81   }
82
83   Serial.println("Connected to wifi");
84   printWifiStatus();
85
86   udp.begin(udp_localport);
87 }
88
89 /*
90  * Main function, get and show the temperature
91  */
92 void loop(void)
93
94
95   String temp_influx_line;
96   String rain_influx_line;
97   float rainfall_1min(0);
98   float tempC(0);
99   
100   // Get rainfall height
101   rainfall_1min = rainfall_ticks * rain_incr;
102   rainfall_ticks = 0;
103   Serial.print("Rain mm/m2: ");
104   Serial.println(rainfall_1min);
105   Serial.println("Sending rain height to influxdb...");
106   rain_influx_line = String("rain,city="+city+"location="+location+" value=" + String(rainfall_1min, 2));
107   udp.beginPacket(influxdb_host, influxdb_port);
108   udp.print(rain_influx_line);
109   udp.endPacket();
110
111   // Temperature
112   tempC = get_temperature();
113   // Check if reading was successful
114   if(tempC != DEVICE_DISCONNECTED_C) 
115   {
116     Serial.print("Temperature for the device 1 (index 0) is: ");
117     Serial.println(tempC);
118     temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
119     // send the packet
120     Serial.println("Sending UDP packet...");
121     udp.beginPacket(influxdb_host, influxdb_port);
122     udp.print(temp_influx_line);
123     udp.endPacket();
124   } 
125   else
126   {
127     Serial.println("Error: Could not read temperature data");
128   }
129   delay(POLL_INT);
130 }
131
132
133 float get_temperature() {
134   float tempC(0);
135   // call sensors.requestTemperatures() to issue a global temperature 
136   // request to all devices on the bus
137   Serial.print("Requesting temperatures...");
138   sensors.requestTemperatures(); // Send the command to get temperatures
139   Serial.println("DONE");
140   // After we got the temperatures, we can print them here.
141   // We use the function ByIndex, we have only one sensor.
142   tempC = sensors.getTempCByIndex(0);
143   return tempC;
144 }
145
146
147 void printWifiStatus() {
148   // print the SSID of the network you're attached to:
149   Serial.print("SSID: ");
150   Serial.println(WiFi.SSID());
151
152   // print your board's IP address:
153   IPAddress ip = WiFi.localIP();
154   Serial.print("IP Address: ");
155   Serial.println(ip);
156
157   // print the received signal strength:
158   long rssi = WiFi.RSSI();
159   Serial.print("signal strength (RSSI):");
160   Serial.print(rssi);
161   Serial.println(" dBm");
162 }
163
164 void getRain ()
165 {
166   rainfall_ticks ++ ; 
167 }