I'm really struggling to get this basic concept to work, I've probably spent 15 hours on this one problem, the time spent searching for answers was full of useless contradictory information.
I've got a client-side process subscribed to MQTT topic, it reacts according to the type of message received. I'm then wanting to send back data to the shore-side process.
I've tried looping forever, start loop, no loop, on and on and ****ing on but the data is NOT appearing in MQTT Explorer (local port forward on 1883 to inspect live feed)
Code
Main
import os
import paho.mqtt.client as mqtt
import logging
import time
import lib.mqtt.mqtt_actions as mqtt_act
logging.basicConfig(level=logging.INFO, filename='/data/client.log', filemode='a+',format='%(asctime)s - %(name)s : %(levelname)s - %(message)s')
def main():
client = mqtt.Client()
client.tls_set(-setup tls certs-)
client.on_connect = mqtt_act.on_connect
client.on_message = mqtt_act.on_message
client.on_log = mqtt_act.on_log
client.connect(-connect to broker-)
client.loop_forever()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
client.loop_stop()
if __name__ == "__main__":
main()
MQTT actions
import json
import logging
import time
import subprocess
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def send_ack(client, _id):
client.publish('a/topic/state_change', payload='{"id": "'+str(_id)+'","status": "START", "code": "003", "timestamp": "'+str(datetime.datetime.now())+'"}')
logging.info("send ack")
client.loop()
def on_log(client,userdata,level,buff):
print(buff)
def on_connect(client, userdata, flags, rc):
logging.info("Connected with return code: "+str(rc))
# get some sort of identifier
_id = get_id()
client.subscribe('a/topic/'+_id+'/#')
def on_message(client, userdata, msg):
_ = json.loads(msg.payload.decode())
logging.info("Received message from master")
if _['action'] == "test123":
_id = _['id']
logging.info("ID: "+str(_id))
send_ack(client, _id)
outcome = get_data()
outcome['id'] = str(_id)
try:
client.publish("a/topic/endpoint/"+str(_id), str(outcome))
client.loop()
except Exception as e:
logging.info(str(e))
Output
host:~$:/tmp/client_test# python3 listen.py
Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b''
Received CONNACK (0, 0)
Sending SUBSCRIBE (d0, m1) [(b'a/topic/222/#', 0)]
Received SUBACK
Received PUBLISH (d0, q0, r0, m0), 'a/topic/222/test123', ... (89 bytes)
Sending PUBLISH (d0, q0, r0, m2), 'b'a/topic/endpoint/state_change'', ... (122 bytes)
Sending PUBLISH (d0, q0, r0, m3), 'b'a/topic/endpoint/64cb76fa-791b-11eb-bde4-005056ae7e22'', ... (2596 bytes)
Received PUBLISH (d0, q0, r0, m0), 'a/topic/222/test123', ... (89 bytes)
What appears in MQTT Explorer
- state_change
- test123