py-sds011
py-sds011 copied to clipboard
how can I check if sensor is in sleep mode?
Hello,
how can I check if sensor is in sleep mode?
I am trying
sensor.sleep(sleep=True)
print (sensor.sleep(read=True))
sensor.sleep()
print (sensor.sleep(read=True))
but this won't work as expected. I am getting two times
None
None
to check serial port you can use "isOpen()", serial.isOpen() try "sensor.ser.isOpen()"
sensor.ser.isOpen()
always give me TRUE
I am not sure if you have read my question properly. I did not want to check if serial port is open, but I'am interested to know if SDS011 is in "sleep mode" - is fan off or on?
Hope we can find a solution.
I'm not an expert and I don't use this library but from the sensor documentation and this code,
sensor.sleep(sleep=True)
is correct to read the status, but the function don't return nothing, so you must modify it how dows "def query(self)" function or create a new function
def sleep(self, read=False, sleep=True):
"""Sleep/Wake up the sensor.
@param sleep: Whether the device should sleep or work.
@type sleep: bool
"""
cmd = self.cmd_begin()
cmd += (self.SLEEP_CMD
+ (self.READ if read else self.WRITE)
+ (self.SLEEP if sleep else self.WORK)
+ b"\x00" * 10)
cmd = self._finish_cmd(cmd)
self._execute(cmd)
--------- modify ------
raw = self._get_reply()
if raw is None:
return None
data = struct.unpack('B', raw[4:5]) #1=work, 0=sleep
0 sleep and 1 run
UPDATE 12/11/2019 20:46 I tested it in my code and it seems to work, but it is necessary to manage the situation when the fan is off (sleep = 1), in this case the documentation says:
"4. Set sleep and work .... The setting is not effective after power off 【stay work state after power on】 "
so it's important to set a timeout when you instantiate the " Serial " object
self.ser = serial.Serial (port = serial_port, baudrate = baudrate, timeout = timeout)
because if the sensor is sleeping and you launch the reading of the device, the program block