ping hc and alarm logging
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tobias Brunner 2020-05-09 14:21:18 +02:00
parent 70158edd55
commit 744b08450a
2 changed files with 17 additions and 1 deletions

View File

@ -1,3 +1,6 @@
# Python scripts for IoT stuff
README missing
TODO:
* pushover on alarm

View File

@ -3,6 +3,7 @@ import json
import os
import logging
import signal
import requests
from datetime import datetime
from influxdb import InfluxDBClient
from dotenv import find_dotenv, load_dotenv
@ -18,6 +19,7 @@ DST_INFLUX_HOST = os.getenv("DST_INFLUX_HOST")
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")
OT_TOPIC="owntracks/tobru/dragino"
OT_TID="dragino"
@ -35,7 +37,10 @@ def on_publish_ot(client, userdata, rc):
def on_log(client, userdata, level, buf):
logging_level = mqtt.LOGGING_LEVEL[level]
logging.log(logging_level, buf)
logging.info("got a log message level %s: %s", level, str(buf))
#logging.info("got a log message level %s: %s", level, str(buf))
if "PINGRESP" in str(buf):
# report to https://healthchecks.io to tell that the connection is alive
requests.get(HC_PING_URL)
# The callback for when a PUBLISH message is received from the server.
def on_message_ttn(client, userdata, msg):
@ -46,6 +51,9 @@ def on_message_ttn(client, userdata, msg):
# max is 4 volts, 3 volts is considered empty
batpercent = round((data["payload_fields"]["batV"] - 3) * 100)
if data["payload_fields"]["alarm"]:
print("ALARM button pressed")
if "latitude" in data["payload_fields"]:
# transform received data into OwnTracks format
ot_data = json.dumps({
@ -64,6 +72,9 @@ def on_message_ttn(client, userdata, msg):
client_ot.publish(OT_TOPIC,ot_data)
else:
logging.info("no GPS data / latitude present")
# set GPS data to 0 for InfluxDB
data["payload_fields"]["latitude"] = 0.0
data["payload_fields"]["longitude"] = 0.0
# write to influxdb
logging.info("writing data to influxdb")
@ -111,6 +122,8 @@ if __name__ == '__main__':
datefmt='%Y-%m-%d %H:%M:%S %Z'
)
logging.info("Starting ioteer lgt92. V1.0")
# Prepare InfluxDB
influxdb = InfluxDBClient(
host=DST_INFLUX_HOST,