kike-canaries / canairio_sensorlib

Particle sensor manager for multiple sensors: Honeywell, Plantower, Panasonic, Sensirion, etc. This is sensors layer of CanAirIO project too.
https://canair.io/docs/sensorlib.html
GNU General Public License v3.0
37 stars 12 forks source link

Possible issue with temperature offset logic (and maybe with altitude) #203

Closed melkati closed 4 months ago

melkati commented 6 months ago

I am very confused with the temperature offsets. I think we have a problem with the logic, in some sensors...

I'm not sure what's going on yet, but something's not right... probably it's a mix of issues...

In my preliminary research I found (unconfirmed, more research is needed) some things to check or fix:

Finally, I would like to point out that I want to implement support for low power sensors, starting with SCD41 (which is already "half supported" and is getting a lot of traction) and will have to be taken into account to make it easier later on.

I would like to invite @hpsaturn, @roberbike and @Coscolin to the discussion.

melkati commented 6 months ago

I moved the sensor registration up in Sensors::CO2scd30Init() and Sensors::CO2scd4xInit() to fix (2).

I don't see any side effects. Tested with both sensors in CO2 Gadget and looks like it's working fine.

https://github.com/melkati/canairio_sensorlib/commit/42329340867bc088a6653f97509f18c757cef30d https://github.com/melkati/canairio_sensorlib/commit/6da72010a3cd7a37673c4d7907ed23f48213b38c

In Sensors::CO2scd30Init() I also changed In Sensors::CO2scd30Init() to multiply toffset * 100 to convert into hundredths of a degree C to comply with Sensirion's API:

https://github.com/melkati/canairio_sensorlib/commit/6da72010a3cd7a37673c4d7907ed23f48213b38c#r138802382

Maybe we should do the same for Sensors::sen5xInit() but I don't have one to test.

@hpsaturn, I sent the PR #204 directed to master (as there isn't another branch to work on) but I think you can change the branch easily before merging. This way I can keep on working without having at the end only one PR with changes to different things.

melkati commented 6 months ago

I just sent a PR to fix last commit and (5). https://github.com/kike-canaries/canairio_sensorlib/pull/204

I didn't add sen5x to Sensors::getTempOffset() and I don't know how it works and don't have one to test.

hpsaturn commented 6 months ago

Maybe @roberbike knows about this sensor.

BRKMK commented 6 months ago

Yes, I have one sen5x from an ikea device. I will test it in a few days.

melkati commented 6 months ago

I'm centered today on SCD30 time to stabilize the temperature.

For (1), no matter what I do, Sensors are still taking a while to stabilize the temperature.

I reviewed the code and looks like the checks are already in place and are working fine (there was a bug comparing the offset saved at the SCD30 with the one in Sensorlib but I already fixed it in https://github.com/kike-canaries/canairio_sensorlib/pull/204/commits/6da72010a3cd7a37673c4d7907ed23f48213b38c).

I would appreciate a second, or even third, pair of eyes reviewing it.

I carefully removed all references to scd30.setTemperatureOffset() in CO2 Gadget, Sensorlib and even Adafruit_SCD30 (commented out code in all three) and sensor is still taking a while to stabilize the temperature. Maybe my SCD30 is malfunctioning? Maybe that is the way this sensor works? I would thank you if you could test it with your SCD30 sensor (if you have one) @hpsaturn @BRKMK @Coscolin...

You can use my fork to test: https://github.com/melkati/canairio_sensorlib/tree/fixOffset

hpsaturn commented 6 months ago

Thanks @melkati On my side, right now I don't have SCD30 here to perform tests. Only I have a new SCD40 builtin sensor, included in the AirQ unit of M5. I'm going to check if is possible replicate the issue there. On the other hand, I could help in the review code of any advance. Thanks again.

melkati commented 6 months ago

I did the same tests with an SCD40 sensor and the same thing happened.

One tip: the programmer using the library must set the float variable sensors.toffset, prior to sensors.init(), to the same temperature offset the program using the library has stored as "actual offset temperature" for everything to be "on sync".

Maybe we should create a method to set this variable before calling sensors.init() so programmers do not have to access the variable directly.

I can't find any problem with manually setting the variable, before calling sensors.init(). @hpsaturn, what do you think?

hpsaturn commented 6 months ago

Yes, I agree. It is normal in some libraries. If we need that, please do it.

melkati commented 6 months ago

Yes, I agree. It is normal in some libraries. If we need that, please do it.

Are initTOffset(float) and float = getTOffset() good names?

It will not check if offset is positive or negative, as some other libraries may need a negative value, although I think we must keep to positives offset to standardize, following the Sensirion path.

hpsaturn commented 5 months ago

Yes, these names are good. I guess that we should admit the two possibilities, positives and negatives.

melkati commented 5 months ago

Just sent PR with these two methods. I didn't make tOffset variable private to maintain backward compatibility. Maybe we should do it.

melkati commented 5 months ago

On my side, this PR is ready for merging.

CO2 Gadget is using this branch (in my fork) and nobody reported issues with sensors, so I think it's working fine.

Point 7. is still pending to improve in the future. I tried to open it on their own issue, so it's not forgotten, but I receive an error:

image

hpsaturn commented 4 months ago

Thanks @melkati for your contributions and time. Then I'm going to close this issue. Thanks!