Thanks TOM.
I have no thing connected to the LoRa to try to connect.
I am using Duinotech shields with Mega 2560 boards.
I have some 868MH and 915MH shields but ensure that I do not mix them up.
Tx transmits to RX and RX receives just fine.
Change the TX and RX boards around and the one way transmissions work just fine BUT
when I try to get either to reply back to other the RX says its sending but the TX receives
"0" packets.
The Lora Shields are Brand New from Jaycar and I have tried several Mega 2560 boards.
The strange thing is that this code was running perfectly for over 7 months I recently decided
to change the LED's on the Sender and try out a " Nextion " board to display the " status".
type or paste code//.................................................................. 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 ............................................
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
float currentDraw = 0;
int AConoff = 0; // AC either ON = 1 or OFF = 0
void setup() {
Serial.begin(9600);
//................................................................. ENERGY MONITOR ......................................
emon1.current(0, CURRENT_CAL); // Current: input pin, calibration.
//................................................................ LED SETUP ............................................
pinMode(6, OUTPUT); //.................TX LED
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
pinMode(7, OUTPUT);//.................RX LED
digitalWrite(7, HIGH);
delay(500);
digitalWrite(7, LOW);
for (aaa = 3; aaa < 6; aaa++) {
pinMode(aaa, OUTPUT); digitalWrite(aaa, LOW); delay(500); digitalWrite(aaa, HIGH);
}
//................................................................. RELAY PIN SETUP ..................................
pinMode(23, OUTPUT); digitalWrite (23, HIGH); // RELAY 1
pinMode(24, OUTPUT); digitalWrite (24, HIGH); // RELAY 2
pinMode(25, OUTPUT); digitalWrite (25, HIGH); // RELAY 3
//................................................................ STARTING LORA ....................................
Serial.println("Start LoRa duplex RX Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
//................................................................. ENERGY MONITOR ......................................
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 > 12){
AConoff = 0; //..... ON ....
Serial.println(" Power is ON AConoff is 0 ");
}
else {
AConoff = 1; //..... OFF ....
Serial.println(" Power is OFF AConoff is 1 ");
}
int temp1 = ((statioN*10)+(AConoff));
//Serial.print("statioN ");Serial.print(statioN);Serial.print(" * 100 ");Serial.print(" + ");Serial.print(" AConoff ");Serial.print(AConoff);Serial.print(" = ");
Serial.print(" Send back to TX ");Serial.println(temp1);
delay(2000);
String send1 = String(temp1);
// Serial.print(" AC On Off is "); Serial.println(send1);
sendMessage(send1); // SEND BACK TO TX
Serial.print(" ****SENT MESSAGE BACK TO TX***");Serial.println(temp1);
delay(500);
}
//................................................................... OUTGOING DATA .....................................
void sendMessage(String outgoing) {
if (reC > 200) {
//Serial.print("Current: ");
//Serial.println(currentDraw);
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
//Serial.print(" sending back " ); Serial.println(outgoing);
}
}
//................................................................... INCOMING DATA ............................................
void receiveMessage(int packetSize) {
//Serial.print(" packet Size is ");Serial.println(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 .............................................
void station() {
if ((reC > 229) && (reC <252))// CHANGE TO >229 & < 252 when using RX as Tank Fill and Fruit trees (230 to 250)
{
Serial.println(reC);
statioN = ((reC / 10));
onofF = (reC - ((reC / 10) * 10));
// Serial.print(" Station Received is "); Serial.println(statioN);
//Serial.print(" Station onoff is "); Serial.println(onofF);
if (onofF == 0) {
digitalWrite((statioN), LOW);// Turn ON Relay
//Serial.print (statioN); Serial.print (" Relay is ON");
}
if (onofF == 1) {
digitalWrite((statioN), HIGH);// Turn OFF Relay
// Serial.print (statioN); Serial.println (" Relay is OFF");
}
currenT();
}
}
//.................................................................... VOID LOOP .............................................
void loop() {
receiveMessage(LoRa.parsePacket()); // RECEIVE MESSAGE
type or paste code here
} here
//................................................................. TX SENDER ..........................................
#include <SPI.h>
#include <LoRa.h>
//................................................................. RTC DS 3231 ............
#include <Wire.h>
#include <ds3231.h>
struct ts t;
//................................................................ LORA ADDRESSES .............
byte localAddress = 0xAA;
byte destinationAddress = 0xBB;
long lastSendTime = 0;
long lastSendTime1 = 0;
unsigned int interval = 10000;
unsigned int interval1 = 0;
//................................................................ VARIABLES
int count = 0; unsigned int staT = 220; // OUTGOING
int countRec = 0; int staTRec = 0; // INCOMING
int statioN = 0; // STATION NUMBER 22 to 33
int onofF = 0; // 0 = ON ... 1 = OFF
int daY = 0; int houR = 0; int miN = 0; int seC = 0;
int MINUTE = 0;
int SECOND = 0;
int adjtime = 59;// Delete after test Set to current minute for sequental station test
int NexNo = 0;
long int pco = 0;
long int fonT = 0;
long int reC = 0;
//________________________________________________________________________________________________________________
//_______________________________________________________________________________________ VOID SETUP __________
void setup() {
Serial.begin(9600);
//.............................................................. RTC START ............
Serial2.begin(9600); // Use Pins 16 & 17 for Serial2
Wire.begin();
DS3231_init;
//.............................................................. SET TIME ...........
//t.hour = 16; t.min = 7; t.sec = 10; t.mday = 6; t.mon = 11; t.year = 2021;
//DS3231_set(t);
//.............................................................. LED SETUP .......................
//............................................................. STARTING LORA ....................
Serial.println("Start LoRa duplex TX Sender");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
//............................................................... LED SETUP ......................
pinMode(22, OUTPUT); digitalWrite(22, HIGH); delay(1000); digitalWrite(22, LOW);
}
//........................................................................... VOID NEXTION END ROUTINE ..............
void NexEnd() {
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
void neXtimE() {
Serial2.print(F("t0.txt="")); // make sure t number is correct
Serial2.print(F("Time "));
Serial2.print(houR);
Serial2.print(F(":"));
Serial2.print(miN);
Serial2.print(F("""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
//.............................................................. CHANGE NEXTION ATTRIBUTES .........................
void neXAttrib()
{
//Serial.print(" NexNo ");Serial.print( NexNo );Serial.print(" fonT ");Serial.print( fonT );Serial.print(" pc0 ");Serial.println(pco);
Serial2.write('n');
Serial2.print(NexNo);
Serial2.print(F(".pco="));
Serial2.print(pco);
NexEnd();
Serial2.write('n');
Serial2.print(NexNo);
Serial2.print(F(".font="));
Serial2.print(fonT);
NexEnd();
}
//.............................................................. VOID TIME PRINT OUT ...........
//.............................................................. VOID SENDING DATA ...........................
void senddatA()
{
if (millis() - lastSendTime > 2000) {
String sensorData = String(staT);
sendMessage(sensorData);
Serial.print(" SENT " + sensorData); Serial.print(" at "); Serial.print(houR); Serial.print(":"); Serial.print(miN); Serial.print(":"); Serial.println(seC);
// Serial.print(" from 0x" + String(localAddress, HEX));
// Serial.println(" to 0x" + String(destinationAddress, HEX));
lastSendTime = millis();
}
}
//............................................................. VOID SEND MESSAGE .............................
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 .........................
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();
delay(500);
reC = incoming.toInt();
}
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;
}
Serial.print(" reC is "); Serial.println(reC);
NexNo = reC / 100;
// Serial.print (" NEXNO STAT is "); Serial.println(NexNo);
onofF =(reC-(NexNo*100));
//Serial.print (" Station is ( 0 is ON 1 is OFF) "); Serial.println(onofF);
if(onofF==0){
//Serial.print(NexNo);Serial.println(" CHANGE TO GREEN");
pco = 2016;
neXAttrib();
}
station();
}
//............................................................. VOID STATION ..............................
void station() {
/* //............................................................ Station 22
if (miN == (adjtime + 0)) {
staT = 220;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
if (onofF==0){
pco = 2016;
}
else{
pco = 63504; // RED
}
neXAttrib();
}
if (miN == (adjtime + 1)) {
staT = 221;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 23
if (miN == (adjtime + 2)) {
staT = 230;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
if (onofF==0){
pco=2016;
}
else{
pco = 63504; // RED until ONOFF CHANGES TO "0"
}
neXAttrib();
}
if (miN == (adjtime + 3)) {
staT = 231;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 24
if (miN == (adjtime + 4)) {
staT = 240;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
if (onofF == 0) {
pco = 2016;
}
else {
pco = 63504; // RED until ONOFF CHANGES TO "0"
}
neXAttrib();
}
if (miN == (adjtime + 5)) {
staT = 241;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 25
if (miN == (adjtime + 6)) {
staT = 250;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
if (onofF == 0) {
pco = 2016 ;
}
else {
pco = 63504; // RED until ONOFF CHANGES TO "0"
}
neXAttrib();
}
if (miN == (adjtime + 7)) {
staT = 251;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
*/
//............................................................ Station 26 .. Fr. ENTRY GARDEND
if (miN == (adjtime + 0)) {
staT = 260;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 1)) {
staT = 261;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 27 .. N.W.GARDENS
if (miN == (adjtime + 2)) {
staT = 270;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 3)) {
staT = 271;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 28 .. N.E.GARDENS
if (miN == (adjtime + 4)) {
staT = 280;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 5)) {
staT = 281;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 29 .. GARDENS ( EAST & FISH POND)
if (miN == (adjtime + 6))
{
staT = 290;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 7))
{
staT = 291;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 30 .. EAST LAWNS
if (miN == (adjtime + 8))
{
staT = 300;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 9))
{
staT = 301;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 31 .. EAST FISH POND
if (miN == (adjtime + 10))
{
staT = 310;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 11))
{
staT = 311;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
//............................................................ Station 32 .. WEST HOUSE GARDEN BEDS
if (miN == (adjtime + 10))
{
staT = 320;
senddatA();
NexNo = (staT / 10);
fonT = 1; // Large Font
pco = 63504; // RED
neXAttrib();
}
if (miN == (adjtime + 11))
{
staT = 321;
senddatA();
NexNo = (staT / 10);
fonT = 3; // Small Font
pco = 65535; // WHITE
neXAttrib();
}
}
//_________________________________________________________________________________________________________________
//_________________________________________________________________________ VOID LOOP ___________
void loop() {
// station(); // TEST ONLY
staT = 230; // TEST ONLY
senddatA(); // TEST ONLY
//............................................................ GET TIME .................
DS3231_get(&t);
daY = (t.mday); houR = (t.hour); miN = (t.min); seC = (t.sec);
//............................................................ RECEIVING DATA .....................
receiveMessage(LoRa.parsePacket());
neXtimE();
}