|
@@ -2,6 +2,14 @@
|
|
#include <Wire.h>
|
|
#include <Wire.h>
|
|
|
|
|
|
#include "config.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;
|
|
WiFiClient espClient;
|
|
|
|
|
|
@@ -25,50 +33,71 @@ void connectWifi() {
|
|
Serial.printf("\n");
|
|
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() {
|
|
void setup() {
|
|
Serial.begin(38400); //Opens USB-Serial connection for terminal
|
|
Serial.begin(38400); //Opens USB-Serial connection for terminal
|
|
delay(1000);
|
|
delay(1000);
|
|
Serial.println("Init of WiFi-Lux-sensor project");
|
|
Serial.println("Init of WiFi-Lux-sensor project");
|
|
connectWifi();
|
|
connectWifi();
|
|
Wire.begin(4, 5); // sda on pin D2, scl on pin D1
|
|
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() {
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
// put your main code here, to run repeatedly:
|
|
byte error, address;
|
|
byte error, address;
|
|
- int nDevices;
|
|
|
|
|
|
|
|
Serial.print("WiFi heartbeat - ms since boot: ");
|
|
Serial.print("WiFi heartbeat - ms since boot: ");
|
|
Serial.print(millis());
|
|
Serial.print(millis());
|
|
Serial.println();
|
|
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);
|
|
delay(1000);
|
|
|
|
|