From 65d2ba4f3af90c93ac0114659ceaf1388468cacf Mon Sep 17 00:00:00 2001 From: orjangje Date: Mon, 14 Jan 2019 11:19:48 +0100 Subject: [PATCH] Fixed bug issue 4. Updated README. --- README.md | 8 ++++++- read_waveplus.py | 59 ++++++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1a4fa92..848c07d 100644 --- a/README.md +++ b/README.md @@ -202,8 +202,14 @@ Let us know how it went! If you want contribute, you can do so by posting issues # Release notes +Release dated 14-Jan-2019 + +* [bug] Fixed issue ([#4][i4]) + Release dated 14-Dec-2018 * Added SAMPLE-PERIOD as an input argument. -Initial release 12-Dec-2018 \ No newline at end of file +Initial release 12-Dec-2018 + +[i4]: https://github.com/Airthings/waveplus-reader/issues/4 \ No newline at end of file diff --git a/read_waveplus.py b/read_waveplus.py index aca19ae..d7071bc 100644 --- a/read_waveplus.py +++ b/read_waveplus.py @@ -106,32 +106,42 @@ class WavePlus(): def __init__(self, SerialNumber): self.periph = None self.curr_val_char = None + self.MacAddr = None + self.SN = SerialNumber self.uuid = UUID("b42e2a68-ade7-11e4-89d3-123b93f75cba") def connect(self): - scanner = Scanner().withDelegate(DefaultDelegate()) - deviceFound = False - searchCount = 0 - while deviceFound is False and searchCount < 50: - devices = scanner.scan(0.1) # 0.1 seconds scan period - searchCount += 1 - for dev in devices: - ManuData = dev.getValueText(255) - SN = parseSerialNumber(ManuData) - if (SN == SerialNumber): - MacAddr = dev.addr - deviceFound = True # exits the while loop on next conditional check - break # exit for loop + # Auto-discover device on first connection + if (self.MacAddr is None): + scanner = Scanner().withDelegate(DefaultDelegate()) + searchCount = 0 + while self.MacAddr is None and searchCount < 50: + devices = scanner.scan(0.1) # 0.1 seconds scan period + searchCount += 1 + for dev in devices: + ManuData = dev.getValueText(255) + SN = parseSerialNumber(ManuData) + if (SN == self.SN): + self.MacAddr = dev.addr # exits the while loop on next conditional check + break # exit for loop + + if (self.MacAddr is None): + print "ERROR: Could not find device." + print "GUIDE: (1) Please verify the serial number." + print " (2) Ensure that the device is advertising." + print " (3) Retry connection." + sys.exit(1) - if (deviceFound is not True): - print "ERROR: Could not find device." - print "GUIDE: (1) Please verify the serial number. (2) Ensure that the device is advertising. (3) Retry connection." - sys.exit(1) - else: - self.periph = Peripheral(MacAddr) + # Connect to device + if (self.periph is None): + self.periph = Peripheral(self.MacAddr) + if (self.curr_val_char is None): self.curr_val_char = self.periph.getCharacteristics(uuid=self.uuid)[0] def read(self): + if (self.curr_val_char is None): + print "ERROR: Devices are not connected." + sys.exit(1) rawdata = self.curr_val_char.read() rawdata = struct.unpack('BBBBHHHHHHHH', rawdata) sensors = Sensors() @@ -141,6 +151,8 @@ def read(self): def disconnect(self): if self.periph is not None: self.periph.disconnect() + self.periph = None + self.curr_val_char = None # =================================== # Class Sensor and sensor definitions @@ -189,9 +201,8 @@ def getUnit(self, sensor_index): return self.sensor_units[sensor_index] try: - #---- Connect to device ----# + #---- Initialize ----# waveplus = WavePlus(SerialNumber) - waveplus.connect() if (Mode=='terminal'): print "\nPress ctrl+C to exit program\n" @@ -207,6 +218,8 @@ def getUnit(self, sensor_index): while True: + waveplus.connect() + # read values sensors = waveplus.read() @@ -226,7 +239,9 @@ def getUnit(self, sensor_index): print tableprint.row(data, width=12) elif (Mode=='pipe'): print data - + + waveplus.disconnect() + time.sleep(SamplePeriod) finally: