0

First thing first, sorry for my bad english

I'm trying to create a MQTT program in python 2.7 that runs under Linux. I'm not a programmer but a automotive mechanic. So keep it simple for me :) It's just a hobby

At the moment i'm using the PAHO-MQTT 1.2.3 package for Python. I got an annoying error and i can't find what i'am doing wrong.

Here is some of the code

def on_connect(client, userdata, flags, rc):
    printLine()
    printMsg("Connected to server")

    printMsg("Address:\t ] %s" % (sServer))
    printMsg("Port:\t\t ] %s" % (sPort))
    printMsg("Username:\t ] %s" % (sUsername))
    #client.subscribe("order/"+sLocation+"/mem")
    #client.subscribe("order/"+sLocation+"/in")
    printLine()


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(sUsername,sPassword)
printMsg("marker 1")
time.sleep( 0.3 )
client.connect(sServer, sPort, 60)
time.sleep( 0.3 )
printMsg("marker 2")
client.loop_start()
time.sleep( 0.3 )
printMsg("marker 3")

7 out 10 is OK (print @ linux terminal)

[========================================
[ Booting.....
[ marker 1
[ marker 2
[========================================
[ Connected to server
[ Address:   ] *******
[ Port:      ] 8883
[ Username:  ] *********
[========================================
[ marker 3
[ send

3 out 10 i'm getting the next error in the linux terminal

[========================================
[ Booting.....
[ marker 1
[ marker 2
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 2630, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1410, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 926, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1209, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1802, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2278, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2430, in _handle_publish
    (topic, packet) = struct.unpack(pack_format, packet)
error: bad char in struct format

[ marker 3
[ send

Can someone point me in the right direction to fix my error.

Site: github.com paho-mqtt

Site: pypi.python.org paho-mqtt

hardillb
  • 54,545
  • 11
  • 67
  • 105
RS81
  • 1
  • 3
  • I'm possibly being daft, but what do you mean by "7 out of 10." Do you mean it usually works but sometimes fails? – blp Jun 02 '17 at 21:41
  • What's the network like between your broker and the client? It looks like something is corrupting packets – hardillb Jun 02 '17 at 22:29
  • @blp Yes, usually it's working. Sometimes it fails. – RS81 Jun 03 '17 at 05:54
  • @hardillb Because I'm developing it is on my laptop and to server is a VPS. I can test the code direct on the VPS and connect it to localhost. If it's done it must work on a PI 3 with touchscreen with WIFI connection. If it's a connection error in need a solution to safely catch to error and retry – RS81 Jun 03 '17 at 06:04
  • Also Wick broker are you using? – hardillb Jun 03 '17 at 08:29
  • @hardillb running Mosquitto @ Debian 8. Running for one week now. – RS81 Jun 03 '17 at 12:23
  • Is there anything of interest in the Mosquitto log files? Have you tried using a public broker (test.mosquitto.org etc.) for testing? – blp Jun 04 '17 at 05:02
  • Today it stopped for 100%... no connection possible. After checking the logs (nothing strange there) I did a reboot with the result that to complete VPS crashed.... Today a fresh install (Debian 8 & Mosquitto) and everything seems to work. But I want a solution to catch the error in my script. Can I do some checks ? – RS81 Jun 04 '17 at 16:28
  • Test broker was good but it's not supporting login with user and password. – RS81 Jun 04 '17 at 16:51
  • Yeah, there is an annoying lack of test servers that allow you to try that out. You can sign up for a free account at CloudMQTT and use a password there to be sure. But it is sounding to me like this is a connection issue more than a code issue and that you are more interested in just retrying the connection. For catching the exception you can always use try / except to catch the error and continue. If you need an example of how to do that with mqtt, I could draw that up as an answer. – blp Jun 04 '17 at 18:27
  • To confirm it is a connection error you could also try a long ping between the two servers (say 3 to 4 hours / once per second) and see if there is a connection drop or high latency. Also, I just realized that we never asked about the output of client.on_log callback. I doubt there is anything meaningful there (it looks like it is failing before the connection is complete) but for completeness sake it is worth trying. (And personally I use it any time that I'm working with MQTT.) – blp Jun 04 '17 at 18:32

0 Answers0