wifi_temp_logger.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  2. //needed for library
  3. #include <DNSServer.h>
  4. #include <ESP8266WebServer.h>
  5. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  6. #include <OneWire.h>
  7. #include <ESP8266WiFiMulti.h>
  8. #include <ESP8266HTTPClient.h>
  9. ESP8266WiFiMulti WiFiMulti;
  10. OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
  11. void setup() {
  12. // put your setup code here, to run once:
  13. //Serial.begin(74880);
  14. Serial.begin(9600);
  15. //WiFiManager
  16. //Local intialization. Once its business is done, there is no need to keep it around
  17. WiFiManager wifiManager;
  18. //reset settings - for testing
  19. //wifiManager.resetSettings();
  20. //sets timeout until configuration portal gets turned off
  21. //useful to make it all retry or go to sleep
  22. //in seconds
  23. wifiManager.setTimeout(180);
  24. //fetches ssid and pass and tries to connect
  25. //if it does not connect it starts an access point with the specified name
  26. //here "AutoConnectAP"
  27. //and goes into a blocking loop awaiting configuration
  28. if(!wifiManager.autoConnect("NashultaTempAP")) {
  29. Serial.println("failed to connect and hit timeout");
  30. delay(3000);
  31. //reset and try again, or maybe put it to deep sleep
  32. ESP.reset();
  33. delay(5000);
  34. }
  35. //if you get here you have connected to the WiFi
  36. Serial.println("connected...yeey :)");
  37. }
  38. // I found a new lib here:
  39. // https://github.com/milesburton/Arduino-Temperature-Control-Library
  40. // It looks complete and more advanced than this one that I use.
  41. double readTemperature() {
  42. byte i;
  43. byte present = 0;
  44. byte data[12];
  45. byte addr[8];
  46. float celsius = 0.0;
  47. // We only have one sensor, so we reset the search every time
  48. ds.reset_search();
  49. delay(250);
  50. if ( !ds.search(addr)) {
  51. Serial.println("Sensor not found");
  52. ds.reset_search();
  53. delay(250);
  54. return 11.6;
  55. }
  56. if (OneWire::crc8(addr, 7) != addr[7]) {
  57. Serial.println("CRC is not valid!");
  58. return 12.6;
  59. }
  60. Serial.println();
  61. ds.reset();
  62. ds.select(addr);
  63. ds.write(0x44, 1); // start conversion, with parasite power on at the end
  64. delay(1200); // maybe 750ms is enough, maybe not
  65. // we might do a ds.depower() here, but the reset will take care of it.
  66. present = ds.reset();
  67. ds.select(addr);
  68. ds.write(0xBE); // Read Scratchpad
  69. for ( i = 0; i < 9; i++) { // we need 9 bytes
  70. data[i] = ds.read();
  71. }
  72. // Convert the data to actual temperature
  73. // because the result is a 16 bit signed integer, it should
  74. // be stored to an "int16_t" type, which is always 16 bits
  75. // even when compiled on a 32 bit processor.
  76. int16_t raw = (data[1] << 8) | data[0];
  77. byte cfg = (data[4] & 0x60);
  78. // at lower res, the low bits are undefined, so let's zero them
  79. if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
  80. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  81. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  82. //// default is 12 bit resolution, 750 ms conversion time
  83. celsius = (double)raw * 0.0625;
  84. Serial.print(" Temperature = ");
  85. Serial.print(celsius);
  86. Serial.println(" Celsius, ");
  87. return celsius;
  88. }
  89. // Functions that until next 10 second period
  90. void waitUntilNextLoop() {
  91. const uint32_t now = millis();
  92. const uint32_t sleep = (((now / 10000)+1)*10000) - now;
  93. delay(sleep);
  94. }
  95. void sendToCHEF_HTTPServer(double temperature)
  96. {
  97. // Check for WiFi connection
  98. if((WiFiMulti.run() == WL_CONNECTED)) {
  99. HTTPClient http;
  100. Serial.print("[HTTP] begin...\n");
  101. // configure traged server and url
  102. // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3
  103. String str = "http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
  104. str += temperature;
  105. Serial.println(str);
  106. http.begin(str);
  107. Serial.print("[HTTP] GET...\n");
  108. // start connection and send HTTP header
  109. int httpCode = http.GET();
  110. // httpCode will be negative on error
  111. if(httpCode > 0) {
  112. // HTTP header has been send and Server response header has been handled
  113. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  114. // file found at server
  115. if(httpCode == HTTP_CODE_OK) {
  116. String payload = http.getString();
  117. Serial.println(payload);
  118. }
  119. } else {
  120. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  121. }
  122. http.end();
  123. }
  124. else {
  125. // Something really bad has happened, reset everything and restart
  126. Serial.println("Reset now !");
  127. ESP.reset();
  128. }
  129. }
  130. #define NO_OF_READS 12 // Send every two minutes
  131. void loop() {
  132. static double totalTemp = 0.0;
  133. static uint16_t loopCnt = 0;
  134. Serial.println("------------------");
  135. Serial.print(millis());
  136. Serial.println(" ms");
  137. const double temp = readTemperature();
  138. //sendToCHEF_HTTPServer( temp + 500 );
  139. totalTemp += temp;
  140. loopCnt++;
  141. // When loopCnt == 1, 1 read
  142. // When loopCnt == 12, 12 reads
  143. // Calc mean lux and send to server.
  144. if( loopCnt == NO_OF_READS ) {
  145. totalTemp /= NO_OF_READS;
  146. sendToCHEF_HTTPServer( totalTemp );
  147. // Reset
  148. loopCnt = 0;
  149. totalTemp = 0.0;
  150. }
  151. // Reset the device once every day, as an extra precaution
  152. if( millis() > (1000*60*60*24) ) {
  153. Serial.println("Reseting.....");
  154. delay(50000);
  155. ESP.reset();
  156. }
  157. waitUntilNextLoop();
  158. }