Commit 118428d7 authored by Frank Heuer's avatar Frank Heuer

New method "reset" which sets the dutycycle, workstate, reportmode and so...

New method "reset" which sets the dutycycle, workstate, reportmode and so forth to the factory default, so that use of the sensor by other code will work. Other code often is not able to handle a sensor at startup that is in sleep mode.

New property device_path that holds info of the device_path the instance is constructed with. One is able to identify the sensor if more than one sensor is in use.

Added a new raise condition in the __send method when sensor is not responding.
parent 06c0b7fa
......@@ -115,7 +115,8 @@ class SDS011(object):
device_path on Win is one of your COM ports, on Linux
one of "/dev/ttyUSB..." or "/dev/ttyAMA...".
'''
logging.debug("Start of constructor")
logging.debug("Start of constructor with device_path {0}".format(device_path))
self.__device_path = device_path
self.device = None
self.device = serial.Serial(device_path,
baudrate=9600, stopbits=serial.STOPBITS_ONE,
......@@ -162,7 +163,11 @@ class SDS011(object):
# it's better to clean up
if self.device is not None:
self.device.close()
# ReportMode
@property
def device_path(self):
"""The device path of the sensor"""
return self.__device_path
# ReportMode
@property
def reportmode(self):
......@@ -377,6 +382,8 @@ class SDS011(object):
# proof the receive value
received = self.__response(command)
logging.debug("Received: %s", received)
if len(received) == 0:
raise ex.NoSensorResponse("Sensor is not responding")
# when no command or command is request command,
# second byte has to be ReceiveByte
if ((command is None or command == self.Command.Request) and
......@@ -455,6 +462,14 @@ class SDS011(object):
logging.debug("response() successful run")
return bytes_received
def reset(self):
'''
Sets reportmode to Initiative, workstate to Measuring and dutycyle to 0
'''
self.workstate = self.WorkStates.Measuring
self.reportmode = self.ReportModes.Initiative
self.dutycycle = 0
def __checksum_make(self, data):
'''
Generates the checksum for data to send or data recieved from sensor.
......
......@@ -46,3 +46,6 @@ class GetStatusError(Error):
class ReportModeError(Error):
"""Exception raised when sensor is in wrong reportmode for requested operation."""
pass
class NoSensorResponse(Error):
"""Exception raised when sensor is unexpected not responding"""
pass
......@@ -49,8 +49,7 @@ print(sensor.dutycycle)
print(sensor.workstate)
print(sensor.reportmode)
# Set dutycyle to nocycle (permanent)
sensor.dutycycle = 0
sensor.workstate = SDS011.WorkStates.Measuring
sensor.reset()
print("Permanent measureing (and make the senor get old. Just 8000 hours working!)\n \
Do you really need permanent measurering?")
for a in range(10):
......@@ -60,34 +59,43 @@ for a in range(10):
print("Values measured: ", values[0], "--", values[1])
break
# Example of switching the WorkState
for a in range(3):
print("waking up if sleeping with a={0}.".format(a))
try:
# Example of switching the WorkState
for a in range(3):
print("waking up if sleeping with a={0}.".format(a))
sensor.workstate = SDS011.WorkStates.Measuring
# Just to demonstrate. Should be 60 seconds to get right values. Sensor
# has to warm up!
time.sleep(10)
while True:
values = sensor.get_values()
if values is not None:
print("Values measured: ", values[0], "--", values[1])
break
time.sleep(2)
print("Going to sleep a while")
sensor.workstate = SDS011.WorkStates.Sleeping
time.sleep(5)
# Example of dutycycle
sensor.workstate = SDS011.WorkStates.Measuring
# Just to demonstrate. Should be 60 seconds to get right values. Sensor has to warm up!
time.sleep(10)
while True:
values = sensor.get_values()
if values is not None:
print("Values measured: ", values[0], "--", values[1])
break
time.sleep(2)
print("Going to sleep a while")
sensor.workstate = SDS011.WorkStates.Sleeping
time.sleep(5)
# Setting this to 0 means permanent (each second)
sensor.dutycycle = 2 # valid values between 0 and 30
print("You have to wait at most {0} minutes before the first measuring.".format(
sensor.dutycycle))
for a in range(2):
print("Dutycycle with a={0}.".format(a))
while True:
values = sensor.get_values()
if values is not None:
print("Values measured: ", values[0], "--", values[1])
break
sensor.reset()
print("Sensor resetted normal")
except KeyboardInterrupt:
sensor.reset()
print("Sensor resetted because of KeyboardInterrupt")
# Example of dutycycle
sensor.workstate = SDS011.WorkStates.Measuring
# Setting this to 0 means permanent (each second)
sensor.dutycycle = 2 # valid values between 0 and 30
print("You have to wait at most {0} minutes before the first measuring.".format(sensor.dutycycle))
for a in range(2):
print("Dutycycle with a={0}.".format(a))
while True:
values = sensor.get_values()
if values is not None:
print("Values measured: ", values[0], "--", values[1])
break
sensor.workstate = SDS011.WorkStates.Sleeping
print("Finished")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment