Transmission of High and Low Bit between 2 Arduinos

I am using 2 Arduino MKR WAN 1310. I want to build a traffic light control. For this, the Arduino transmitter receives a high or low signal via an interface, which it should send to the Arduino receiver with LoRa. The receiver is supposed to switch the traffic light to green or red based on this signal. For testing purposes, I give the transmitter a 1 and a 0 directly. At the end of each message, I put a "!" because I transmit it as a string and use that as a signal character to be able to split the message again in the receiver code. However, my receiver does not manage to receive the message and it returns "Test1", "Test2", "Test11" and "Low". So it does not go into the "if(LoRa.available())". As this is my first project with an Arduino and LoRa, I would like to ask you to tell me if it is more the hardware or if I have made a mistake in my code.
Sorry about all the Serial.println, but I put that in to see what point in the code it gets to.
Thanks a lot

Code transmitter

#include <SPI.h>
#include <LoRa.h>

int Pin15 = 15;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("LoRa Sender");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {  
  //Test
  int valueToSend = 0;
  String message = String(valueToSend);
  message += "!";
  //send packet
  LoRa.beginPacket();
  LoRa.print(message);
  LoRa.endPacket();
  delay(2000);
  Serial.println(message);

  //Test
  valueToSend = 1;
  message = String(valueToSend);
  message += "!";
  //send packet
  LoRa.beginPacket();
  LoRa.print(message);
  LoRa.endPacket();
  delay(2000);
  Serial.println(message);
}

Code receiver

#include <SPI.h>
#include <LoRa.h>

int PinRot = 16;
int PinGruen = 17;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.println("Test1");
  String receivedMessage = "";
  Serial.println("Test2");
  int receivedValue = 0;

  if(LoRa.available()){
    Serial.println("Test3");
    receivedMessage = LoRa.readString();
    Serial.println("Test4");
    int separatorIndex = receivedMessage.indexOf("!");
    Serial.println("Test5");
    if(separatorIndex >= 0){
      Serial.println("Test6");
      String valueString = receivedMessage.substring(0, separatorIndex);
      Serial.println("Test7");
      receivedValue = valueString.toInt();
      Serial.println("Test8");
      Serial.println(String(receivedValue));
      Serial.println("Test9");
    }else {
      Serial.println("Test10");
      Serial.println("Error!");
    }
  }

  int rssi = LoRa.packetRssi();
  Serial.print("Signal Strength: ");
  Serial.println(rssi);
  
  if (receivedValue == 0){
    Serial.println("Test11");
    Serial.println("Low");
  }else{
    Serial.println("Test12");
    Serial.println("High");
  }
  //delay(2000);
}

TX > if (!LoRa.begin(433E6))
RX > if (!LoRa.begin(868E6))

Ooops!

consider
should only process input when available, not unconditionally
(i set "LoRa" to "Serial" to tested thru serial monitor

#define MyHW
#ifdef MyHW
# include "sim.hh"
#define LoRa    Serial

#else
# include <SPI.h>
# include <LoRa.h>
#endif

int PinRot = 16;
int PinGruen = 17;

void setup () {
    Serial.begin (9600);
    while (!Serial);

    Serial.println ("LoRa Receiver");

#ifndef MyHW
    if (!LoRa.begin (868E6)) {
        Serial.println ("Starting LoRa failed!");
        while (1);
    }
#endif
}


void loop () {
#if 0
    Serial.println ("Test1");
    Serial.println ("Test2");
#endif

    if (LoRa.available ())  {
        int val = LoRa.readString ().toInt ();

        switch (val)  {
        case 0:
            Serial.println ("Low");
            break;
        case 1:
            Serial.println ("High");
            break;

        default:
            Serial.println (val);
            break;
        }
    }
}

Thank you, I have corrected it. But unfortunately it still doesn't work.

We can't see your corrections

Hi,
Check the Lora modules and see what frequency band they are set to operate at then set your

LoRa.begin (868E6)

to that frequency band.

Have you tried the send and receive examples that are in the LoRa Examples tab?
If not then try them, unedited except for frequency band before trying your code.

Tom.. :smiley: :+1: :coffee: :australia:

Thanks to everyone who commented on this post. I have been able to solve the problem. I was quite code blind and made it more and more complicated. I could have solved it quite easily.
here again the code for sender and receiver, in case someone is working on the same issue:
Sender:

#include <SPI.h>
#include <LoRa.h>

byte counter = 0;
byte counter2 = 1;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Sender");

  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.println(counter);

  // send packet
  LoRa.beginPacket();
  LoRa.write(counter);
  LoRa.endPacket();

  delay(5000);

  Serial.println(counter2);
  LoRa.beginPacket();
  LoRa.write(counter2);
  LoRa.endPacket();

  delay(5000);
}

Receiver:

#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    //Serial.print("Received packet '");

    // read packet

    while (LoRa.available()) {
      int var = LoRa.read();
      Serial.println(var);
      if (var == 0) {
        Serial.println("Low");
      }else{
        Serial.println("High");
      }
    }
  }
}