LoRa Star Network #620
Unanswered
Anonymouse-X
asked this question in
Q&A
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using Ra-02 Ai-Thinker with Arduino Uno and Arduino Mega. Can someone help me with this? Why I am not receiving the sent byte from Sensor_Data_Receiver to Sensor_Data_Sender? And also can you verify wheter what I did in coding is correct and what are the improvements I can make here.
What I am trying to do here is to send request first from Sensor_Data_Receiver to Sensor_Data_Sender before sending the sensor data.
I will connect 2 other nodes later. It will be like 1 receiver with 3 sensor data transmitters.
Here are the codes
`//Sensor Data Receiver
#include <SPI.h> // include libraries
#include <LoRa.h>
struct Data {
byte a;
float b;
float c;
float d;
float e;
float f;
float g;
float h;
float i;
} data;
unsigned long previousMillis=0;
unsigned long int previoussecs = 0;
unsigned long int currentsecs = 0;
unsigned long currentMillis = 0;
int interval= 1 ; // updated every 1 second
int Secs = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");
LoRa.setPins(10, 9, 2); // 10 for UNO, 53 for Mega
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setGain(6);
LoRa.setSyncWord(0x22);
LoRa.onReceive(onReceive);
LoRa.onTxDone(onTxDone);
LoRa_rxMode();
}
void loop() {
// put your main code here, to run repeatedly:
currentMillis = millis();
currentsecs = currentMillis / 1000;
if ((unsigned long)(currentsecs - previoussecs) >= interval) {
Secs = Secs + 1;
if ( Secs >= 7){
Secs = 1;
}
}
onReceive(LoRa.parsePacket());
}
void LoRa_rxMode(){
LoRa.disableInvertIQ(); // normal mode
LoRa.receive(); // set receive mode
Serial.println("Rx Mode!");
}
void LoRa_txMode(){
LoRa.idle(); // set standby mode
LoRa.enableInvertIQ(); // active invert I and Q signals
Serial.println("Tx Mode!");
}
void sendMessage(byte otherNode){
LoRa.beginPacket(); // start packet
LoRa.write(otherNode); // add destination address
LoRa.endPacket(true); // finish packet and send it
Serial.println(otherNode);
}
void onReceive(int packetSize){
// try to parse packet
if (packetSize) {
// received a packet
// Serial.print("\nReceived packet size ");
// Serial.print(packetSize);
// read packet
while (LoRa.available())
for (int i = 0; i < packetSize; i++) {
((byte *) &data)[i] = LoRa.read();
// Serial.print(' ');
// Serial.print(((byte *) &data)[i]);
}
// print RSSI of packet
// Serial.print(" with RSSI ");
// Serial.println(LoRa.packetRssi());
Serial.print(data.a); Serial.print(" ");
Serial.print(data.b); Serial.print(" ");
Serial.print(data.c); Serial.print(" ");
Serial.print(data.d); Serial.print(" ");
Serial.print(data.e); Serial.print(" ");
Serial.print(data.f); Serial.print(" ");
Serial.print(data.g); Serial.print(" ");
Serial.print(data.h); Serial.print(" ");
Serial.print(data.i); Serial.print(" ");
Serial.println(" ");
}
}
void onTxDone() {
Serial.println("TxDone");
Serial.println(" ");
LoRa_rxMode();
}`
`//Sensor Data Sender Node 1
#include <SPI.h> // include libraries
#include <LoRa.h>
struct Data {
byte a;
float b;
float c;
float d;
float e;
float f;
float g;
float h;
float i;
};
Data data = {0xBB, 0, 0, 0, 0, 0, 0, 0, 0};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Sender");
LoRa.setPins(53, 9, 2); // 10 for UNO, 53 for Mega
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setGain(6);
LoRa.setSyncWord(0x22);
LoRa.onReceive(onReceive);
LoRa.onTxDone(onTxDone);
LoRa_rxMode();
}
void loop() {
// put your main code here, to run repeatedly:
onReceive(LoRa.parsePacket());
}
void LoRa_rxMode(){
LoRa.disableInvertIQ(); // normal mode
LoRa.receive(); // set receive mode
Serial.println("Rx Mode!");
}
void LoRa_txMode(){
LoRa.idle(); // set standby mode
LoRa.enableInvertIQ(); // active invert I and Q signals
Serial.println("Tx Mode!");
}
void sendMessage(struct Data) {
LoRa_txMode(); // set tx mode
LoRa.beginPacket(); // start packet
for (unsigned int i = 0; i < sizeof(Data);i++) {
// Serial.print(' ');
LoRa.write(((byte *) &data)[i]);
}
LoRa.endPacket(true); // finish packet and send it
}
void onReceive(int packetSize) {
LoRa.receive();
if (packetSize == 0) return;
byte recipient = LoRa.read();
Serial.print(recipient);
while (recipient != 0xBB){
byte recipient = LoRa.read();
}
data.b += 1;
data.c += 1;
data.d += 1;
data.e += 1;
data.f += 1;
data.g += 1;
data.h += 1;
data.i += 1;
sendMessage(data);
}
void onTxDone() {
Serial.println("TxDone");
Serial.println(" ");
LoRa_rxMode();
}`
Beta Was this translation helpful? Give feedback.
All reactions