From ab3c84e2599fb8fc70c9bca4f4d2a897fd8af0e5 Mon Sep 17 00:00:00 2001 From: andreask Date: Sat, 9 May 2020 20:23:58 +0200 Subject: [PATCH] brightness measurement via LED --- Firmware/src/main.cpp | 54 +++++++++++++++++++++++++++++++++++++--- Firmware/src/secconfig.h | 22 +++++++++++++--- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Firmware/src/main.cpp b/Firmware/src/main.cpp index 4cd090d..9e0a51a 100644 --- a/Firmware/src/main.cpp +++ b/Firmware/src/main.cpp @@ -55,12 +55,12 @@ #endif // Create Sensor Class -#ifdef HAS_BME280 +#if defined HAS_BME280 || defined HAS_BME280_BRIGHTNESS #include BME280 sensor; #endif -#ifdef HAS_SHT21 +#if defined HAS_SHT21 || defined HAS_SHT21_BRIGHTNESS #include SHT21 sensor; #endif @@ -80,6 +80,33 @@ void blink(uint8_t num) { } #endif + +#if defined HAS_SHT21_BRIGHTNESS || defined HAS_BME280_BRIGHTNESS + +int16_t brightness() +{ + unsigned int counter; + // LED Anode auf OUTPUT/LOW + pinMode(LED_A, OUTPUT); + digitalWrite(LED_A, LOW); + + // LED Kathode auf OUTPUT/HIGH + pinMode(LED_K, OUTPUT); + digitalWrite(LED_K, HIGH); + delayMicroseconds(4); + + // Kathode wieder auf INPUT + pinMode(LED_K,INPUT); + digitalWrite(LED_K, LOW); + // In einer Schleife zählen, bis Kathode auf LOW geht + for ( counter = 0; counter < 65000; counter++) { + if (digitalRead(LED_K)==0) break; + delayMicroseconds(5); + } + return counter; +} +#endif + // Setup Wakeup Interrupt Timer void init_wdt() { @@ -213,6 +240,13 @@ void loop() int32_t temperature; int32_t humidity; } __attribute__ ((packed)) data; + #elif defined HAS_SHT21_BRIGHTNESS + struct lora_data { + uint8_t bat; + int32_t temperature; + int32_t humidity; + int16_t brightness; + } __attribute__ ((packed)) data; #elif defined HAS_BME280 struct lora_data { uint8_t bat; @@ -220,18 +254,30 @@ void loop() int32_t humidity; int32_t pressure; } __attribute__ ((packed)) data; + #elif defined HAS_BME280_BRIGHTNESS + struct lora_data { + uint8_t bat; + int32_t temperature; + int32_t humidity; + int32_t pressure; + int16_t brightness; + } __attribute__ ((packed)) data; #endif // Get Sensor Data - #ifdef HAS_BME280 + #if defined HAS_BME280 || defined HAS_BME280_BRIGHTNESS sensor.getData(&data.temperature, &data.pressure, &data.humidity); #endif - #ifdef HAS_SHT21 + #if defined HAS_SHT21 || defined HAS_SHT21_BRIGHTNESS data.temperature = (int32_t)(sensor.getTemperature()*100); data.humidity = (int32_t)(sensor.getHumidity()*100); #endif + #if defined HAS_SHT21_BRIGHTNESS || defined HAS_BME280_BRIGHTNESS + data.brightness = (int16_t)(brightness()); + #endif + // Add Battery Voltage, 20mv steps, encoded into 1 Byte uint32_t batv = readVcc(); data.bat = (uint8_t)(batv/20); diff --git a/Firmware/src/secconfig.h b/Firmware/src/secconfig.h index 7cd8dde..06c1bcd 100644 --- a/Firmware/src/secconfig.h +++ b/Firmware/src/secconfig.h @@ -9,10 +9,17 @@ #define HAS_NO_SENSOR - No Sensor, just send Battery Voltage (Beacon Mode) #define HAS_SHT21 - Sensirion SHT21 Temperature + Humidity Sensor #define HAS_BME280 - Bosch BME280 Temperature + Humidity + Pressure Sensor - + #define HAS_SHT21_BRIGHTNESS - SHT21 + Brightness via LED + #define HAS_BME280_BRIGHTNESS - BME280 + Brightness via LED + * LED Support #define LED_PIN PIN_A7 - LED is connected to ATTiny84 Pin A7 on TinyTX SMD / TinyLora - + + * Brightness Measurement + LED connected between 2 pins, switched in reverse direction for brightness measurement + #define LED_K PIN_A7 - LED Cathode + #define LED_A PIN_B2 - LED Anode + * Time between Measurements #define SLEEP_TIME 528 - Time in Seconds between Measurements. Try it out to get a good Approximation Examples from my Tests:: @@ -42,4 +49,13 @@ #define RFM69_GATEWAY 1 // NodeID of the Receiver, 1-255 #define RFM69_TXPOWER 31 // Transmitting Power 0-31, see datasheet for details #define RFM69_ENCKEY "0123456789ABCDEF" // AES encryption key, exactly 16 characters, if set messages will be encrypted using AES 128bit -*/ \ No newline at end of file +*/ + +/* +// LoRa RFM95 + BME280 + Brightness Measurement + #define RF_LORA + #define HAS_BME280_BRIGHTNESS + #define LED_K PIN_A7 + #define LED_A PIN_B2 + #define SLEEP_TIME 544 // ca + */