wifi_temp_logger.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. float celsius = 0.0;
  12. void setup() {
  13. // put your setup code here, to run once:
  14. //Serial.begin(74880);
  15. Serial.begin(9600);
  16. //WiFiManager
  17. //Local intialization. Once its business is done, there is no need to keep it around
  18. WiFiManager wifiManager;
  19. //reset settings - for testing
  20. //wifiManager.resetSettings();
  21. //sets timeout until configuration portal gets turned off
  22. //useful to make it all retry or go to sleep
  23. //in seconds
  24. wifiManager.setTimeout(180);
  25. //fetches ssid and pass and tries to connect
  26. //if it does not connect it starts an access point with the specified name
  27. //here "AutoConnectAP"
  28. //and goes into a blocking loop awaiting configuration
  29. if(!wifiManager.autoConnect("NashultaTempAP")) {
  30. Serial.println("failed to connect and hit timeout");
  31. delay(3000);
  32. //reset and try again, or maybe put it to deep sleep
  33. ESP.reset();
  34. delay(5000);
  35. }
  36. //if you get here you have connected to the WiFi
  37. Serial.println("connected...yeey :)");
  38. }
  39. void oneWireRead(void) {
  40. byte i;
  41. byte present = 0;
  42. byte data[12];
  43. byte addr[8];
  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;
  52. }
  53. if (OneWire::crc8(addr, 7) != addr[7]) {
  54. Serial.println("CRC is not valid!");
  55. return;
  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. }
  85. void loop() {
  86. unsigned long now = millis(); // Get the current time
  87. static unsigned long previous_now = 0; // Get the current time
  88. static unsigned long previous_http_time = 20000; // Start 1st measure after 20 sec
  89. // Check millis() for wrapping (after 49 days)
  90. if( previous_now > now ) {
  91. previous_http_time = 0;
  92. }
  93. if( now > previous_http_time ) {
  94. // Set next measure to 1 minutes
  95. previous_http_time = now + (1*60*1000);
  96. // Read the temp sensor.....
  97. oneWireRead();
  98. // wait for WiFi connection
  99. if((WiFiMulti.run() == WL_CONNECTED)) {
  100. HTTPClient http;
  101. Serial.print("[HTTP] begin...\n");
  102. // configure traged server and url
  103. // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3
  104. String str = "http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
  105. str += celsius;
  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. }