Skip to content

Instantly share code, notes, and snippets.

@martinschierle
Created May 29, 2021 15:29
Show Gist options
  • Save martinschierle/45d06d877dcdcb157192e890f3344cc6 to your computer and use it in GitHub Desktop.
Save martinschierle/45d06d877dcdcb157192e890f3344cc6 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include "SSD1306.h"
#include "SPIFFS.h"
#define SCK 5 // GPIO5 -- SX1278's SCK
#define MISO 19 // GPIO19 -- SX1278's MISO
#define MOSI 27 // GPIO27 -- SX1278's MOSI
#define SS 18 // GPIO18 -- SX1278's CS
#define RST 14 // GPIO14 -- SX1278's RESET
#define DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND 868E6
SSD1306 display(0x3c, 21, 22);
String rssi = "RSSI --";
String packSize = "--";
String packet ;
File file;
void loraData(){
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0 , 15 , "Received "+ packSize + " bytes");
display.drawStringMaxWidth(0 , 26 , 128, packet);
display.drawString(0, 0, rssi);
display.display();
Serial.println(rssi);
if (file.print(packet + "\n")) {
Serial.println("File was written");
} else {
Serial.println("File write failed");
}
}
void cbk(int packetSize) {
packet ="";
packSize = String(packetSize,DEC);
for (int i = 0; i < packetSize; i++) { packet += (char) LoRa.read(); }
rssi = "RSSI " + String(LoRa.packetRssi(), DEC) ;
loraData();
}
void setup() {
pinMode(16,OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high、
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.println("LoRa Receiver Callback");
SPI.begin(SCK,MISO,MOSI,SS);
LoRa.setPins(SS,RST,DI0);
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setSyncWord(0xB5);
//LoRa.onReceive(cbk);
LoRa.receive();
Serial.println("init ok");
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
file = SPIFFS.open("/loradata.txt", FILE_WRITE);
if (!file) {
Serial.println("There was an error opening the file for writing");
return;
}
delay(1500);
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) { cbk(packetSize); }
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment