Browse Source

initial commit

master
Tobias Brunner 2 years ago
commit
22e86e14e7
  1. 13
      .drone.yml
  2. 1
      .gitignore
  3. 8
      Dockerfile
  4. 122
      main.py
  5. 3
      requirements.txt

13
.drone.yml

@ -0,0 +1,13 @@
kind: pipeline
name: default
steps:
- name: docker
image: plugins/docker
settings:
username:
from_secret: username
password:
from_secret: password
repo: tobru/ioteer
auto_tag: true

1
.gitignore

@ -0,0 +1 @@
.env

8
Dockerfile

@ -0,0 +1,8 @@
FROM python:3.8
WORKDIR /usr/src/ioteer
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./main.py" ]

122
main.py

@ -0,0 +1,122 @@
import paho.mqtt.client as mqtt
import json
import os
import logging
from datetime import datetime
from influxdb import InfluxDBClient
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
SRC_MQTT_HOST = os.getenv("SRC_MQTT_HOST")
SRC_MQTT_USER = os.getenv("SRC_MQTT_USER")
SRC_MQTT_PASS = os.getenv("SRC_MQTT_PASS")
DST_MQTT_HOST = os.getenv("DST_MQTT_HOST")
DST_MQTT_USER = os.getenv("DST_MQTT_USER")
DST_MQTT_PASS = os.getenv("DST_MQTT_PASS")
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")
OT_TOPIC="owntracks/tobru/dragino"
OT_TID="dragino"
def on_connect_ttn(client, userdata, flags, rc):
logging.info("connected to ttn %s - %s", SRC_MQTT_HOST, str(rc))
client.subscribe("+/devices/+/up")
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))
# 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"])
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"],
"t": "p",
"tid": OT_TID,
"tst": int(datetime.timestamp(datetime.now())),
})
# publish to owntracks
logging.info("publishing data to owntracks")
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"]),
}
}
]
)
else:
logging.info("no GPS data present")
logging.info("data processing done")
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %Z'
)
# Prepare InfluxDB
influxdb = InfluxDBClient(
host=DST_INFLUX_HOST,
port=443,
database=DST_INFLUX_DB,
username=DST_INFLUX_USER,
password=DST_INFLUX_PASS,
ssl=True,
verify_ssl=True,
)
# Prepare MQTT
client_ttn = mqtt.Client()
client_ttn.on_connect = on_connect_ttn
client_ttn.on_message = on_message_ttn
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.on_connect = on_connect_ot
client_ot.on_publish = on_publish_ot
client_ot.username_pw_set(DST_MQTT_USER,DST_MQTT_PASS)
client_ot.tls_set()
client_ot.connect(DST_MQTT_HOST, 8883, 60)
try:
# Connect to MQTT and react to messages
client_ot.loop_start()
client_ttn.loop_forever()
except KeyboardInterrupt:
client_ot.disconnect()
client_ot.loop_stop()
client_ttn.disconnect()
client_ttn.loop_stop()
logging.info("tschuess")

3
requirements.txt

@ -0,0 +1,3 @@
influxdb==5.3.0
python-dotenv==0.13.0
paho-mqtt==1.5.0
Loading…
Cancel
Save