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;
32 long debouncing_time = 30;
33 volatile unsigned long last_micros;
35 // Initiate udp client
38 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
39 OneWire oneWire(ONE_WIRE_BUS);
41 // Pass our oneWire reference to Dallas Temperature.
42 DallasTemperature sensors(&oneWire);
45 * The setup function. We only start the sensors here
52 // Start up the onewire library for temp sensor
55 pinMode(rainfall_sensor_pin, INPUT);
56 attachInterrupt(digitalPinToInterrupt(rainfall_sensor_pin), getRain, RISING);
58 // check for the WiFi module:
59 if (WiFi.status() == WL_NO_MODULE) {
60 Serial.println("Communication with WiFi module failed!");
65 String fv = WiFi.firmwareVersion();
66 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
67 Serial.println("Please upgrade the firmware");
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);
76 // attempt to connect to Wifi network:
77 while (WiFi.status() != WL_CONNECTED) {
78 WiFi.begin(wifi_ssid, wifi_pass);
80 if (WiFi.status() != WL_CONNECTED) {
85 Serial.println("Connected to wifi");
88 udp.begin(udp_localport);
92 * Main function, get and show the temperature
98 // Reconnect if needed
99 while (WiFi.status() != WL_CONNECTED) {
100 WiFi.begin(wifi_ssid, wifi_pass);
102 if (WiFi.status() != WL_CONNECTED) {
107 if ( WiFi.status() == WL_CONNECTED ) {
108 String temp_influx_line;
109 String rain_influx_line;
110 float rainfall_1min(0);
113 // Get rainfall height
114 rainfall_1min = rainfall_ticks * rain_incr;
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);
125 tempC = get_temperature();
126 // Check if reading was successful
127 if(tempC != DEVICE_DISCONNECTED_C)
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));
133 Serial.println("Sending UDP packet...");
134 udp.beginPacket(influxdb_host, influxdb_port);
135 udp.print(temp_influx_line);
140 Serial.println("Error: Could not read temperature data");
146 float get_temperature() {
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);
160 void printWifiStatus() {
161 // print the SSID of the network you're attached to:
162 Serial.print("SSID: ");
163 Serial.println(WiFi.SSID());
165 // print your board's IP address:
166 IPAddress ip = WiFi.localIP();
167 Serial.print("IP Address: ");
170 // print the received signal strength:
171 long rssi = WiFi.RSSI();
172 Serial.print("signal strength (RSSI):");
174 Serial.println(" dBm");
179 if((long)(micros() - last_micros) >= debouncing_time * 1000) {
181 last_micros = micros();