|
@@ -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();
|
|
|
}
|