On the attached sketch I am seeking a way to substitute (Line 35) "Lora.write(Node2) "for a variable "Lora.write(Variable).
eg If variable was a "23" then thats for for Node1.
If variable was a "76" then thats for for Node2. etc, etc
Is that possible ??
Thanks in advance
#include <SPI.h>
#include <LoRa.h>
byte Master = 0xAA; // int is 170
byte Node1 = 0xBB; // int is 187
byte Node2 = 0xCC; // int is 204
byte Node3 = 0xDD; // int is 221
byte Node4 = 0xEE; // int is 238
long int senT = 0;
long int reC = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start LoRa duplex TX Sender");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
void senddatA()
{
String sendbacK = String(senT);
sendMessage(sendbacK);
}
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(Node2);
LoRa.write(Master);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
}
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 != Master) {
//Serial.println("Error: Recipient address does not match local address");
return;
}
reC = incoming.toInt();
}
void loop() {
senT = 120; senddatA();
receiveMessage(LoRa.parsePacket());
}
Thanks but what I am looking for is something that can be changed easilly depending on the what variable is rteceived from somewhere else in the sketch
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(Node2); ****** I am looking for some way to change this using another "variable".
EG if I want to send an interger only to node 1 ,then I would use
If ("variable"= 1){ "then node = Node1"}
if ("variable"= 2){ "then node = Node2"}
if ("variable"= 3){ "then node = Node3"}
********
LoRa.write(Master);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
}
LoRa.write(Node2); ****** I am looking for some way to change this using another "variable".
EG if I want to send an interger only to node 1 ,then I would use
If ("variable"= 1){ "then node = Node1"}
if ("variable"= 2){ "then node = Node2"}
if ("variable"= 3){ "then node = Node3"}
I realise that I can change the Node number manually but I wish to change it from outside " void sendMessage(String outgoing()" .
Perhaps you could give me an example of how this is done.
I apologise for not being able to express my problem adequately.
Heres a way I have come up with achieving my goal using "case" but
its the first time using "case" and I cannot get it work.
The " switch (var) " selects only the first "case" so that if the " switch (var) " is 1 it works just fine however if the " switch (var) " is 2 , 3 , or 4 it does not seem to switch to the appropriate "case 2,3 or4".
What am I missing ?
#include <SPI.h>
#include <LoRa.h>
byte Master = 0xAA; // int is 170
byte Node1 = 0xBB; // int is 187
byte Node2 = 0xCC; // int is 204
byte Node3 = 0xDD; // int is 221
byte Node4 = 0xEE; // int is 238
long int senT = 0;
long int reC = 0;
int var = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start LoRa duplex TX Sender");
if (!LoRa.begin(915E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
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();
}
void SenDDatA() {
Serial.print(" Inside SENDDATA() (VAR) = "); Serial.println(var);
switch (var)
{
case 1:
Serial.println(" Inside CASE 1 ");
String outgoing1 = String(senT);
LoRa.beginPacket();
Serial.print(" Sent to Node 1 ");
LoRa.write(Node1);
LoRa.write(Master);
LoRa.write(outgoing1.length());
LoRa.print(outgoing1);
Serial.println(outgoing1);
LoRa.endPacket();
break;
case 2:
Serial.println(" Inside CASE 2 ");
String outgoing2 = String(senT);
LoRa.beginPacket();
LoRa.write(Node2);
LoRa.write(Master);
LoRa.write(outgoing2.length());
LoRa.print(outgoing2);
Serial.println(outgoing2);
LoRa.endPacket();
break;
default:
Serial.println(" No Case Number ");
}
}
void loop() {
var = 2;
senT = 65; SenDDatA();
receiveMessage(LoRa.parsePacket());
}