Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The sender code sends a simple message, with a delay of 5 seconds. Make sure you change the contents of myData.

Code Block
languagecpp
#include <SPI.h>
#include <LoRa.h>

int counter = 0;
String myData = "my name";

void setup() {
  Serial.begin(9600);// wait until serial has started 
  Serial.println("LoRa Sender");
  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  delay(1000);
 pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);
  // send packet
  LoRa.beginPacket();
  LoRa.print(myData);
  LoRa.endPacket();
  counter++;
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                  
  digitalWrite(LED_BUILTIN, LOW);   
  delay(4000);   
}

Receiver Code

Code Block
languagecpp
#include <SPI.h>
#include <LoRa.h>

String contents = "";

void setup() {
  Serial.begin(9600);
  while (!Serial); // wait until serial has started 
  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()) {  
      contents += (char)LoRa.read();
    }
    Serial.println(contents);
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    contents = "";
  }
}

...

Sender Code with an Analog Sensor.

Code Block
languagecpp
#include <SPI.h>
#include <LoRa.h>

int counter = 0;
String myUniqueId = "asdfw1";
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("LoRa Sender");
  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while(1);
  }
 delay(1000);
 pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(A0);

  Serial.print("Sending packet: ");
  Serial.println(counter);
  // send packet
  LoRa.beginPacket();
  LoRa.print(sensorValue);
  LoRa.print(",");
  LoRa.print(myUniqueId);
  LoRa.endPacket();
  counter++;
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);  
  delay(4000);    
}

Receiver Code

Code Block
languagecpp
#include <SPI.h>
#include <LoRa.h>

String contents = "";
String myUniqueId = "asdfw1";

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

  Serial.println("LoRa Receiver");

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

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

    // read packet
    while (LoRa.available()) {
      contents += (char)LoRa.read();
    }
    Serial.println(contents);
    // print RSSI of packet (Received Signal Strength Indicator)
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    int commaIndex = contents.indexOf(',');
    String data = contents.substring(0, commaIndex);
    String ID = contents.substring(commaIndex + 1);

    if (data && ID) {
      Serial.println("Recieved Matching format");
      if (ID.equals(myUniqueId)) {
          Serial.println("ID matches!");
          digitalWrite(LED_BUILTIN, HIGH);
          Serial.print("data: ");
          Serial.println(data);
        } else {
          Serial.print(myUniqueId);
          Serial.print(" does not match ");
          Serial.println(ID);
          digitalWrite(LED_BUILTIN, LOW);
        }
    }
    contents = "";
  }
}

If you want to send more than one piece of data at a time, you will need to parse multiple comma-separated values. This code snippet below shows how this can be done using the substring() and indexOf() methods of the String object.

Code Block
languagecpp
   // This can get confusing as you send more data. It can be helpful to write out your expected string and note where the indexes are.    
   // data1,data2,uniqueId
   //      ↑     ↑
   //  IndexA  IndexB
    
    int IndexA = contents.indexOf(',');  //finds location of first item
    String data1 = contents.substring(0, IndexA);
    int IndexB = contents.indexOf(',', IndexA + 1 ); //finds location of 2nd item
    String data2 = contents.substring(IndexA + 1, IndexB);
    String ID = contents.substring(IndexB + 1);


    if (ID.equals(myUniqueId)) {
      Serial.println("ID matches!");
      digitalWrite(LED_BUILTIN, HIGH);
      Serial.print("data1: ");
      Serial.println(data1);
      Serial.print("data2: ");
      Serial.println(data2);
      Serial.println();
    }