123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- //needed for library
- #include <DNSServer.h>
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- #include <OneWire.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266HTTPClient.h>
- 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 :)");
-
- }
- 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();
- delay(250);
-
- if ( !ds.search(addr)) {
- Serial.println("Sensor not found");
- ds.reset_search();
- delay(250);
- return 0.0;
- }
- if (OneWire::crc8(addr, 7) != addr[7]) {
- Serial.println("CRC is not valid!");
- return 0.0;
- }
- 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, ");
- 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(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());
- }
- 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 float totalTemp = 0;
- static uint16_t loopCnt = 0;
- Serial.println("------------------");
- Serial.print(millis());
- Serial.println(" ms");
- const float temp = readTemperature();
- totalTemp += temp;
- loopCnt++;
- // Calc mean lux and send to server.
- if( loopCnt == NO_OF_READS ) {
- totalTemp /= NO_OF_READS;
- loopCnt = 0;
- 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();
- }
|