4 changed files with 97 additions and 13 deletions
@ -1,2 +1,3 @@ |
|||
data/** |
|||
log/** |
|||
energy-data/** |
|||
|
|||
@ -1,17 +1,23 @@ |
|||
version: "3.8" |
|||
|
|||
services: |
|||
mosquitto: |
|||
image: eclipse-mosquitto |
|||
container_name: mosquitto |
|||
restart: always |
|||
volumes: |
|||
- "./config:/mosquitto/config" |
|||
- "./data:/mosquitto/data" |
|||
- "./log:/mosquitto/log" |
|||
ports: |
|||
- '1883:1883' |
|||
- '9001:9001' |
|||
cap_add: |
|||
- NET_ADMIN |
|||
mosquitto: |
|||
image: eclipse-mosquitto |
|||
container_name: iot-mosquitto |
|||
restart: always |
|||
volumes: |
|||
- "./config:/mosquitto/config" |
|||
- "./data:/mosquitto/data" |
|||
- "./log:/mosquitto/log" |
|||
ports: |
|||
- '1883:1883' |
|||
- '9001:9001' |
|||
cap_add: |
|||
- NET_ADMIN |
|||
|
|||
extractor: |
|||
build: ./extractor-image |
|||
container_name: iot-energy-extractor |
|||
restart: always |
|||
volumes: |
|||
- "./energy-data:/data" |
|||
|
|||
@ -0,0 +1,10 @@ |
|||
FROM python:slim |
|||
|
|||
# python packages |
|||
RUN pip install --no-cache-dir paho-mqtt && pip cache purge |
|||
|
|||
WORKDIR /app |
|||
|
|||
COPY extractor.py /app/. |
|||
|
|||
ENTRYPOINT ["python", "/app/extractor.py"] |
|||
@ -0,0 +1,67 @@ |
|||
#!/bin/python3 |
|||
|
|||
import paho.mqtt.client as mqtt |
|||
import json, csv |
|||
import datetime |
|||
|
|||
|
|||
broker_hostname ="www.weltenpendler.at" |
|||
port = 1883 |
|||
username = 'ms54' |
|||
password = '5SFHTpoZ' |
|||
|
|||
topic = "flying-socket/status/energy" |
|||
filename = '/data/flying-socket' |
|||
|
|||
def on_connect(client, userdata, flags, return_code): |
|||
if return_code == 0: |
|||
print("connected") |
|||
client.subscribe(topic) |
|||
else: |
|||
print("could not connect, return code:", return_code) |
|||
|
|||
|
|||
def writeCSV(jsondata, id, ts): |
|||
s = jsondata[f's{id}'] |
|||
data = { |
|||
'datetime': ts, |
|||
'aenergy_minute_ts': s['aenergy']['minute_ts'], |
|||
'aenergy_by_minute_0': s['aenergy']['by_minute'][0], |
|||
'aenergy_by_minute_1': s['aenergy']['by_minute'][1], |
|||
'aenergy_by_minute_2': s['aenergy']['by_minute'][2], |
|||
'aenergy_total': s['aenergy']['total'], |
|||
'pf': s['pf'], |
|||
'current': s['current'], |
|||
'voltage': s['voltage'], |
|||
'apower': s['apower'], |
|||
'temperature': s['temperature']['tC'], |
|||
} |
|||
|
|||
with open(f'{filename}-s{id}.csv', 'a') as file: |
|||
writer = csv.DictWriter(file, fieldnames=data.keys()) |
|||
if file.tell() == 0: writer.writeheader() |
|||
writer.writerow(data) |
|||
|
|||
def on_message(client, userdata, message): |
|||
message = str(message.payload.decode("utf-8")) |
|||
jsondata = json.loads(message) |
|||
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
|||
|
|||
with open(filename+'.log', 'a') as f: |
|||
print(message, file=f) |
|||
|
|||
writeCSV(jsondata, 0, now) |
|||
writeCSV(jsondata, 1, now) |
|||
|
|||
|
|||
|
|||
|
|||
client = mqtt.Client("AutomaticDataExtractor") |
|||
client.connect(broker_hostname) |
|||
client.username_pw_set(username=username, password=password) |
|||
client.on_connect=on_connect |
|||
client.on_message=on_message |
|||
print('starting ...') |
|||
|
|||
client.loop_forever() |
|||
|
|||
Loading…
Reference in new issue