-
-
Save FlavorFx/04349b7e4622536c561d3d8167277d80 to your computer and use it in GitHub Desktop.
Lora Communication with SX1276 and SX1262
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Adafruit_BME280.h> | |
#include <LoRa.h> | |
// LORA | |
#define SCK 5 // GPIO5 -- SX1278's SCK | |
#define MISO 19 // GPIO19 -- SX1278's MISnO | |
#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 LED 25 // GPIO25 -- LED Light | |
// BME via I2C | |
Adafruit_BME280 bme; | |
RTC_DATA_ATTR int counter = 0; | |
const int SensorId = 0x1017; | |
void setup() | |
{ | |
// Initialize Serial Communication | |
Serial.begin(9600); | |
while (!Serial); | |
Serial.println(); | |
Serial.println("LoRaGarageDoorSensor"); | |
// Initilize Temperatur and Humidity Sensor | |
if (!bme.begin(0x76)) | |
{ | |
Serial.println("Could not find a valid temperature sensor, check wiring!"); | |
while (true); | |
} | |
// Initialize gpio output for led | |
pinMode(LED, OUTPUT); | |
// Inizialize SPI Communication | |
SPI.begin(SCK, MISO, MOSI, SS); | |
// Inizialize Lora Communication | |
LoRa.setPins(SS, RST, DI0); | |
if (!LoRa.begin(868E6)) | |
{ | |
Serial.println("Starting LoRa failed!"); | |
while (true); | |
} | |
LoRa.setSyncWord(0x12); | |
LoRa.setTxPower(17); | |
} | |
void loop() | |
{ | |
Serial.print("Sending packet: "); | |
Serial.println(counter); | |
Serial.print("Temperature = "); | |
Serial.print(bme.readTemperature()); | |
Serial.println("°C"); | |
Serial.print("Humidity = "); | |
Serial.print(bme.readHumidity()); | |
Serial.println("%"); | |
Serial.print("Pressure = "); | |
Serial.print(bme.readPressure()/ 100); | |
Serial.println("hPa"); | |
// turn led on | |
digitalWrite(LED, HIGH); | |
// send packet | |
LoRa.beginPacket(); | |
// Create JSON message | |
String msg = "{\"id\":" + String(SensorId) + ",\"temperature\":" + String(bme.readTemperature()) + ",\"humidity\":" + String(50.3) + ",\"pressure\":" + String(bme.readPressure() / 100.0) + "}"; | |
LoRa.print(msg.c_str()); | |
LoRa.endPacket(); | |
// turn led off | |
digitalWrite(LED, LOW); | |
// Initialize Deep Sleep | |
Serial.println("Going to sleep..."); | |
Serial.flush(); | |
counter++; | |
// wakeup every 15 min | |
esp_sleep_enable_timer_wakeup(600 * 1000000); | |
esp_deep_sleep_start(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include the library | |
#include <SPI.h> | |
#include <RadioLib.h> | |
SX1262 radio = new Module(8, 14, 12, 13); | |
void setup() | |
{ | |
Serial.begin(9600); | |
SPI.begin(9, 11, 10, 8); | |
// initialize SX1262 with default settings | |
Serial.print(F("[SX1262] Initializing ... ")); | |
int state = radio.begin(); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
Serial.println(F("success!")); | |
} | |
else | |
{ | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
while (true); | |
} | |
// set the function that will be called when new packet is received | |
radio.setDio1Action(setFlag); | |
// start listening for LoRa packets | |
Serial.print(F("[SX1262] Starting to listen ... ")); | |
state = radio.startReceive(); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
Serial.println(F("success!")); | |
} | |
else | |
{ | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
while (true); | |
} | |
// if needed, 'listen' mode can be disabled by calling any of the following methods: | |
// | |
// radio.standby() | |
// radio.sleep() | |
// radio.transmit(); | |
// radio.receive(); | |
// radio.readData(); | |
// radio.scanChannel(); | |
// set LoRa sync word | |
if (radio.setSyncWord(0x12) != RADIOLIB_ERR_NONE) { | |
Serial.println(F("Unable to set sync word!")); | |
while (true); | |
} | |
} | |
// flag to indicate that a packet was received | |
volatile bool receivedFlag = false; | |
// this function is called when a complete packet is received by the module | |
#if defined(ESP8266) || defined(ESP32) | |
ICACHE_RAM_ATTR | |
#endif | |
void setFlag(void) | |
{ | |
// we got a packet, set the flag | |
receivedFlag = true; | |
} | |
void loop() | |
{ | |
// check if the flag is set | |
if(receivedFlag) | |
{ | |
// reset flag | |
receivedFlag = false; | |
// you can read received data as an Arduino String | |
String str; | |
int state = radio.readData(str); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
// packet was successfully received | |
Serial.println(F("[SX1262] Received packet!")); | |
// print data of the packet | |
Serial.print(F("[SX1262] Data:\t\t")); | |
Serial.println(str); | |
// print RSSI (Received Signal Strength Indicator) | |
Serial.print(F("[SX1262] RSSI:\t\t")); | |
Serial.print(radio.getRSSI()); | |
Serial.println(F(" dBm")); | |
// print SNR (Signal-to-Noise Ratio) | |
Serial.print(F("[SX1262] SNR:\t\t")); | |
Serial.print(radio.getSNR()); | |
Serial.println(F(" dB")); | |
} | |
else if (state == RADIOLIB_ERR_CRC_MISMATCH) | |
{ | |
// packet was received, but is malformed | |
Serial.println(F("CRC error!")); | |
} | |
else | |
{ | |
// some other error occurred | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
} | |
// put module back to listen mode | |
radio.startReceive(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <RadioLib.h> | |
const int SensorId = 0x1018; | |
SX1262 radio = new Module(8, 14, 12, 13); | |
void setup() | |
{ | |
Serial.begin(9600); | |
SPI.begin(9, 11, 10, 8); | |
// initialize SX1262 with default settings | |
Serial.print(F("[SX1262] Initializing ... ")); | |
int state = radio.begin(); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
Serial.println(F("success!")); | |
} | |
else | |
{ | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
while (true); | |
} | |
// set spreading factor | |
if (radio.setSpreadingFactor(9) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) { | |
Serial.println(F("Selected spreading factor is invalid for this module!")); | |
while (true); | |
} | |
// set LoRa sync word | |
if (radio.setSyncWord(0x12) != RADIOLIB_ERR_NONE) { | |
Serial.println(F("Unable to set sync word!")); | |
while (true); | |
} | |
} | |
void loop() | |
{ | |
Serial.print(F("[SX1262] Transmitting packet ... ")); | |
String msg = "{\"id\":" + String(SensorId) + ",\"temperature\":" + "0" + ",\"humidity\":" + "0" + ",\"pressure\":" + "0" + "}"; | |
int state = radio.transmit(msg); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
// the packet was successfully transmitted | |
Serial.println(F("success!")); | |
// print measured data rate | |
Serial.print(F("[SX1262] Datarate:\t")); | |
Serial.print(radio.getDataRate()); | |
Serial.println(F(" bps")); | |
} | |
else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) | |
{ | |
// the supplied packet was longer than 256 bytes | |
Serial.println(F("too long!")); | |
} | |
else if (state == RADIOLIB_ERR_TX_TIMEOUT) | |
{ | |
// timeout occured while transmitting packet | |
Serial.println(F("timeout!")); | |
} | |
else | |
{ | |
// some other error occurred | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
} | |
delay(1000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <RadioLib.h> | |
// LORA | |
#define SCK 5 // GPIO5 -- SX1278's SCK | |
#define MISO 19 // GPIO19 -- SX1278's MISnO | |
#define MOSI 27 // GPIO27 -- SX1278's MOSI | |
#define SS 18 // GPIO18 -- SX1278's CS | |
#define RST 14 // GPIO14 -- SX1278's RESET | |
#define DIO1 26 // GPIO26 -- SX1278's IRQ(Interrupt Request) | |
const int SensorId = 0x1017; | |
SX1276 radio = new Module(SS, DIO1, RST); | |
void setup() | |
{ | |
Serial.begin(9600); | |
SPI.begin(SCK, MISO, MOSI, SS); | |
// Inizialize Lora Communication | |
Serial.print(F("[SX1276] Initializing ... ")); | |
int state = radio.begin(868.0); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
Serial.println(F("success!")); | |
} | |
else | |
{ | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
while (true); | |
} | |
// set spreading factor | |
if (radio.setSpreadingFactor(9) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) { | |
Serial.println(F("Selected spreading factor is invalid for this module!")); | |
while (true); | |
} | |
// set LoRa sync word | |
if (radio.setSyncWord(0x12) != RADIOLIB_ERR_NONE) { | |
Serial.println(F("Unable to set sync word!")); | |
while (true); | |
} | |
} | |
void loop() | |
{ | |
Serial.print("[SX1276] Sending packet: "); | |
//String msg = "{\"id\":" + String(SensorId) + ",\"temperature\":" + "0" + ",\"humidity\":" + "0" + ",\"pressure\":" + "0" + "}"; | |
String msg = "Hello World!"; | |
int state = radio.transmit(msg); | |
if (state == RADIOLIB_ERR_NONE) | |
{ | |
// the packet was successfully transmitted | |
Serial.println(F("success!")); | |
// print measured data rate | |
Serial.print(F("[SX1276] Datarate:\t")); | |
Serial.print(radio.getDataRate()); | |
Serial.println(F(" bps")); | |
} | |
else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) | |
{ | |
// the supplied packet was longer than 256 bytes | |
Serial.println(F("too long!")); | |
} | |
else if (state == RADIOLIB_ERR_TX_TIMEOUT) | |
{ | |
// timeout occured while transmitting packet | |
Serial.println(F("timeout!")); | |
} | |
else | |
{ | |
// some other error occurred | |
Serial.print(F("failed, code ")); | |
Serial.println(state); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment