Skip to content

5. Let's code!

FelipeIslas edited this page Aug 19, 2020 · 1 revision

Hello World in LoRa:

Below you will find the code for 2 sketches. The first is for the receiver, and the second is the sender. BastWAN uses Sandeep Mistryʼs library (you could probably use others, but the examples are based on this one). Make sure you have it before starting to play with your new toy…

Receiver

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

void setup() {
  Serial.begin(9600);
  pinMode(RFM_SWITCH, OUTPUT);
  digitalWrite(RFM_SWITCH, 1);
  Serial.println("LoRa Receiver");
  LoRa.setPins(SS, RFM_RST, RFM_DIO0);
  if (!LoRa.begin(915E6)) {
    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()) {
      Serial.print((char)LoRa.read());
    }
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

Sender

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

int counter = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("LoRa Sender");
  pinMode(RFM_TCXO, OUTPUT);
  pinMode(RFM_SWITCH, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  LoRa.setPins(SS, RFM_RST, RFM_DIO0);
  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);
  digitalWrite(LED_BUILTIN, 1);
  // send packet
  LoRa.beginPacket();
  digitalWrite(RFM_SWITCH, 0);
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();
  counter++;
  delay(500);
  digitalWrite(LED_BUILTIN, 0);
  delay(500);
}

In both examples the variables RFM_SWITCH, RFM_RST, RFM_DIO0, * and *SS are declared in the board definitions, so you don't need to use the pin numbers.

Hello World in LoRaWAN:

Below you will find an example using the BastWAN as a LoRaWAN Class A device with ABP activation. This is just an example but in order to use it correctly, you will need to read the documentation of the library, because you will depend on the configurations of your LoRaWAN gateway and you need to configure it correctly in order to get this working. You will need to put your LoRaWAN credentials to get access to your LoRaWAN server

In the examples the variables SS, RFM_SWITCH, RFM_RST, RFM_DIO0, RFM_DIO1 RFM_DIO2, RFM_DIO3, RFM_DIO4, RFM_DIO5 are declared in the board definitions, so you don't need to use the pin numbers.

/**
* Example of ABP device
* Authors:
* Ivan Moreno
* Eduardo Contreras
* June 2019
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*/

#include <lorawan.h>

//ABP Credentials
const char *devAddr = "00000000";
const char *nwkSKey = "00000000000000000000000000000000";
const char *appSKey = "00000000000000000000000000000000";
const unsigned long interval = 10000; // 10 s interval to send message
unsigned long previousMillis = 0; // will store last time message sent
unsigned int counter = 0; // message counter
char myStr[50];
char outStr[255];
byte recvStatus = 0;

const sRFM_pins RFM_pins = {
.CS = SS,
.RST = RFM_RST,
.DIO0 = RFM_DIO0,
.DIO1 = RFM_DIO1,
.DIO2 = RFM_DIO2,
.DIO5 = RFM_DIO5,
};

void setup() {
  // Set up LoRaWAN access
  Serial.begin(115200);
  delay(2000);
  if(!lora.init()){
    Serial.println("RFM95 not detected");
    delay(5000);
    return;
  }
  // Set LoRaWAN Class change CLASS_A or CLASS_C
  lora.setDeviceClass(CLASS_A);
  // Set Data Rate
  lora.setDataRate(SF8BW125);
  // set channel to random
  lora.setChannel(MULTI);
  // Put ABP Key and DevAddress here
  lora.setNwkSKey(nwkSKey);
  lora.setAppSKey(appSKey);
  lora.setDevAddr(devAddr);
}

void loop() {
  // Check interval overflow
  if(millis() - previousMillis > interval) {
    previousMillis = millis();
    sprintf(myStr, "Counter-%d", counter);
    Serial.print("Sending: ");
    Serial.println(myStr);
    lora.sendUplink(myStr, strlen(myStr), 0);
    counter++;
  }
  recvStatus = lora.readData(outStr);
  if(recvStatus) {
    Serial.println(outStr);
  }
  // Check Lora RX
  lora.update();
}
Clone this wiki locally