diff --git a/software/lib/MQ135.py b/software/lib/MQ135.py index bb3f976..dfa35e5 100644 --- a/software/lib/MQ135.py +++ b/software/lib/MQ135.py @@ -57,7 +57,11 @@ def get_corrected_ppm(self, temperature, humidity): """Returns the ppm of CO2 sensed (assuming only CO2 in the air) corrected for temperature/humidity""" g_cr = self.get_corrected_resistance(temperature, humidity) - return self.PARA * math.pow((g_cr / self.RZERO), -self.PARB) + try: + val = self.PARA * math.pow((g_cr / self.RZERO), -self.PARB) + except: + val = 0 + return val def get_rzero(self): """Returns the resistance RZero of the sensor (in kOhms) for calibration purposes""" diff --git a/software/lib/SDS011.py b/software/lib/SDS011.py index 4e76d0a..7810fbf 100644 --- a/software/lib/SDS011.py +++ b/software/lib/SDS011.py @@ -50,8 +50,8 @@ def make_command(self, cmd, mode, param): return header + cmd + mode + param + padding + checksum + tail def get_response(self, command_ID): - # try for 120 bytes (0.1) second to get a response from sensor (typical response time 12~33 bytes) - for _ in range(120): + # try for 120 bytes (0.2) second to get a response from sensor (typical response time 12~33 bytes) + for _ in range(240): try: header = self._uart.read(1) if header == b'\xaa': diff --git a/software/main.py b/software/main.py index 85ae3d6..b6fbe0d 100644 --- a/software/main.py +++ b/software/main.py @@ -25,6 +25,7 @@ display.text("GPS gestart!", 1, 1) display.show() + t1 = time.time() while True: while com2.any(): # wait for incoming communication print('.', end = '') @@ -32,7 +33,7 @@ for x in my_sentence: gps.update(chr(x)) # decode it through micropyGPS - if gps.latitude > 0 and gps.longitude > 0: # once we found valid data, power off GPS module and show data on display + if (gps.latitude > 0 and gps.longitude > 0) or time.time() - t1 > 60: # once we found valid data, power off GPS module and show data on display gps_en.value(1) # disable GPS power print("GPS: NB %f, OL %f, H %f" % (gps.latitude, gps.longitude, gps.altitude)) @@ -82,9 +83,15 @@ lora = network.LoRa(mode = network.LoRa.LORAWAN, region = network.LoRa.EU868, sf = settings.LORA_SF) # create LoRa object if wake_reason != machine.RTC_WAKE and wake_reason != machine.PIN_WAKE: - app_eui = ubinascii.unhexlify(settings.APP_EUI) - app_key = ubinascii.unhexlify(settings.APP_KEY) - lora.join(activation = network.LoRa.OTAA, auth = (app_eui, app_key), timeout = 0, dr = settings.LORA_DR) # join with SF9 + #app_eui = ubinascii.unhexlify(settings.APP_EUI) + #app_key = ubinascii.unhexlify(settings.APP_KEY) + #lora.join(activation = network.LoRa.OTAA, auth = (app_eui, app_key), timeout = 0, dr = settings.LORA_DR) # join with SF9 + + dev_addr = struct.unpack(">l", ubinascii.unhexlify('260BD834'))[0] + nwk_swkey = ubinascii.unhexlify('ABA8C83456AA7F0537BD1998017F77F0') + app_swkey = ubinascii.unhexlify('0C4A1DD8684BFC394705D06963E1E690') + lora.join(activation = network.LoRa.ABP, auth = (dev_addr, nwk_swkey, app_swkey), dr = settings.LORA_DR) + display.poweron() display.text("Verbinden...", 1, 1) display.show() @@ -93,7 +100,7 @@ while not lora.has_joined(): print('.', end = '') time.sleep(0.5) - + display.text("Verbonden!", 1, 11) display.show() @@ -134,7 +141,7 @@ mq135_en = machine.Pin('P8', mode=machine.Pin.OUT) # MQ135 VIN pin mq135_en.hold(False) # disable hold from deepsleep mq135_en.value(1) # preheat -mq135 = MQ135('P17') # CO2 sensor +mq135 = MQ135('P16') # CO2 sensor # active: 40 mA, sleep: 0.0 mA sds011_en = machine.Pin('P21', mode=machine.Pin.OUT) # voltage regulator SHDN pin @@ -183,7 +190,8 @@ else: machine.sleep((settings.WAKE_TIME - settings.PEAK_TIME) * 1000) -while not sds011.read(): # make sure we get response from SDS011 +t1 = time.time() +while (not sds011.read()) and (time.time() - t1 < 5): # try to get a response from SDS011 within 5 seconds pass pm_25 = sds011.pm25 diff --git a/software/settings.py b/software/settings.py index 87e194b..90d377b 100644 --- a/software/settings.py +++ b/software/settings.py @@ -1,16 +1,16 @@ ## Here we store settings, credentials and constants. # LoRa config info -APP_EUI = '30AEA459134C0000' -APP_KEY = '38C9AD4E8B69241FE50AB179932CAE1F' -LORA_SF = 9 # LoRa Spreading Factor (7 to 12) +APP_EUI = '30AEA47870B40000' +APP_KEY = '93F032DE681792632F0B928B555DAAB8' +LORA_SF = 12 # LoRa Spreading Factor (7 to 12) LORA_DR = 12 - LORA_SF # Data Rate complement of Spreading Factor (counted inversely from 0 to 5) MEASURE_INTERVAL = 60 # measure every ... seconds BOOT_TIME = 3 # time in seconds (approx) it takes to start SDS011 from poweron -WAKE_TIME = 30 # time in seconds that the SDS011 and MQ135 need to stabilize +WAKE_TIME = 32 # time in seconds that the SDS011 and MQ135 need to stabilize PEAK_TIME = 6 # time in seconds to wait after starting voltage regulator DISPLAY_TIME = 6 # time in seconds to display values on the internal display SLEEP_TIME = MEASURE_INTERVAL - BOOT_TIME - WAKE_TIME -DEBUG_MODE = True \ No newline at end of file +DEBUG_MODE = False \ No newline at end of file