TypeError: function takes 2 positional arguments b

2019-07-25 13:24发布

问题:

I'm trying to create a seperate class for MQTT subscribe and publish for a LoPy device, written in micropython.

This is my code of main.py:

import pycom
import time
import communicationmod

pycom.heartbeat(False)

if __name__ == '__main__':

    communication = communicationmod.Com()

    while True:

        communication.update()
        print (communication.getmessage())
        time.sleep(1.00)

And this is the code of communicationmod.py:

import pycom
import time
from umqtt import MQTTClient
import machine
import ujson

class Com:

    mess = ""
    client = None

    def __init__(self):

        print ('init')

        self.client = MQTTClient("pycom", "192.168.123.50", port=1883, user="simon", password="****")
        self.client.settimeout = self.settimeout
        self.client.connect()

        self.client.set_callback(self._recv_msg_callback)
        print ('Callback setted!')
        self.client.subscribe("/Upload")
        print ('Subsribed!')

    def settimeout(duration):
        pass

    def _recv_msg_callback(topic, msg):
        print("{}".format(msg))

    def update(self):
        self.client.check_msg()
        self.client.publish("/Download", "this is a test string")

    def getmessage(self):
        return self.mess

But I get an error:

File "main.py", line 44, in

File "communicationmod.py", line 32, in update

File "umqtt.py", line 194, in check_msg

File "umqtt.py", line 181, in wait_msg

TypeError: function takes 2 positional arguments but 3 were given

MicroPython v1.8.6-489-g246ea51a on 2017-03-02; LoPy with ESP32

The publish method works and when I put all the code of the class Com in the main.py file (without classes) the client.check_msg() works too. I don't understand why I'm getting this error and why it works without it is in the class.

I use this library for MQTT