Browse Source

always send to influxdb

master
Tobias Brunner 10 months ago
parent
commit
70158edd55
1 changed files with 27 additions and 30 deletions
  1. +27
    -30
      lgt92.py

+ 27
- 30
lgt92.py View File

@ -43,11 +43,10 @@ def on_message_ttn(client, userdata, msg):
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"])
if "latitude" in data["payload_fields"]:
# max is 4 volts, 3 volts is considered empty
batpercent = round((data["payload_fields"]["batV"] - 3) * 100)
# max is 4 volts, 3 volts is considered empty
batpercent = round((data["payload_fields"]["batV"] - 3) * 100)
if "latitude" in data["payload_fields"]:
# transform received data into OwnTracks format
ot_data = json.dumps({
"_type": "location",
@ -61,34 +60,32 @@ def on_message_ttn(client, userdata, msg):
})
# publish to owntracks
logging.info("publishing data to owntracks")
logging.info("publishing data to owntracks topic on mqtt")
client_ot.publish(OT_TOPIC,ot_data)
# write to influxdb
logging.info("writing data to influxdb")
influxdb.write_points(
[
{
"measurement": "dragino",
"tags": {
"device": "lgt92",
},
"fields": {
"bat": data["payload_fields"]["batV"],
"pitch": data["payload_fields"]["pitch"],
"roll": data["payload_fields"]["roll"],
"lat": data["payload_fields"]["latitude"],
"lon": data["payload_fields"]["longitude"],
"alarm": int(data["payload_fields"]["alarm"]),
"counter": data["counter"],
"airtime": data["metadata"]["airtime"],
"rssi": data["metadata"]["gateways"][0]["rssi"],
}
}
]
)
else:
logging.info("no GPS data present - skipping")
logging.info("no GPS data / latitude present")
# write to influxdb
logging.info("writing data to influxdb")
influxdb.write_points(
[{
"measurement": "dragino",
"tags": {
"device": "lgt92",
},
"fields": {
"bat": data["payload_fields"]["batV"],
"pitch": data["payload_fields"]["pitch"],
"roll": data["payload_fields"]["roll"],
"lat": data["payload_fields"]["latitude"],
"lon": data["payload_fields"]["longitude"],
"alarm": int(data["payload_fields"]["alarm"]),
"counter": data["counter"],
"airtime": data["metadata"]["airtime"],
"rssi": data["metadata"]["gateways"][0]["rssi"],
}
}]
)
logging.info("data processing done")


Loading…
Cancel
Save