Hi everyone! I've been having issues with sending multiple bits of data with LoRa and ESP8266 (https://forum.arduino.cc/index.php?topic=727395.0). I finally figured it out so I decided to write a tutorial for me next time, and for anyone else who would find it useful also.
The Hook UpHere is a wiring diagram of the RA-02 and ESP8266:
I switched some of the connections though. Here is my connection list:
RA-02ESP
DIO0 D2
RST D3
SCK D5
MISO D6
MOSI D7
NSS D1
For a more in depth tutorial, go here:
https://circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial
Transmitter Code BreakdownHere is a in depth breakdown of my transmitter code.
#include <SPI.h>
#include <LoRa.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include
all the libraries.
#define ss 5
#define rst 0
#define dio0 4
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 4800;
Defining the LoRa's and GPS's pin connections, and baud rate.
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);
Definitions of the GPS and Serial libraries.
byte msgCount = 0;
byte Latitude;
byte Longitude;
byte Speed;
byte Course;
byte localAddress = 0xBB;
byte destination = 0xFF;
The message info variables we're going to send.
void setup() {
Serial.begin(9600);
gpsSerial.begin(GPSBaud);
Serial.println("LoRa Sender");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initializing OK!");
}
In setup()
, we start the serial monitor, the GPS serial, and the LoRa.
void loop() {
Serial.print("Sending packet: ");
Serial.println(msgCount);
Latitude = gps.location.lat();
Longitude = gps.location.lng();
Course = gps.course.deg();
Speed = gps.speed.mph();
String outgoing = String(Latitude) + String(Longitude) + String(Course) + String(Speed);
// send packet
LoRa.beginPacket();
LoRa.write(destination);
LoRa.write(localAddress);
LoRa.write(msgCount);
LoRa.write(outgoing.length());
LoRa.write(Latitude);
LoRa.write(Longitude);
LoRa.write(Course);
LoRa.write(Speed);
LoRa.endPacket();
msgCount++;
delay(4000);
}
In loop()
, we add all the data to the variables and send it inside the LoRa.beginPacket();
and the LoRa.endPacket();
. After, we increment the message counter, and wait 4 seconds.
Here is a in depth breakdown of my receiver code.
#include <SPI.h>
#include <LoRa.h>
#include
libraries.
#define ss 5
#define rst 0
#define dio0 4
LoRa connections to the ESP.
byte localAddress = 0xFF;
int recipient;
String incoming;
byte sender;
byte incomingMsgId;
byte incomingLength;
byte Latitude;
byte Longitude;
byte Course;
byte Speed;
Variables to hold the incoming data.
void setup() {
Serial.begin(9600);
Serial.println("LoRa Receiver");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
In setup()
, we start the serial monitor, and the LoRa.
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize == 0) return;
recipient = LoRa.read();
sender = LoRa.read();
incomingMsgId = LoRa.read();
incomingLength = LoRa.read();
Latitude = LoRa.read();
Longitude = LoRa.read();
Course = LoRa.read();
Speed = LoRa.read();
incoming = String(Latitude) + String(Longitude) + String(Course) + String(Speed);
In the loop()
, we store all the incoming data in the variables. The last variable incoming
will be explained in the next one down.
if (incomingLength != incoming.length()) {
Serial.println("error: message length does not match length");
return;
}
if (recipient != localAddress) {
Serial.println("This message is not for me.");
return;
}
Here, we go through two fail-safes to prevent us getting a wrong, or incomplete, message.
Serial.println("********************************************");
Serial.println("Received from: 0x" + String(sender, HEX));
Serial.println("Sent to: 0x" + String(recipient, HEX));
Serial.println("Message ID: " + String(incomingMsgId));
Serial.println("Message length: " + String(incomingLength));
Serial.println("Latitude: " + String(Latitude));
Serial.println("Longitude: " + String(Longitude));
Serial.println("Course: " + String(Course));
Serial.println("Speed: " + String(Speed));
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println("");
}
Now we print all the data on the serial monitor.
Wrapping UpThis is what the serial monitor prints on the receiver side:
********************************************
Received from: 0xbb
Sent to: 0xff
Message ID: 204
Message length: 4
Latitude: 0
Longitude: 0
Course: 0
Speed: 0
RSSI: -116
Snr: 3.50
As you can see, I am implementing this in a tracking device. You can change anything you want about it, or the data you send.
If anyone has any questions, please post in the comments! And check out my other tutorials!
and my website
Comments