Commit 20168767 authored by wetter's avatar wetter
Browse files

fipy-nbiot-rtd v0.0.3-rc7: gps might now work, reading more...

fipy-nbiot-rtd v0.0.3-rc7: gps might now work, reading more lte-signal-quality, litte more timers, tuning mqtt-keepalive (as of http://www.steves-internet-guide.com/mqtt-keep-alive-by-example/) to 6 seconds, lte does now do dis/connect instead of pppsuspend/resume
parent 1dc0fc55
No related merge requests found
Showing with 144 additions and 57 deletions
+144 -57
##!/usr/bin/env python
# fipy-nbiot-rtd v0.0.3-rc6, https://git.cicer.de/autonome-zelle/fipy-nbiot-rtd
# fipy-nbiot-rtd v0.0.3-rc7, https://git.cicer.de/autonome-zelle/fipy-nbiot-rtd
# Connecting a Pt100-thermometer via an MAX31865 Adafruit-board at an PyCom FiPy via Narrow-Band-IoT-LTE to a MQTT-Server.
#
# by The Hiveeyes Project, https://hiveeyes.org/
......@@ -67,7 +67,7 @@ def get_sqnmoni():
return sqnmoni
#def settimeout(duration): pass
def settimeout(duration): pass
# program start
......@@ -150,14 +150,12 @@ except:
print("[GPS ] Initialization ...")
uart = UART(1, 9600) # init with given baudrate
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
GPS_TIMEOUT_SECS=30
GPS_TIMEOUT_SECS=10
# write to address of GPS
raw = bytearray(1)
# create MicropyGPS instance
gps = MicropyGPS()
# start a timer
chrono = machine.Timer.Chrono()
chrono.start()
# store results here.
last_data = {}
print("[GPS ] ... done.")
......@@ -173,6 +171,9 @@ def check_for_valid_coordinates(gps):
last_data['longitude'] = gps.longitude_string()
def get_fix():
# start a timer
chrono = machine.Timer.Chrono()
chrono.start()
while True:
# read some data from module via UART
raw=uart.read()
......@@ -189,8 +190,13 @@ def get_fix():
# gps successfully parsed a message from module
# see if we have valid coordinates
res = check_for_valid_coordinates(gps)
#print("[GPS ] Got: " + line + " " + sentence + ". Now valid coords? " + str(res))
if res is not None:
break
#print(sentence)
elapsed = chrono.read()
if elapsed > GPS_TIMEOUT_SECS:
print("[GPS ] reached timeout: " + str(GPS_TIMEOUT_SECS))
break
gps_payload="\"gps_lat\": " + str(gps.latitude_decimal()) + ", \"gps_lon\": " + str(gps.longitude_decimal()) + ", \"gps_height\": " + str(gps.geoid_height) + ", \"gps_speed_kmh\": " + str(gps.speed_string('kph')[0]) + "." + str(gps.speed_string('kph')[2]) +", \"gps_time\": \"" + str(gps.timestamp) + "\", \"gps_date\": \"" + str(gps.date) + "\", " + "\"gps_clean_msgs\": " + str(gps.clean_sentences) +", \"gps_parsed_msgs\": " + str(gps.parsed_sentences) + ", \"gps_crcfails\":" + str(gps.crc_fails)
#print(gps_payload)
......@@ -201,11 +207,18 @@ def get_fix():
return gps_payload
print("[GPS ] Start reading GPS data ...")
boot_gps_payload=''
boot_gps_payload=get_fix()
print("[GPS ] Got fix: " + str(boot_gps_payload))
# GPS
gps_payload=''
gps_payload=get_fix()
print("[GPS ] Got fix: " + str(gps_payload))
print("[GPS ] test: " + str(gps.latitude_decimal()))
time.sleep(1)
# LTE: get infos from Modem
#
#lte.send_at_cmd('AT!="RRC:setDbgPerm full"')
......@@ -213,6 +226,7 @@ print("[GPS ] test: " + str(gps.latitude_decimal()))
#print(lte.send_at_cmd('AT!="showver"'))
#sqnsupgrade.info()
time.sleep(1)
# LTE: init
......@@ -293,10 +307,17 @@ print('[NTP ] syncing ... (LED low orange)')
pycom.rgbled(0x272000)
try:
rtc.ntp_sync("192.53.103.108", update_period=43200) # ptbtime1.ptb.de, 12h
# while not rtc.synced(): # can possible go on forever ?!
# time.sleep(0.25)
# print('[NTP ] ... syncing ...')
time.sleep(4)
NTP_TIMEOUT_SECS = 30
chrono = machine.Timer.Chrono()
chrono.start()
while not rtc.synced(): # can possible go on forever ?!
time.sleep(1)
print('[NTP ] ... syncing ...')
elapsed = chrono.read()
if elapsed > NTP_TIMEOUT_SECS:
print("[GPS ] reached timeout: " + str(NTP_TIMEOUT_SECS))
break
#time.sleep(10)
print("[NTP ] ... synced, RTC believes via utime.time that secs since 1970 = " + str(utime.time()) )
except:
print("[NTP ] ... exception. Let us reset modem ...")
......@@ -308,23 +329,24 @@ except:
finally:
if utime.time() < 1564377822: # 29. July 2019 05:23:42
print ("[NTP ] ... I DO NOT BELIEVE IN THIS! Current time can not be older than me! Let us reset modem ...")
errlog = "{\"text\": \"I DO NOT BELIEVE IN THIS! Current time can not be older than me! Let us reset\",\"tags\":\"error\",\"title\":\"init/ntp/time-check\",\"time\": " + str(utime.time()) + " }"
lte.deinit(reset=True)
print("[LTE ] ... let us sleep three seconds ...")
time.sleep(3)
print ("[NTP ] ... and machine ...")
machine.reset()
# GPS
gps_payload=''
gps_payload=get_fix()
print("[GPS ] Got fix: " + str(gps_payload))
print("[GPS ] test: " + str(gps.latitude_decimal()))
# LTE: SQNMONI
sqnmoni_out = ''
sqnmoni_out = get_sqnmoni()
print("[LTE ] sqnmoni: " + sqnmoni_out)
# Define telemetry payload (boot)
bootmsg = "{ \"title\": \"[system] booted\", \"text\": \"ntp-sync finished, it is now " + str(rtc.now()) + "\", \"tags\": \"system,boot\" , \"time\": " + str(utime.time()) + " } "
##payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", " + sqnmoni_out + str(gps_payload) + ", \"time\": " + str(Tseconds) + " } "
bootmsg = "{ \"title\": \"[system] booted\", \"text\": \"ntp-sync finished, it is now " + str(rtc.now()) +str(gps_payload)+ "\", \"tags\": \"system,boot\" , \"time\": " + str(utime.time()) + " } "
print("[system ] payload ... (LED x)" + bootmsg)
# MQTT: publish bootmsg
......@@ -332,14 +354,14 @@ print("[system ] payload ... (LED x)" + bootmsg)
print("[MQTT ] setting up client ... (LED red)")
pycom.rgbled(0x330000)
try:
client = MQTTClient("ef3423be2", "46.4.251.67", port=1883, keepalive=30) # weather.hiveeyes.org
# client.settimeout = settimeout(10) # i do not think that this has any affect. or does it? hmm.
client = MQTTClient("ef3423be2", "46.4.251.67", port=1883, keepalive=6) # weather.hiveeyes.org
#client.settimeout = settimeout(60) # i do not think that this has any affect. or does it? hmm.
print("[MQTT ] connecting ... (LED red)")
client.connect()
print("[MQTT ] ... connected, sending: " + bootmsg)
client.publish("umwelt/airrohr/research/pt100-wtf-02/event.json", bootmsg)
print("[MQTT ] ... done. not waiting a second.")
# time.sleep(1)
print("[MQTT ] ... done. waiting a second.")
time.sleep(1)
except Exception as e:
print(e)
errlog = "{\"text\": \"" + str(e) + "\",\"tags\":\"error\",\"title\":\"init/mqtt/connect\",\"time\": " + str(utime.time()) + " }"
......@@ -349,10 +371,9 @@ except Exception as e:
print("[LTE ] ... let us sleep three seconds ...")
time.sleep(3)
print ("[MQTT ] ... and machine ...")
machine.reset()
# checking memory for errmsg
# checking rtc memory
print("[RTC ] read memory ...")
try:
......@@ -375,17 +396,51 @@ except:
print("[system] ... empty.")
# MQTT: disconnect client
print("[MQTT ] disconnecting client ... (LED red)")
pycom.rgbled(0x330000)
try:
client.disconnect()
except Exception as e:
print(e)
errlog = "{\"text\": \"" + str(e) + "\",\"tags\":\"error\",\"title\":\"loop/mqtt/disconnect\",\"time\": " + str(utime.time()) + " }"
pycom.nvs_set('errlog', errlog)
print("[MQTT ] !!! DISCONNECT FAILED !!! (exception). Let us reset modem ...")
lte.deinit(reset=True)
print ("[MQTT ] ... and machine ...")
machine.reset()
# LTE: pppsuspend
print('[LTE ] suspending ppp ... (LED yellow)')
#print('[LTE ] suspending ppp ... (LED yellow)')
#pycom.rgbled(0x171700)
#try:
# lte.pppsuspend()
# print('[LTE ] ... done.')
#except:
# print("[LTE ] ... failed. will reset machine ...")
# machine.reset()
# LTE: disconnect
print('[LTE ] disconnecting ... (LED light-yellow)')
pycom.rgbled(0x171700)
try:
lte.pppsuspend()
lte.disconnect()
while lte.isconnected():
time.sleep(0.25)
print('[LTE ] ... disconnecting ...')
print('[LTE ] ... done.')
except:
time.sleep(0.5)
except Exception as e:
print(e)
print("[LTE ] ... failed. will reset machine ...")
machine.reset()
# system: main loop
print("[system] ==== starting main loop ... ====")
......@@ -409,6 +464,14 @@ while True:
print("[Pt100 ] ... failed. will reset machine ...")
machine.reset()
# GPS
gps_payload=''
gps_payload=get_fix()
print("[GPS ] Got fix: " + str(gps_payload))
print("[GPS ] test: " + str(gps.latitude_decimal()))
time.sleep(1)
# RTC: get time
......@@ -421,8 +484,6 @@ while True:
print("[RTC ] ... no time. setting empty.")
# LTE: SQNMONI
sqnmoni_out = ''
......@@ -438,36 +499,45 @@ while True:
print("[LTE ] QSC: " + csq)
# GPS
gps_payload=''
gps_payload=get_fix()
print("[GPS ] Got fix: " + str(gps_payload))
print("[GPS ] test: " + str(gps.latitude_decimal()))
# time.sleep(2)
# Define telemetry payload
#payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", " + sqnmoni_out + str(gps_payload) + ", \"time\": " + str(Tseconds) + " } "
payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", " + str(gps_payload) + ", \"time\": " + str(Tseconds) + " } "
##payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", " + sqnmoni_out + " \"time\": " + str(Tseconds) + " } " #moll
#payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", \"time\": " + str(Tseconds) + " } " #moll
payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", " + sqnmoni_out + str(gps_payload) + ", \"time\": " + str(Tseconds) + " } "
#payload = "{ \"Tpoly\": " + str(Tpoly) + ", \"lte_csq\": " + str(csq) + ", " + str(gps_payload) + ", \"time\": " + str(Tseconds) + " } "
# LTE: pppresume
# LTE: connect
print('[LTE ] resuming ppp ... (LED yellow)')
print('[LTE ] connecting ... (LED light-yellow)')
pycom.rgbled(0x171700)
try:
lte.pppresume()
time.sleep(0.5)
lte.connect()
while not lte.isconnected():
time.sleep(0.25)
print('[LTE ] ... connecting ...')
print('[LTE ] ... done.')
time.sleep(0.5)
except Exception as e:
print(e)
pycom.nvs_set('errlog', str(e))
print("[LTE ] ... failed. Let us reset modem ...")
lte.deinit(reset=True)
print ("[MQTT ] ... and machine ...")
print("[LTE ] ... failed. will reset machine ...")
machine.reset()
# LTE: pppresume
# print('[LTE ] resuming ppp ... (LED yellow)')
# pycom.rgbled(0x171700)
# try:
# lte.pppresume()
# print('[LTE ] ... done.')
# except Exception as e:
# print(e)
# pycom.nvs_set('errlog', str(e))
# print("[LTE ] ... failed. Let us reset modem ...")
# lte.deinit(reset=True)
# print ("[MQTT ] ... and machine ...")
# machine.reset()
# time.sleep(1)
# print('[uping ] 2x pinging 46.4.251.67, timeout=10s ... (LED yellow)')
# pycom.rgbled(0x171700)
# try:
......@@ -482,11 +552,12 @@ while True:
print("[MQTT ] connecting client ... (LED red)")
pycom.rgbled(0x330000)
try:
mqtt_timeout = utime.time() + 30 # 30s timout
client = MQTTClient("ef3423be2", "46.4.251.67", port=1883, keepalive=30) # weather.hiveeyes.org .67
client = MQTTClient("ef3423be2", "46.4.251.67", port=1883, keepalive=6) # weather.hiveeyes.org .67
#client.settimeout = settimeout(60) # i do not think that this has any affect. or does it? hmm.
print("[MQTT ] ... parameters set, connecting ...")
client.connect()
print("[MQTT ] ... connected.")
time.sleep(1)
except Exception as e:
print(e)
errlog = "{\"text\": \"" + str(e) + "\",\"tags\":\"error\",\"title\":\"loop/mqtt/connect\",\"time\": " + str(utime.time()) + " }"
......@@ -506,7 +577,7 @@ while True:
print("[MQTT ] ... sending: " + payload)
client.publish("umwelt/airrohr/research/pt100-wtf-02/data.json", payload)
print("[MQTT ] ... sent.")
# time.sleep(3)
time.sleep(3)
except Exception as e:
print(e)
errlog = "{\"text\": \"" + str(e) + "\",\"tags\":\"error\",\"title\":\"loop/mqtt/publish\",\"time\": " + str(utime.time()) + " }"
......@@ -535,15 +606,31 @@ while True:
# LTE: pppsuspend
print('[LTE ] suspending ppp ... (LED yellow)')
# print('[LTE ] suspending ppp ... (LED yellow)')
# pycom.rgbled(0x171700)
# try:
# lte.pppsuspend()
# print('[LTE ] ... done.')
# except:
# print("[LTE ] ... failed. Let us reset modem ...")
# lte.deinit(reset=True)
# print ("[MQTT ] ... and machine ...")
# machine.reset()
# LTE: disconnect
print('[LTE ] disconnecting ... (LED light-yellow)')
pycom.rgbled(0x171700)
try:
lte.pppsuspend()
lte.disconnect()
while lte.isconnected():
time.sleep(0.25)
print('[LTE ] ... disconnecting ...')
print('[LTE ] ... done.')
except:
print("[LTE ] ... failed. Let us reset modem ...")
lte.deinit(reset=True)
print ("[MQTT ] ... and machine ...")
time.sleep(0.5)
except Exception as e:
print(e)
print("[LTE ] ... failed. will reset machine ...")
machine.reset()
# system: idle
......
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