diff --git a/code/b-parasite/README.md b/code/b-parasite/README.md index edf8847..d870802 100644 --- a/code/b-parasite/README.md +++ b/code/b-parasite/README.md @@ -39,7 +39,7 @@ Sensor data is encoded in unsigned 16 bits (2 bytes), and whenever multiple | 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) | | 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) | | 10-15 | b-parasite's own MAC address, big-endian format | -| 16-17 | Ambient light, scaled from from 0 (dark) to 0xffff (bright) | +| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) | # Supported Modules diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index 68211f1..a3b08a4 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -69,7 +69,7 @@ static void rtc_callback() { prst_ble_update_adv_data(batt_read.millivolts, temp_humi.temp_millicelcius, temp_humi.humidity, soil_read.relative, - photo_read.lux, run_counter); + photo_read.brightness, run_counter); prst_adv_start(); nrf_delay_ms(PRST_BLE_ADV_TIME_IN_MS); prst_adv_stop(); diff --git a/code/b-parasite/src/prst/adc.c b/code/b-parasite/src/prst/adc.c index dc056c9..0467488 100644 --- a/code/b-parasite/src/prst/adc.c +++ b/code/b-parasite/src/prst/adc.c @@ -146,13 +146,21 @@ prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) { 1e4 * (battery_voltage - ret.voltage) / ret.voltage; // TODO: Now that we have the resistor value of the photo resistor, we need to - // estimate the value in lux. This needs to be calibrated with a real board in - // complete dark and in a super bright environment. - // This current value is just a placeholder. - ret.lux = (uint16_t)UINT16_MAX * (photo_resistance / 1e4); + // estimate the brightness level. This needs to be calibrated with a real + // board in complete dark and in a super bright environment. This current + // value is just a placeholder. + // Dark resistance: 1 MOhm. + const double kDarkResistance = 1e6; + // Light resistance: 10 kOhm. + const double kLightResistance = 1e4; + // A value in 0x0 (dark) - 0xffff (light). + // A little better, but still not great. + ret.brightness = (uint16_t)UINT16_MAX * (kDarkResistance - photo_resistance) / + (kDarkResistance - kLightResistance); #if PRST_ADC_PHOTO_DEBUG - NRF_LOG_INFO("[adc] Read lux level: %d (raw); %d (lux)", ret.raw, ret.lux); + NRF_LOG_INFO("[adc] Read brightness level: %d (raw); %d (brightness)", + ret.raw, ret.brightness); #endif return ret; } \ No newline at end of file diff --git a/code/b-parasite/src/prst/adc.h b/code/b-parasite/src/prst/adc.h index 41130a5..2a733db 100644 --- a/code/b-parasite/src/prst/adc.h +++ b/code/b-parasite/src/prst/adc.h @@ -21,7 +21,7 @@ typedef struct prst_adc_photo_sensor { // A value from 0x0000 (no light) 0xffff (direct sun). // Might need calibration. double voltage; - uint16_t lux; + uint16_t brightness; } prst_adc_photo_sensor_t; void prst_adc_init(); diff --git a/code/b-parasite/src/prst/ble.c b/code/b-parasite/src/prst/ble.c index 8dd8190..2a83cbd 100644 --- a/code/b-parasite/src/prst/ble.c +++ b/code/b-parasite/src/prst/ble.c @@ -33,7 +33,7 @@ | 6-7 | Relative air humidity, scaled from 0 (0%) to 0xffff (100%) | | 8-9 | Soil moisture, scaled from from 0 (0%) to 0xffff (100%) | | 10-15 | b-parasite's own MAC address | -| 16-17 | Lux level from the photoresistor | +| 16-17 | Ambient brightness level from 0 (dark) to 0xffff (bright) | */ #define SERVICE_DATA_LEN 18 static uint8_t service_data[SERVICE_DATA_LEN]; @@ -136,7 +136,7 @@ void prst_ble_init() { void prst_ble_update_adv_data(uint16_t batt_millivolts, uint16_t temp_millicelcius, uint16_t humidity, - uint16_t soil_moisture, uint16_t lux, + uint16_t soil_moisture, uint16_t brightness, uint8_t run_counter) { // 4 bits for a small wrap-around counter for deduplicating messages on the // receiver. @@ -154,8 +154,8 @@ void prst_ble_update_adv_data(uint16_t batt_millivolts, service_data[8] = soil_moisture >> 8; service_data[9] = soil_moisture & 0xff; - service_data[16] = lux >> 8; - service_data[17] = lux & 0xff; + service_data[16] = brightness >> 8; + service_data[17] = brightness & 0xff; // Encodes adv_data_ into .gap_adv_data_. uint32_t err_code = ble_advdata_encode( diff --git a/code/b-parasite/src/prst/ble.h b/code/b-parasite/src/prst/ble.h index b3b7f62..50a2710 100644 --- a/code/b-parasite/src/prst/ble.h +++ b/code/b-parasite/src/prst/ble.h @@ -13,7 +13,7 @@ void prst_adv_stop(); void prst_ble_update_adv_data(uint16_t batt_millivolts, uint16_t temp_millicelcius, uint16_t humidity, - uint16_t soil_moisture, uint16_t lux, + uint16_t soil_moisture, uint16_t brightness, uint8_t run_counter); #endif // _PRST_BLE_H_ \ No newline at end of file