Having my first results in hand I have to modify my software a bit. The DHT11 sometimes give strange measuring values (0°C, 6°C) and I want to change my measuring procedure. I will measure 3 values for temperature and humidity and I will take the middle values. The soil moisture measurement is quite unstable so I will take the average of three measurements.
Additionally I have cleaned up the code structure.
I had troubles with two issues:
- My measurement values are stored in arrays of struct. I learned that the typedef must be done in a header.
- The ESP8266 does not support the std library. So I use a macro for the min function
That’s the result:
1 2 3 4 5 6 |
// Struct for measuring values typedef struct { float temp; float hum; float soil; } value_type; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
#include #include "Logger.h" // min macro for ESP #define espmin(X, Y) (((X)<(Y))?(X):(Y)) // WiFi connection const char* ssid = "SSID"; const char* password = "PASSWORD"; char server[] = "SERVERIP"; int port = 80; WiFiClient client; const unsigned long postingInterval = 1800L * 1000000L; // delay between updates, in microseconds //DHT #include #define DHTPIN 4 #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE,16); // needed to avoid link error on ram check extern "C" { #include "user_interface.h" } void setup() { value_type values[3]; // start serial pinMode(12, OUTPUT); pinMode(A0, INPUT); dht.begin(); Serial.begin(115200); Serial.println("WLAN Greenhouse Data Logger - Electric Playground"); // First measuring values[0]= domeasuring(); delay(1000); // Second measuring values[1]= domeasuring(); delay(1000); // Third measuring values[2]= domeasuring(); // Finding the middle value float temp; float hum; float minmin = espmin(espmin(values[0].temp, values[1].temp), values[2].temp); if (values[0].temp = minmin) { temp=espmin(values[1].temp, values[2].temp); hum=espmin(values[1].hum, values[2].hum); } else if (values[1].temp = minmin) { temp=espmin(values[0].temp, values[2].temp); hum=espmin(values[0].hum, values[2].hum); } else { temp=espmin(values[0].temp, values[1].temp); hum=espmin(values[0].hum, values[1].hum); } float soil=(values[0].soil+values[1].soil+values[1].soil)/3.0; // send data // Connect to WIFI WiFi.mode(WIFI_STA); WiFiStart(); // debug while (client.available()) { char c = client.read(); Serial.write(c); } Serial.println("Humidity:"+String(hum,2)+"\tTemperature:"+String(temp,2)+"\tSoil:"+String(soil,2)); httpRequest(hum,temp,soil); Serial.println("Good Night"); ESP.deepSleep(postingInterval, WAKE_NO_RFCAL); //WAKE_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED. delay(1000); } void loop() { // do nothing } // Measueing value_type domeasuring() { digitalWrite(12, HIGH); delay(300); value_type retval; retval.hum = dht.readHumidity(); if (isnan(retval.hum)) retval.hum=999; retval.temp = dht.readTemperature(); if (isnan(retval.temp)) retval.temp=999; retval.soil = analogRead(A0); digitalWrite(12, LOW); Serial.println("Humidity:"+String(retval.hum,2)+"\tTemperature:"+String(retval.temp,2)+"\tSoil:"+String(retval.soil,2)); return retval; } void httpRequest(float h, float t, float s) { // close any connection before send a new request. // This will free the socket on the WiFi shield client.stop(); delay(100); // We now create a URI for the request String url = "/newrecord.php?"; url += "temp="; url += String(t,2); url += "&humidity="; url += String(h,2); url += "&soil="; url += String(s,2); // if there's a successful connection: if (client.connect(server, port)) { Serial.println("connecting..."); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n"); Serial.println("data submitted."); delay(10); } else { // if you couldn't make a connection: Serial.println("connection failed"); } } void WiFiStart() { // Connect to WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(250); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Print the IP address Serial.println(WiFi.localIP()); } |