// D4: temperature sensor // D2: rain sensor // Include the libraries we need #include #include #include #include #include #include "station-meteo-config.h" // Wifi configuration char wifi_ssid[] = WIFI_SSID; // your network SSID (name) char wifi_pass[] = WIFI_PASS; // your network password (use for WPA, or use as key for WEP) int wifi_status = WL_IDLE_STATUS; // the Wifi radio's status // Influxdb configuration byte influxdb_host[] = INFLUXDB_IP; int influxdb_port = INFLUXDB_PORT; unsigned int udp_localport = 2390; String city = CITY; String location = LOCATION; // Temperature sensor #define ONE_WIRE_BUS 4 // Rain fall sensor int rainfall_sensor_pin = 2; float rain_incr = 0.2794; // this is mm/m2 for each sensor tick int rainfall_ticks = 0; // Timing unsigned long previousMillis= 0; unsigned long previousMillis2= 0; // Initiate udp client WiFiUDP udp; // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); /* * The setup function. We only start the sensors here */ void setup(void) { // start serial port Serial.begin(9600); // Start up the onewire library for temp sensor sensors.begin(); pinMode(rainfall_sensor_pin, INPUT); attachInterrupt(digitalPinToInterrupt(rainfall_sensor_pin), getRain, RISING); // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } Serial.print("Attempting to connect to SSID: "); Serial.println(wifi_ssid); // Connect to WPA/WPA2 network WiFi.begin(wifi_ssid, wifi_pass); // attempt to connect to Wifi network: while (WiFi.status() != WL_CONNECTED) { delay(500); } Serial.println("Connected to wifi"); printWifiStatus(); udp.begin(udp_localport); } /* * Main function, get and show the temperature */ void loop(void) { delay(POLL_INT); // Check wifi connexion if ( WiFi.status() != WL_CONNECTED ) { int retry = 0; WiFi.begin(wifi_ssid, wifi_pass); while (retry < 10 || WiFi.status() != WL_CONNECTED) { retry++; delay(500); } } if ( WiFi.status() == WL_CONNECTED ) { String temp_influx_line; String rain_influx_line; float rainfall_1min(0); float tempC(0); // Get rainfall height rainfall_1min = rainfall_ticks * rain_incr; rainfall_ticks = 0; Serial.print("Rain mm/m2: "); Serial.println(rainfall_1min); Serial.println("Sending rain height to influxdb..."); rain_influx_line = String("rain,city="+city+"location="+location+" value=" + String(rainfall_1min, 2)); udp.beginPacket(influxdb_host, influxdb_port); udp.print(rain_influx_line); udp.endPacket(); // Temperature tempC = get_temperature(); // Check if reading was successful if(tempC != DEVICE_DISCONNECTED_C) { Serial.print("Temperature for the device 1 (index 0) is: "); Serial.println(tempC); temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2)); // send the packet Serial.println("Sending UDP packet..."); udp.beginPacket(influxdb_host, influxdb_port); udp.print(temp_influx_line); udp.endPacket(); } else { Serial.println("Error: Could not read temperature data"); } } } float get_temperature() { float tempC(0); // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus Serial.print("Requesting temperatures..."); sensors.requestTemperatures(); // Send the command to get temperatures Serial.println("DONE"); // After we got the temperatures, we can print them here. // We use the function ByIndex, we have only one sensor. tempC = sensors.getTempCByIndex(0); return tempC; } void printWifiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your board's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); } void getRain () { rainfall_ticks ++ ; }