Envío y recepción básicos en LoRa
Se trata de enviar del emisor al receptor el tiempo que transcurre en el emisor a través de un contador, también se hace uso de la pantalla OLED. El receptor además envía el registro a puerto serie.
Programa emisor de tiempo:
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23
#define DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 866E6
//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//contador de tiempo
int counter=0;
//creacion de pantalla (objeto display)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
//funciones
void envia_lora(){//Send LoRa packet to receiver
LoRa.beginPacket();
LoRa.print(counter);
LoRa.endPacket();
}
void envia_oled(){
display.clearDisplay();
display.setCursor(0,0);
display.println("Club Robotica Granada");
display.setCursor(0,20);
display.setTextSize(1);
display.print("Paquete LoRa enviado.");
display.setCursor(0,30);
display.print("Contador: ");
display.setCursor(60,30);
display.print(counter);
display.display();
}
void setup() {
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("LORA SENDER ");
display.display();
/*initialize Serial Monitor
Serial.begin(115200);
Serial.println("Prueba de envio LoRa");*/
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
display.setCursor(0,10);
display.print("Starting LoRa failed!");
display.display();
//Serial.println("Starting LoRa failed!");
while (1);
}
//Serial.println("LoRa Inicializacion OK!");
display.setCursor(0,10);
display.print("Inicializacion OK!");
display.display();
delay(2000);
}
void loop() {
envia_lora();
envia_oled();
counter++;
delay(1000);
}
Programa receptor:
Este programa será el que proponemos definitivo para recepción de datos en PC.
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23
#define DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 866E6
//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
String LoRaData;
void setup() {
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("LORA RECEIVER ");
display.display();
//initialize Serial Monitor
Serial.begin(115200);
Serial.println("LoRa Receiver Test");
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1)
;
}
Serial.println("LoRa Initializing OK!");
display.setCursor(0, 10);
display.println("LoRa Inicializacion OK!");
display.display();
}
void loop() {
//try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
//received a packet
//Serial.print("Received packet ");
//read packet
while (LoRa.available()) {
LoRaData = LoRa.readString();
Serial.print(LoRaData);
Serial.print("\r\n");
}
//print RSSI of packet
int rssi = LoRa.packetRssi();
//Serial.print(" with RSSI ");
//Serial.println(rssi);
// Display information
display.clearDisplay();
display.setCursor(0, 0);
display.print("CANSAT CRG");
display.setCursor(0, 10);
display.print("Mensaje recibido:");
display.setCursor(0, 20);
display.print(LoRaData);
display.setCursor(0, 50);
display.print("RSSI:");
display.setCursor(30, 50);
display.print(rssi);
display.display();
}
}