Networking

sendData([string|array], [CRITICAL | DEFAULT | LOW_POWER] )

Example:

void sendReading() {
    // You can send a JSON object as string
    sendData(JSON.stringify({
        key: "value"
    }));
    // Or you can send a plain string
    sendData("hello world");
    // Or you can send an array of bytes
    sendData([0x10, 12, 0x4F]);
    // Or a mixed array of strings, bytes or nested arrays
    sendData([
        "one", [0x10, 0x20, 0x30],
        "two", [0x40, 0x50, "x"]
    ]);
}

These functions provide a handy abstraction to the data uplink of the device.

The sendData function will schedule a transmission of a raw string using either of the available uplink modules. The optional second argument controls the transmission strategy to use:

  • CRITICAL denotes that the message contains critical information and must be sent as quickly as possible. Critical messages never expire and all the available uplinks will be considered. So be cautious when using this argument in low-power environments.

  • LOW_POWER denotes that the message should be sent using as little energy as possible. Low power messages might have bigger transmission latency, but are guaranteed to use only the low-power uplinks. If a low-power message cannot be transmitted with any of the low-power uplinks it will be discarded.

  • DEFAULT strategy (selected by default if missing) is a balance between critical and low-power strategies. All the uplinks will be considered, starting with the ones with the smallest possible power consumption and continuing with the more power-hungry until it’s transmitted. If it cannot be transmitted, the message will be discarded.

sampleAllSensors()
sampleAdd(sensor, { key: value })
samplesFlush()

Example:

function setup() {
  // When started, ask all sensors to commit their
  // findings to the SensorHub
  sampleAllSensors();
}

// SensorHub will call-out this function when the module should
// provide measurements.
onEvent("sensors.sample", function () {
  // Add a sample on the "battery" sensor with the given values
  sampleAdd("battery", {
    soc: getBatterySoC(),
    v: getBatteryVoltage(),
  });

  // Let other modules collect sensor values and commit
  // the data after 100 milliseconds.
  setTimeout(function () {
    samplesFlush();
  }, 100);
});

The addSample function adds one or more measurement values for a given sensor to the system. You can call this function as many times as needed and it will always replace the previous values with the new ones.

Important: No data will be sent unless you call samplesFlush!

The sampleAllSensors function will ask all modules registered in the SensorHub (including the javascript run-time) to take a sample and commit their findings to the hub.

The samplesFlush function summarizes the current values from all sensors in the SensorHub and sends them via the uplink path.