this is my code, it connects, does 1 publish and then reads the messages that come up in my localhost server.
import time
import paho.mqtt.client as paho
broker="localhost"
def on_message(client, userdata, message):
time.sleep(1)
print("received message =", str(message.payload.decode("utf-8")))
client = paho.Client("client-001")
client.on_message = on_message
print("connecting to broker ", broker)
client.connect(broker, port=1883)#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("anyisfine")#subscribe
time.sleep(2)
print("publishing ")
#client.publish("anyisfine", "on")#publish
client.publish("anyisfine", "on")#publish
time.sleep(4)
client.loop_forever()
the error im getting is this one
connecting to broker localhost
subscribing
publishing
received message = on
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\simba\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\simba\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 3452, in _thread_main
self.loop_forever(retry_first_connection=True)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1572, in loop_read
rc = self._packet_read()
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 2266, in _packet_read
byte = self._sock_recv(1)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 664, in _sock_recv
return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'
and sometimes even this one, it really depends
connecting to broker localhost
subscribing
publishing
received message = on
Traceback (most recent call last):
File "C:/Users\simba/PycharmProjects/hivemqq/main.py", line 27, in <module>
client.loop_forever()
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1572, in loop_read
rc = self._packet_read()
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 2295, in _packet_read
data = self._sock_recv(self._in_packet['to_process'])
File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 664, in _sock_recv
return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'
Process finished with exit code 1
lastly, it will sometimes connect succesfully with a single publish but then will not read messages that are incoming, and sometimes everything works, until it doesnt(random everytime). it will then give one of those two errors. any help would be very appreciated.
remove client.loop_start() loop_forever() doesnot need loop.start() then it will work fine