wifi_lux_sensor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 sendToHTTPServer(uint32_t lux)
  50. {
  51. HTTPClient http;
  52. Serial.print("[HTTP] begin...\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://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
  56. str += lux;
  57. http.begin(str);
  58. Serial.print("[HTTP] GET...\n");
  59. // start connection and send HTTP header
  60. int httpCode = http.GET();
  61. // httpCode will be negative on error
  62. if(httpCode > 0) {
  63. // HTTP header has been send and Server response header has been handled
  64. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  65. // file found at server
  66. if(httpCode == HTTP_CODE_OK) {
  67. String payload = http.getString();
  68. Serial.println(payload);
  69. }
  70. } else {
  71. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  72. }
  73. }
  74. void waitUntilNextLoop() {
  75. const uint32_t now = millis();
  76. const uint32_t sleep = (((now / 10000)+1)*10000) - now;
  77. delay(sleep);
  78. }
  79. void loop() {
  80. static uint32_t totalLux = 0;
  81. static uint16_t loopCnt = 0;
  82. Serial.print(millis());
  83. Serial.println(" mS ------------------");
  84. const uint32_t lux = readLuxSensor();
  85. totalLux += lux;
  86. loopCnt++;
  87. // Once every minute, calc mean lux and send to server.
  88. if( loopCnt == 6 ) {
  89. totalLux /= 6;
  90. loopCnt = 0;
  91. sendToHTTPServer( totalLux );
  92. }
  93. // Reset the device once every day, as an extra precaution
  94. if( millis() > (1000*60*60*24) ) {
  95. Serial.println("Reseting.....");
  96. ESP.reset();
  97. }
  98. waitUntilNextLoop();
  99. }