# MKRWAN.h sending a byte to TTS

**URL:** <https://www.thethingsnetwork.org/forum/t/mkrwan-h-sending-a-byte-to-tts/55282>\
**Category:** End Devices (Nodes)\
**Created:** [February 17, 2022, 7:54am UTC](https://www.thethingsnetwork.org/forum/t/mkrwan-h-sending-a-byte-to-tts/55282 "2022-02-17T07:54:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonLat](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/ronlat/32/35697_2.png) [@RonLat](https://www.thethingsnetwork.org/forum/u/RonLat)\
**Post date:** [February 17, 2022, 7:54am UTC](https://www.thethingsnetwork.org/forum/t/mkrwan-h-sending-a-byte-to-tts/55282/1 "2022-02-17T07:54:57Z")

</div>

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.

```auto
  // 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”.

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![dreimal8](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/dreimal8/32/35697_2.png) [@dreimal8](https://www.thethingsnetwork.org/forum/u/dreimal8)\
**Post date:** [February 17, 2022, 10:28am UTC](https://www.thethingsnetwork.org/forum/t/mkrwan-h-sending-a-byte-to-tts/55282/2 "2022-02-17T10:28:26Z")

</div>

Hi,  
this is how it works for me:

I use this code to send with my mkrwan1310:

```auto
  // 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:

```auto
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…

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [February 17, 2022, 10:36am UTC](https://www.thethingsnetwork.org/forum/t/mkrwan-h-sending-a-byte-to-tts/55282/3 "2022-02-17T10:36:39Z")

</div>

This may help with the fundamentals:

[https://www.thethingsnetwork.org/docs/devices/bytes/](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)

---

<div class="post-metadata">

**Author:** ![system](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/6/2/6250ea44fd2b0d6259489e1a92748df681e1f970.png) [@system](https://www.thethingsnetwork.org/forum/u/system)\
**Post date:** [February 18, 2022, 3:04pm UTC](https://www.thethingsnetwork.org/forum/t/mkrwan-h-sending-a-byte-to-tts/55282/4 "2022-02-18T15:04:16Z")

</div>

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