Browse Source

improve all the things

master
Tobias Brunner 9 months ago
parent
commit
ac19187352
1 changed files with 21 additions and 5 deletions
  1. +21
    -5
      main.py

+ 21
- 5
main.py View File

@ -29,24 +29,34 @@ def on_connect_ot(client, userdata, flags, rc):
logging.info("connected to ot %s - %s", DST_MQTT_HOST, str(rc))
def on_publish_ot(client, userdata, rc):
logging.info("published data to ot - %s", str(rc))
logging.info("published data to ot")
def on_log(client, userdata, level, buf):
logging_level = mqtt.LOGGING_LEVEL[level]
logging.log(logging_level, buf)
logging.info("got a log message for %s level %s: %s", str(userdata), level, str(buf))
# The callback for when a PUBLISH message is received from the server.
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["gateways"][0]["gtw_id"])
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)
if data["payload_fields"]["latitude"]:
# transform received data into OwnTracks format
ot_data = json.dumps({
"_type": "location",
"lat": data["payload_fields"]["latitude"],
"lon": data["payload_fields"]["longitude"],
"batt": data["payload_fields"]["batV"],
"batt": batpercent,
"t": "p",
"tid": OT_TID,
"tst": int(datetime.timestamp(datetime.now())),
"conn": "m",
})
# publish to owntracks
@ -69,12 +79,14 @@ def on_message_ttn(client, userdata, msg):
"lat": data["payload_fields"]["latitude"],
"lon": data["payload_fields"]["longitude"],
"alarm": int(data["payload_fields"]["alarm"]),
"counter": data["counter"],
"airtime": data["metadata"]["airtime"],
}
}
]
)
else:
logging.info("no GPS data present")
logging.info("no GPS data present - skipping")
logging.info("data processing done")
@ -97,15 +109,19 @@ influxdb = InfluxDBClient(
# Prepare MQTT
client_ttn = mqtt.Client()
client_ttn.enable_logger()
client_ttn.on_connect = on_connect_ttn
client_ttn.on_message = on_message_ttn
client_ttn.on_log = on_log
client_ttn.username_pw_set(SRC_MQTT_USER,SRC_MQTT_PASS)
client_ttn.tls_set()
client_ttn.connect(SRC_MQTT_HOST, 8883, 60)
client_ot = mqtt.Client()
client_ot.enable_logger()
client_ot.on_connect = on_connect_ot
client_ot.on_publish = on_publish_ot
client_ot.on_log = on_log
client_ot.username_pw_set(DST_MQTT_USER,DST_MQTT_PASS)
client_ot.tls_set()
client_ot.connect(DST_MQTT_HOST, 8883, 60)


Loading…
Cancel
Save