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);
78 // attempt to connect to Wifi network:
79 while (WiFi.status() != WL_CONNECTED) {
80 WiFi.begin(wifi_ssid, wifi_pass);
82 if (WiFi.status() != WL_CONNECTED) {
87 Serial.println("Connected to wifi");
90 udp.begin(udp_localport);
94 * Main function, get and show the temperature
100 // Reconnect if needed
101 while (WiFi.status() != WL_CONNECTED) {
102 WiFi.begin(wifi_ssid, wifi_pass);
104 if (WiFi.status() != WL_CONNECTED) {
109 if ( WiFi.status() == WL_CONNECTED ) {
110 String temp_influx_line;
111 String rain_influx_line;
112 float rainfall_1min(0);
115 // Get rainfall height
116 rainfall_1min = rainfall_ticks * rain_incr;
118 Serial.print("Rain mm/m2: ");
119 Serial.println(rainfall_1min);
120 Serial.println("Sending rain height to influxdb...");
121 rain_influx_line = String("rain,city="+city+",location="+location+" value=" + String(rainfall_1min, 2));
122 udp.beginPacket(influxdb_host, influxdb_port);
123 udp.print(rain_influx_line);
127 tempC = get_temperature();
128 // Check if reading was successful
129 if(tempC != DEVICE_DISCONNECTED_C)
131 Serial.print("Temperature for the device 1 (index 0) is: ");
132 Serial.println(tempC);
133 temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
135 Serial.println("Sending UDP packet...");
136 udp.beginPacket(influxdb_host, influxdb_port);
137 udp.print(temp_influx_line);
142 Serial.println("Error: Could not read temperature data");
148 float get_temperature() {
150 // call sensors.requestTemperatures() to issue a global temperature
151 // request to all devices on the bus
152 Serial.print("Requesting temperatures...");
153 sensors.requestTemperatures(); // Send the command to get temperatures
154 Serial.println("DONE");
155 // After we got the temperatures, we can print them here.
156 // We use the function ByIndex, we have only one sensor.
157 tempC = sensors.getTempCByIndex(0);
162 void printWifiStatus() {
163 // print the SSID of the network you're attached to:
164 Serial.print("SSID: ");
165 Serial.println(WiFi.SSID());
167 // print your board's IP address:
168 IPAddress ip = WiFi.localIP();
169 Serial.print("IP Address: ");
172 // print the received signal strength:
173 long rssi = WiFi.RSSI();
174 Serial.print("signal strength (RSSI):");
176 Serial.println(" dBm");