Hi! First of all, sorry if I messed up something in this post, I read the guide and tried my best.
I can't make a consistent and useful communication between two Nodemcu ESP8266 each with his own SX1278 ra01 433mhz LoRa module.
I recreated this exact esample: LoRa SX1278 & ESP8266 Transmitter Receiver with DHT11 Sensor , firstly with the same code and because I got no communication at all (no compiling or LoRa initialization errors, I was not receiving any message), I tried different version:
-
I tried specifying the destination address, the transmitter address, a messageId, the message length with the LoRa.write() method. (Code1)
-
I tried using LoRa.write() even for the message payload, using char* instead of String and convert it to uint8_t as request by the function.
-
Instead of using the callback method in the setup, I run a custom onReceive method in the loop function. (Code2)
-
I tried messing around with the TxPower and the SyncWord without really understanding them. (Code3)
-
I tried different delay in the transmitter side.
My result were a good 95% of not receiving anything, and a 5% of packet with strange characters as payload and correct information about transmitter, messageId, message length.
I hate myself because I didn't get a copy of a received message to show you. Just know that every data written in the packet with the LoRa.write() method was received correctly, the payload (which was received only with the LoRa.print() method) was full of garbage random characters similar to when you set the wrong baud rate in the serial monitor.
My exact components are:
-
ESP8266 NodeMCU 1.0 (ESP-12E Module) Amazon link
-
SX1278 ra01 433mhz (I'm in Europe and I Know about the rules, I just can't find any 868mhz cheap and available) Amazon link
I'm using this LoRa library: LoRa.h by Sandeep Mistry
The wire connection are the same as the article I tried to recreate. The only thing to note is that he switch the definition of the receiver DIO0 pin in the second example (from 2 to 4) so I went 4 in both the transmitter (I know he doesn't need DIO0, but to be sure and to try and switch the two code on the two board I connected it too).
Code:
[Code 1]:
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add transmitter address
LoRa.write(msgCount); // add message ID
LoRa.write(message.length()); // add payload length
LoRa.print(message); // add payload
LoRa.endPacket();
[Code 2]:
void loop() {
onReceive(LoRa.parsePacket());
}
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 transmitter = LoRa.read(); // transmitter address
byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length
byte message = LoRa.read();
Serial.print("Recipient: ");
Serial.println(recipient);
Serial.print("transmitter: ");
Serial.println(transmitter);
Serial.print("incomingMsgId: ");
Serial.println(incomingMsgId);
Serial.print("incomingLength: ");
Serial.println(incomingLength);
Serial.print("Message: ");
Serial.println((String)message);
Serial.print("Received packet: ");
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
// read packet
Serial.println("");
Serial.print("Rest of packet: ");
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI: ");
Serial.println(LoRa.packetRssi());
Serial.print("' with SNR: ");
Serial.println(LoRa.packetSnr());
}
[Code 3]:
void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa transmitter");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
delay(100);
while (1);
}
//DONE IN BOTH TRANSMITTER AND RECEIVER
LoRa.setSyncWord(0xF3);
LoRa.setTxPower(10);
Serial.println("LoRa transmitter initialized");
}
Whole transmitter and receiver code:
[Receiver code]:
#include <SPI.h>
#include <LoRa.h>
#define ss 15
#define rst 16
#define dio0 4
byte localAddress = 0xFF; // address of this device
byte destination = 0xBB; // destination to send to
bool busy = false;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver Callback");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
//LoRa.setSyncWord(0xF3);
//LoRa.setTxPower(10);
// register the receive callback
//LoRa.onReceive(onReceive);
// put the radio into receive mode
//LoRa.receive();
Serial.println("Lora Receiver initialized");
}
void loop() {
onReceive(LoRa.parsePacket());
}
void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return
busy = true;
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte transmitter = LoRa.read(); // transmitter address
byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length
byte message = LoRa.read();
Serial.print("Recipient: ");
Serial.println(recipient);
Serial.print("transmitter: ");
Serial.println(transmitter);
Serial.print("incomingMsgId: ");
Serial.println(incomingMsgId);
Serial.print("incomingLength: ");
Serial.println(incomingLength);
Serial.print("Messagee: ");
Serial.println((String)message);
Serial.print("Received packet: ");
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
// read packet
Serial.println("");
Serial.print(" Resto del pacchetto: ");
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI: ");
Serial.println(LoRa.packetRssi());
Serial.print("' with SNR: ");
Serial.println(LoRa.packetSnr());
busy = false;
}
[transmitter code]:
#include <SPI.h>
#include <LoRa.h>
#define ss 15
#define rst 16
#define dio0 4
String message;
int msgCount = 0; // count of outgoing messages
byte localAddress = 0xBB; // address of this device
byte destination = 0xFF; // destination to send to
void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa transmitter");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
delay(100);
while (1);
}
//LoRa.setSyncWord(0xF3);
//LoRa.setTxPower(10);
Serial.println("LoRa transmitter initialized ");
}
void loop()
{
Serial.print("Mandando il messaggio numero: ");
Serial.println(msgCount);
message = "Test message";
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add transmitter address
LoRa.write(msgCount); // add message ID
LoRa.write(message.length()); // add payload length
LoRa.print(message); // add payload
LoRa.endPacket();
msgCount++;
delay(5000);
}
}
I hope to have given the best information in the best way, I have very high expectation in LoRa and its capability. I'm a software developer trying to understand electronics to develop some IoT applications, I have done many experiments and usually the problem was my poor electronics skills, but now everything looks like its wired correctly (LoRa.begin() checks that) and it looks like some software or radio transmission problem.
This is my last resort, I have never asked something on a forum, hope that you guys can save me!
Thanks a lot.