update to work with ttnv3 and fw 1.6
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
c14f631420
commit
df89854803
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"python.formatting.provider": "black"
|
||||
"python.formatting.provider": "black",
|
||||
"python.pythonPath": "/home/tobru/.cache/pypoetry/virtualenvs/ioteer-xuj1sZwc-py3.9/bin/python"
|
||||
}
|
98
lgt92.py
98
lgt92.py
|
@ -20,7 +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 = "v2.0"
|
||||
VERSION = "v3.0"
|
||||
|
||||
OT_TOPIC = "owntracks/tobru/dragino"
|
||||
OT_TID = "dragino"
|
||||
|
@ -52,7 +52,9 @@ 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", data["end_device_ids"]["device_id"]
|
||||
"message from ttn received for %s via %s",
|
||||
data["end_device_ids"]["device_id"],
|
||||
data["uplink_message"]["network_ids"]["cluster_id"],
|
||||
)
|
||||
|
||||
# retrieve info about gateway
|
||||
|
@ -70,41 +72,49 @@ def on_message_ttn(client, userdata, msg):
|
|||
except:
|
||||
logging.info("received via gw %s", gtw_id)
|
||||
|
||||
# max is 4 volts, 3 volts is considered empty
|
||||
batpercent = round((data["uplink_message"]["decoded_payload"]["BatV"] - 3) * 100)
|
||||
# the decoded data
|
||||
bat_v = data["uplink_message"]["decoded_payload"]["BatV"]
|
||||
motion_detection = data["uplink_message"]["decoded_payload"]["MD"]
|
||||
led_status = data["uplink_message"]["decoded_payload"]["LON"]
|
||||
fw_version = data["uplink_message"]["decoded_payload"]["FW"]
|
||||
pitch = data["uplink_message"]["decoded_payload"]["Pitch"]
|
||||
roll = data["uplink_message"]["decoded_payload"]["Roll"]
|
||||
latitude = data["uplink_message"]["decoded_payload"]["Latitude"]
|
||||
longitude = data["uplink_message"]["decoded_payload"]["Longitude"]
|
||||
rssi = data["uplink_message"]["rx_metadata"][0]["rssi"]
|
||||
airtime = data["uplink_message"]["consumed_airtime"][:-1]
|
||||
|
||||
if data["uplink_message"]["decoded_payload"]["ALARM_status"]:
|
||||
# max is 4 volts, 3 volts is considered empty
|
||||
batpercent = round((bat_v - 3) * 100)
|
||||
|
||||
alarm_status = False
|
||||
if data["uplink_message"]["decoded_payload"]["ALARM_status"] == "TRUE":
|
||||
alarm_status = True
|
||||
logging.info("Red button pushed!")
|
||||
|
||||
logging.info(
|
||||
"Motion detection: %s", data["uplink_message"]["decoded_payload"]["MD"]
|
||||
)
|
||||
logging.info(
|
||||
"LED status for position: %s", data["uplink_message"]["decoded_payload"]["LON"]
|
||||
)
|
||||
logging.info(
|
||||
"Firmware version: %s", data["uplink_message"]["decoded_payload"]["FW"]
|
||||
)
|
||||
logging.info("Motion detection: %s", motion_detection)
|
||||
logging.info("LED status for position: %s", led_status)
|
||||
logging.info("Firmware version: %s", fw_version)
|
||||
|
||||
got_fix = False
|
||||
if data["uplink_message"]["decoded_payload"]["Latitude"] == 0:
|
||||
if latitude == 0:
|
||||
logging.info("no GPS data (Latitude) present")
|
||||
# set GPS data to 0 for InfluxDB
|
||||
data["uplink_message"]["decoded_payload"]["Latitude"] = 0.0
|
||||
data["uplink_message"]["decoded_payload"]["Longitude"] = 0.0
|
||||
latitude = 0.0
|
||||
longitude = 0.0
|
||||
else:
|
||||
logging.info(
|
||||
"GPS data (Latitude) present: lat %s, lon %s",
|
||||
data["uplink_message"]["decoded_payload"]["Latitude"],
|
||||
data["uplink_message"]["decoded_payload"]["Longitude"],
|
||||
latitude,
|
||||
longitude,
|
||||
)
|
||||
got_fix = True
|
||||
# transform received data into OwnTracks format
|
||||
ot_data = json.dumps(
|
||||
{
|
||||
"_type": "location",
|
||||
"lat": data["uplink_message"]["decoded_payload"]["Latitude"],
|
||||
"lon": data["uplink_message"]["decoded_payload"]["Longitude"],
|
||||
"lat": latitude,
|
||||
"lon": longitude,
|
||||
"batt": batpercent,
|
||||
"t": "p",
|
||||
"tid": OT_TID,
|
||||
|
@ -119,32 +129,26 @@ def on_message_ttn(client, userdata, msg):
|
|||
|
||||
# write to influxdb
|
||||
logging.info("writing data to influxdb")
|
||||
influxdb.write_points(
|
||||
[
|
||||
{
|
||||
"measurement": "dragino",
|
||||
"tags": {
|
||||
"device": "lgt92",
|
||||
},
|
||||
"fields": {
|
||||
"bat": float(data["uplink_message"]["decoded_payload"]["BatV"]),
|
||||
"pitch": float(data["uplink_message"]["decoded_payload"]["Pitch"]),
|
||||
"roll": float(data["uplink_message"]["decoded_payload"]["Roll"]),
|
||||
"lat": float(data["uplink_message"]["decoded_payload"]["Latitude"]),
|
||||
"lon": float(
|
||||
data["uplink_message"]["decoded_payload"]["Longitude"]
|
||||
),
|
||||
# "alarm": int(
|
||||
# data["uplink_message"]["decoded_payload"]["ALARM_status"]
|
||||
# ),
|
||||
"counter": data["counter"],
|
||||
"airtime": data["metadata"]["airtime"],
|
||||
"rssi": data["metadata"]["gateways"][0]["rssi"],
|
||||
"fix": got_fix,
|
||||
},
|
||||
}
|
||||
]
|
||||
)
|
||||
influxdb_points = [
|
||||
{
|
||||
"measurement": "dragino",
|
||||
"tags": {
|
||||
"device": "lgt92",
|
||||
},
|
||||
"fields": {
|
||||
"bat": float(bat_v),
|
||||
"pitch": float(pitch),
|
||||
"roll": float(roll),
|
||||
"lat": float(latitude),
|
||||
"lon": float(longitude),
|
||||
"alarm": int(alarm_status),
|
||||
"airtime": float(airtime),
|
||||
"rssi": rssi,
|
||||
"fix": got_fix,
|
||||
},
|
||||
}
|
||||
]
|
||||
influxdb.write_points(influxdb_points)
|
||||
|
||||
logging.info("data processing done")
|
||||
|
||||
|
|
Loading…
Reference in New Issue