Browse Source

lwt, location retain, better logging

master
Tobias Brunner 8 months ago
parent
commit
9b710ec727
1 changed files with 20 additions and 5 deletions
  1. +20
    -5
      lgt92.py

+ 20
- 5
lgt92.py View File

@ -20,6 +20,7 @@ DST_INFLUX_USER = os.getenv("DST_INFLUX_USER")
DST_INFLUX_PASS = os.getenv("DST_INFLUX_PASS")
DST_INFLUX_DB = os.getenv("DST_INFLUX_DB")
HC_PING_URL = os.getenv("HC_PING_URL")
VERSION = "V1.1"
OT_TOPIC="owntracks/tobru/dragino"
OT_TID="dragino"
@ -46,7 +47,15 @@ def on_log(client, userdata, level, buf):
def on_message_ttn(client, userdata, msg):
data = json.loads(msg.payload)
logging.info("message from ttn received for %s - #%s", data["dev_id"], data["counter"])
logging.info("received via gw %s", data["metadata"]["gateways"][0]["gtw_id"])
# retrieve info about gateway
gtw_id = data["metadata"]["gateways"][0]["gtw_id"]
gtw_info = requests.get("https://www.thethingsnetwork.org/gateway-data/gateway/"+gtw_id).json()
logging.info("received via gw %s, %s, owned by %s",
data["metadata"]["gateways"][0]["gtw_id"],
gtw_info[gtw_id]["description"],
gtw_info[gtw_id]["owner"],
)
# max is 4 volts, 3 volts is considered empty
batpercent = round((data["payload_fields"]["batV"] - 3) * 100)
@ -68,8 +77,8 @@ def on_message_ttn(client, userdata, msg):
})
# publish to owntracks
logging.info("publishing data to owntracks topic on mqtt")
client_ot.publish(OT_TOPIC,ot_data)
logging.info("publishing data to owntracks via mqtt %s", OT_TOPIC)
client_ot.publish(OT_TOPIC, payload=ot_data, retain=True, qos=1)
else:
logging.info("no GPS data / latitude present")
# set GPS data to 0 for InfluxDB
@ -122,7 +131,7 @@ if __name__ == '__main__':
datefmt='%Y-%m-%d %H:%M:%S %Z'
)
logging.info("Starting ioteer lgt92. V1.0")
logging.info("Starting ioteer lgt92. "+VERSION)
# Prepare InfluxDB
influxdb = InfluxDBClient(
@ -135,7 +144,7 @@ if __name__ == '__main__':
verify_ssl=True,
)
# Prepare MQTT
# Prepare MQTT for The Things Network
client_ttn = mqtt.Client()
client_ttn.enable_logger()
client_ttn.on_connect = on_connect_ttn
@ -145,6 +154,11 @@ if __name__ == '__main__':
client_ttn.tls_set()
client_ttn.connect(SRC_MQTT_HOST, 8883, 60)
# Prepare MQTT for OwnTracks
ot_lwt = json.dumps({
"_type": "lwt",
"tst": int(datetime.timestamp(datetime.now())),
})
client_ot = mqtt.Client()
client_ot.enable_logger()
client_ot.on_connect = on_connect_ot
@ -152,6 +166,7 @@ if __name__ == '__main__':
client_ot.on_log = on_log
client_ot.username_pw_set(DST_MQTT_USER,DST_MQTT_PASS)
client_ot.tls_set()
client_ot.will_set(OT_TOPIC, payload=ot_lwt, qos=1, retain=True)
client_ot.connect(DST_MQTT_HOST, 8883, 60)
try:


Loading…
Cancel
Save