123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <Wire.h>
- #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 sendToHTTPServer(uint32_t lux)
- {
- HTTPClient http;
- Serial.print("[HTTP] begin...\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://chef.suka.se/nashulta/report_nashulta_temp.php?outtemp=";
- 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.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;
- sendToHTTPServer( totalLux );
- }
- // Reset the device once every day, as an extra precaution
- if( millis() > (1000*60*60*24) ) {
- Serial.println("Reseting.....");
- ESP.reset();
- }
-
- waitUntilNextLoop();
- }
|