Hey guys, I'm about to start my project using LoRa to transfer the data from the transmitter to the receiver. Each of the devices includes an Arduino Uno and a LoRa E220-900T22D module by EByte. But first, I want to make sure that the range between both of them can be analyzed.
So, here's the code for the transmitter.
#include <SoftwareSerial.h>
SoftwareSerial lora(2, 3);
int messageCount = 0; // Counter to track the number of messages sent
void setup() {
Serial.begin(9600);
lora.begin(9600);
}
void loop() {
// Check if 10 seconds have passed
static unsigned long lastMillis = 0;
if (millis() - lastMillis >= 10000) {
lastMillis = millis(); // Update the lastMillis value
// Increment the message count
messageCount++;
// Send the word "hello" over SoftwareSerial
lora.print("hello");
// Print the message count
Serial.print("Message Count: ");
Serial.println(messageCount);
lora.println("+" + String(messageCount));
}
// Check if there is data available on the Serial monitor
if (Serial.available()) {
// Read the data from Serial and send it over SoftwareSerial
lora.write(Serial.read());
}
}
And the code for the receiver.
#include "Arduino.h"
#include "LoRa_E220.h"
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
// Define software serial pins
SoftwareSerial mySerial(2, 3); // RX on pin 2, TX on pin 3
LoRa_E220 e220ttl(&mySerial);
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long oldTime = 0;
unsigned long totalReceived = 0;
unsigned long failedTransmissions = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
delay(500);
e220ttl.begin();
Serial.println("Start receiving!");
lcd.begin(20, 4);
lcd.init();
lcd.backlight();
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - oldTime > 10000) {
oldTime = currentTime; // Update the oldTime value
if (e220ttl.available() > 1) {
totalReceived++; // Increment total received transmissions
Serial.println("Message received!");
ResponseContainer rc = e220ttl.receiveMessage();
if (rc.status.code != 1) {
failedTransmissions++; // Increment failed transmissions
Serial.println(rc.status.getResponseDescription());
} else {
Serial.println("Device: End_Node_1");
Serial.print("Message: ");
Serial.println(rc.data);
Serial.print("Status: ");
Serial.println(rc.status.getResponseDescription());
Serial.print("RSSI: ");
Serial.println(rc.rssi, DEC);
Serial.println();
//Calculate Packet Loss Percentage
float packetLossPercentage = (failedTransmissions / (float)totalReceived) * 100.0;
Serial.print("Packet Loss: ");
Serial.print(packetLossPercentage);
Serial.println("%");
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Device: End_Node_1");
lcd.setCursor(0, 1);
lcd.print("Message: ");
lcd.print(rc.data);
lcd.setCursor(0, 2);
lcd.print("Packet Loss: ");
lcd.print(packetLossPercentage);
lcd.print(" %");
lcd.setCursor(0, 3);
lcd.print("RSSI: ");
lcd.print(rc.rssi, DEC);
lcd.print(" dBm");
// Delay before next readings
delay(9000); // Adjusted delay to make it 10 seconds in total (10000 - 1000)
}
}
}
}
The problem is that I can't read the RSSI or the packet loss value. I believe both of the functions have been available in the library of E220 by Renzo Mischianti.