Quellcode durchsuchen

Starting to add some lux-read functions.

Thomas Chef vor 8 Jahren
Ursprung
Commit
3ee4041c69
3 geänderte Dateien mit 109 neuen und 27 gelöschten Zeilen
  1. 11 1
      README.md
  2. 43 0
      lux_sensor.h
  3. 55 26
      wifi_lux_sensor.cpp

+ 11 - 1
README.md

@@ -1,10 +1,20 @@
+#WiFi-connected Lux-sensor
+
 This project is for builing a WiFi-connected Lux-sensor for the garage.
 
-The sensor used is Adafruit TSL2561.
+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:
+```c++
 #define WIFI_SSID "WiFi-ssid"
 #define WIFI_PASSWORD "password"
+```
+
+Cable-connection to sensor:
+Red: +3.3V
+Black: GND
+Yellow: SDA -> GPIO4
+Blue: SCL -> GPIO5
 
 Useful links:
 http://blog.abarbanell.de/arduino-esp8266/iot/i2c/

+ 43 - 0
lux_sensor.h

@@ -0,0 +1,43 @@
+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
+};

+ 55 - 26
wifi_lux_sensor.cpp

@@ -2,6 +2,14 @@
 #include <Wire.h> 
 
 #include "config.h"
+#include "lux_sensor.h"
+
+// ----------- DEFINES ---------------
+#define LUX_ID 0x39     // I2C-Id of the lux-sensor
+
+// ----------- GLOBALS ---------------
+tsl2591Gain_t _gain;
+tsl2591IntegrationTime_t _integration;
 
 WiFiClient espClient;
 
@@ -25,50 +33,71 @@ void connectWifi() {
   Serial.printf("\n");
 }
 
+
+
+void write8 (uint8_t reg, uint8_t value)
+{
+  Wire.beginTransmission(LUX_ID);
+  Wire.write(reg);
+  Wire.write(value);
+  Wire.endTransmission();
+}
+
+void enable(void)
+{
+  // Enable the device by setting the control bit to 0x01
+  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
+}
+
+void disable(void)
+{
+  // Disable the device by setting the control bit to 0x00
+  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
+}
+
+void setConfiguration(tsl2591Gain_t gain, tsl2591IntegrationTime_t integration) 
+{
+  enable();
+  _gain = gain;
+  _integration = integration;
+  write8( TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
+  disable();
+}
+
 void setup() {
   Serial.begin(38400); //Opens USB-Serial connection for terminal
   delay(1000);
   Serial.println("Init of WiFi-Lux-sensor project");
   connectWifi();
   Wire.begin(4, 5); // sda on pin D2, scl on pin D1
+
+  // Config lux-sensor
+  _integration = TSL2591_INTEGRATIONTIME_100MS;
+  _gain        = TSL2591_GAIN_MED;
+
+  // Set default integration time and gain
+  setConfiguration(_gain, _integration);
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
   byte error, address;
-  int nDevices;
   
   Serial.print("WiFi heartbeat - ms since boot: ");
   Serial.print(millis());
   Serial.println();
-  Serial.println("Scanning i2c devices... ");
-
 
-  nDevices = 0; 
-  for(address = 1; address < 127; address++) {
-    Wire.beginTransmission(address);
-    error = Wire.endTransmission();
+  Wire.beginTransmission(LUX_ID);
+  error = Wire.endTransmission();
 
-    if (error == 0) {
-      Serial.print("I2C device found at address 0x");
-      if (address<16)
-        Serial.print("0");
-      Serial.print(address,HEX);
-      Serial.println("  !");
- 
-      nDevices++;
-    }
-    else if (error==4) {
-      Serial.print("--Unknown error at address 0x");
-      if (address<16)
-        Serial.print("0");
-      Serial.println(address,HEX);
-    }    
+  if (error == 0) {
+    Serial.println("I2C-Lux device found");
   }
-  if (nDevices == 0)
-    Serial.println("--No I2C devices found\n");
-  else
-    Serial.println("done\n");
+  else if (error==4) {
+    Serial.println("--Unknown error.");
+  }    
+
+  Serial.println("done\n");
  
   delay(1000);