How to transmit integer without converting to string?

Started by Lensdigital, January 27, 2021, 10:16:49 AM

Lensdigital

Is there an easy way to transmit/receive integer value  (i.e. 0 to 99) with RFM69?  I need to send/receive constant stream of values very fast (without retry, verification or conversion)... Would love to see an example.
I've tried this:
Transmitter (sends number 4)
radio.send(GATEWAYID,4,1);

Receiver:
Serial.println (radio.DATA[0]);

But I'm receiving '83'...

Felix

The send functions definitions are as follows:

virtual void send(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
virtual bool sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=RFM69_ACK_TIMEOUT);


They do not take integers. They take byte arrays, and do not care what those bytes are. How you convert something to a byte array is more of a code/C++ question.
Integers come as signed/unsigned, and 1/2/4 byte long.
You should use sprintf or bit shifting to convert your somethings into a byte buffer/array which you then pass to the send function.
You can also use the excellent Arduiniana Streaming and PString libraries which help you format things into strings/byte buffers.

Lensdigital

Thank you Felix!
I haven't touched Arduino code in few years, lol so it's hard for me remember all of this...
If anyone have a simple couple of line code snippet, I'd really appreciate it!  I just spent whole day banging my head over this lol...
i.e. transmit number 35, receive it as integer... Would probably take 30 seconds for someone who's working with RFM modules every day...
I think example like this would help a lot of people who might have similar issue...  If no-one can help, I understand. I will post code once I figure it out :)  Will probably take me few days...

Felix

:-) This is not javascript, c++ can only understand bits and bytes ....... What is "number 35"? Is this a signed number? What is the min/max value for it?

If just a 0-255 byte then this is sufficient:

byte buff[1];
buff[0]=number;
send(TO, buff, 1);


On the other side you unpack it in reverse.

If you don't define exactly what your magic "number" can be, then I can give you a face value solution and you will come back and report that ...  :'( :-\ ??? :o ... "Number -65538" comes across as something else.

Lensdigital

Awesome thank you Felix!!!  This example is very helpful!
Yeah number is very small, it's just value from IR distance sensor. Range is 0 to 50, it's never negative or more than a byte :)   35 was just an example, if it works for this number I can make it work for any other in the range specified :)

TomWS

The fastest way to send (using standard RFM69 methods) would be to gather the data in blocks of, say, 50 or so, and then transmit the block.  That would significantly reduce the overhead per data byte sent.  This works well unless latency is more critical than throughout.  In this case, sending each data value, as it occurs, is optimum.


Lensdigital

Quote from: TomWS on January 28, 2021, 10:11:41 PM
The fastest way to send (using standard RFM69 methods) would be to gather the data in blocks of, say, 50 or so, and then transmit the block.  That would significantly reduce the overhead per data byte sent.  This works well unless latency is more critical than throughout.  In this case, sending each data value, as it occurs, is optimum.
In this case unfortunately latency is super important. Visual feedback needs to be provided almost in real time as sensor data is received. I'm working on minimizing transmission pockets by ignoring sensor data if it hasn't changed.