Skip to content

Instantly share code, notes, and snippets.

@sarasantos
Last active June 4, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarasantos/9835bac061a4e5d4992edb897614ca7f to your computer and use it in GitHub Desktop.
Save sarasantos/9835bac061a4e5d4992edb897614ca7f to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
//define the pins used by the transceiver module
#define ss 5
#define rst 14
#define dio0 2
String Temperature;
String Pressure;
String Humidity;
String Altitude;
String readingID;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
int delayTime = 5000;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("LoRa Receiver");
display.display();
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");
//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);
//replace the LoRa.begin(---E-) argument with your location's frequency
//note: the frequency should match the sender's frequency
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
if (!LoRa.begin(866E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initializing OK!");
display.setCursor(0,10);
display.println("LoRa Initializing OK!");
display.display();
} //End void setup
void loop() {
//try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
//received a packet
Serial.print("Received packet ");
display.clearDisplay();
display.setCursor(0,0);
display.print("Received packet ");
display.display();
//read packet
while (LoRa.available()) {
String LoRaData = LoRa.readString();
Serial.println(LoRaData);
int pos1 = LoRaData.indexOf('/');
int pos2 = LoRaData.indexOf('&');
int pos3 = LoRaData.indexOf('#');
int pos4 = LoRaData.indexOf('%');
readingID = LoRaData.substring(0, pos1);
Temperature = LoRaData.substring(pos1 +1, pos2);
Humidity = LoRaData.substring(pos2+1, pos3);
Pressure = LoRaData.substring(pos3+1, pos4);
Altitude = LoRaData.substring(pos4+1, LoRaData.length());
Serial.println(readingID);
Serial.println(Temperature);
Serial.println(Humidity);
Serial.println(Pressure);
Serial.println(Altitude);
} //End while
//print RSSI of packet
int rssi = LoRa.packetRssi();
Serial.print(" with RSSI ");
Serial.println(rssi);
display.setCursor(0,20);
display.print("dB: ");
display.setCursor(30,20);
display.print(rssi);
display.display();
delay(delayTime);
//print SENSOR data
display.clearDisplay();
display.setCursor(0,0);
display.print("Reading ID = ");
display.print(readingID);
display.setCursor(0,10);
display.print("Temp = ");
display.print(Temperature);
display.cp437(true);
display.write(167);
display.print("C");
display.setCursor(0,20);
display.print("Pressure = ");
display.print(Pressure);
display.print(" hPa");
display.setCursor(0,30);
display.print("Altitude = ");
display.print(Altitude);
display.print(" m");
display.setCursor(0,40);
display.print("Humidity = ");
display.print(Humidity);
display.print(" %");
display.display();
delay(delayTime);
display.clearDisplay();
} //End if
} //End void loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment