Plugin MQTT et RTL_...
 
Share:

Plugin MQTT et RTL_433  

  RSS
titrax35
(@titrax35)
New Member

Les messages du rtl_433 sont en json

La commande de rtl est donc : rtl_433 -F json

ce qui va donner des messages de la forme :

home/rtl_433 {"time" : "2019-09-16 19:22:58", "model" : "TFA-Twin-Plus-30.3049", "id" : 39, "channel" : 2, "battery" : "OK", "temperature_C" : 26.100, "humidity" : 99, "mic" : "CHECKSUM"}

home/rtl_433 {"time" : "2019-09-16 19:26:44", "model" : "Acurite 606TX Sensor", "id" : 71, "battery" : "OK", "temperature_C" : 18.200, "mic" : "CHECKSUM"}

Pour les modifier en

home/rtl_433/TFA-Twin-Plus-30.3049/temperature_C

home/rtl_433/Acurite 606TX Sensor/temperature_C

Qui va être directement lu par le plugin MQTT, se référer  https://github.com/mverleun/RTL433-to-mqtt qui permet de convertir les données JSON du rtl_433 diretement en format MQTT

Quote
Posté : 17/09/2019 2:47
titrax35
(@titrax35)
New Member

Pour ceux qui veulent, j'ai refait le script python,

Il faut que rtl_433 soit installé (source : ICI :

# prepare os
sudo apt-get update
sudo apt-get upgrade

# install dependencies
sudo apt-get install libtool libusb-1.0.0-dev librtlsdr-dev rtl-sdr cmake

# clone rtl_433 Git Repository
sudo git clone https://github.com/merbanan/rtl_433.git /opt/rtl_433

# change to directory
cd /opt/rtl_433

# make build folder
sudo mkdir build
cd build/

# compile

sudo cmake ../

# make
sudo make

# install
sudo make install

# test if rtl_433 was properly installed
rtl_433 -h

Puis utiliser le fichier suivant :

sudo apt-get install python3-pip
pip3 install paho-mqtt

Puis créer un fichier avec ce code (exmple : rtl2mqtt.py) :

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# Begin import section
import subprocess
import sys
import time
import paho.mqtt.client as mqtt
import os
import json
import re

from config import *

important_rtl_output_re = re.compile("^(Found|Tuned)")
# End import section

# Config section

rtl_433_path = "/opt/rtl_433/build/src"
MQTT_USER=""
MQTT_PASS=""
MQTT_HOST="localhost"
MQTT_PORT=1883
MQTT_TOPIC="sensors/rtl_433"
MQTT_QOS=0
DEBUG=False # Change to True to log all MQTT messages
# End config section

# Define MQTT event callbacks
rtl_433_cmd = rtl_433_path+"/rtl_433 -F json"
def on_connect(client, userdata, flags, rc):
     connect_statuses = {
         0: "Connected",
         1: "incorrect protocol version",
         2: "invalid client ID",
        3: "server unavailable",
        4: "bad username or password",
        5: "not authorised"
    }
    print("MQTT: " + connect_statuses.get(rc, "Unknown error"))

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print("Unexpected disconnection")
    else:
        print("Disconnected")

def on_message(client, obj, msg):
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

def on_publish(client, obj, mid):
    print("Pub: " + str(mid))

def on_subscribe(client, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(client, obj, level, string):
    print(string)

# Setup MQTT connection
mqttc = mqtt.Client()

mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_disconnect = on_disconnect

if DEBUG:
    print("Debugging messages enabled")
     mqttc.on_log = on_log
    mqttc.on_message = on_message
    mqttc.on_publish = on_publish

if MQTT_PASS:
    print("Connecting with authentication")
     mqttc.username_pw_set(MQTT_USER, password=MQTT_PASS)
else:
    print("Connecting without authentication")

mqttc.connect(MQTT_HOST, MQTT_PORT, 60)
mqttc.loop_start()

# Start RTL433 listener
print("Starting RTL433")
rtl433_proc = subprocess.Popen(rtl_433_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

while True:
    if rtl433_proc.poll() is not None:
        print("RTL433 exited with code " + str(rtl433_proc.poll()))
        sys.exit(rtl433_proc.poll())

    for line in iter(rtl433_proc.stdout.readline, '\n'):
        if DEBUG:
            print("RTL: " + line)
        elif important_rtl_output_re.match(line):
            print(line)

        if rtl433_proc.poll() is not None:
            print("RTL433 exited with code " + str(rtl433_proc.poll()))
            sys.exit(rtl433_proc.poll())

        if "time" in line:
            mqttc.publish(MQTT_TOPIC, payload=line, qos=MQTT_QOS, retain=True)
            json_dict = json.loads(line)
            for item in json_dict:
                value = json_dict[item]
                if "model" in item:
                    subtopicmodel = value
                if "id" in item:
                    subtopicid = str(value)

            for item in json_dict:
                value = json_dict[item]
                if not "model" in item:
                     mqttc.publish(MQTT_TOPIC+"/"+subtopicmodel+"/id "+subtopicid+"/"+item, payload=value, qos=MQTT_QOS, retain=True)

Modifier la partie # Config section avec vos informations.

Puis on édite : nano /etc/rc.local (si non présent : ici )

et on y ajoute :

python3 /<emplacement du script>/script_nextdom/rtl2mqtt.py &

Et voila, en installant le plugin MQTT, les informations remonteront sous cette forme :

sensors/rtl_433/Acurite 606TX Sensor/id 71/temperature_C

sensors/rtl_433/Acurite 606TX Sensor/id 71/id

RépondreQuote
Posté : 17/09/2019 11:49
Esso1-jeedom
(@esso1-jeedom)
Membre Admin

eux tu peux m'expliquer ce que tu fais ...  ? sniffer 433 comme le RF LINK mais en wifi ?

RépondreQuote
Posté : 19/09/2019 6:59
Share:
fr_FRFrançais
en_USEnglish fr_FRFrançais

Veuillez Connexion ou Inscription