Community
Participate
Working Groups
Hi, i use a nanopi with an Ubuntu 16.04.3 LTS 4.11.2 Image. i written an example in Python: ############################################# #!/usr/bin/env python from builtins import input import paho.mqtt.client as mqtt mqtt_broker_address = '<IP-Address-of-Broker>' mqtt_broker_port = 1883 switch_state=[0,0,0,0,0,0] def on_connect(client, userdata, rc): print("Connected with result code: %s" % rc) client.subscribe('home/#') def on_message(client, userdata, msg): print("%s: %s" % (msg.topic, msg.payload)) if msg.topic == 'home/indoor/switch': print('new Value: %s' % msg.payload) # see https://pypi.python.org/pypi/paho-mqtt/ client = mqtt.Client() client.username_pw_set('<mqtt-username>', '<mqtt-password>') client.on_connect = on_connect client.on_message = on_message client.connect(mqtt_broker_address, mqtt_broker_port) client.loop_start() # this puts the loop in the background try: while True: topic = "home/indoor/switch/" input_value = int(input('select a switch [1 - 6] for %s to change state and press enter:' % topic)) topic = "home/indoor/switch" + str(input_value) if switch_state[input_value-1] == 0: switch_state[input_value-1] = 1 value = 'ON' else: switch_state[input_value-1] = 0 value = 'OFF' print("publish data (switch%s): %s" % (input_value, value)) client.publish(topic, value) except KeyboardInterrupt: client.loop_stop(force=True) ###########################################EOF i get the result in Python 2.7.12: pi@NanoPi-NEO:~/Python$ sudo python mqtt_test_switch.py select a switch [1 - 6] for home/indoor/switch/ to change state and press enter:Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 754, in run self.__target(*self.__args, **self.__kwargs) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2606, in _thread_main self.loop_forever(retry_first_connection=True) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1470, in loop_forever rc = self.loop(timeout, max_packets) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 995, in loop rc = self.loop_read(max_packets) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1273, in loop_read rc = self._packet_read() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1838, in _packet_read rc = self._packet_handle() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2291, in _packet_handle return self._handle_connack() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2349, in _handle_connack self.on_connect(self, self._userdata, flags_dict, result) TypeError: on_connect() takes exactly 3 arguments (4 given) i get the result in Python 3.5.2: pi@NanoPi-NEO:~/Python$ sudo python3 mqtt_test_switch.py select a switch [1 - 6] for home/indoor/switch/ to change state and press enter:Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 2606, in _thread_main self.loop_forever(retry_first_connection=True) File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 1470, in loop_forever rc = self.loop(timeout, max_packets) File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 995, in loop rc = self.loop_read(max_packets) File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 1273, in loop_read rc = self._packet_read() File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 1838, in _packet_read rc = self._packet_handle() File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 2291, in _packet_handle return self._handle_connack() File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py", line 2349, in _handle_connack self.on_connect(self, self._userdata, flags_dict, result) TypeError: on_connect() takes 3 positional arguments but 4 were given best regards Peter
Hi, the version of mqtt-client are -> __version__ = "1.3.0" regards Peter
H