In this blog post, I will be using an RFM95W LoRa chip together with an ESP8266/ESP32 to transmit (receive/send) data and check if this data is complete.
Preperations
My first course of action was to get two ESPs ready to send/receive data via LoRa. If you have trouble or are looking for a dedicated tutorial have a look at these posts for the ESP32 and ESP8266. I have decided that my ESP8266 will send data and my ESP32 receive this data.
As a simple first test, I used the provided example sketches. This worked pretty well, I received my packets on the ESP32. After looking at it for a while I found a problem: RF has (like everything else) the problem of packet corruption, it doesn’t happen very often but when it does, it gets annoying.
The Problem
Our main problem is data corruption, my goal for this is to detect data corruption and ignore the received message. Let’s take an example: You have a simple LoRa node connected to a motion sensor, we send a data packet containing the battery state and the motion sensor’s state, like so:
0;1
The zero being the motion state (0, meaning false / no motion), and the 1 being the battery health (1, meaning true / healthy). Now let’s imagine there is a packet corruption and the first part (0;
) gets removed, now our first item is a 1. If the receiver doesn’t notice the broken package, it might be that a false alarm gets triggered. After all the first bit, so motion, was true. This is the reason we need to check if a package is actually healthy.
My idea was to send the data and append some kind of hash to it. I will be using a sha1 hash for this, as ESPs can calculate this hash pretty easily, even though it’s a little overkill for this application. My new package will now look like this:
Actual data here;hash
Hello 4;a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
Okay, after making the theory clear let’s implement it!
The implementation
Starting with my sending code, it looks like this:
Use the right frequencies for your region
If you adapt this code please check if you use the right frequency. Different regions use different frequencies for LoRa. Please check your local guidelines. Europe uses 868MHz (868E6). Update your LoRa.begin line! Here is a useful table by TTN.
#include <SPI.h>
#include <LoRa.h>
#include "Hash.h"
int counter = 0;
// - Pin configs - ESP32 -
// #define ss 5
// #define rst 14
// #define dio0 2
// - Pin configs - ESP8266 -
#define ss 4 // D2
#define rst 5 // D1
#define dio0 -1 // Disabled
void setup() {
Serial.begin(115200);
while (!Serial);
delay(1000);
Serial.println("LoRa Sender");
// Setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
delay(5000);
Serial.println("Now waiting for Watchdog to restart");
while (1);
}else{
Serial.println("Starting LoRa successful");
}
}
// Sends a string every 5000ms (5 seconds)
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
String result = sha1("hello " + String(counter)); // Creating the hash value for our string
LoRa.print("hello ");
LoRa.print(counter);
LoRa.print(";");
LoRa.print(result);
LoRa.endPacket();
counter++;
delay(5000);
}
This will send our packets with the hash appended. For the next bit, we will need to receive the message and check if the supplied hash is correct.
#include <SPI.h>
#include <LoRa.h>
#include <Hash.h>
// - Pin configs - ESP32 -
#define ss 5
#define rst 14
#define dio0 2
// - Pin configs - ESP8266 -
// #define ss 4 // D2
// #define rst 5 // D1
// #define dio0 -1 // Disabled
// Taken from https://forum.arduino.cc/t/arduino-split-funktion-eines-strings/595753
String split(String s, char parser, int index) {
String rs = "";
int parserIndex = index;
int parserCnt = 0;
int rFromIndex = 0, rToIndex = -1;
while (index >= parserCnt) {
rFromIndex = rToIndex + 1;
rToIndex = s.indexOf(parser, rFromIndex);
if (index == parserCnt) {
if (rToIndex == 0 || rToIndex == -1) return "";
return s.substring(rFromIndex, rToIndex);
} else parserCnt++;
}
return rs;
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
delay(5000);
Serial.println("Now waiting for Watchdog to restart");
while (1);
}else{
Serial.println("Starting LoRa successful");
}
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// Create a string of the incoming message
String message = "";
while (LoRa.available()) {
message += (char)LoRa.read();
}
Serial.print(message);
String result = sha1(split(message, ';', 0)); // Take the first element, the raw message and hashing it
String tempy = message;
tempy.replace(split(message, ';', 0) + ";", ""); // Now replace the message so only the hash is left
if (result == tempy) { // Is the hash the same?
Serial.print(" Hash ok ");
} else {
Serial.print(" Hash fail, expectd hash ");
Serial.print(tempy);
Serial.print(" ");
Serial.print(split(message, ';', 1));
Serial.print(" ");
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Using the hash library on ESP32
For some reason, the ESP32 does not implement the same hash functions as the ESP8266. Therefore I used this third party library to get the same functinality.
After running the code for a while I got this output on my receiver serial console. It worked! Sadly it’s pretty hard to break a package on purpose, but I checked it and this code works, however, I sadly don’t have a screenshot of a broken transmission.
This concludes my little experiment/project here, I have achieved my goal to detect broken transmissions and that’s it! I hoped this helped someone on their way.
Recent Comments