Etienne Charbonnier
/
plant_monitoring
Plant Monitoring CS
Revision 62:4763f2aa486c, committed 2020-03-26
- Comitter:
- titi9211
- Date:
- Thu Mar 26 10:01:31 2020 +0000
- Parent:
- 61:700f3f204adc
- Commit message:
- TSL2561
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TSL2561.cpp Thu Mar 26 10:01:31 2020 +0000 @@ -0,0 +1,182 @@ +/* + * mbed library program + * Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output) + * TSL2561 by Texas Advanced Optoelectronic Solutions Inc. + * + * Copyright (c) 2015,'17 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: Feburary 21st, 2015 + * Revised: August 23rd, 2017 + */ + +#include "TSL2561.h" + +TSL2561::TSL2561 (PinName p_sda, PinName p_scl) + : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p) +{ + TSL2561_addr = TSL2561_ADDRESS_GND; + init(); +} + +TSL2561::TSL2561 (PinName p_sda, PinName p_scl, uint8_t addr) + : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p) +{ + TSL2561_addr = addr; + init(); +} + +TSL2561::TSL2561 (I2C& p_i2c) + : _i2c(p_i2c) +{ + TSL2561_addr = TSL2561_ADDRESS_GND; + init(); +} + +TSL2561::TSL2561 (I2C& p_i2c, uint8_t addr) + : _i2c(p_i2c) +{ + TSL2561_addr = addr; + init(); +} + +/////////////// Read Lux from sensor ////////////////////// +/* +For 0 < CH1/CH0 < 0.50 Lux = 0.0304 x CH0-0.062 x CH0 x ((CH1/CH0)1.4) +For 0.50 < CH1/CH0 < 0.61 Lux = 0.0224 x CH0-0.031 x CH1 +For 0.61 < CH1/CH0 < 0.80 Lux = 0.0128 x CH0-0.0153 x CH1 +For 0.80 < CH1/CH0 < 1.30 Lux = 0.00146 x CH0-0.00112x CH1 +For CH1/CH0 > 1.30 Lux = 0 + */ +float TSL2561::lux() +{ + double lux0, lux1; + double ratio; + double dlux; + + dt[0] = CMD_MULTI + TSL2561_DATA0LOW; + _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); + _i2c.read(TSL2561_addr, (char *)dt, 2, false); + ch0 = dt[1] << 8 | dt[0]; + dt[0] = CMD_MULTI + TSL2561_DATA1LOW; + _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); + _i2c.read(TSL2561_addr, (char *)dt, 2, false); + ch1 = dt[1] << 8 | dt[0]; + if (ch0 == 0xFFFF) { + return 2500.0; + } + lux0 = (double)ch0; + lux1 = (double)ch1; + ratio = lux1 / lux0; + read_timing_reg(); + lux0 *= (402.0/integ_time); + lux1 *= (402.0/integ_time); + lux0 /= gain; + lux1 /= gain; + if (ratio <= 0.5) { + dlux = 0.03040 * lux0 - 0.06200 * lux0 * pow(ratio,1.4); + } else if (ratio <= 0.61) { + dlux = 0.02240 * lux0 - 0.03100 * lux1; + } else if (ratio <= 0.80) { + dlux = 0.01280 * lux0 - 0.01530 * lux1; + } else if (ratio <= 1.30) { + dlux = 0.00146 * lux0 - 0.00112 * lux1; + } else { + dlux = 0; + } + return (float)dlux; +} + +/////////////// Initialize //////////////////////////////// +void TSL2561::init() +{ + _i2c.frequency(100000); + power_up(); + set_timing_reg(TIMING_DEFAULT); +} + +/////////////// Timing Register /////////////////////////// +uint8_t TSL2561::set_timing_reg(uint8_t parameter) +{ + dt[0] = CMD_SINGLE + TSL2561_TIMING; + dt[1] = parameter; + _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); + dt[0] = CMD_SINGLE + TSL2561_TIMING; + _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); + _i2c.read(TSL2561_addr, (char *)dt, 1, false); + return dt[0]; +} + +uint8_t TSL2561::read_timing_reg(void) +{ + uint8_t i; + + dt[0] = CMD_SINGLE + TSL2561_TIMING; + _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); + _i2c.read(TSL2561_addr, (char *)dt, 1, false); + if (dt[0] & TIMING_GAIN_16){ + gain = 16; + } else { + gain = 1; + } + i = dt[0] & 0x3; + switch (i) { + case 0: + integ_time = 13.7; + break; + case 1: + integ_time = 101.0; + break; + case 2: + integ_time = 402.0; + break; + default: + integ_time = 0; + break; + } + return dt[0]; +} + +/////////////// ID //////////////////////////////////////// +uint16_t TSL2561::read_ID() +{ + dt[0] = CMD_SINGLE + TSL2561_ID; + _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); + _i2c.read(TSL2561_addr, (char *)dt, 2, false); + id_number = dt[0] << 8 | dt[1]; + return id_number; +} + +uint8_t TSL2561::who_am_i() +{ + read_ID(); + if ((id_number >> 4) == I_AM_TSL2561) { + return 1; + } else { + return 0; + } +} + +/////////////// Power ON/OFF ////////////////////////////// +void TSL2561::power_up() +{ + dt[0] = CMD_SINGLE + TSL2561_CONTROL; + dt[1] = 3; + _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); +} + +void TSL2561::power_down() +{ + dt[0] = CMD_SINGLE + TSL2561_CONTROL; + dt[1] = 0; + _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); +} + +/////////////// I2C Freq. ///////////////////////////////// +void TSL2561::frequency(int hz) +{ + _i2c.frequency(hz); +} + + + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TSL2561.h Thu Mar 26 10:01:31 2020 +0000 @@ -0,0 +1,162 @@ +/* + * mbed library program + * Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output) + * TSL2561 by Texas Advanced Optoelectronic Solutions Inc. + * + * Copyright (c) 2015,'17 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: Feburary 21st, 2015 + * Revised: August 23rd, 2017 + */ +/* + *---------------- REFERENCE ---------------------------------------------------------------------- + * https://docs.google.com/viewer?url=http%3A%2F%2Fwww.adafruit.com%2Fdatasheets%2FTSL256x.pdf + * https://learn.adafruit.com/tsl2561?view=all + * http://www.adafruit.com/products/439 + * http://akizukidenshi.com/catalog/g/gM-08219/ + */ + +#ifndef TSL2561_H +#define TSL2561_H + +#include "mbed.h" + +// Luminosity sensor, TSL2561 +// Address b7=0,b6=1,b5=1,b4=1,b3=0,b2=0,b1=1, b0=R/W +#define TSL2561_ADDRESS_GND (0x29 << 1) +#define TSL2561_ADDRESS_FLOAT (0x39 << 1) +#define TSL2561_ADDRESS_VDD (0x49 << 1) + +////////////// Registers ////////////////////////////////// +// Register definition +#define TSL2561_CONTROL 0x00 +#define TSL2561_TIMING 0x01 +#define TSL2561_THRESHLOWLOW 0x02 +#define TSL2561_THRESHHIGHLOW 0x04 +#define TSL2561_INTERRUPT 0x06 +#define TSL2561_CRC 0x08 +#define TSL2561_ID 0x0A +#define TSL2561_DATA0LOW 0x0C +#define TSL2561_DATA0HIGH 0x0D +#define TSL2561_DATA1LOW 0x0E +#define TSL2561_DATA1HIGH 0x0F + +////////////// TIMING PARAMETER /////////////////////////// +#define TIMING_GAIN_1 (0UL << 4) +#define TIMING_GAIN_16 (1UL << 4) +#define TIMING_TIME_13R7 (0x0) +#define TIMING_TIME_101 (0x1) +#define TIMING_TIME_402 (0x2) +#define TIMING_TIME_MANU (0x3) +#define TIMING_DEFAULT (TIMING_GAIN_1 + TIMING_TIME_402) + +////////////// ID ///////////////////////////////////////// +#define I_AM_TSL2561 0x50 +#define REG_NO_MASK 0x0F + +////////////// COMMAND //////////////////////////////////// +#define CMD_CMDMODE (1UL << 7) +#define CMD_CLEAR (1UL << 6) +#define CMD_WORD (1UL << 5) +#define CMD_BLOCK (1UL << 4) +#define CMD_SINGLE (CMD_CMDMODE) +#define CMD_MULTI (CMD_CMDMODE + CMD_WORD) + +/** Interface for Luminosity sensor, TSL2561 + * @code + * #include "mbed.h" + * #include "TSL2561.h" + * + * // I2C Communication + * TSL2561 lum(dp5,dp27); // TSL2561 SDA, SCL + * // If you connected I2C line not only this device but also other devices, + * // you need to declare following method. + * I2C i2c(dp5,dp27); // SDA, SCL + * TSL2561 lum(i2c); // TSL2561 SDA, SCL (Data available every 400mSec) + * + * int main() {; + * while(true){ + * printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux()); + * wait(1.0); + * } + * } + * @endcode + */ + +class TSL2561 +{ +public: + /** Configure data pin + * @param data SDA and SCL pins + */ + TSL2561(PinName p_sda, PinName p_scl); + TSL2561(PinName p_sda, PinName p_scl, uint8_t addr); + + /** Configure data pin (with other devices on I2C line) + * @param I2C previous definition + */ + TSL2561(I2C& p_i2c); + TSL2561(I2C& p_i2c, uint8_t addr); + + /** Get approximates the human eye response + * in the commonly used Illuminance unit of Lux + * @param none + * @return Lux + */ + float lux(void); + + /** Set timing register + * @param timing parameter + * @return timing read data + */ + uint8_t set_timing_reg(uint8_t parameter); + + /** Read timing register + * @param timing parameter + * @return timing read data + */ + uint8_t read_timing_reg(void); + + /** Set I2C clock frequency + * @param freq. + * @return none + */ + void frequency(int hz); + + /** check Device ID number + * @param none + * @return TSL2561 = 1, others 0 + */ + uint8_t who_am_i(void); + + /** Read ID and Revision Number + * @param none + * @return ID + REVNO + */ + uint16_t read_ID(void); + + /** Power Up/Down + * @param none + * @return none + */ + void power_up(void); + void power_down(void); + +protected: + I2C *_i2c_p; + I2C &_i2c; + + void init(void); + +private: + uint8_t TSL2561_addr; + uint8_t dt[4]; + uint32_t ch0; + uint32_t ch1; + int8_t gain; + uint8_t id_number; + double integ_time; +}; + +#endif // TSL2561_H \ No newline at end of file
--- a/main.cpp Wed Mar 25 16:06:17 2020 +0000 +++ b/main.cpp Thu Mar 26 10:01:31 2020 +0000 @@ -18,7 +18,7 @@ #include "TCPSocket.h" #include "HTS221Sensor.h" #include "DHT22.h" -#include "TSL2591.h" +#include "TSL2561.h" #define WIFI_IDW0XX1 2 #define UBIDOTS_TOKEN "BBFF-xp89MM8kmzzbp6eJ074XjDpqz7qryh" @@ -96,11 +96,11 @@ AnalogIn adc_temp(ADC_TEMP); // Internal Temp Sensor to ADC Channel AnalogIn adc_vbat(ADC_VBAT); // VBAT / 3 internal to ADC channel - static DevI2C devI2c(PB_11,PB_10); - // static HTS221Sensor sen_hum_temp(&devI2c); + // static DevI2C devI2c(PB_11,PB_10); + // static DevI2C devI2c(D14,D15); + //static HTS221Sensor sen_hum_temp(&devI2c); static DHT22 tempSensor(D2); // Vérifier que le capteur DHT22 est relié au PIN D2 - static I2C i2c1(I2C_SDA, I2C_SCL); - static TSL2591 sensor1(i2c1, TSL2591_ADDR); // Vérifier que le capteur TSL2591 est bien relié aux ports SDA et SCL + static TSL2561 LUM(PB_11, PB_10); // Vérifier que le capteur TSL2591 est bien relié aux ports SDA et SCL static AnalogIn analog_value(A0); // Vérifier que le capteur d'humidité du sol est bien relié au port analogique A0 //sen_hum_temp.init(NULL); @@ -136,9 +136,7 @@ h = (float) tempSensor.getHumidity(); hum = h/10; printf("Luminosity acquisition\n"); - sensor1.getALS(); - sensor1.calcLux(); - lux = (float) sensor1.full; + lux = (float) LUM.lux(); printf("Soil moisture acquisition\n"); moist_r = analog_value.read(); printf("Soil moisture conversion\n");
--- a/mbed_app.json Wed Mar 25 16:06:17 2020 +0000 +++ b/mbed_app.json Thu Mar 26 10:01:31 2020 +0000 @@ -6,11 +6,11 @@ }, "wifi-ssid": { "help": "WiFi SSID", - "value": "\"SSID\"" + "value": "\"brand0388\"" }, "wifi-password": { "help": "WiFi Password", - "value": "\"Password\"" + "value": "\"tloa7568\"" }, "wifi-tx": { "help": "TX pin for serial connection to external device",