byte to hex for Sigfox message - LPWAN

Hi,
I have problem with correct transfer of message (12 bytes) from Arduino to Sigfox LPWAN node + CALLBACK to webpage.

I would like to send 12 different values from 12 sensors. Each value is a byte type in Arduino. Then I create a message for Sigfox LPWAN module which can be up to 12 bytes. According to documentation the message has to be in HEX format. Then I have a CALLBACK created in Sigfox back end.

I tested many options already but still not doing what I am expecting :smiley:

I think there is problem with correct representation of the message in HEX format. My last try is as following in Arduino code:

  byte dataArray[12];

  Sigfox.print("AT$SF=");
  Sigfox.print(dataArray[0], HEX);
  Sigfox.print(dataArray[1], HEX);
  Sigfox.print(dataArray[2], HEX);
  Sigfox.print(dataArray[3], HEX);
  Sigfox.print(dataArray[4], HEX);
  Sigfox.print(dataArray[5], HEX);
  Sigfox.print(dataArray[6], HEX);
  Sigfox.print(dataArray[7], HEX);
  Sigfox.print(dataArray[8], HEX);
  Sigfox.print(dataArray[9], HEX);
  Sigfox.print(dataArray[10], HEX);
  Sigfox.print(dataArray[11], HEX);
  Sigfox.print('\r');

Custom payload definition in Sigfox backend:

var1::uint:8 var2::uint:8 var3::uint:8 var4::uint:8 var5::uint:8 var6::uint:8 var7::uint:8 var8::uint:8 var9::uint:8 var10::uint:8 var11::uint:8 var12::uint:8

For now I think the problem is that the numbers are sent in HEX but in case that the number is not starting with 1 in the bit form, then there are missing the spaces in the final message. So I think there is required some kind of bitshifting or something like this. When I tested to send mumber 255 on all positions then I get correct CALLBACK response on the Sigfox backend. But when there are smaller numbers which do not start with 1 in bit representation of the number, then the CALLBACK do not correctly read the numbers and display "null" in the custom payloads variables. I think it must be caused by the representaton of the HEX numbers. But I do not know how to solve it. Maybe there is more simple way how to send the Sigfox message in HEX and I am going wrong direction :slight_smile: So any advice will be appreciated.

Thank you in advance.

It may be that printing HEX only prints one digit for values 0..9, where you probably need e.g. "01"

try this:

// toHex
// - convert value to two hex characters
// - the usual Arduino functions output e.g. "0" instead of "00"
//
char *toHex( uint8_t c )
{
  const static char *hex="0123456789ABCDEF";
  static char toHexBuf[3];
  toHexBuf[0]= hex[c>>4];
  toHexBuf[1]= hex[c & 0x0f];
  toHexBuf[2]=0;
  return toHexBuf;
}

and do....

Sigfox.print( toHex(dataArray[0]) );

Yours,
TonyWilk

In my view easiest to use the sprintf() function; e.g.

uint8_t dataArray[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12};

void setup() {
  Serial.begin(115200);

  char outputBuffer[25]; // 2*data length + 1 for terminating null
  sprintf(outputBuffer, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
          dataArray[0], dataArray[1], dataArray[2], dataArray[3],
          dataArray[4], dataArray[5], dataArray[6], dataArray[7],
          dataArray[8], dataArray[9], dataArray[10], dataArray[11]);
  Serial.println(outputBuffer);
}
void loop() {
 }

edit:
or

uint8_t dataArray[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12};

void setup() {
  Serial.begin(115200);

  char outputBuffer[25]; // 2*data length + 1 for terminating null

  for (int i = 0; i < sizeof(dataArray); i++)
       sprintf(outputBuffer + i * 2, "%02x",
               dataArray[i]);
       Serial.println(outputBuffer);
}
void loop() {
}
[code]

Hi,

thanks both of you for your fast reply. I really appreciate your help as I spend already quite long time on this problem :slight_smile:

Solution from TonyWilk - works on the first try as a charm. Thanks a lot for this solution. I just have to try to read it couple of times to correctly understand the code. If you have some hints regarding to it, would be appreciated. But anyway I will try to get info from arduino sources. One more thanks for your help.

Solution from Stowite - I tried the first one and got strange result on the Sigfox reply and did not get the data in the Sigfox backend. Anyway maybe the problem is caused by something between the keyboard and the chair :slight_smile: so I am not saying that it is incorrect. By the way I already tried the sprintf function before but with incorrect form I see as I used %01x which was mistake and instead of char I used String as the final result. It was not working for me anyway :frowning:

One more thanks to both of you for your fast help.

boylucky:
Solution from TonyWilk - works on the first try as a charm. Thanks a lot for this solution. I just have to try to read it couple of times to correctly understand the code. If you have some hints regarding to it, would be appreciated. But anyway I will try to get info from arduino sources. One more thanks for your help.

Great! ... some hints...

// toHex
// - convert value to two hex characters
// - the usual Arduino functions output e.g. "0" instead of "00"
//
char *toHex( uint8_t c )
{
  const static char *hex="0123456789ABCDEF";
  static char toHexBuf[3];
  toHexBuf[0]= hex[c>>4];
  toHexBuf[1]= hex[c & 0x0f];
  toHexBuf[2]=0;
  return toHexBuf;
}

toHex() is passed a character (which is an 8-bit value) and returns a string containing two HEX chartacters and a '\0' zero terminator - that's a standard C string (not a C++ String).

toHexBuf[0]= hex[c>>4]; takes the character 'c', shifts the upper 4 bits down and uses that to index the hex array to get the first HEX character.
toHexBuf[1]= hex[c & 0x0f]; ANDs the character with 0x0f to keep only the lower 4 bits, indexex hex and gets the second HEX character.

Hope that makes sense :slight_smile:

Yours,
TonyWilk

Thanks a lot for your hints. Now it is a bit more clear for me. Perfect and simple solution.