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);
}