#include //https://github.com/esp8266/Arduino //needed for library #include #include #include //https://github.com/tzapu/WiFiManager #include #include #include ESP8266WiFiMulti WiFiMulti; OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary) void setup() { // put your setup code here, to run once: //Serial.begin(74880); Serial.begin(9600); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset settings - for testing //wifiManager.resetSettings(); //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds wifiManager.setTimeout(180); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration if(!wifiManager.autoConnect("NashultaTempAP")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } // I found a new lib here: // https://github.com/milesburton/Arduino-Temperature-Control-Library // It looks complete and more advanced than this one that I use. double 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(); delay(250); if ( !ds.search(addr)) { Serial.println("Sensor not found"); ds.reset_search(); delay(250); return 11.6; } if (OneWire::crc8(addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return 12.6; } Serial.println(); ds.reset(); ds.select(addr); ds.write(0x44, 1); // start conversion, with parasite power on at the end delay(1200); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. int16_t raw = (data[1] << 8) | data[0]; byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time celsius = (double)raw * 0.0625; Serial.print(" Temperature = "); Serial.print(celsius); Serial.println(" Celsius, "); return celsius; } // Functions that until next 10 second period void waitUntilNextLoop() { const uint32_t now = millis(); const uint32_t sleep = (((now / 10000)+1)*10000) - now; delay(sleep); } void sendToCHEF_HTTPServer(double 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; Serial.println(str); 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()); } http.end(); } else { // Something really bad has happened, reset everything and restart Serial.println("Reset now !"); ESP.reset(); } } #define NO_OF_READS 12 // Send every two minutes void loop() { static double totalTemp = 0.0; static uint16_t loopCnt = 0; Serial.println("------------------"); Serial.print(millis()); Serial.println(" ms"); const double temp = readTemperature(); //sendToCHEF_HTTPServer( temp + 500 ); totalTemp += temp; loopCnt++; // When loopCnt == 1, 1 read // When loopCnt == 12, 12 reads // Calc mean lux and send to server. if( loopCnt == NO_OF_READS ) { totalTemp /= NO_OF_READS; sendToCHEF_HTTPServer( totalTemp ); // Reset loopCnt = 0; totalTemp = 0.0; } // Reset the device once every day, as an extra precaution if( millis() > (1000*60*60*24) ) { Serial.println("Reseting....."); delay(50000); ESP.reset(); } waitUntilNextLoop(); }