Apr-11-2022, 08:23 AM
(Apr-11-2022, 05:27 AM)deanhystad Wrote: This looks interesting.
Quote:Tkinter has its own event- and GUI-update- loop (mainloop()). Mqtts start_loop() will block Tkinters mainloop() and vice versa.
General ways to solve this are
Polling
If you can use Mqtt without its loop but with manual polling, then establish an userloop under control of Tkinters mainloop with after() method. In said userloop poll messages from mqtt.
Multithreading or multiprocessing
If you must use Mqtts loop put all mqtt stuff in a thread or process and communicate with Tkinter over Queues (unidirectional) or Pipes (bidirectional). Tkinter can handle this communication in userloop explained above in minimal intervals of few 10 ms.
What i read briefly from IMHO you have both options (Other loop...() functions are available that give a threaded interface and a manual interface)
https://pypi.org/project/paho-mqtt/
Thanks a lot for your answer, as you can see in my code I already tried multithreading (the part of my code which is commented ) and I got the errors above.
#def run_client(): # threading.Timer(1.0, run_client).start() # client.loop() #def run_mqtt_client(): # threading.Timer(0.1, run_client).start() # mqtt_client.loop_start() #run_client() #run_mqtt_client()The issue is that temperature, humidity... values are all sent correctly to my devices, but the commands which are sent from mydevices are received with a response error as you can see in the log I previously sent. The button from mydevices keep running (turning) without any change into the interface. When I change delays, things change and works correctly.
Error:PUB v1/1xxxxxxxxxxxxxxxxxxxxxxxd/things/1xxxxxxxxxxxxxxxxxxxxxxxd/sys/model Python
PUB v1/1xxxxxxxxxxxxxxxxxxxxxxxd/things/1xxxxxxxxxxxxxxxxxxxxxxxd/sys/version 1.1.0
Error: Unrecognised command 0
Exception in thread Thread-257:
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 1180, in run
self.function(*self.args, **self.kwargs)
File "test_update.py", line 216, in run_client
client.loop()
File "/usr/local/lib/python3.5/dist-packages/cayenne_mqtt-1.1.0-py3.5.egg/cayenne/client.py", line 122, in loop
self.client.loop()
File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 1072, in loop
rc = self.loop_read(max_packets)
File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 1374, in loop_read
rc = self._packet_read()
File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 2071, in _packet_read
rc = self._packet_handle()
File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 2568, in _packet_handle
return self._handle_suback()
File "/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg/paho/mqtt/client.py", line 2701, in _handle_suback
(mid, packet) = struct.unpack(pack_format, self._in_packet['packet'])
struct.error: bad char in struct format
Disconnected with result code 2