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 // 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);
79 // wait 10 seconds for connection:
83 Serial.println("Connected to wifi");
86 udp.begin(udp_localport);
90 * Main function, get and show the temperature
95 String temp_influx_line;
96 String rain_influx_line;
97 float rainfall_1min(0);
100 // Get rainfall height
101 rainfall_1min = rainfall_ticks * rain_incr;
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);
112 tempC = get_temperature();
113 // Check if reading was successful
114 if(tempC != DEVICE_DISCONNECTED_C)
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));
120 Serial.println("Sending UDP packet...");
121 udp.beginPacket(influxdb_host, influxdb_port);
122 udp.print(temp_influx_line);
127 Serial.println("Error: Could not read temperature data");
133 float get_temperature() {
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);
147 void printWifiStatus() {
148 // print the SSID of the network you're attached to:
149 Serial.print("SSID: ");
150 Serial.println(WiFi.SSID());
152 // print your board's IP address:
153 IPAddress ip = WiFi.localIP();
154 Serial.print("IP Address: ");
157 // print the received signal strength:
158 long rssi = WiFi.RSSI();
159 Serial.print("signal strength (RSSI):");
161 Serial.println(" dBm");