Skip to content

Commit

Permalink
Prevent SDS011 and GPS hanging; prevent MQ135 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenCellist committed Jul 4, 2022
1 parent 85fc49e commit 5bb63a6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
6 changes: 5 additions & 1 deletion software/lib/MQ135.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions software/lib/SDS011.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
22 changes: 15 additions & 7 deletions software/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
display.text("GPS gestart!", 1, 1)
display.show()

t1 = time.time()
while True:
while com2.any(): # wait for incoming communication
print('.', end = '')
my_sentence = com2.readline() # read NMEA sentence
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))
Expand Down Expand Up @@ -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()
Expand All @@ -93,7 +100,7 @@
while not lora.has_joined():
print('.', end = '')
time.sleep(0.5)

display.text("Verbonden!", 1, 11)
display.show()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions software/settings.py
Original file line number Diff line number Diff line change
@@ -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
DEBUG_MODE = False

0 comments on commit 5bb63a6

Please sign in to comment.