I wish to remove the ""EmonLib.h" from the RX instance of this sketch using LORA.
With the ""Emon"" instance included in the RX sketch all is fine. Tx sends the instruction, RX receives OK and re-sends data back to the Tx unit.
However when I try to remove "" Emon"" from the Rx sketch the RX unit fails to re-send the data
back to the TX unit.
No error messages are shown at any stage except that no data is received at the TX unit.
type or paste code here
```//................................................................. TX SENDER ..........................................
#include <SPI.h>
#include <LoRa.h>
//................................................................ LORA ADDRESSES .............SENDER
byte localAddress = 0xAA;
byte destinationAddress = 0xBB;
//................................................................ VARIABLES
long lastSendTime = 0;
int staT = 0 ;
int staTRec = 0;
//________________________________________________________________________________________________________________
//_______________________________________________________________________________________ VOID SETUP __________SENDER
void setup() {
Serial.begin(9600);
//............................................................. STARTING LORA ....................
Serial.println("Start LoRa duplex TX Sender");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//.............................................................. VOID SENDING DATA ...........................SENDER
void senddatA()
{
if (millis() - lastSendTime > 1000) {
String datAsenT = String(staT);
sendMessage(datAsenT);
Serial.print(" SENT "); Serial.println(datAsenT);
lastSendTime = millis();
}
}
//............................................................. VOID SEND MESSAGE .............................SENDER
void sendMessage(String outgoing) {
digitalWrite(12, HIGH);
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
delay(500);
digitalWrite(12, LOW);
}
//............................................................. VOID RECEIVING MESSAGE .........................SENDER
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
Serial.println (packetSize);
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
if (incomingLength != incoming.length()) {
//Serial.println("Error: Message length does not match length");
return;
}
if (recipient != localAddress) {
//Serial.println("Error: Recipient address does not match local address");
return;
}
staTRec = incoming.toInt();
Serial.print(" Received Back "); Serial.println(staTRec);
}
//_________________________________________________________________________________________________________________
//_______________________________________________________________________________________ VOID LOOP ___________SENDER
void loop() {
staT = 729;
senddatA();
//............................................................ RECEIVING DATA .....................
receiveMessage(LoRa.parsePacket());
}
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' RX """""""""""""""""""""""""""""""""""
`
type or paste code here
//.................................................................. RX RECEIVER ..........................................
#include <SPI.h>
#include <LoRa.h>
byte localAddress = 0xBB;
byte destinationAddress = 0xAA;
//................................................................. ENERGY MONITOR ......................................
#include "EmonLib.h" // Include Emon Library
#define CURRENT_CAL 250
EnergyMonitor emon1; // Create an instance
//................................................................. VARIABLES ............................................RX RECEIVER
long lastSendTime = 0;
int reC = 0;
int statioN = 0;
int onofF = 0;
int incom = reC;
int aaa = 0; // Temp Variable
int loopCount = 0; // Energy Monitor Loop
int currentDraw = 0;
int AConoff = 0; // AC either ON = 1 or OFF = 0
int staToNOFF = 0; // Even number is OFF odd number is ON
int relay = 0;
void setup() {
Serial.begin(9600);
//................................................................. ENERGY MONITOR ......................................RX RECEIVER
emon1.current(0, CURRENT_CAL); // Current: input pin, calibration.
//................................................................ STARTING LORA ....................................RX RECEIVER
Serial.println("Start LoRa duplex RX Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//................................................................. ENERGY MONITOR ......................................RX RECEIVER
void currenT() {
emon1.calcVI(20, 2000); // Calculate all. No.of half wavelengths (crossings), time-out
// float currentDraw = emon1.Irms; //extract Irms into Variable
// Serial.print("Current Read: ");
// Serial.println(currentDraw);
// if (currentDraw > 2.0) {
// AConoff = 1;
// }
//else {
// AConoff = 0;
//}
// delay(500);
}
//................................................................... VOID OUTGOING DATA .....................................RX RECEIVER
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
Serial.print(" sending back " ); Serial.println(outgoing);
}
//................................................................... VOID INCOMING DATA ............................................RX RECEIVER
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
reC = incoming.toInt();
Serial.print(" Received "); Serial.println(reC);
//...... SEND BACK ..............
currenT();
String sendbacK = String(reC);
sendMessage(sendbacK);
Serial.print(" Sentback "); Serial.println(sendbacK);
}
//.................................................................... VOID LOOP .............................................RX RECEIVER
void loop() {
receiveMessage(LoRa.parsePacket()); // RECEIVE MESSAGE
}
`
pylon
August 25, 2022, 5:54pm
2
You cannot remove that as EnergyMonitor object depends on that library.
Why do you want to remove that library?
I no longer have any use for the EMon sensors and wish to remove them entirely from my project.
and remove the library as it is no longer necessary, but when I do remove it,it stops the RX TX function of LORA. Hence my original question.
Remove ALL of the Emon-related code and test the result.
If you still have problems, post the revised code.
TX SKETCH ( Unchanged)
//................................................................. TX SENDER ..........................................
#include <SPI.h>
#include <LoRa.h>
//................................................................ LORA ADDRESSES .............SENDER
byte localAddress = 0xAA;
byte destinationAddress = 0xBB;
//................................................................ VARIABLES
long lastSendTime = 0;
int staT = 0 ;
int staTRec = 0;
int daY = 0; int houR = 0; int miN = 0; int seC = 0;
//................................................................. RTC DS 3231 ............
#include <Wire.h>
#include <ds3231.h>
struct ts t;
//________________________________________________________________________________________________________________
//_______________________________________________________________________________________ VOID SETUP __________SENDER
void setup() {
Serial.begin(9600);
//.............................................................. RTC START ............
Wire.begin();
DS3231_init;
//.............................................................. SET TIME ...........
//t.hour = 11; t.min = 49; t.sec = 5; t.mday = 19; t.mon = 8; t.year = 2020;
//DS3231_set(t);
//............................................................. STARTING LORA ....................
Serial.println("Start LoRa duplex TX Sender");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//.............................................................. VOID SENDING DATA ...........................SENDER
void senddatA()
{
if (millis() - lastSendTime > 1000) {
String datAsenT = String(staT);
sendMessage(datAsenT);
Serial.print(" SENT "); Serial.println(datAsenT);
lastSendTime = millis();
}
}
//............................................................. VOID SEND MESSAGE .............................SENDER
void sendMessage(String outgoing) {
digitalWrite(12, HIGH);
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
delay(500);
digitalWrite(12, LOW);
}
//............................................................. VOID RECEIVING MESSAGE .........................SENDER
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
if (incomingLength != incoming.length()) {
//Serial.println("Error: Message length does not match length");
return;
}
if (recipient != localAddress) {
//Serial.println("Error: Recipient address does not match local address");
return;
}
staTRec = incoming.toInt();
Serial.print(" Received Back "); Serial.println(staTRec);
}
//.............................................................. VOID TIME PRINT OUT ...........
void timE() {
Serial.print(t.mday);
Serial.print("/");
Serial.print(t.mon);
Serial.print("/");
Serial.print(t.year);
Serial.print(" ");
Serial.print(t.hour);
Serial.print(":");
Serial.print(t.min);
Serial.print(".");
Serial.println(t.sec);
}
//_________________________________________________________________________________________________________________
//_______________________________________________________________________________________ VOID LOOP ___________SENDER
void loop() {
staT = 221;
senddatA();
//............................................................ GET TIME .................
DS3231_get(&t);
daY = (t.mday); houR = (t.hour); miN = (t.min); seC = (t.sec);
//Serial.print(daY);Serial.print(":");Serial.print(houR);Serial.print(":");Serial.print(miN);Serial.print(":");Serial.println(seC);
//timE(); //.............................................. ONLY IF NEED TIME PRINT ............
//............................................................ RECEIVING DATA .....................
receiveMessage(LoRa.parsePacket());
}
RX SKETCH WITH ALL EMon removed from sketch
type or paste code here
//.................................................................. RX RECEIVER ..........................................
#include <SPI.h>
#include <LoRa.h>
byte localAddress = 0xBB;
byte destinationAddress = 0xAA;
//................................................................. VARIABLES ............................................RX RECEIVER
long lastSendTime = 0;
int reC = 0;
int statioN = 0;
int onofF = 0;
int incom = reC;
int aaa = 0; // Temp Variable
int loopCount = 0; // Energy Monitor Loop
int currentDraw = 0;
int AConoff = 0; // AC either ON = 1 or OFF = 0
int staToNOFF = 0; // Even number is OFF odd number is ON
int relay = 0;
void setup() {
Serial.begin(9600);
//................................................................ STARTING LORA ....................................RX RECEIVER
Serial.println("Start LoRa duplex RX Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//................................................................... VOID OUTGOING DATA .....................................RX RECEIVER
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
Serial.print(" sending back " ); Serial.println(outgoing);
}
//................................................................... VOID INCOMING DATA ............................................RX RECEIVER
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
reC = incoming.toInt();
Serial.print(" Received "); Serial.println(reC);
//...... SEND BACK ..............
String sendbacK = String(reC);
sendMessage(sendbacK);
Serial.print(" Sentback "); Serial.println(sendbacK);
}
//.................................................................... VOID LOOP .............................................RX RECEIVER
void loop() {
receiveMessage(LoRa.parsePacket()); // RECEIVE MESSAGE
}
Uploading: With all EMon removed from RX sketch.png...
Thanks
Please edit your post to add code tags.
Apologies. Hope this is better
//.................................................................. RX RECEIVER ..........................................
#include <SPI.h>
#include <LoRa.h>
byte localAddress = 0xBB;
byte destinationAddress = 0xAA;
//................................................................. VARIABLES ............................................RX RECEIVER
long lastSendTime = 0;
int reC = 0;
int statioN = 0;
int onofF = 0;
int incom = reC;
int aaa = 0; // Temp Variable
int loopCount = 0; // Energy Monitor Loop
int currentDraw = 0;
int AConoff = 0; // AC either ON = 1 or OFF = 0
int staToNOFF = 0; // Even number is OFF odd number is ON
int relay = 0;
void setup() {
Serial.begin(9600);
//................................................................ STARTING LORA ....................................RX RECEIVER
Serial.println("Start LoRa duplex RX Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//................................................................... VOID OUTGOING DATA .....................................RX RECEIVER
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
Serial.print(" sending back " ); Serial.println(outgoing);
}
//................................................................... VOID INCOMING DATA ............................................RX RECEIVER
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
reC = incoming.toInt();
Serial.print(" Received "); Serial.println(reC);
//...... SEND BACK ..............
String sendbacK = String(reC);
sendMessage(sendbacK);
Serial.print(" Sentback "); Serial.println(sendbacK);
}
//.................................................................... VOID LOOP .............................................RX RECEIVER
void loop() {
receiveMessage(LoRa.parsePacket()); // RECEIVE MESSAGE
}
Please edit your posts to add code tags.
For instructions, see the "How to get the best out of the forum" post, linked at the head of every forum category.
//.................................................................. RX RECEIVER ..........................................
#include <SPI.h>
#include <LoRa.h>
byte localAddress = 0xBB;
byte destinationAddress = 0xAA;
//................................................................. VARIABLES ............................................RX RECEIVER
long lastSendTime = 0;
int reC = 0;
int statioN = 0;
int onofF = 0;
int incom = reC;
int aaa = 0; // Temp Variable
int loopCount = 0; // Energy Monitor Loop
int currentDraw = 0;
int AConoff = 0; // AC either ON = 1 or OFF = 0
int staToNOFF = 0; // Even number is OFF odd number is ON
int relay = 0;
void setup() {
Serial.begin(9600);
//................................................................ STARTING LORA ....................................RX RECEIVER
Serial.println("Start LoRa duplex RX Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//................................................................... VOID OUTGOING DATA .....................................RX RECEIVER
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
Serial.print(" sending back " ); Serial.println(outgoing);
}
//................................................................... VOID INCOMING DATA ............................................RX RECEIVER
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
reC = incoming.toInt();
Serial.print(" Received "); Serial.println(reC);
//...... SEND BACK ..............
String sendbacK = String(reC);
sendMessage(sendbacK);
Serial.print(" Sentback "); Serial.println(sendbacK);
}
//.................................................................... VOID LOOP .............................................RX RECEIVER
void loop() {
receiveMessage(LoRa.parsePacket()); // RECEIVE MESSAGE
}
Thank you for using code tags. What do you expect the code to do, and what does it do instead?
As an aside, if you are using an AVR-based Arduino (Uno, Nano, Mega, etc.) it is a serious mistake to use Strings. They cause memory problems and unexpected program crashes.
As per my original post I wish to remove all instances of EMon from the RX sketch but when I doTX fails to receive any message from the RX. Leaving the EMon instances IN the RX sketch and TX receives message OK
You have posted new, edited code.
What, exactly, does the new code do when you run it? What have you done to debug the problem?
"Fails to receive any message" does not tell us much.
Here are Screen prints of Serial Monitor Monitor Outputs
WITH EMON INCLUDED IN RX SKETCH
WITH EMON REMOVED FROM RX SKETCH
pylon
August 29, 2022, 5:06pm
14
Remove the delay() call from sendMessage() routine in your TX sketch.
If you use the Emon call your RX sketch wastes some time there and sends the message back later. Without that call it sends the message back immediately, but the TX sketch is delaying and therefor missing the data.
Thank you so much Pylon for your solution.
Without the delay it works just fine.
As they say " Timing is everythinmg "
Appreciated