MKRWAN.h sending a byte to TTS

I‘m using the Arduino MKR WAN 1310 to send messages to The Things Stack. The examples of the MKRWAN.h library are referring only to sending strings. This works fine. I’d like to send a byte though, so I changed the code slightly.

  // Define an array with a byte
  uint8_t msg[1];
  byte var = 143;
  msg[0] = var;


  // Send the message
  int err;
  modem.beginPacket();
  modem.print(msg[0]);
  err = modem.endPacket(true);

  // Check if the message has been sent correctly
  if (err > 0) {
    Serial.println("Message sent correctly!");
  } else {
    Serial.println("Error sending message :(");
  }

The message is being send correctly to the TTS, but as it seems as a string. In TTS the string formater gives me the correct result of “143”.

function Decoder(bytes, port) {
 var result = ""; 
 for (var i = 0; i < bytes.length; i++) { 
   result += String.fromCharCode(parseInt(bytes[i])); 
 } 
 return { payload: result, };
}

A formater for bytes gives me not the correct result of 143 but of 49.

function Decoder(bytes, port) {
	var decoded_var = bytes[0];
	
	return {
	var: decoded_var
	}
}

Is the MKRWAN.h designed only to send strings to the TTS?

Hi,
this is how it works for me:

I use this code to send with my mkrwan1310:

  // Read sensor values and multiply by 100 to effectively keep 2 decimals
  // Unsigned 16 bits integer, 0 up to 65535
  uint16_t e = i_entfernung;
  uint16_t v = voltage * 100;

  byte buffer[4];
  buffer[0] = e>> 8;
  buffer[1] = e;
  buffer[2] = v >> 8;
  buffer[3] = v;

  int err;
  modem.beginPacket();
  modem.write(buffer,4);
  err = modem.endPacket(false);
  if (err > 0) {
    Serial.println("Message sent correctly!");
  } else {
    Serial.println("Error sending message :(");
  }

And this is my payload decoder:

function decodeUplink(input) {
  var data = {};

  data.distance_cm = ((input.bytes[0] << 8) + input.bytes[1]);
  data.battery_volt = ((input.bytes[2] << 8) + input.bytes[3]) / 100;
  var warnings = [];

  return {
    data: data,
    warnings: warnings
  };
}

Hope this can help…

This may help with the fundamentals:

https://www.thethingsnetwork.org/docs/devices/bytes/

(it’s OK to ignore the pink warning, the info is all generic / theory, not related to any particular version of TTN/TTS)

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