简体   繁体   中英

Raspberry Pi as MQTT publisher and subscriber

I am trying to establish RPi as publisher and subscriber at the same time. I will do that in such way, that I will put subscriber.py and publisher.py as threads and run them one after another. When it comes to the codes I have followed https://iotbytes.wordpress.com/mosquitto-mqtt-broker-on-raspberry-pi/ and I took the codes from here too. At first, I run both codes without any threads. Publisher.py works fine. When I run subscriber.py I get the following:

Traceback (most recent call last):
  File "/home/pi/subscriber.py", line 34, in <module>
    mqttc.loop_forever()
  File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
  File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1556, in loop_read    rc = self._packet_read()
  File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
  File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3039, in _packet_handle
    return self._handle_connack()
  File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3138, in _handle_connack
    on_connect(
TypeError: on_connect() takes 3 positional arguments but 4 were given

Which is weird, cause it does not put 4 arguments.

The code:

import paho.mqtt.client as mqtt

# Define Variables
MQTT_BROKER = "test.mosquitto.org" # "192.168.0.13" #"MQTT Broker IP or DNS Name"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "testTopic"

# Define on_connect event Handler
def on_connect(mosq, obj, rc):
        #Subscribe to a the Topic
        mqttc.subscribe(MQTT_TOPIC, 0)

# Define on_subscribe event Handler
def on_subscribe(mosq, obj, mid, granted_qos):
    print ("Subscribed to MQTT Topic")

# Define on_message event Handler
def on_message(mosq, obj, msg):
        print (msg.payload)

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register Event Handlers
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe

# Connect with MQTT Broker
mqttc.connect(MQTT_BROKER, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)

# Continue the network loop
mqttc.loop_forever()

The callback function on_connect is always called with four arguments. There are several examples under https://github.com/eclipse/paho.mqtt.python/ , eg here :

def on_connect(mqttc, obj, flags, rc):
    print("rc: " + str(rc))

This is not easily discernible since the function call happens in the background and you don't get to see the arguments with which the function is being called.

In your code you only define three arguments. This leads to the confusing error message.

You need to define the function with four arguments even when you are not using them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM