Need help ASAP: MKR WAN 1300 not sending proper data to TTN

I have been having a problem with trying to send data to the LoRa network using the MKR WAN 1300. The code is:

#include <MKRWAN.h>
#include "arduino_secrets.h"
LoRaModem modem;

bool pump_running = false;
int water_level = 0;
const int empty_level = 0;
const int full_level = 5;

void setup() {
  while (!Serial);
  if (!modem.begin(EU868)){
    Serial.println("Failed to start module");
    while (1){}
  Serial.print("Your device EUI is: ");

  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected){
    Serial.println("Something went wrong! Retry");
    while (1){}
  Serial.println("Successfully joined the network!");
  pinMode(13,OUTPUT); //Set pin 13 as OUTPUT pin, to send signal to relay
void loop() {
  // put your main code here, to run repeatedly:
  for (int i =0;i<10;i++){ /*10 * 1 second delay*/
void adjust_water_level(){
  if (pump_running){
    if (water_level < full_level){ /*Can't be overfilled*/
       water_level += 1; /*Water level rises*/
  else {
    if (water_level > empty_level){ /*Can't be less than empty*/
      water_level -= 1; /*Water level drops*/

void send_level(){
  int err;
  Serial.print("Sending water level: ");
  err = modem.endPacket(true);
  if (err > 0) {
    Serial.println("Message sent correctly!");
  else {
    Serial.println("Error sending message :(");

void check_downlink_messages(){
  if (!modem.available()) { /*If there was no message, get out of function*/
    Serial.println("No downlink message received at this time.");
  else { /*If there is a message*/
    Serial.print("Message Received: ");
    if (!= 0){ /*The first byte is non-zero*/
      Serial.println("Starting the pump");
      pump_running = true;
      digitalWrite(13,HIGH); //if pump is not running, send HIGH value to relay
      Serial.println("Stopping the pump");
      pump_running = false;
      digitalWrite(13,LOW); //if pump is not running, send LOW value to relay

The message gets uploaded to the TTN, however I get Payload: {ledState: null} 02 00 00 00 when I send an integer with a value of 2 to the network using the modem.write() function. I did this just for testing purposes. Later I used a uplink simulation to see if perhaps I used the wrong payload formatting, however that is not the case. I should be getting a Payload similar to the simulation, that being Payload: {port: 4, water_level=2).

Note that I am completely new to both arduino and lora, and this is a simple school project with a deadline, so I would appreciate if someone could help me out ASAP. After TTN v3 came out I am not sure if any older online resources are helpful anymore and I desperately need help.

It is quite hard to read code like this. Could you use code tags and proper indentation?

I guess the sketch has a delay() function issue too.

Sorry for the inconvenience, I am also new to this forum. The post is updated and is using code tags.

Hi, thank you for the reply. Which delay() functions should I edit/delete?

Ask yourself which one are needed to reach the desired funktionality of the sketch.

Line 34:   for (int i =0;i<10;i++){ /*10 * 1 second delay*/
	Line 36:     delay(1000);
	Line 67:   delay(1000 * 60);
	Line 89:   delay(400);

Yes, I can see it too now, and it looks like this is the biggest offender:

It looks like the OP is trying to invoke send_level() once a minute, but this approach just freezes the uC minute after minute so, @clyro, try to implement a non-blocking timed event based on the millis counter.

I have just tested the newly updated code where I removed some of the delay() functions, however I was still not able to fix my main issue. The decoded_payload still only contains ledState, while I am sending an int with the modem.write() function.

Perhaps my Uplink payload formatter code is not correct.

Be aware that your questions are not about LoRa as such, which is a low level radio protocol, but about an application called TTN\LoRaWAN that just happens to use LoRa in the background.

There is a forum which specifically supports the TTN application you are using;

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