-1

I am using my python code to publish messages to node-red via mqttc. The problem is after 20 mqttc publish,the data is not getting published to nodered. Here is the python code i used for posting to node-red.

mqttc.connect("nodered&mqtt_server_ip",1883)
i=1
while True:

    i=i+1
    mqttc.publish("sensor",i,1)
    time.sleep(1)

Also tried

 i=1
 while True:

    mqttc.connect("nodered&mqtt_server_ip",1883)
    i=i+1
    mqttc.publish("sensor",i,1)
    mqttc.disconnect()
    time.sleep(1)

Anyway the code publishes to node red for first 20 values then stops.

My Nodered workflow is as follows..

enter image description here

1

Just a single word change and all is working fine.. Figured it out. Instead of 1 in mqttc.publish 3rd argument tried with 0 and all is well. The parameter for mqttc connect is Quality of Service http://mosquitto.org/man/mqtt-7.html

MQTT defines three levels of Quality of Service (QoS). The QoS defines how hard the broker/client will try to ensure that a message is received. Messages may be sent at any QoS level, and clients may attempt to subscribe to topics at any QoS level. This means that the client chooses the maximum QoS it will receive. For example, if a message is published at QoS 2 and a client is subscribed with QoS 0, the message will be delivered to that client with QoS 0. If a second client is also subscribed to the same topic, but with QoS 2, then it will receive the same message but with QoS 2. For a second example, if a client is subscribed with QoS 2 and a message is published on QoS 0, the client will receive it on QoS 0.

Higher levels of QoS are more reliable, but involve higher latency and have higher bandwidth requirements.

0: The broker/client will deliver the message once, with no confirmation.

1: The broker/client will deliver the message at least once, with confirmation required.

2: The broker/client will deliver the message exactly once by using a four step handshake.

mqttc.connect("nodered&mqtt_server_ip",1883)
i=1
while True:

    i=i+1
    mqttc.publish("sensor",i,0)//corrected code
    time.sleep(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.