#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) float celsius = 0.0; 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 :)"); } void oneWireRead(void) { byte i; byte present = 0; byte data[12]; byte addr[8]; // 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; } if (OneWire::crc8(addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return; } Serial.println(); ds.reset(); ds.select(addr); ds.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // 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 = (float)raw / 16.0; Serial.print(" Temperature = "); Serial.print(celsius); Serial.println(" Celsius, "); } void loop() { 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 // Check millis() for wrapping (after 49 days) if( previous_now > now ) { previous_http_time = 0; } if( now > previous_http_time ) { // Set next measure to 1 minutes previous_http_time = now + (1*60*1000); // Read the temp sensor..... oneWireRead(); // wait 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 += celsius; 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(); } } }