mkrfox 1200 problems receiving ravdata sigfox backend

I'm having trouble sending data to sigfox backend.

Here is ravdata from backend

07d007000003000400050006

If I sort it in byte
07 d0070000 0300 0400 0500 06
07 is OK
2000 decimal is 07D0 in hex

I sending 7 2000 3 4 5 6

Why is the variable bigger than 2Byte switch around ?

Here is some of my arduino code

typedef struct __attribute__ ((packed)) sigfox_message {
  uint8_t mode;
  int32_t kg;
  uint16_t moduleTemperature;
  uint16_t Temperature;
  uint16_t bat;
  uint8_t ekstra;
} SigfoxMessage;

SigfoxMessage msg;

And here I insert data

msg.mode = 7; 
msg.kg = 2000; 
msg.moduleTemperature = 3;
msg.Temperature = 4; 
msg.bat = 5;   
msg.ekstra = 6;

Here is my send code

 // Start the module
  SigFox.begin();
  // Wait at least 30ms after first configuration (100ms before)
  delay(100);

  // Clears all pending interrupts
  SigFox.status();
  delay(1);

  SigFox.beginPacket();
  SigFox.write((uint8_t*)&msg, sizeof(msg));
  int ret =  SigFox.endPacket(); // if (ret == 0) = OK
  Serial.println("Status: " + String(ret));
  SigFox.end();

The problem is little endian

I solved it with 2 functions which swapper bytes before they are sent so they fit in the backend

#if !defined(ntohl)
uint32_t ntohl(uint32_t n)
{
    return ((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) | ((n & 0xFF000000) >> 24);
}
#endif

#if !defined(htons)
uint16_t htons(uint16_t a)
{
return (a << 8) | (a >> 8);
}
#endif
 msg.mode = 7;
 msg.kg =  ntohl(2000);
 msg.moduleTemperature = htons(3);
 msg.beeTemperature = htons(4);
 msg.bat = htons(5);
 msg.ekstra = 6;

And output from backend

07000007d000030004000506

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.