Sfoglia il codice sorgente

Changed to read every 2 minutes. Code make-over

Thomas Chef 8 anni fa
parent
commit
1cd5144dd7
2 ha cambiato i file con 75 aggiunte e 50 eliminazioni
  1. 1 1
      makefile
  2. 74 49
      wifi_temp_logger.ino

+ 1 - 1
makefile

@@ -18,7 +18,7 @@
 #====================================================================================
 
 #=== Project specific definitions: sketch and list of needed libraries
-SKETCH ?= $(HOME)/makeEspArduino/WiFi_Temp_Logger/wifi_temp_logger.ino
+SKETCH ?= $(HOME)/makeEspArduino/WiFi_Temp_Logger/wifi_temp_logger.cpp
 LIBS ?= \
 		$(ESP_LIBS)/ESP8266WiFi \
         $(ESP_LIBS)/OneWire \

+ 74 - 49
wifi_temp_logger.ino

@@ -11,8 +11,6 @@
 ESP8266WiFiMulti WiFiMulti;
 
 OneWire  ds(2);  // on pin 10 (a 4.7K resistor is necessary)
-float celsius = 0.0;
-
 
 void setup() {
 
@@ -49,11 +47,12 @@ void setup() {
  
 }
 
-void oneWireRead(void) {
+float readTemperature() {
   byte i;
   byte present = 0;
   byte data[12];
   byte addr[8];
+  float celsius = 0.0;
 
   // We only have one sensor, so we reset the search every time
   ds.reset_search();
@@ -63,12 +62,12 @@ void oneWireRead(void) {
     Serial.println("Sensor not found");
     ds.reset_search();
     delay(250);
-    return;
+    return 0.0;
   }
 
   if (OneWire::crc8(addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
-      return;
+      return 0.0;
   }
   Serial.println();
 
@@ -104,65 +103,91 @@ void oneWireRead(void) {
   Serial.print("  Temperature = ");
   Serial.print(celsius);
   Serial.println(" Celsius, ");
+
+  return celsius;
 }
 
-void loop() {
+// Functions that until next 10 second period
+void waitUntilNextLoop() {
+  const uint32_t now = millis();
 
-    unsigned long now = millis(); // Get the current time
-    static unsigned long previous_now = 0; // Get the current time
-    static unsigned long previous_http_time = 20000; // Start 1st measure after 20 sec
+  const uint32_t sleep = (((now / 10000)+1)*10000) - now;
 
-    // Check millis() for wrapping (after 49 days)
-    if( previous_now > now ) {
-      previous_http_time = 0;
-    }
+  delay(sleep);
+}
 
+void sendToCHEF_HTTPServer(float temperature)
+{
+    // Check for WiFi connection
+    if((WiFiMulti.run() == WL_CONNECTED)) {
+
+        HTTPClient http;
+
+        Serial.print("[HTTP] begin...\n");
+        // configure traged server and url
+        // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3
+        String str = "http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
+        str += temperature;
+        http.begin(str);
+
+        Serial.print("[HTTP] GET...\n");
+        // start connection and send HTTP header
+        int httpCode = http.GET();
+
+        // httpCode will be negative on error
+        if(httpCode > 0) {
+            // HTTP header has been send and Server response header has been handled
+            Serial.printf("[HTTP] GET... code: %d\n", httpCode);
+
+            // file found at server
+            if(httpCode == HTTP_CODE_OK) {
+                String payload = http.getString();
+                Serial.println(payload);
+            }
+        } else {
+            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
+        }
 
-    if( now > previous_http_time  ) {
+        http.end();
+    }
+    else {
+        // Something really bad has happened, reset everything and restart
+        Serial.println("Reset now !");
+        ESP.reset();
+    }
+}
 
-        // Set next measure to 1 minutes
-        previous_http_time = now + (3*60*1000);
+#define NO_OF_READS   12  // Send every two minutes
 
-        // Read the temp sensor.....
-        oneWireRead();
+void loop() {
+    static float totalTemp = 0;
+    static uint16_t loopCnt = 0;
 
-        // wait for WiFi connection
-        if((WiFiMulti.run() == WL_CONNECTED)) {
+    Serial.println("------------------");
+    Serial.print(millis());
+    Serial.println(" ms");    
 
-            HTTPClient http;
+    const float temp = readTemperature();
 
-            Serial.print("[HTTP] begin...\n");
-            // configure traged server and url
-            // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3
-            String str = "http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
-            str += celsius;
-            http.begin(str);
+    totalTemp += temp;
 
-            Serial.print("[HTTP] GET...\n");
-            // start connection and send HTTP header
-            int httpCode = http.GET();
+    loopCnt++;
 
-            // httpCode will be negative on error
-            if(httpCode > 0) {
-                // HTTP header has been send and Server response header has been handled
-                Serial.printf("[HTTP] GET... code: %d\n", httpCode);
+    // Calc mean lux and send to server.
+    if( loopCnt == NO_OF_READS ) {
 
-                // file found at server
-                if(httpCode == HTTP_CODE_OK) {
-                    String payload = http.getString();
-                    Serial.println(payload);
-                }
-            } else {
-                Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
-            }
+        totalTemp /= NO_OF_READS;
+        loopCnt = 0;
 
-            http.end();
-        }
-        else {
-            // Something really bad has happened, reset everything and restart
-            Serial.println("Reset now !");
-            ESP.reset();
-        }
+        sendToCHEF_HTTPServer( totalTemp );
     }
 
+    // Reset the device once every day, as an extra precaution
+    if( millis() > (1000*60*60*24) ) {
+        Serial.println("Reseting.....");
+        delay(50000);
+        ESP.reset();
+    }
+   
+    waitUntilNextLoop();
 }