日別アーカイブ: 2024年11月13日

Weather Stationの室外データを rtl_433, influxDB, grafanaで可視化

(myenv) mars@pi4Bu:~ $ cat weather-influx.py
#coding: utf-8
# encoding: utf-8

import sys
import subprocess
import json
import struct
from influxdb import InfluxDBClient

client = InfluxDBClient(host='XXX.XXX.XXX.XXX', port=8086,
                        username='root', password='XXXXX', database='iot01')
measurement = 'weather-station'
tags = {'place': 'leaf-weather', 'host': 'weather'}

cmd = [
    "rtl_433",
    "-F", "json"
]

process = subprocess.Popen(
    cmd,
    stdout=subprocess.PIPE,
    universal_newlines=True
)

for line in iter(process.stdout.readline, ''):
    if "time" in line:
        data = json.loads(line)
        temp = round((float(data["temperature_F"])-32)/1.8,1)
        humi = data["humidity"]
        wspd = round(float(data["wind_avg_km_h"])/3600,1)
        wdir = data["wind_dir_deg"]
        rain = data["rain_mm"]
    json_body = [
        {
        'measurement': measurement,
        'tags': tags,
        'fields': {'temp': temp, 'humi': humi, 'wdir': wdir, 'wspd':wspd}
        }
    ]
    client.write_points(json_body)
    print(json_body)