esphome / esphome-core

🚨 No longer used 🚨 - The C++ framework behind ESPHome
https://esphome.io/
GNU General Public License v3.0
544 stars 114 forks source link

Light sensor support #38

Closed jladefoged closed 5 years ago

jladefoged commented 6 years ago

Did I miss something or are no light sensors supported yet? This would be a nice feature when using automatic lights.

OttoWinter commented 6 years ago

I mean they're kind of already supported via the Analog To Digital Converted Sensor. You can just hook up a photodiode to an analog pin and measure the voltage on that pin. I've been using that method myself for quite some time now.

But digital light sensors are currently unsupported, though they would probably be good to have too. The problem is that there are just so many different types of light sensors out there and I don't have much knowledge about which ones are usually used in projects. I would like to support them, is the light sensor you would like something like the TSL2561 or something else?

jladefoged commented 6 years ago

I don't really have any suggestions as I don't really have any experience with any. But I found this article and maybe some of them are easier to integrate than others? https://www.intorobotics.com/common-budgeted-arduino-light-sensors/

OttoWinter commented 6 years ago

Thanks for finding that link! Most of the sensors are already kind of supported through the ADC sensor and for the ESP32 the pulse counter sensor, but I will take a look at the i2c sensors. They probably won't be too difficult to implement 👍

jladefoged commented 6 years ago

When using a photoresistor or TEMT6000 (i'm currently testing with the last one) on ADC do you convert the voltage to lux when using it in HA?

OttoWinter commented 6 years ago

In general, you'll have to consult the datasheet of the sensor. Every sensor has their own characteristic collector current to illuminance curve. Then you can use the Lambda Sensor Filter to convert the incoming values to another unit, see Converting Celsius to Fahrenheit for an example of this. Above links are for esphomeyaml, if you're just using esphomelib I can also show you have it would be done in there.

Typically you'll hook up a photoresistor as a voltage divider using a known value resistor. On the TEMT6000 Sparkfun Board this resistor has a value of 10kΩ. The datasheet of the TEMT6000 has a figure that shows you the relationship between current and illuminance in lux. With the 10kΩ series resistor and VCC hooked up to 3.3V, the current across the TEMT6000 should be about adcvalue * 3.3 / 10000.0. And based on the datasheet there's a proportional relationship between current and illuminance: every 2 microamps in current correspond to 1 lux. Therefore the formula should be (adcvalue * 3.3 / 10000.0) * 2000000.0.

So if using esphomeyaml, the configuration would be something like this:

sensor:
  - platform: adc
    pin: A0
    name: Illuminance
    unit_of_measurement: lux
    filters:
      - lambda: (x * 3.3 / 10000.0) * 2000000.0

I'm not 100% sure how esphomelib/esphomeyaml should handle these 2nd order sensors that use the ADC. It would be awesome if we could have predefined components like the TEMT6000 that use the existing components (here ADC) and setup all filters for the user. However that would make the project a lot bigger and less maintainable :/ So I guess one option would be to do all of this 2nd order component stuff within esphomeyaml. If you can verify that above filter value conversion works, that would be awesome 😀

jladefoged commented 6 years ago

My suggestion would be to recommend users to use a sensor that works out-of-the-box and reports an actual lux value to make it easy for them. If one or two i2c sensors is supported by esphomelib and we know they work that would be great. Maybe ADC should be for "advanced use/users only"? I'm not very experienced with any of the sensors i'm actually using and most code is copy-paste from somewhere :-)

I've tried the formula for TEMT6000 and compared the results with an Aeon Labs MultiSensor 6. Same room and same location (inside living room, cloudy day):

MS6: 15lux EHL: 5lux

MS6: 11lux EHL: 4lux

jladefoged commented 6 years ago

I found a random link in a comment on a webshop with code for BH1750: https://github.com/Genotronex/BH1750FVI_Master

OttoWinter commented 6 years ago

Thanks! It will require a bit of cleaning up but would be relatively easy to add support for. I'll have to take a look at this in a few days as I currently don't have too much time on my hand.

jladefoged commented 6 years ago

I've ordered some BH1750 and TSL2561 (both i2c) on Aliexpress a week ago but it takes forever to delivered. I've currently only have one TEMT6000 analog sensor to play with :grinning:

jladefoged commented 6 years ago

I'm still only using the one TEMT6000 sensor I have but it works good enough so i've removed the Aeon Labs MultiSensor 6 from my setup inside the house. Regarding the values calculated with TEMT6000: I don't think they a very accurate with the ADC and seems to cap at 726 lux so a digital sensor would be nice. I've set my indoor light to turn on below 20lux (combined with a PIR sensor) and that works nicely.

jladefoged commented 6 years ago

Here is an example where it caps at 726 lux. For use with automatic light this is not an issue but nice to know.

1

OttoWinter commented 6 years ago

Ah sorry, didn't see this message, I've just put the BH1750 and TSL2561 on my task list as they seems easy to support without any core changes.

About the scoping out. So using above formula this turns out to be exactly 1.1V, the maximum the internal ADC can handle using the default settings. On the ESP you have the option to set an ADC attenuation of up to 11db (corresponds to about 3.5V). See.the link on the documentation page for what maximum voltage these attenuations map to.

jladefoged commented 6 years ago

That would be nice. I've recieved 5 TSL2561 sensors the other day. The board with the TEMT6000 is a ESP32 so I'll try set the attenuation to 11db and see what happens.

