123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include <ESP8266WiFi.h>
- #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;
- 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 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;
-
- Serial.print("WiFi heartbeat - ms since boot: ");
- Serial.print(millis());
- Serial.println();
- Wire.beginTransmission(LUX_ID);
- error = Wire.endTransmission();
- if (error == 0) {
- Serial.println("I2C-Lux device found");
- }
- else if (error==4) {
- Serial.println("--Unknown error.");
- }
- Serial.println("done\n");
-
- delay(1000);
-
- }
|