Problem with Accelerometer RAK1904 negative values to TTS

Dear All ,

i’m always working on my Wisblock , one of my sensors is RAK1904, it work fine, and from serial monitor i see the right reads .
The problem born when i send the data to TTS the negative values, all crash , some idea ?

Yes, some idea :wink:

You are likely to be storing the numbers on the device in a signed variable.

These don’t transfer so well, depending on how you decode them at the other end.

See Working with Bytes | The Things Network

The trick is to add an offset (say 100) to the values then subtract that offset at the decoder.

What code are you running? Probably we can have a look?

now i try , thank you , i’ll update u.

code was incomplete, excuse me , i had repost in the new comment

I can’t quite see where you build a payload and get it sent - is there more code?

Can you show us the payload that does arrive on TTS.

1 Like

< code deleted for privacy >

and this is the payload sent to TTS :

010BD811F8000187580001BF42000000000000000000510514057805DC

@projectsbminfo

Can you use
```c++
before you copy your code and
```
after your code. It makes your code much better to read.

Or select your code and use the
icon to convert it to code block.
image

Thank you.

1 Like

< code deleted for privacy >

Thank you.
How do you decode the data in the server? Can you send the decoding function as well?

< code deleted for privacy >

OK, so it’s not a problem before it even leaves the device.

Your topic title mentions the RAK1904 but there’s no include for the LIS3DH and I can see you’ve hard coded the values. So it must be something else causing the crash.

That said, accelerometer values are floats, so you will at some point either convert them in to something easier to put in to a payload.

But in the meanwhile, you may want to look at the Working with Bytes link as this:

//  uint8_t device_latitude_unit8[sizeof(float)];
//  uint8_t device_longitude_unit8[sizeof(float)];
//  

     //device_latitude = 14.764589 *1000000;
     //device_longitude = 37.234567 *1000000;
     device_latitude_unit8 = device_latitude;
     device_longitude_unit8 = device_longitude;
//    ::memcpy(device_latitude_unit8, &device_latitude);
//    ::memcpy(device_latitude_unit8, &device_longitude);

shows you appear to be stuck translating the lat & long floats in to something you can easily put in to the payload.

I you try to hardcode a negative value you can see the effects …

Which one - the position or the accelerometer?

excuse me for my english . The condition when the board is connected on serial port and of course Speaking about accelerometer value output , it’s can be positive or negative, on my device , if one of the three values go negative, the software crash. I’ve see this observing the serial monitor . Is possible see this effects hardcoding the values of accelerometer .

Not a problem, doesn’t matter which language, I still couldn’t figure out which value you were referring to.

You really really need to read the Working with Bytes link - it’s not possible to push floats in to integers and then send them as unsigned bytes.

At present you’ve got your x_axis defined as an uint16 when the LIS3DH library will return a float.

If you change your hard coded value of 13 * 100 to -13 * 100, bad things will happen to the data as the variable is unsigned.

There is some learning to do as much as bug fixing your code.

Here is some code to review:

  byte payload[10];

  float Temperature = -12.45;

  unsigned int TemperatureToSend = (Temperature + 50) * 100;

  // This will give a result of (-12.45 + 50) * 100 = 3755

  // By using an offset to 50, we can transmit readings as low as -50, you can adjust this value to suit your environment.

  // This can then go in to two bytes of a payload:

  payload[0] = highByte(TemperatureToSend); // this is an Arduino helper function for ((TemperatureToSend & 0xFF00) >> 8)
  payload[1] = lowByte(TemperatureToSend);  // this is an Arduino helper function for (TemperatureToSend & 0xFF)


  /*

  At the decoder end you'd convert this back with ...


  Javascript:

  var asInt = (payload[0] << 8) + payload[1];
  var Temperature = (asInt - 50) / 100;


  Python:

  asInt = (bytes_data[0] << 8) + bytes_data[1]
  Temperature = (asInt - 50) / 100

  */

thank you very much for the help , now i try and i’ll post the ( successiful i hope ) result .

greetings Gianmario

Prego!

It’s often easier to have several sketches that do just one thing and then add one in sketch in to the master at a time.

1 Like

we ( we are programming in 2 person ) arrived to the code that i had post starting from examples code … but payload we found some trouble on the way … as this one of the accelerometer