sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.63k stars 626 forks source link

Transmission and recieved problems. #22

Closed datvuspkt closed 7 years ago

datvuspkt commented 7 years ago

i used lora sx1278 thinker ra-2 to make a system. having lora master and 2 lora slaves. but i can't receive feedback message from slave when i sent message from master. i think it has some conflict when Transmissing and receiving. here is code, i just copy and past and fixed a bit master:

#include <SPI.h>
#include <LoRa.h>
String outgoing;              // outgoing message
byte msgCount = 0;            // count of outgoing messages
byte localAddress = 0xFF;     // address of this device
byte destination = 0xBB; 
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("LoRa Receiver");
  LoRa.setPins(10,9,2);
  LoRa.setSPIFrequency(1E6);
  LoRa.begin(433E6);
//  int ok=0;
//do
//{
//  delay(2000);
//  if (!LoRa.begin(433E6)) {
//    Serial.println("Starting LoRa failed!");
//    ok=0;
//  }
//  else
//  {
//    ok=1;
//  }
//}
//while(!ok);
  while(!LoRa.begin(433E6))
    {
      Serial.println("Starting LoRa failed!");delay(1000);
    }
}
void loop() {
  // try to parse packet
//  int packetSize = LoRa.parsePacket();
//  if (packetSize) {
//    // received a packet
//
//    Serial.print("Received packet '");
//
//    // read packet
//    while (LoRa.available()) {
//       Serial.print((char)LoRa.read());
//    }
//    // print RSSI of packet
//    Serial.print("' with RSSI ");
//    Serial.println(LoRa.packetRssi());
//  }
//  delay(100);
onReceive(LoRa.parsePacket()); delay(500);
if(incoming == "ask1")
{
   outgoing="ok";
  sendMessage(outgoing);
  }
}
void onReceive(int packetSize) {
  if (packetSize == 0) return;          // if there's no packet, return

  // read packet header bytes:
  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address
  byte incomingMsgId = LoRa.read();     // incoming msg ID
  byte incomingLength = LoRa.read();    // incoming msg length
  String incoming = "";
                            // skip rest of function
  while (LoRa.available()) {
    incoming += (char)LoRa.read(); 
  }
  if (incomingLength != incoming.length()) {   // check length for error
    Serial.println("error: message length does not match length");
    return;                             // skip rest of function
  }                            // skip rest of function
    if (sender != 0xBB && recipient != 0xFF) {
    Serial.println("This message is not for me.");
    return; }

   Serial.println("Received from:" + String(sender, HEX));
   Serial.println("Sent to:" + String(recipient, HEX));
   Serial.println("Message ID:" + String(incomingMsgId));
   Serial.println("Message length:" + String(incomingLength));
   Serial.println("Message:" + incoming);
   Serial.println("RSSI:" + String(LoRa.packetRssi()));
   Serial.println("Snr:" + String(LoRa.packetSnr()));
   Serial.println(); 
}
void sendMessage(String outgoing){
    LoRa.beginPacket();
    LoRa.write(destination);
    LoRa.write(localAddress);
    LoRa.write(msgCount);
    LoRa.write(outgoing.length());   
    LoRa.print(outgoing);
    LoRa.endPacket();        
    msgCount++; 
 }

and slave:

#include <SPI.h>
#include <LoRa.h>
String outgoing;              // outgoing message
byte msgCount = 0;            // count of outgoing messages
byte localAddress = 0xFF;     // address of this device
byte destination = 0xBB; 
void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("LoRa Receiver");
  LoRa.setPins(10,9,2);
  LoRa.setSPIFrequency(1E6);
  LoRa.begin(433E6);
//  int ok=0;
//do
//{
//  delay(2000);
//  if (!LoRa.begin(433E6)) {
//    Serial.println("Starting LoRa failed!");
//    ok=0;
//  }
//  else
//  {
//    ok=1;
//  }
//}
//while(!ok);
  while(!LoRa.begin(433E6))
    {
      Serial.println("Starting LoRa failed!");delay(1000);
    }
}
void loop() {
  // try to parse packet
//  int packetSize = LoRa.parsePacket();
//  if (packetSize) {
//    // received a packet
//
//    Serial.print("Received packet '");
//
//    // read packet
//    while (LoRa.available()) {
//       Serial.print((char)LoRa.read());
//    }
//    // print RSSI of packet
//    Serial.print("' with RSSI ");
//    Serial.println(LoRa.packetRssi());
//  }
//  delay(100);
onReceive(LoRa.parsePacket()); delay(500);
if(incoming == "ask1")
{
   outgoing="ok";
  sendMessage(outgoing);
  }
}
void onReceive(int packetSize) {
  if (packetSize == 0) return;          // if there's no packet, return

  // read packet header bytes:
  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address
  byte incomingMsgId = LoRa.read();     // incoming msg ID
  byte incomingLength = LoRa.read();    // incoming msg length
  String incoming = "";
                            // skip rest of function
  while (LoRa.available()) {
    incoming += (char)LoRa.read(); 
  }
  if (incomingLength != incoming.length()) {   // check length for error
    Serial.println("error: message length does not match length");
    return;                             // skip rest of function
  }                            // skip rest of function
    if (sender != 0xBB && recipient != 0xFF) {
    Serial.println("This message is not for me.");
    return; }

   Serial.println("Received from:" + String(sender, HEX));
   Serial.println("Sent to:" + String(recipient, HEX));
   Serial.println("Message ID:" + String(incomingMsgId));
   Serial.println("Message length:" + String(incomingLength));
   Serial.println("Message:" + incoming);
   Serial.println("RSSI:" + String(LoRa.packetRssi()));
   Serial.println("Snr:" + String(LoRa.packetSnr()));
   Serial.println(); 
}
void sendMessage(String outgoing){
    LoRa.beginPacket();
    LoRa.write(destination);
    LoRa.write(localAddress);
    LoRa.write(msgCount);
    LoRa.write(outgoing.length());   
    LoRa.print(outgoing);
    LoRa.endPacket();        
    msgCount++; 
 }

thanks alot.

sandeepmistry commented 7 years ago

@datvuspkt it doesn't look like there is anything in the loop to trigger sending, please see the new duplex examples in https://github.com/sandeepmistry/arduino-LoRa/tree/master/examples