1 // D4: temperature sensor
4 // Include the libraries we need
10 #include <DallasTemperature.h>
11 #include "station-meteo-config.h"
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
18 // Influxdb configuration
19 byte influxdb_host[] = INFLUXDB_IP;
20 int influxdb_port = INFLUXDB_PORT;
21 unsigned int udp_localport = 2390;
23 String location = LOCATION;
26 #define ONE_WIRE_BUS 4
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;
34 unsigned long previousMillis= 0;
35 unsigned long previousMillis2= 0;
37 // Initiate udp client
40 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
41 OneWire oneWire(ONE_WIRE_BUS);
43 // Pass our oneWire reference to Dallas Temperature.
44 DallasTemperature sensors(&oneWire);
47 * The setup function. We only start the sensors here
54 // Start up the onewire library for temp sensor
57 pinMode(rainfall_sensor_pin, INPUT);
58 attachInterrupt(digitalPinToInterrupt(rainfall_sensor_pin), getRain, RISING);
60 // check for the WiFi module:
61 if (WiFi.status() == WL_NO_MODULE) {
62 Serial.println("Communication with WiFi module failed!");
67 String fv = WiFi.firmwareVersion();
68 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
69 Serial.println("Please upgrade the firmware");
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);
77 // attempt to connect to Wifi network:
78 while (WiFi.status() != WL_CONNECTED) {
82 Serial.println("Connected to wifi");
85 udp.begin(udp_localport);
89 * Main function, get and show the temperature
95 // Check wifi connexion
96 if ( WiFi.status() != WL_CONNECTED ) {
98 WiFi.begin(wifi_ssid, wifi_pass);
99 while (retry < 10 || WiFi.status() != WL_CONNECTED) {
105 if ( WiFi.status() == WL_CONNECTED ) {
106 String temp_influx_line;
107 String rain_influx_line;
108 float rainfall_1min(0);
111 // Get rainfall height
112 rainfall_1min = rainfall_ticks * rain_incr;
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);
123 tempC = get_temperature();
124 // Check if reading was successful
125 if(tempC != DEVICE_DISCONNECTED_C)
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));
131 Serial.println("Sending UDP packet...");
132 udp.beginPacket(influxdb_host, influxdb_port);
133 udp.print(temp_influx_line);
138 Serial.println("Error: Could not read temperature data");
144 float get_temperature() {
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);
158 void printWifiStatus() {
159 // print the SSID of the network you're attached to:
160 Serial.print("SSID: ");
161 Serial.println(WiFi.SSID());
163 // print your board's IP address:
164 IPAddress ip = WiFi.localIP();
165 Serial.print("IP Address: ");
168 // print the received signal strength:
169 long rssi = WiFi.RSSI();
170 Serial.print("signal strength (RSSI):");
172 Serial.println(" dBm");