|
@@ -1,4 +1,5 @@
|
|
|
#include <ESP8266WiFi.h>
|
|
|
+#include <ESP8266HTTPClient.h>
|
|
|
#include <Wire.h>
|
|
|
|
|
|
#include "config.h"
|
|
@@ -10,7 +11,7 @@
|
|
|
// ----------- GLOBALS ---------------
|
|
|
WiFiClient espClient;
|
|
|
|
|
|
-Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(0x39, 12345);
|
|
|
+Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(LUX_ID, 12345);
|
|
|
|
|
|
void connectWifi() {
|
|
|
WiFi.disconnect(false);
|
|
@@ -36,27 +37,15 @@ void connectWifi() {
|
|
|
void setup() {
|
|
|
Serial.begin(38400); //Opens USB-Serial connection for terminal
|
|
|
delay(1000);
|
|
|
- Serial.println("Init of WiFi-Lux-sensor project");
|
|
|
- //connectWifi();
|
|
|
+ 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();
|
|
|
-
|
|
|
- Serial.println("Set gain.");
|
|
|
- // You can change the gain on the fly, to adapt to brighter/dimmer light situations
|
|
|
- tsl.setGain(TSL2561_GAIN_1X); // 1x gain (bright light)
|
|
|
- //tsl.setGain(TSL2591_GAIN_MED); // 25x gain
|
|
|
- //tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
|
|
|
-
|
|
|
- // Changing the integration time gives you a longer time over which to sense light
|
|
|
- // longer timelines are slower, but are good in very low light situtations!
|
|
|
- tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
|
|
|
- //tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
|
|
|
- //tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
|
|
|
- //tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
|
|
|
- //tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
|
|
|
- //tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
|
|
|
+ tsl.begin(); // Startup the sensor
|
|
|
|
|
|
+ Serial.println("Setup TSL2561");
|
|
|
+ tsl.setGain(TSL2561_GAIN_1X);
|
|
|
+ tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);
|
|
|
tsl.enableAutoRange(true);
|
|
|
}
|
|
|
|
|
@@ -67,54 +56,78 @@ uint32_t readLuxSensor(void)
|
|
|
|
|
|
tsl.getLuminosity(&broadband, &ir);
|
|
|
uint32_t lux = tsl.calculateLux(broadband, ir);
|
|
|
- Serial.printf("Lux:%u ", lux);
|
|
|
+ Serial.printf("Lux:%u\n", lux);
|
|
|
+ if( lux > 65000 ) lux=65000; // Set a maximum value
|
|
|
+ return lux;
|
|
|
+}
|
|
|
|
|
|
- Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
|
|
|
- Serial.print("IR: "); Serial.print(ir); Serial.print(" ");
|
|
|
- Serial.print("broadband: "); Serial.print(broadband); Serial.print(" ");
|
|
|
- Serial.print("Visible: "); Serial.print(broadband - ir); Serial.println(" ");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if ( (lux > 4294966000.0) ||
|
|
|
- (lux <-4294966000.0) )
|
|
|
- {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- else {
|
|
|
- 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());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-unsigned int readI2CRegister16bit(int addr, int reg) {
|
|
|
- Wire.beginTransmission(addr);
|
|
|
- Wire.write(reg);
|
|
|
- Wire.endTransmission();
|
|
|
- delay(200);
|
|
|
- Wire.requestFrom(addr, 2);
|
|
|
- unsigned int t = Wire.read() << 8;
|
|
|
- t = t | Wire.read();
|
|
|
- return t;
|
|
|
+void waitUntilNextLoop() {
|
|
|
+ const uint32_t now = millis();
|
|
|
+
|
|
|
+ const uint32_t sleep = (((now / 10000)+1)*10000) - now;
|
|
|
+
|
|
|
+ delay(sleep);
|
|
|
}
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
- // put your main code here, to run repeatedly:
|
|
|
- byte error, address;
|
|
|
-
|
|
|
|
|
|
- //unsigned int value = readI2CRegister16bit(0x39, 0xAC);
|
|
|
+ static uint32_t totalLux = 0;
|
|
|
+ static uint16_t loopCnt = 0;
|
|
|
|
|
|
- //Serial.printf("Value:%u\n", value);
|
|
|
-
|
|
|
- /*
|
|
|
- Serial.print("WiFi heartbeat - ms since boot: ");
|
|
|
Serial.print(millis());
|
|
|
- Serial.println();
|
|
|
- */
|
|
|
+ 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;
|
|
|
|
|
|
- readLuxSensor();
|
|
|
+ sendToHTTPServer( totalLux );
|
|
|
+ }
|
|
|
+
|
|
|
+ // Reset the device once every day, as an extra precaution
|
|
|
+ if( millis() > (1000*60*60*24) ) {
|
|
|
+ Serial.println("Reseting.....");
|
|
|
+ ESP.reset();
|
|
|
+ }
|
|
|
|
|
|
- delay(1000);
|
|
|
-
|
|
|
+ waitUntilNextLoop();
|
|
|
}
|