vshymanskyy / blynk-library-python

Blynk library for Python. Works with Python 2, Python 3, MicroPython.
https://blynk.io/
MIT License
286 stars 98 forks source link

struct.error: unpack requires a string argument of length 5 #10

Closed litnimax closed 5 years ago

litnimax commented 6 years ago

When I run the example app and press run the project on mobile I get:

MacBook-Pro-Max:pysense max$ python blynkapp.py
TCP: Connecting to blynk-cloud.com:8442
Blynk connection successful, authenticating...
Access granted, happy Blynking!
Traceback (most recent call last):
  File "blynkapp.py", line 15, in <module>
    blynk.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/BlynkLib.py", line 363, in run
    msg_type, msg_id, msg_len = struct.unpack(HDR_FMT, data)
struct.error: unpack requires a string argument of length 5
MacBook-Pro-Max:pysense max$

Here is the example app - https://gist.github.com/litnimax/16a286b6db725c51c905988396ba647f

JussiRoos commented 6 years ago

Same problem here! I think it something similar to this: https://stackoverflow.com/questions/2611858/struct-error-unpack-requires-a-string-argument-of-length-4 But I haven't gotten deep enough to solve it yet.

wmarchewka commented 6 years ago

I am getting the error as well. Its stemming from the fact that he is trying to unpack the received data to a specific header and it appears the header is wrong or is receiving the wrong data for the header. In my case, i am receiving a value of "vw4325" which i am assuming is a virtual write to pin 4 with a value of 325. This, according to python , is a value of 8, whereas he is trying to unpack it as "!BHH" which is a unsigned char and 2 unsigned shorts (1 byte + 2 bytes + 2 bytes) for a value of 5 bytes. So i am not sure if the format of the data received has changed? This is my first day with Blynk, so i will try and investigate further.

Sending a button press from the phone i received a "vw11" which was set up for virtual pin one receiving a value of 1. The had a size of 6. I am guessing that each char is 1 byte and each number is 2 bytes in this case. In the case above , the "vw4325" culd be 1 byte for each char, two bytes for the pin "4", and 4 bytes for the value "325" to give 8 bytes?

mehdijouan commented 6 years ago

Hi, I'm facing the same issue!

It's my first day learning python, so I'm not sure to understand what I really do. Based on the error message :

struct.error: unpack requires a string argument of length 5

I assumed that a message without this is not normal so I changed the code by adding a check on the data length to match the buffer one.

                if data and len(data) == HDR_LEN:
                    msg_type, msg_id, msg_len = struct.unpack(HDR_FMT, data)

I can now read values on my mobile.

I'm pretty sure that it is not a solution but, at least it's a quick fix for me.

burnpanck commented 6 years ago

Still an issue here on macOS. I have previously used blynk successfully on Ubuntu 18.04.

tarjes commented 6 years ago

Same issue here on macOS Sierra using python 3.6.5. How is that quick fix working for you @mehdijouan ?

vshymanskyy commented 5 years ago

Please check the latest version from master branch. The library was rewritten, and this issue should not happen anymore

vshymanskyy commented 5 years ago

v0.2.0 is released and available via pip: https://github.com/vshymanskyy/blynk-library-python/releases/latest