Totally unrelated question: Where is the best place for general discussion on esphomelib/esphomeyaml? The Home Assistant forum?

OttoWinter commented 6 years ago

So to your last question: I don't really know myself. The problem is that for me keeping track of all the Github issues (even though there are only ~10) is quite a pain, even with e-mail notifications.

The positive side of using the hass community forums is that it's a lot more real time and I can keep track of all threads much easier. But for tracking issues github is definitely better IMO.

I've now also done a big i2c update (here) that will be released with 1.5.0. It includes (experimental) support for the TSL2561, BH1750, BME280, BME680 and SHT3XD 🎉

jladefoged commented 6 years ago

No luck with the TSL2561. I haven't recieved the BH1750 sensors yet. The TSL2561 sensor shows up on a i2c scan with address 0x39, but I keep getting the same value 2.8lx (actually 2.814720). I have only tested with one TSL2561 sensor for now.

From the log: [20:41:21][I][i2c:051]: Scanning i2c bus for active devices... [20:41:21][I][i2c:057]: Found i2c device at address 0x39 ... [20:41:22][D][sensor.tsl2561:129]: Got illuminance=2.8lx [20:41:22][D][sensor.mqtt:039]: 'ESP test TSL2561': Pushing out value 2.814720 with accuracy 1 [20:41:35][D][sensor.tsl2561:129]: Got illuminance=2.8lx (same values repeat)

OttoWinter commented 6 years ago

Bummer... In 1.5.0 I've added a new log level that should help with resolving these issues a lot: VERY_VERBOSE. For i2c it for example dumps the entire transmitted and received values for debugging.

Could you try setting the level: in the logger component to VERY_VERBOSE? That would be great, thanks!

jladefoged commented 6 years ago

I have no idea what I'm looking at :grinning: Broken sensor is also a possibility. I can test with another, but not today.

[21:07:36][VV][component:107]: Running interval 'update':0 with interval=15000 last_execution=194969 (now=209970)
[21:07:36][VV][i2c:076]: Beginning Transmission to 0x39:
[21:07:36][VV][i2c:109]:     Writing 0b00000000 (0x00)
[21:07:36][VV][i2c:109]:     Writing 0b00000011 (0x03)
[21:07:36][VV][i2c:081]:     Transmission ended. Status code: 0x00
[21:07:36][V][component:054]: set_timeout(name='illuminance', timeout=422)
[21:07:37][VV][component:107]: Running timeout 'illuminance':1 with interval=422 last_execution=209997 (now=210420)
[21:07:37][VV][i2c:076]: Beginning Transmission to 0x39:
[21:07:37][VV][i2c:109]:     Writing 0b00001100 (0x0C)
[21:07:37][VV][i2c:081]:     Transmission ended. Status code: 0x00
[21:07:37][VV][i2c:103]: Requesting 2 bytes from 0x39:
[21:07:37][VV][i2c:133]:     Received 0b00001100 (0x0C)
[21:07:37][VV][i2c:133]:     Received 0b00000010 (0x02)
[21:07:37][VV][i2c:076]: Beginning Transmission to 0x39:
[21:07:37][VV][i2c:109]:     Writing 0b00001110 (0x0E)
[21:07:37][VV][i2c:081]:     Transmission ended. Status code: 0x00
[21:07:37][VV][i2c:103]: Requesting 2 bytes from 0x39:
[21:07:37][VV][i2c:133]:     Received 0b00001110 (0x0E)
[21:07:37][VV][i2c:133]:     Received 0b00000010 (0x02)
[21:07:37][VV][i2c:076]: Beginning Transmission to 0x39:
[21:07:37][VV][i2c:109]:     Writing 0b00000000 (0x00)
[21:07:37][VV][i2c:109]:     Writing 0b00000000 (0x00)
[21:07:37][VV][i2c:081]:     Transmission ended. Status code: 0x00
[21:07:37][D][sensor.tsl2561:129]: Got illuminance=2.8lx
[21:07:37][V][sensor.sensor:022]: 'ESP test TSL2561': Received new value 2.814720
[21:07:37][V][sensor.sensor:028]: 'ESP test TSL2561':  Filter #0 aborted chain
OttoWinter commented 6 years ago

Thanks for the logs! I've now identified at least one issue using them. Basically, for the implementation I was mostly following the datasheet which had an implementation detail only very hidden.

I'll try to fix the source code for the TSL2561 today and get it into 1.5.1 :)

jladefoged commented 6 years ago

I recieved the BH1750 sensors and it seem to work. Getting some realistic values. :grin:

jladefoged commented 6 years ago

It's definitely better than using the TEMT6000 with the ADC in low light conditions. The last 2 hours the TEMT6000 value was 0 in my living room and the BH1750 about 30 down to 7 (low light but with 2 lamps turned on).

OttoWinter commented 5 years ago

Ok, all your suggested sensors are now supported and should work. Please open a new issue if one of them doesn't work.

jladefoged commented 5 years ago

I haven't tested TSL2561 recently, but BH1750 works like a charm.

2016for commented 5 years ago

@ladefoged81 Hey I have been trying to get my TEMT6000 working but it seems like the Voltage readings from the sensor are pretty low (0.15V in a decently lit room) (which corresponds to 3lux or so)

What calculation did you use?

Did you also face the problem of exceptionally low values?

I just want to use it to see if it's somewhat dark to turn on light.

Thanks.

jladefoged commented 5 years ago

@2016for I used the code provided by Otto however I have moved to a bh1750 i2c sensor instead.