Currently i have a project to read data from a node (using ESP32) and then send it to a master node using ESP32 too and RA-02 SX1278 LoRa 433MHz, which then will be uploaded to the database. T
he Master Node works fine with Node A only, even though the connection between nodes are the same. I had switch the LoRa Module with other ESP32 (LoRa A with ESP32B, LoRa D with ESP32 C, etc) and I have concluded that all ESP32 works with LoRa A but No ESP32 works with the other LoRa.
Receiver
#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include <string.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
WiFiClient client;
const char* ssid = "Redmi Note 9 Pro";
const char* pass = "Yaitudah";
// const char* ssid = "Bruh Guy";
// const char* pass = "Bruh Guy";
#define mosi 23
#define miso 19
#define sck 18
#define ss 5
#define rst 16
#define dio0 17
const char* serverName = "https://somethingsomething.com/databaseexample";
//json format = "{\"rawData\":\"LoraID(2 digits) + Data(2 digits)\"}";
String loraID = "01"; // Example LoraID
String data = "23"; // Example Data
String LoraTempt;
void connectToWifi(){
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
//unsigned long startAttemotTime = millis();
while(WiFi.status() != WL_CONNECTED){
Serial.println(".");
delay(100);
}
if(WiFi.status() != WL_CONNECTED){
Serial.println("Failed");
}else{
Serial.print("Connected to ");
Serial.println(WiFi.localIP());
}
}
void setup() {
Serial.begin(115200);
SPI.begin(sck, miso, mosi, ss);
LoRa.setPins(ss, rst, dio0);
connectToWifi();
while (!Serial);
//Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setSpreadingFactor(12); //12 Max Range
LoRa.setSignalBandwidth(15.6E3); //The higher the higher the bandwidth
LoRa.setSyncWord(0x12); //LoRa Sync Word
}
void loop() {
Serial.println("Running code");
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet ");
// read packet
while (LoRa.available()) {
LoraTempt = LoRa.readString();
}
Serial.print(LoraTempt);
// print RSSI of packet
Serial.print(" with RSSI ");
Serial.println(LoRa.packetRssi());
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(serverName); // Specify the URL
http.addHeader("Content-Type", "application/json"); // Specify content-type header
// Construct the JSON formatted string
String jsonData = "{\"rawData\":\"" + LoraTempt + "\"}";
int httpResponseCode = http.POST(jsonData); // Send the request
if (httpResponseCode > 0) { // Check for the returning code
String response = http.getString(); // Get the response to the request
Serial.println(httpResponseCode); // Print return code
Serial.println(response); // Print request answer
Serial.println(jsonData);
delay(2000);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); // Free the resources
} else {
Serial.println("Error in WiFi connection");
}
}
}
Node A
//Definition
#include <SPI.h>
#include <LoRa.h>
#include <string.h>
#define Input_low 15
#define Input_medium 4
#define Input_high 2
#define mosi 21 //21
#define miso 19 //19
#define sck 18 //18
#define ss 5 //5
#define rst 32 //34
#define dio0 33 //35
float adcVolt(int input) {
return input * 3.3 / 4095;
}
double Input_1;
double Input_2;
double Input_3;
double I1=0, I2=0, I3=0;
double FI1=0, FI2=0, FI3=0;
int n=0; //Counter
//This LoRa ID: 00
//Main ESP32
//Data Format, "(xx)(yy)", xx = LoraID, yy = Data
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
LoRa.setPins(ss, rst, dio0);
SPI.begin(sck, miso, mosi, ss);
pinMode(Input_low, INPUT);
pinMode(Input_medium, INPUT);
pinMode(Input_high, INPUT);
Serial.println("LoRa Sender");
//LoRa.setSPIFrequency(8E6);
LoRa.begin(433E6);
if (!LoRa.begin(433E6)){
Serial.println("LoRa Failed");
ESP.restart();
while(1);
}
LoRa.setTxPower(20); //20 Max Power
LoRa.setSpreadingFactor(12); //12 Max Range
LoRa.setSignalBandwidth(15.6E3); //The higher the higher the bandwidth
LoRa.setSyncWord(0x12); //LoRa Sync Word
}
void loop() {
// put your main code here, to run repeatedly:
if (n == 501) //Reset Value
{
n = 0;
I1=0; I2=0; I3=0;
FI1=0; FI2=0; FI3=0;
}
Input_1 = adcVolt(analogRead(Input_low));
Input_2 = adcVolt(analogRead(Input_medium));
Input_3 = adcVolt(analogRead(Input_high));
I1 += Input_1; //Adding Input
I2 += Input_2;
I3 += Input_3;
if (n==500){ //Average value
FI1 = I1/n;
FI2 = I2/n;
FI3 = I3/n;
Serial.print("1 = ");
Serial.println(FI1);
Serial.print("2 = ");
Serial.println(FI2);
Serial.print("3 = ");
Serial.println(FI3);
if (FI1 < 3){
if (FI2 <= 3){
if (FI3 <= 3){
LoRa.beginPacket();
LoRa.print("0011");
Serial.println("Air Tinggi ");
LoRa.endPacket();
}
else{
LoRa.beginPacket();
LoRa.print("0010");
Serial.println("Air Sedang ");
LoRa.endPacket();
}
}
else{
LoRa.beginPacket();
LoRa.print("0001");
Serial.println("Air Rendah ");
LoRa.endPacket();
}
}
else{
LoRa.beginPacket();
LoRa.print("0000");
Serial.println("Tidak ada air ");
LoRa.endPacket();
}
}
n++; //Increment Counter
}
After trying a few more times, I noticed that my RX only receive either from only 1 TX or TX with the stronger signal. Therefore I'm planning to delay (or maybe put my ESP to sleep in the future) so my RX could receive signal from other TX.
My RX could send back signal but my TX could not receive the signal.
Receiver
#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include <string.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
WiFiClient client;
// const char* ssid = "Redmi Note 9 Pro";
// const char* pass = "Yaitudah";
const char* ssid = "DTKOMP-WIFI";
const char* pass = "";
// const char* ssid = "Bruh Guy";
// const char* pass = "Bruh Guy";
// const char* ssid = "VillaTK";
// const char* pass = "Anaktekkom21";
#define mosi 23
#define miso 19
#define sck 18
#define ss 5
#define rst 16
#define dio0 17
const char* serverName = "https://somethingsomething.com/databaseexample";
//json format = "{\"rawData\":\"LoraID(2 digits) + Data(2 digits)\"}";
//loraID = "01"; Example LoraID
//data = "23"; Example Data
byte LoraTX;
String LoraTempt;
void connectToWifi(){
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
//unsigned long startAttemotTime = millis();
while(WiFi.status() != WL_CONNECTED){
Serial.println(".");
delay(100);
}
if(WiFi.status() != WL_CONNECTED){
Serial.println("Failed");
}else{
Serial.print("Connected to ");
Serial.println(WiFi.localIP());
}
}
void setup() {
Serial.begin(115200);
SPI.begin(sck, miso, mosi, ss);
LoRa.setPins(ss, rst, dio0);
connectToWifi();
while (!Serial);
//Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setTxPower(20);
LoRa.setSpreadingFactor(12); //12 Max Range
LoRa.setSignalBandwidth(62.5E3); //The higher the higher the bandwidth
LoRa.setSyncWord(0x12); //LoRa Sync Word
}
void loop() {
Serial.println("Running code");
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet ");
// read packet
while (LoRa.available()) {
LoraTX = LoRa.read();
LoraTempt = LoRa.readString();
}
Serial.print(LoraTempt);
// print RSSI of packet
Serial.print(" with RSSI ");
Serial.println(LoRa.packetRssi());
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(serverName); // Specify the URL
http.addHeader("Content-Type", "application/json"); // Specify content-type header
// Construct the JSON formatted string
String jsonData = "{\"rawData\":\"" + LoraTempt + "\"}";
int httpResponseCode = http.POST(jsonData); // Send the request
if (httpResponseCode > 0) { // Check for the returning code
String response = http.getString(); // Get the response to the request
Serial.println(httpResponseCode); // Print return code
Serial.println(response); // Print request answer
Serial.println(jsonData);
LoRa.beginPacket();
LoRa.write(LoraTX);
LoRa.endPacket();
Serial.println(LoraTX);
Serial.println("LoRa Response Sent back to Sender");
delay(2000);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); // Free the resources
} else {
Serial.println("Error in WiFi connection");
}
}
}
Transmitter
//Definition
#include <SPI.h>
#include <LoRa.h>
#include <string.h>
#define Input_low 15
#define Input_medium 4
#define Input_high 2
#define mosi 21 //21
#define miso 19 //19
#define sck 18 //18
#define ss 5 //5
#define rst 32 //34
#define dio0 33 //35
float adcVolt(int input) {
return input * 3.3 / 4095;
}
double Input_1;
double Input_2;
double Input_3;
double I1=0, I2=0, I3=0;
double FI1=0, FI2=0, FI3=0;
int n=0; //Counter
//This LoRa ID: 01
//Ardy's ESP32
byte LoRaTX = 0xBB;
byte LoRaTempt;
//Data Format, "(xx)(yy)", xx = LoraID, yy = Data
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
LoRa.setPins(ss, rst, dio0);
SPI.begin(sck, miso, mosi, ss);
pinMode(Input_low, INPUT);
pinMode(Input_medium, INPUT);
pinMode(Input_high, INPUT);
Serial.println("LoRa Sender Ardy");
//LoRa.setSPIFrequency(8E6);
if (!LoRa.begin(433E6)){
Serial.println("LoRa Failed");
ESP.restart();
while(1);
}
LoRa.setTxPower(20); //20 Max Power
LoRa.setSpreadingFactor(12); //12 Max Range
LoRa.setSignalBandwidth(62.5E3); //The higher the higher the bandwidth
LoRa.setSyncWord(0x12); //LoRa Sync Word
}
void loop() {
// put your main code here, to run repeatedly:
int packetSize = LoRa.parsePacket(); //Chekcing for response from RX
if (packetSize)
{
Serial.println("Packet detected");
while (LoRa.available()){
LoRaTempt = LoRa.read();
}
Serial.print("Receive: ");
Serial.println(LoRaTempt);
Serial.print("Checking if");
delay(500);
if (LoRaTempt == LoRaTX)
{
Serial.println("Pausing ESP for 10 Seconds");
delay (10000);
}
} else {
if (n == 501) //Reset Value
{
n = 0;
I1=0; I2=0; I3=0;
FI1=0; FI2=0; FI3=0;
}
Input_1 = adcVolt(analogRead(Input_low));
Input_2 = adcVolt(analogRead(Input_medium));
Input_3 = adcVolt(analogRead(Input_high));
I1 += Input_1; //Adding Input
I2 += Input_2;
I3 += Input_3;
if (n==500){ //Average value
FI1 = I1/n;
FI2 = I2/n;
FI3 = I3/n;
Serial.print("1 = ");
Serial.println(FI1);
Serial.print("2 = ");
Serial.println(FI2);
Serial.print("3 = ");
Serial.println(FI3);
if (FI1 < 3){
if (FI2 <= 3){
if (FI3 <= 3){
LoRa.beginPacket();
LoRa.write(LoRaTX);
LoRa.print("0111");
Serial.println("Air Tinggi ");
LoRa.endPacket();
}
else{
LoRa.beginPacket();
LoRa.write(LoRaTX);
LoRa.print("0110");
Serial.println("Air Sedang ");
LoRa.endPacket();
}
}
else{
LoRa.beginPacket();
LoRa.write(LoRaTX);
LoRa.print("0101");
Serial.println("Air Rendah ");
LoRa.endPacket();
}
}
else{
LoRa.beginPacket();
LoRa.write(LoRaTX);
LoRa.print("0100");
Serial.println("Tidak ada air ");
LoRa.endPacket();
}
}
}
n++; //Increment Counter
}