Float to bytearray conversion for sigfox payload
-
@rcolistete @rcolistete Regading to the receiver side:
There isn't any so far, so it could be anything. :)
But I am thinking about google cloud platform (maps api)Thanks for tips, I will try to imlement them, and I will be back with the result.
-
@tttadam I am not sure if anyone still follows this topic, but this is the current status.
I think I can't compress the gps coordinates anymore, but there is still space some other telemetry data.sfMessage=(str(my_gps.latitude[0]*100000)[1:-2]+str(my_gps.longitude[0]*100000)[1:-2]).encode() sf.send(bytes(sfMessage)) -
@tttadam Nope, I think there is still place for improving.
This send the caracters as string not as byte array. -
@tttadam But using a string representation requires always more space. As binary representation, you encode 8 bit per byte, as a (decimal) string, you encode about 3 bit per byte.
Example: A 32 bit integer (or float) always requires 4 byte space, wheras as string it may take need up to 11 bytes for integers and up to 14 bytes for floats.
P.S.: I recall that you had problems to decode binary numbers at the receiving side. -
@robert-hh I don't really have a problem with decoding on the receiving side, because there is no receing side. only chack the backend.sigfox with my eyes only. My issue right now that I can't convert this string to bytes. I am not sure what method should I use. Probably something trivial I think.
This is the useful information:
lat =47.497132 lon =19.063296 #(famous ruinpub in Budapest) str(lat*100000)[1:-2]+str(lon*100000)[1:-2] -
@tttadam You use struct.pack() to put these two floats into an 8 byte array without converting them to a string. That results in the shortest possible message, not readable for human eyes withou doing an unpack.
import ustruct lat =47.497132 lon =19.063296 msg = ustruct.pack("ff", lat, lon) # .. # and unpack lat, lon = ustruct.unpack("ff", msg) -
@robert-hh said in Float to bytearray conversion for sigfox payload:
ustruct.unpack("ff", msg)
Nice, thank you.
I think is still posible save some more space if cut the first caracter and the dot. (And then possible to include more information)
Which I try and fail to doing here. I think the unpacked value is rounded somehow.>>> lat 47.49713 >>> lon 19.0633 >>> msg2 =int((str(lat*100000)[1:-2]+str(lon*100000)[1:-2])) >>> msg = ustruct.pack("ff", msg2) >>> ret = ustruct.unpack("ff", msg) >>> print(ret) (7.497139e+11, 0.0) -
@tttadam What you are doing is covering two floats to strings, concatenating them, converting the total to an int, packing that in as a float, and unpacking.
Forget about
str. Just usepackdirectly with the original values:ustruct.pack("ff",lat,lon)Or if you want to avoid sending floats over the air, you may convert them to ints and pack than as such instead.
-
@tttadam msg2 contains an single int. So you must not use "ff" for pack, since that tells, that two float numbers are supplied. You have to use "i", which will pack (and unpack) it into a 4 byte format.
Please have a look at the documentation of struct: https://docs.python.org/3/library/struct.html -
@robert-hh said in Float to bytearray conversion for sigfox payload:
I see now. Okay. But I think I am still missing some information. Because I can't get back the same number.
>>> lat 47.49713 >>> lon 19.0633 >>> msg2 =int(str(lat*100000)[1:-2]+str(lon*100000)[1:-2]) >>> msg2 749713906330 >>> msg = ustruct.pack("i", msg2) >>> msg b'\x9aZn\x8e' >>> ret = ustruct.unpack("i", msg) >>> ret (-1905370470,) -
@tttadam The numer is too long for the i format, then you have to use the "q" format (long long). But that results in 8 bytes, and then you can also directly pack the two floats and have no further need for reformatting or scaling (see my post below).
Using the "q" format, you could eventually drop the last three bytes, they are zero, and add them before unpacking. But that does not seem to be worth the effort. -
@tttadam Or you could split the number:
>>> msg2 749713906330 >>> msg = ustruct.pack("IB", msg2 // 256, msg2 % 256) >>> msg b'Zn\x8e\xae\x9a' >>> msb,lsb = struct.unpack("IB", msg) >>> ret=msb * 256 + lsb >>> ret 749713906330But still that is overkill compared to the float packing, unless you fight for every single byte.
-
@robert-hh So basicly your implamentation is the best. :)
Thank you. -
@tttadam why do you keep doing the same stuff you have already been told twice is not correct?
You have been told twice that the solution is very simply
ustruct.pack(“ff”,lat,long). What’s wrong with that? Stop with thestrand the concatenation and all that. Just pack the two values, and you’re done. -
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login