Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NodeMCU + XL1276 LoRa + Receiver
//Receiver
//Woon Jun Shen
//NodeMCU + XL1276 (LORA Spreading High Sensitivity XL1276-D01 SX1278 Wireless Transceiver Module 433MHz from aliexpress)
//https://sites.google.com/site/fypprojectscepter/home
#include "SX1278.h"
#include <SPI.h>
#define LORA_MODE 4
#define LORA_CHANNEL CH_6_BW_125
#define LORA_ADDRESS 4
#define LORA_LED 9
int e;
char my_packet[100];
void setup()
{
pinMode(LORA_LED, OUTPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
// Print a start message
Serial.println(F("sx1278 module and Arduino: receiving packets"));
// Power ON the module
if (sx1278.ON() == 0) {
Serial.println(F("Setting power ON: SUCCESS "));
} else {
Serial.println(F("Setting power ON: ERROR "));
}
// Set transmission mode and print the result
if (sx1278.setMode(LORA_MODE) == 0) {
Serial.println(F("Setting Mode: SUCCESS "));
} else {
Serial.println(F("Setting Mode: ERROR "));
}
// Set header
if (sx1278.setHeaderON() == 0) {
Serial.println(F("Setting Header ON: SUCCESS "));
} else {
Serial.println(F("Setting Header ON: ERROR "));
}
// Select frequency channel
if (sx1278.setChannel(LORA_CHANNEL) == 0) {
Serial.println(F("Setting Channel: SUCCESS "));
} else {
Serial.println(F("Setting Channel: ERROR "));
}
// Set CRC
if (sx1278.setCRC_ON() == 0) {
Serial.println(F("Setting CRC ON: SUCCESS "));
} else {
Serial.println(F("Setting CRC ON: ERROR "));
}
// Select output power (Max, High, Intermediate or Low)
if (sx1278.setPower('M') == 0) {
Serial.println(F("Setting Power: SUCCESS "));
} else {
Serial.println(F("Setting Power: ERROR "));
}
// Set the node address and print the result
if (sx1278.setNodeAddress(LORA_ADDRESS) == 0) {
Serial.println(F("Setting node address: SUCCESS "));
} else {
Serial.println(F("Setting node address: ERROR "));
}
// Print a success message
Serial.println(F("sx1278 configured finished"));
Serial.println();
}
void loop(void)
{
// Receive message for 10 seconds
e = sx1278.receivePacketTimeout(10000);
if (e == 0) {
digitalWrite(LORA_LED, HIGH);
delay(1000);
digitalWrite(LORA_LED, LOW);
Serial.println(F("Package received!"));
for (unsigned int i = 0; i < sx1278.packet_received.length; i++) {
my_packet[i] = (char)sx1278.packet_received.data[i];
}
Serial.print(F("Message: "));
Serial.println(my_packet);
} else {
Serial.print(F("Package received ERROR\n"));
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment