#include #include #include #include "config.h" #include "Adafruit_TSL2561_U.h" // ----------- DEFINES --------------- #define LUX_ID 0x39 // I2C-Id of the lux-sensor // ----------- GLOBALS --------------- WiFiClient espClient; Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(LUX_ID, 12345); void connectWifi() { WiFi.disconnect(false); Serial.printf("Wi-Fi mode set to WIFI_STA: %s\n", WiFi.mode(WIFI_STA) ? "Ok" : "Failed!"); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to ");Serial.println(WIFI_SSID); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print(WiFi.status()); Serial.print(".\r\n"); //WiFi.printDiag(Serial); } Serial.println(""); Serial.print("Connected with IP: "), Serial.println(WiFi.localIP()); Serial.printf("\n"); } void setup() { Serial.begin(38400); //Opens USB-Serial connection for terminal delay(1000); Serial.println("Init of WiFi-Lux-sensor project\r\n"); connectWifi(); Wire.begin(4, 5); // sda on pin D2, scl on pin D1 tsl.begin(); // Startup the sensor Serial.println("Setup TSL2561"); tsl.setGain(TSL2561_GAIN_1X); tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); tsl.enableAutoRange(true); } uint32_t readLuxSensor(void) { uint16_t broadband; uint16_t ir; tsl.getLuminosity(&broadband, &ir); uint32_t lux = tsl.calculateLux(broadband, ir); Serial.printf("Lux:%u\n", lux); if( lux > 65000 ) lux=65000; // Set a maximum value return lux; } void sendToCHEF_HTTPServer(uint32_t lux) { HTTPClient http; Serial.print("[HTTP] begin...Chef\n"); // configure traged server and url // The target web page is: http://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=10.3 String str = "http://192.168.1.110/lux/report_lux.php?lux="; str += lux; str += "&password="; str += LUX_PHP_PASSWORD; http.begin(str); Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); // httpCode will be negative on error if(httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } } void sendToMICKE_HTTPServer(uint32_t lux) { HTTPClient http; Serial.print("[HTTP] begin...Micke\n"); String str = "http://hagaholm.ddns.net/report_lux/report_lux.php?lux="; str += lux; http.begin(str); Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); // httpCode will be negative on error if(httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } } void waitUntilNextLoop() { const uint32_t now = millis(); const uint32_t sleep = (((now / 10000)+1)*10000) - now; delay(sleep); } void loop() { static uint32_t totalLux = 0; static uint16_t loopCnt = 0; Serial.println("------------------"); Serial.print(millis()); Serial.println(" ms"); const uint32_t lux = readLuxSensor(); totalLux += lux; loopCnt++; // Once every minute, calc mean lux and send to server. if( loopCnt == 6 ) { totalLux /= 6; loopCnt = 0; sendToCHEF_HTTPServer( totalLux ); sendToMICKE_HTTPServer( totalLux ); } // Reset the device once every day, as an extra precaution if( millis() > (1000*60*60*24) ) { Serial.println("Reseting....."); delay(50000); ESP.reset(); } waitUntilNextLoop(); }