Yet another post in the ongoing saga.
For months I have struggled to get the " callback " function of the LoRa .h working.
I have been unable to get any of the " example " call backs to work.
One Way transmission TX to RX and RX to TX works just fine but I just cannot get the
Rx to TX " callback function to work.
I have " cut and pasted" several sketches I have found and now finally have a functioning
program which does exactly as I require, but I have no idea how or why it works.
I have attached an example of part of the code in the hope that someone might be able to
offer a reason for the following anomalies;
-
TX code. The callback function only works if I use a MILLIS timing loop with an INTERVAL
greater than 1999.
If I use a DELAY the TX works, however I get no callback. -
RX code. Unless I use the " VOID CURRENT" I will not get the CALLBACK to work.
I hope someone can offer some relevant and helpful response.
ty//................................................................. TX SENDER ..........................................
#include <SPI.h>
#include <LoRa.h>
//................................................................ LORA ADDRESSES .............SENDER
byte localAddress = 0xAA;
byte destinationAddress = 0xBB;
long lastSendTime = 0;
unsigned int interval = 0;
//................................................................ VARIABLES
int count = 0; unsigned int staT = 220; // OUTGOING
int countRec = 0; long int staTRec = 0; // INCOMING
int statioN = 0; // STATION NUMBER 22 to 33
int onofF = 0; // 0 = ON ... 1 = OFF
//________________________________________________________________________________________________________________
//_______________________________________________________________________________________ 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 > 2000) {
String sensorData = String(staT);
sendMessage(sensorData);
Serial.println(" SENT " + sensorData);
digitalWrite(5, HIGH); delay(1000); digitalWrite(5, LOW);
// Serial.print(" from 0x" + String(localAddress, HEX));
// Serial.println(" to 0x" + String(destinationAddress, HEX));
lastSendTime = millis();
}
}
//............................................................. VOID SEND MESSAGE .............................SENDER
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
delay(500);
}
//............................................................. 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 LOOP ___________SENDER
void loop() {
//............................................................ RECEIVING DATA .....................
receiveMessage(LoRa.parsePacket());
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< DATA TO SEND >>>>>>>>>>>>>>>>>>>>>>>>>
if (millis() - lastSendTime > 2000) {// 2000 and above TX both sends and Receives.If Interval is reduced to less than 2000 it does not send data
staT = 280; senddatA();
lastSendTime = millis();
}
/*
staT = 280; senddatA();
delay(4000); // Using delay TX sends ok and RX sends back but TX does NOT RECEIVE
*/
}pe or paste code here
===============================================================================
typ//.................................................................. 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 interval = 2000;
unsigned 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
}
//................................................................... 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);
digitalWrite(7, HIGH); delay(500); digitalWrite(7, LOW);
}
//................................................................... 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(" REC "); Serial.println(reC);
station();
}
//.................................................................... STATIONS .............................................RX RECEIVER
void station() {
statioN = ((reC / 10));
currenT(); //<<<<<<<<<<<<<<<<<< Delete this line is left IN the TX does not receive the sendback.
//<<<<<<<<<<<<<<<<<< with left in TX receives the SENDBACK OK >>>>>>>>>>>>>>>>>>>>>>>>>>
String staToNOFF = String(4287);
Serial.print(" SendBack 25 to 33 "); Serial.println(staToNOFF);
sendMessage(staToNOFF); // SEND BACK TO TX
}
//.................................................................... VOID LOOP .............................................RX RECEIVER
void loop() {
receiveMessage(LoRa.parsePacket()); // RECEIVE MESSAGE
}e or paste code here