New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDS011 dust sensor implementation update #139
Conversation
tested, ready to merge. |
Thanks @danbates2 this is great! Unfortunately Im having trouble with the blocking nature of the read function, when you try and restart or stop emonhub it just hangs there until its finished the latest set of readings. I've had a go at trying to call del from a signal or trigger from the main emonhub thread but no luck so far. I've also made some other minor modifications here |
I tired adding adding the following to emonhub.py in the close section to then call a close method in the interfacer
and then in the SDS011 interfacer:
But no luck, need to somehow get the message that emonhub needs to close/restart into the SDS011 library thread... |
Perhaps using this library might be easier? |
Hmm not sure that Im getting that library to work ... might be that the sds011 has the settings from the previous library and there's some conflict between active and passive mode not sure.. |
Right, I see what you mean with the emonhub restarts. That other library could be the solution in that case.. (that other library gives us easier control of sleep times and readings too..) I guess another way would be to put the process into a thread. I also don't know the procedure for emonhub stopping interfacers. Is there a function it calls to stop interfacers? |
Yes and I tried tried to get that to work above, see 3rd comment back, but no luck in affecting the underlying thread unfortunately.. |
Sure. Who knows emonhub better? What's the procedure for closing interfacers? |
That wasnt the issue, the close command was getting through to the interfacer, it was just that it wasnt getting into the sds011 library thread to close it as it where.. |
When stopping emonhub, the interfacers get stopped too.
|
Because one option is to put the serial stuff into a thread, and stop the thread when closing the interfacer. |
The interfacers have a run method with a while loop, this loop exists if the stop flag is set to true: the stop flag is set from emonhub.py when the users attempts to stop/restart emonhub here: the run method calls the read method that you see in the SDS011 interfacer: |
Cool.
? |
I can see the stops in the main emonhub, but I can't see how each interfacer is actually closed. |
Guess that's why a non-blocking read is important for being able to stop/start emonhub or an interfacer. |
I think its just that the loop exits when it receives the flag and the interfacer closes automatically, I tried adding an additional close method above that would then have the ability to close a thread within an interfacer e.g by calling sds.del() Looking at the code behind sds.del() it appears to only delete the socket, but we are not using sockets so I dont think it does anything.. |
I see what you mean... |
In the case of SDS011 or more generally? Yes the original library was blocking during read() |
Yes more generally. I mean emonhub isn't able to exit an interfacer's read(), instead it's dependent on it returning a value. So the blocking library caused a problem. |
switched library important to do the following: $ sudo pip3 uninstall sds011 $ sudo pip3 install py-sds011
@TrystanLea I've changed the library and now everything is broken. It was working on my laptop with the sensor plugged in by usb, but not on the rpi. if you're trying the new one : $ sudo pip3 uninstall sds011 New problem too, if changing setting in emonhub it jamms up here:
|
this is what I'm running on the laptop to test it. might help. |
@TrystanLea Got it. remember to uninstall the old module and install the new. |
line 54: self.sensor.set_work_period(read=False, work_time=0) |
@TrystanLea I've been testing the new methods for 24h and it's looking good. |
Thanks for this Dan, works a treat! |
PS: here's the code to optionally load the module |
Handy. |
This uses a much improved library to manage the SDS011 dust sensor.
It enables the rate at which the sensor is read to be changed to assist in the internal laser lasting longer.
As we know, the laser, running 24/7, will reach it's rated life in less than a year.
Setting readinterval to 5 will give a dust sensor reading ever 5 minutes, elongating the life of the sensor to something more like 5 years.
Below is required to install the python library.
$ sudo pip3 install sds011