#include <SPI.h>
#include <LoRa.h>
//----------------------------------------
//---------------------------------------- LoRa Pin /khai bao chan IO
#define ss 5
#define rst 14
#define dio0 2
//---------------------------------------- Bien luu du lieu vao ra
String Incoming = "";
String Message = "";
//----------------------------------------
//---------------------------------------- Cau hinh LoRa data transmission.
byte LocalAddress = 0x01; //--> dia chi cua gateway (Master Address).
byte Destination_ESP32_Slave_1 = 0x02; //--> dia chi cua node 1 (ESP32).
byte Destination_ESP32_Slave_2 = 0x03; //--> dia chi cua node 2 (ESP32).
//---------------------------------------- BIen thoi gian gui du lieu.
unsigned long previousMillis_SendMSG = 0;
const long interval_SendMSG = 1000;
// bien lua chon node nhan du lieu.
byte Slv = 0;
void sendMessage(String Outgoing, byte Destination) {
LoRa.beginPacket(); // Bắt đầu gói tin
LoRa.write(Destination); // Địa chỉ đích
LoRa.write(LocalAddress); // Địa chỉ gửi
LoRa.write(Outgoing.length()); // Độ dài dữ liệu
LoRa.print(Outgoing); // Nội dung dữ liệu
LoRa.endPacket(); // Kết thúc và gửi đi
}
//________________________________________________________________________________
//________________________________________________________________________________ Subroutines for receiving data (LoRa Ra-02).
void onReceive(int packetSize) {
if (packetSize == 0) return; //--> if there's no packet, return
//---------------------------------------- read packet header bytes:
int recipient = LoRa.read(); //--> recipient address
byte sender = LoRa.read(); //--> sender address
byte incomingLength = LoRa.read(); //--> incoming msg length
//----------------------------------------
// Clears Incoming variable data.
Incoming = "";
//---------------------------------------- Get all incoming data.
while (LoRa.available()) {
Incoming += (char)LoRa.read();
}
//----------------------------------------
//---------------------------------------- Check length for error.
if (incomingLength != Incoming.length()) {
Serial.println();
Serial.println("error: message length does not match length");
return; //--> skip rest of function
}
//----------------------------------------
//---------------------------------------- Checks whether the incoming data or message for this device.
if (recipient != LocalAddress) {
Serial.println();
Serial.println("This message is not for me.");
return; //--> skip rest of function
}
//----------------------------------------
//---------------------------------------- if message is for this device, or broadcast, print details:
Serial.println();
Serial.println("Received from: 0x" + String(sender, HEX));
//Serial.println("Message length: " + String(incomingLength));
Serial.println("Message: " + Incoming);
//Serial.println("RSSI: " + String(LoRa.packetRssi()));
//Serial.println("Snr: " + String(LoRa.packetSnr()));
//----------------------------------------
}
//________________________________________________________________________________
//________________________________________________________________________________ VOID SETUP
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//---------------------------------------- Settings and start Lora Ra-02.
LoRa.setPins(ss, rst, dio0);
Serial.println("Start LoRa init...");
if (!LoRa.begin(433E6)) { // initialize ratio at 915 or 433 MHz
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
Serial.println("LoRa init succeeded.");
//----------------------------------------
}
//________________________________________________________________________________
//________________________________________________________________________________ VOID LOOP
void loop() {
// put your main code here, to run repeatedly:
//---------------------------------------- Millis or Timer to send message / command data to slaves every 1 second (see interval_SendCMD variable).
// Messages are sent every one second is alternately.
// > Master sends message to Slave 1, delay 1 second.
// > Master sends message to Slave 2, delay 1 second.
unsigned long currentMillis_SendMSG = millis();
if (currentMillis_SendMSG - previousMillis_SendMSG >= interval_SendMSG) {
previousMillis_SendMSG = currentMillis_SendMSG;
Slv++;
if (Slv > 2) Slv = 1;
Message = "SDS" + String(Slv);
//::::::::::::::::: Condition for sending message / command data to Slave 1 (ESP32 Slave 1).
if (Slv == 1) {
Serial.println();
Serial.print("Send message to ESP32 Slave " + String(Slv));
Serial.println(" : " + Message);
sendMessage(Message, Destination_ESP32_Slave_1);
}
//:::::::::::::::::
//::::::::::::::::: Condition for sending message / command data to Slave 2 (UNO Slave 2).
if (Slv == 2) {
Serial.println();
Serial.print("Send message to ESP32 Slave " + String(Slv));
Serial.println(" : " + Message);
sendMessage(Message, Destination_ESP32_Slave_2);
}
//:::::::::::::::::
}
//----------------------------------------
//---------------------------------------- parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket());
//----------------------------------------
}
//________________________________________________________________________________
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<