-#include <ESP8266WiFi.h>
+#include <WiFi.h>
#include <WiFiUdp.h>
#include <HCSR04.h>
+#include <Adafruit_Sensor.h>
+#include <DHT.h>
+#include <DHT_U.h>
#include "cave-config.h"
// TRIGGER, ECHO
UltraSonicDistanceSensor distanceSensor(14, 12);
+// Temp
+#define DHTPIN 27
+#define DHTTYPE DHT22
+DHT_Unified dht(DHTPIN, DHTTYPE);
+
void setup() {
#if defined(DEBUG)
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#endif
+
+ dht.begin();
+ sensor_t sensor;
+ dht.temperature().getSensor(&sensor);
+ dht.humidity().getSensor(&sensor);
+
}
void loop() {
- // FIXME: get temperature from sensor and use it here
- float temp = 20.0;
- double water_dist = distanceSensor.measureDistanceCm(temp);
- #if defined(DEBUG)
- Serial.println("Distance: "+String(water_dist));
- #endif
- sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
- delay(POLL_INT);
+ delay(POLL_INT);
+
+ // Check wifi connexion
+ if ( WiFi.status() != WL_CONNECTED ) {
+ int retry = 0;
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(WIFI_SSID, WIFI_PASS);
+ while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+ retry++;
+ delay(500);
+ }
+ }
+
+ if ( WiFi.status() == WL_CONNECTED ) {
+ sensors_event_t event;
+
+ dht.temperature().getEvent(&event);
+ if (isnan(event.temperature)) {
+ #if defined(DEBUG)
+ Serial.println("DHT 22 temperature error");
+ #endif
+ } else {
+ #if defined(DEBUG)
+ Serial.println("Temperature: "+String(event.temperature));
+ #endif
+ sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+ double water_dist = distanceSensor.measureDistanceCm(event.temperature);
+ #if defined(DEBUG)
+ Serial.println("Distance: "+String(water_dist));
+ #endif
+ sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
+ }
+ dht.humidity().getEvent(&event);
+ if (isnan(event.relative_humidity)) {
+ #if defined(DEBUG)
+ Serial.println("DHT 22 humidity error");
+ #endif
+ } else {
+ #if defined(DEBUG)
+ Serial.println("Humidity: "+String(event.relative_humidity));
+ #endif
+ sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+ }
+ }
}
void sendToInfluxDB(String measure, String key, String value) {
String line = measure+" "+key+"="+value;
- int line_length = line.length()+1;
- char influx_line[line_length];
- line.toCharArray(influx_line, line_length);
Udp.begin(localPort);
Udp.beginPacket(influxdb_ip, influxdb_port);
- Udp.write(influx_line);
+ Udp.print(line);
Udp.endPacket();
}