wifi_temp_logger.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. float readTemperature() {
  39. byte i;
  40. byte present = 0;
  41. byte data[12];
  42. byte addr[8];
  43. float celsius = 0.0;
  44. // We only have one sensor, so we reset the search every time
  45. ds.reset_search();
  46. delay(250);
  47. if ( !ds.search(addr)) {
  48. Serial.println("Sensor not found");
  49. ds.reset_search();
  50. delay(250);
  51. return 0.0;
  52. }
  53. if (OneWire::crc8(addr, 7) != addr[7]) {
  54. Serial.println("CRC is not valid!");
  55. return 0.0;
  56. }
  57. Serial.println();
  58. ds.reset();
  59. ds.select(addr);
  60. ds.write(0x44, 1); // start conversion, with parasite power on at the end
  61. delay(1000); // maybe 750ms is enough, maybe not
  62. // we might do a ds.depower() here, but the reset will take care of it.
  63. present = ds.reset();
  64. ds.select(addr);
  65. ds.write(0xBE); // Read Scratchpad
  66. for ( i = 0; i < 9; i++) { // we need 9 bytes
  67. data[i] = ds.read();
  68. }
  69. // Convert the data to actual temperature
  70. // because the result is a 16 bit signed integer, it should
  71. // be stored to an "int16_t" type, which is always 16 bits
  72. // even when compiled on a 32 bit processor.
  73. int16_t raw = (data[1] << 8) | data[0];
  74. byte cfg = (data[4] & 0x60);
  75. // at lower res, the low bits are undefined, so let's zero them
  76. if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
  77. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  78. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  79. //// default is 12 bit resolution, 750 ms conversion time
  80. celsius = (float)raw / 16.0;
  81. Serial.print(" Temperature = ");
  82. Serial.print(celsius);
  83. Serial.println(" Celsius, ");
  84. return celsius;
  85. }
  86. // Functions that until next 10 second period
  87. void waitUntilNextLoop() {
  88. const uint32_t now = millis();
  89. const uint32_t sleep = (((now / 10000)+1)*10000) - now;
  90. delay(sleep);
  91. }
  92. void sendToCHEF_HTTPServer(float temperature)
  93. {
  94. // Check for WiFi connection
  95. if((WiFiMulti.run() == WL_CONNECTED)) {
  96. HTTPClient http;
  97. Serial.print("[HTTP] begin...\n");
  98. // configure traged server and url
  99. // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3
  100. String str = "http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
  101. str += temperature;
  102. http.begin(str);
  103. Serial.print("[HTTP] GET...\n");
  104. // start connection and send HTTP header
  105. int httpCode = http.GET();
  106. // httpCode will be negative on error
  107. if(httpCode > 0) {
  108. // HTTP header has been send and Server response header has been handled
  109. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  110. // file found at server
  111. if(httpCode == HTTP_CODE_OK) {
  112. String payload = http.getString();
  113. Serial.println(payload);
  114. }
  115. } else {
  116. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  117. }
  118. http.end();
  119. }
  120. else {
  121. // Something really bad has happened, reset everything and restart
  122. Serial.println("Reset now !");
  123. ESP.reset();
  124. }
  125. }
  126. #define NO_OF_READS 12 // Send every two minutes
  127. void loop() {
  128. static float totalTemp = 0;
  129. static uint16_t loopCnt = 0;
  130. Serial.println("------------------");
  131. Serial.print(millis());
  132. Serial.println(" ms");
  133. const float temp = readTemperature();
  134. totalTemp += temp;
  135. loopCnt++;
  136. // Calc mean lux and send to server.
  137. if( loopCnt == NO_OF_READS ) {
  138. totalTemp /= NO_OF_READS;
  139. loopCnt = 0;
  140. sendToCHEF_HTTPServer( totalTemp );
  141. }
  142. // Reset the device once every day, as an extra precaution
  143. if( millis() > (1000*60*60*24) ) {
  144. Serial.println("Reseting.....");
  145. delay(50000);
  146. ESP.reset();
  147. }
  148. waitUntilNextLoop();
  149. }