Page 1 of 1

Unpacking data in arduino

Posted: Sat Jan 18, 2020 6:43 am
by Kahroba
Hello,
I have been working on streaming data from OpenMv to NodeMcu.
I did manage to establish a protocol between OpenMv and Nodemcu to send a request from NodeMCU to OpenMv in order to start sending data.
But the data will be started with 4 packed bytes, representing the size of the image. Is there any idea how can I unpack these 4 bytes to reach the size of the image on the NodeMcu as I do need the size of the image in order to implement a flow control mechanism.

Re: Unpacking data in arduino

Posted: Sat Jan 18, 2020 3:00 pm
by kwagyeman
You just treat the 4 bytes as an int.

Code: Select all

int x = 0;
x |= byte3 << 24;
x |= byte2 << 16;
x |= byte1 << 8;
x |= byte0 << 0;
x now has the value in in. Please read this: https://www.cs.cmu.edu/afs/cs/academic/ ... kesden.pdf

Both processors are little endian. https://www.thethingsnetwork.org/docs/d ... bytes.html

Re: Unpacking data in arduino

Posted: Sun Jan 19, 2020 7:15 am
by Kahroba
kwagyeman wrote:
Sat Jan 18, 2020 3:00 pm
You just treat the 4 bytes as an int.

Code: Select all

int x = 0;
x |= byte3 << 24;
x |= byte2 << 16;
x |= byte1 << 8;
x |= byte0 << 0;
x now has the value in in. Please read this: https://www.cs.cmu.edu/afs/cs/academic/ ... kesden.pdf

Both processors are little endian. https://www.thethingsnetwork.org/docs/d ... bytes.html
Many thanks for the response. It works fine.
I am just wondering if the obtained number is the size of the image in bytes?
For instance, if obtained 2048 as the size of the image should I create a byte array with the size of 2048 to store this image? Am I right?

Re: Unpacking data in arduino

Posted: Sun Jan 19, 2020 11:39 am
by kwagyeman
The Arduino Uno has not memory onboard really so if you are using that you lost likely won't be able to do much. If you are using an arm based one you might be able to handle an image size. Mmm... Nodemcu is the esp32 right? That has plenty of ram.

Anyway, if you sent the length of the image to the Arduino then that's the image size in bytes. So, yes, you write the image to the byte array. Note that jpeg images are variable size.

Do this, allocated a fixed buffer as big as you can make it. Write the received image to it as long as the image is less than the max buffer size. If it's bigger just toss the bytes after reading them. This should let you safely received images.

Re: Unpacking data in arduino

Posted: Sun Jan 19, 2020 2:02 pm
by Kahroba
kwagyeman wrote:
Sun Jan 19, 2020 11:39 am
The Arduino Uno has not memory onboard really so if you are using that you lost likely won't be able to do much. If you are using an arm based one you might be able to handle an image size. Mmm... Nodemcu is the esp32 right? That has plenty of ram.

Anyway, if you sent the length of the image to the Arduino then that's the image size in bytes. So, yes, you write the image to the byte array. Note that jpeg images are variable size.

Do this, allocated a fixed buffer as big as you can make it. Write the received image to it as long as the image is less than the max buffer size. If it's bigger just toss the bytes after reading them. This should let you safely received images.
Yes, It is an ESP32 nodemcu board and I think it has sufficient ram to store a low-quality image, but I still have a problem.
The problem is the length of sent data is not as same as the size of the image.
For instance, I got 1487 as the size of the image but just 628 bytes have been received by NodeMCU. I am not sure what is wrong?!

Re: Unpacking data in arduino

Posted: Sun Jan 19, 2020 8:50 pm
by kwagyeman
Code? And if possible please post the part that has the issue and not everything.

For future post... Please remember this is a forum where I'm answering questions. If you can post them in a way where I might be able to answer it quickly that helps