Go Down

Topic: Cannot decode message (Read 1 time) previous topic - next topic

BigMike81

Oct 30, 2019, 10:21 pm Last Edit: Oct 31, 2019, 04:51 pm by BigMike81
Hello,
I just cannot decode my sensors data...

Here are relevant parts of my code:

Code: [Select]

typedef struct __attribute__ ((packed)) sigfox_message {
  int8_t moduleTemperature;
  int16_t mpcTemperature;
  uint16_t bmePressure;
  uint16_t bmeHumidity;
} SigfoxMessage;

##################################################

  float pressure = bme.readPressure();
  msg.bmePressure = convertoFloatToUInt16(pressure, 200000);
  
  float humidity = bme.readHumidity();
  msg.bmeHumidity = convertoFloatToUInt16(humidity, 110);  
  
  tempsensor.wake();
  float outside_temperature = tempsensor.readTempC();
  msg.mpcTemperature = convertoFloatToInt16(outside_temperature, 60, -60);  

  SigFox.begin();
  float module_temperature = SigFox.internalTemperature();
  msg.moduleTemperature = convertoFloatToInt16(module_temperature, 60, -60);


  SigFox.status();
  delay(1);

  SigFox.beginPacket();
  SigFox.write((uint8_t*)&msg, 12);


Here is code that handle conversion:

Code: [Select]

#define UINT16_t_MAX  65536
#define INT16_t_MAX   UINT16_t_MAX/2

int16_t convertoFloatToInt16(float value, long max, long min) {
  float conversionFactor = (float) (INT16_t_MAX) / (float)(max - min);
  return (int16_t)(value * conversionFactor);
}

uint16_t convertoFloatToUInt16(float value, long max, long min = 0) {
  float conversionFactor = (float) (UINT16_t_MAX) / (float)(max - min);
  return (uint16_t)(value * conversionFactor);
}


Here is custom grammar:

Code: [Select]

Temperature_Int::int:8 Temperature_Ext::int:16 Pressure::uint:16 Humidity::uint:16


Raw and Decoded message is:

Code: [Select]

44221a6f805d7600dc030020
Temperature_Int: 68
Temperature_Ext: 8730
Pressure: 28544
Humidity: 23926




When I print float versions of sensors data to serial monitor, values are correct. What did I wrong?

Thank you.

BigMike81

Got it! I just have to divide my values with conversionFactor. DUH!

Go Up