arduinoarduino-mkr1000

Arduino SigFox temperature data Problems


Can someone help me with this Sigfox setup? What have I done wrong? :-)

The only thing I wanted to achieve was to post the internal temperature and the status to the sigfox backend. Each 15min, the data would be posted. A configured an email from the service showing the temperature in degrees Celcius.

Posting to the backend is working. However the email message and the data seems not to correspond. When running the code in debug mode. The temperature is showing correctly in degrees Celcius.

Is there a manual available?

Code Arduino MKR FOX 1200

Temp.ino

#include <ArduinoLowPower.h>
#include <SigFox.h>
#include "conversions.h"

// Set oneshot to false to trigger continuous mode when you finisched setting up the whole flow
int oneshot = false;


#define STATUS_OK     0

/*
    ATTENTION - the structure we are going to send MUST
    be declared "packed" otherwise we'll get padding mismatch
    on the sent data - see http://www.catb.org/esr/structure-packing/#_structure_alignment_and_padding
    for more details
*/
typedef struct __attribute__ ((packed)) sigfox_message {
  int16_t moduleTemperature;
  uint8_t lastMessageStatus;
} SigfoxMessage;

// stub for message which will be sent
SigfoxMessage msg;

void setup() {

  if (oneshot == true) {
    // Wait for the serial
    Serial.begin(115200);
    while (!Serial) {}
  }

  if (!SigFox.begin()) {
    // Something is really wrong, try rebooting
    // Reboot is useful if we are powering the board using an unreliable power source
    // (eg. solar panels or other energy harvesting methods)
    reboot();
  }

  //Send module to standby until we need to send a message
  SigFox.end();

  if (oneshot == true) {
    // Enable debug prints and LED indication if we are testing
    SigFox.debug();
  }

 }

void loop() {
  // Every 15 minutes, read all the sensor and send the data
  // Let's try to optimize the data format
  // Only use floats as intermediate representaion, don't send them directly

  //sensors_event_t event;

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

  // We can only read the module temperature before SigFox.end()
  float temperature = SigFox.internalTemperature();
  msg.moduleTemperature = convertoFloatToInt16(temperature, 60, -60);

  if (oneshot == true) {
    Serial.println("Internal temp: " + String(temperature));
  }

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

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

  msg.lastMessageStatus = SigFox.endPacket();

  if (oneshot == true) {
    Serial.println("Status: " + String(msg.lastMessageStatus));
  }

  SigFox.end();

  if (oneshot == true) {
    // spin forever, so we can test that the backend is behaving correctly
    while (1) {}
  }

  //Sleep for 15 minutes
  LowPower.sleep(1 * 60 * 1000);
}

void reboot() {
  NVIC_SystemReset();
  while (1);
}

Conversion.h

#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);
}

Settings Sigfox Backend - email Callbacks enter image description here


Solution

  • With the current convertoFloatToInt16, it will not be possible to display the real temperature in an email directly from Sigfox Backend.

    You either need to use a callback from Sigfox Backend to a server that implements convertoFloatToUInt16 and then sends the email, or send the temperature in a format that the Sigfox Backend can decode natively (32-bit float is probably the most suitable).