wifi_lux_sensor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <Wire.h>
  4. #include "config.h"
  5. #include "Adafruit_TSL2561_U.h"
  6. // ----------- DEFINES ---------------
  7. #define LUX_ID 0x39 // I2C-Id of the lux-sensor
  8. // ----------- GLOBALS ---------------
  9. WiFiClient espClient;
  10. Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(LUX_ID, 12345);
  11. void connectWifi() {
  12. WiFi.disconnect(false);
  13. Serial.printf("Wi-Fi mode set to WIFI_STA: %s\n", WiFi.mode(WIFI_STA) ? "Ok" : "Failed!");
  14. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  15. Serial.print("Connecting to ");Serial.println(WIFI_SSID);
  16. while (WiFi.status() != WL_CONNECTED) {
  17. delay(1000);
  18. Serial.print(WiFi.status());
  19. Serial.print(".\r\n");
  20. //WiFi.printDiag(Serial);
  21. }
  22. Serial.println("");
  23. Serial.print("Connected with IP: "),
  24. Serial.println(WiFi.localIP());
  25. Serial.printf("\n");
  26. }
  27. void setup() {
  28. Serial.begin(38400); //Opens USB-Serial connection for terminal
  29. delay(1000);
  30. Serial.println("Init of WiFi-Lux-sensor project\r\n");
  31. connectWifi();
  32. Wire.begin(4, 5); // sda on pin D2, scl on pin D1
  33. tsl.begin(); // Startup the sensor
  34. Serial.println("Setup TSL2561");
  35. tsl.setGain(TSL2561_GAIN_1X);
  36. tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);
  37. tsl.enableAutoRange(true);
  38. }
  39. uint32_t readLuxSensor(void)
  40. {
  41. uint16_t broadband;
  42. uint16_t ir;
  43. tsl.getLuminosity(&broadband, &ir);
  44. uint32_t lux = tsl.calculateLux(broadband, ir);
  45. Serial.printf("Lux:%u\n", lux);
  46. if( lux > 65000 ) lux=65000; // Set a maximum value
  47. return lux;
  48. }
  49. void sendToCHEF_HTTPServer(uint32_t lux)
  50. {
  51. HTTPClient http;
  52. Serial.print("[HTTP] begin...Chef\n");
  53. // configure traged server and url
  54. // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3
  55. String str = "http://192.168.1.110/lux/report_lux.php?lux=";
  56. str += lux;
  57. str += "&password=";
  58. str += LUX_PHP_PASSWORD;
  59. http.begin(str);
  60. Serial.print("[HTTP] GET...\n");
  61. // start connection and send HTTP header
  62. int httpCode = http.GET();
  63. // httpCode will be negative on error
  64. if(httpCode > 0) {
  65. // HTTP header has been send and Server response header has been handled
  66. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  67. // file found at server
  68. if(httpCode == HTTP_CODE_OK) {
  69. String payload = http.getString();
  70. Serial.println(payload);
  71. }
  72. } else {
  73. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  74. }
  75. }
  76. void sendToMICKE_HTTPServer(uint32_t lux)
  77. {
  78. HTTPClient http;
  79. Serial.print("[HTTP] begin...Micke\n");
  80. String str = "http://hagaholm.ddns.net/report_lux/report_lux.php?lux=";
  81. str += lux;
  82. http.begin(str);
  83. Serial.print("[HTTP] GET...\n");
  84. // start connection and send HTTP header
  85. int httpCode = http.GET();
  86. // httpCode will be negative on error
  87. if(httpCode > 0) {
  88. // HTTP header has been send and Server response header has been handled
  89. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  90. // file found at server
  91. if(httpCode == HTTP_CODE_OK) {
  92. String payload = http.getString();
  93. Serial.println(payload);
  94. }
  95. } else {
  96. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  97. }
  98. }
  99. void waitUntilNextLoop() {
  100. const uint32_t now = millis();
  101. const uint32_t sleep = (((now / 10000)+1)*10000) - now;
  102. delay(sleep);
  103. }
  104. void loop() {
  105. static uint32_t totalLux = 0;
  106. static uint16_t loopCnt = 0;
  107. Serial.println("------------------");
  108. Serial.print(millis());
  109. Serial.println(" ms");
  110. const uint32_t lux = readLuxSensor();
  111. totalLux += lux;
  112. loopCnt++;
  113. // Once every minute, calc mean lux and send to server.
  114. if( loopCnt == 6 ) {
  115. totalLux /= 6;
  116. loopCnt = 0;
  117. sendToCHEF_HTTPServer( totalLux );
  118. sendToMICKE_HTTPServer( totalLux );
  119. }
  120. // Reset the device once every day, as an extra precaution
  121. if( millis() > (1000*60*60*24) ) {
  122. Serial.println("Reseting.....");
  123. delay(50000);
  124. ESP.reset();
  125. }
  126. waitUntilNextLoop();
  127. }