Portál AbcLinuxu, 5. května 2025 23:21
dyť to jakoby máš nakonci toho kroku 7 v přidaným zdrojáčku pythoním ukázaný :O ;D
hele ;D
vtý funkci/callbacku on_message to máš pokaždý v msg.payload ;D
""" Python MQTT Subscription client Thomas Varnish (https://github.com/tvarnish), (https://www.instructables.com/member/Tango172) Written for my Instructable - "How to use MQTT with the Raspberry Pi and ESP8266" """ import paho.mqtt.client as mqtt # Don't forget to change the variables for the MQTT broker! mqtt_username = "MQTT Username" mqtt_password = "MQTT Password" mqtt_topic = "Your Topic" mqtt_broker_ip = "Your Broker IP" client = mqtt.Client() # Set the username and password for the MQTT client client.username_pw_set(mqtt_username, mqtt_password) # These functions handle what happens when the MQTT client connects # to the broker, and what happens then the topic receives a message def on_connect(client, userdata, flags, rc): # rc is the error code returned when connecting to the broker print "Connected!", str(rc) # Once the client has connected to the broker, subscribe to the topic client.subscribe(mqtt_topic) def on_message(client, userdata, msg): # This function is called everytime the topic is published to. # If you want to check each message, and do something depending on # the content, the code to do this should be run in this function print "Topic: ", msg.topic + "\nMessage: " + str(msg.payload) # The message itself is stored in the msg variable # and details about who sent it are stored in userdata # Here, we are telling the client which functions are to be run # on connecting, and on receiving a message client.on_connect = on_connect client.on_message = on_message # Once everything has been set up, we can (finally) connect to the broker # 1883 is the listener port that the MQTT broker is using client.connect(mqtt_broker_ip, 1883) # Once we have told the client to connect, let the client object run itself client.loop_forever() client.disconnect()
dotoho ti jako nechci kecat ale python mi příde vo moc jednoduší než bash ;D
jak jako ta nějaká zpráva má vypadat a do jaký podoby ji jakoby chceš naparsovat?? :O :O
takle ňák?? :O :O
#!/usr/bin/env bash mosquitto_sub -u uzivatel -P heslo -t topic | while read zprava; do echo "mam zpravu: $zprava" done
koukám žeto je jakoby pro starej python2 :O :O :/ :/
víc novější ukázkovej kód je u dokumentace tý knihovny hele :O ;D
supr ;D
kdybys to eště jakoby chtěl pouštět přes systemd službu tak ve složšce /etc/systemd/system si mužeš vyrobit soubor 'nazev_sluzby.service' a do něj dát text
[Unit] Description=nejaky popis sluzby # vodhaduju ze jako mas primo na tom rpi nainstalovanyho mosquitto brokera # jestli ne tak to mosquitto.service skrtni na vobou radcich Requires=network.target mosquitto.service After=network.target mosquitto.service [Service] ExecStart=/cesta/ke/skriptu/skript.sh Restart=always # uzivatel pod kterym se tamten skript jakoze pousti User=pi [Install] WantedBy=multi-user.target
pak udělat
sudo systemctl daemon-reload sudo systemctl enable nazev_sluzby sudo systemctl start nazev_sluzby
a hotovo jako :D ;D
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.