Kaynağa Gözat

Reading average of 6, sending every minute.

Thomas Chef 8 yıl önce
ebeveyn
işleme
4071a0d28e
4 değiştirilmiş dosya ile 83 ekleme ve 109 silme
  1. 11 9
      README.md
  2. 0 43
      lux_sensor.h
  3. 2 0
      makefile
  4. 70 57
      wifi_lux_sensor.cpp

+ 11 - 9
README.md

@@ -1,22 +1,24 @@
-#WiFi-connected Lux-sensor
+# WiFi-connected Lux-sensor
 
-This project is for building a WiFi-connected Lux-sensor for the garage.
+This project is for building a WiFi-connected Lux-sensor for the garage. The sensor reads the lux-value once every 10 seconds, and sends the value to the server once a minute.
 
 The sensor used is Adafruit TSL2561 with I2C-Id: 0x39
 
-WiFi ssid and password are stored in a file called config.h, which is not commited but looks like this:
+WiFi ssid and password are stored in a file called config.h, which is not commited to git but looks like this:
 ```c++
 #define WIFI_SSID "WiFi-ssid"
 #define WIFI_PASSWORD "password"
 ```
+#### Sensor connections:
+| Colour        | ESP8266           | Sensor  |
+| ------------- |-------------| -----|
+| Red      | +3.3V | +3.3V  |
+| Black      | GND      |   GND |
+| Yellow | GPIO4      |    SDA |
+| Blue | GPIO5      |    SCL |
 
-Cable-connection to sensor:  
-Red: +3.3V  
-Black: GND  
-Yellow: SDA -> GPIO4  
-Blue: SCL -> GPIO5
 
-Useful links:  
+#### Useful links:  
 <http://blog.abarbanell.de/arduino-esp8266/iot/i2c/>  
 <https://github.com/esp8266/Arduino#documentation>  
 <https://github.com/adafruit/Adafruit_TSL2561>  

+ 0 - 43
lux_sensor.h

@@ -1,43 +0,0 @@
-typedef enum
-{
-  TSL2591_GAIN_LOW                  = 0x00,    // low gain (1x)
-  TSL2591_GAIN_MED                  = 0x10,    // medium gain (25x)
-  TSL2591_GAIN_HIGH                 = 0x20,    // medium gain (428x)
-  TSL2591_GAIN_MAX                  = 0x30,    // max gain (9876x)
-}
-tsl2591Gain_t;
-
-typedef enum
-{
-  TSL2591_INTEGRATIONTIME_100MS     = 0x00,
-  TSL2591_INTEGRATIONTIME_200MS     = 0x01,
-  TSL2591_INTEGRATIONTIME_300MS     = 0x02,
-  TSL2591_INTEGRATIONTIME_400MS     = 0x03,
-  TSL2591_INTEGRATIONTIME_500MS     = 0x04,
-  TSL2591_INTEGRATIONTIME_600MS     = 0x05,
-}
-tsl2591IntegrationTime_t;
-
-#define TSL2591_COMMAND_BIT       (0xA0)    // bits 7 and 5 for 'command normal'
-
-#define TSL2591_ENABLE_POWERON    (0x01)
-#define TSL2591_ENABLE_POWEROFF   (0x00)
-#define TSL2591_ENABLE_AEN        (0x02)
-#define TSL2591_ENABLE_AIEN       (0x10)
-
-enum
-{
-  TSL2591_REGISTER_ENABLE           = 0x00,
-  TSL2591_REGISTER_CONTROL          = 0x01,
-  TSL2591_REGISTER_THRESHHOLDL_LOW  = 0x02,
-  TSL2591_REGISTER_THRESHHOLDL_HIGH = 0x03,
-  TSL2591_REGISTER_THRESHHOLDH_LOW  = 0x04,
-  TSL2591_REGISTER_THRESHHOLDH_HIGH = 0x05,
-  TSL2591_REGISTER_INTERRUPT        = 0x06,
-  TSL2591_REGISTER_CRC              = 0x08,
-  TSL2591_REGISTER_ID               = 0x0A,
-  TSL2591_REGISTER_CHAN0_LOW        = 0x14,
-  TSL2591_REGISTER_CHAN0_HIGH       = 0x15,
-  TSL2591_REGISTER_CHAN1_LOW        = 0x16,
-  TSL2591_REGISTER_CHAN1_HIGH       = 0x17
-};

+ 2 - 0
makefile

@@ -23,6 +23,8 @@ ADDITIONAL_FILES ?= Adafruit_TSL2561_U.cpp
 LIBS ?= \
 		$(ESP_LIBS)/ESP8266WiFi \
         $(ESP_LIBS)/Wire \
+        $(ESP_LIBS)/ESP8266HTTPClient
+
 
 # Esp8266 Arduino git location
 ESP_ROOT ?= $(HOME)/esp8266

+ 70 - 57
wifi_lux_sensor.cpp

@@ -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();
 }