sandeepmistry / arduino-LoRa

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

lora duplex: error: message length does not match length #472

Closed mongtron closed 3 years ago

mongtron commented 3 years ago

Hi sir. i'm getting trouble in my project. i have 2 esp32 modules and 2 lora ra-02 modules. one lora module is gateway and another is end node. the gateway will ask the end node: "node1". and the end node will check the string that it received, if the string equal to "node1" it will send string to gateway: "20.5 70.0". in stead of receiving "20.5 70.5", the gateway received: "node170.0". i used lora duplex in example folder and change something in it. I look forward to everyone's help. thank you. sorry, because my english is quite bad. here is my gateway code:

include // include libraries

include

const int csPin = 5; // LoRa radio chip select const int resetPin = 14; // LoRa radio reset const int irqPin = 2;

String outgoing; String dl; int a = 0; float t, h; byte msgCount = 0; // count of outgoing messages byte localAddress = 0xBB; // address of this device byte destination = 0xFF; // destination to send to

void setup() { Serial.begin(9600); // initialize serial while (!Serial); Serial.println("LoRa Duplex, lora gateway");

// override the default CS, reset, and IRQ pins (optional) LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin

if (!LoRa.begin(433E6)) { // initialize ratio at 915 MHz Serial.println("LoRa init failed. Check your connections."); while (true); // if failed, do nothing }

Serial.println("LoRa init succeeded."); } void sendMessage(String outgoing) { LoRa.beginPacket(); // start packet LoRa.write(destination); // add destination address LoRa.write(localAddress); // add sender address LoRa.write(msgCount); // add message ID LoRa.write(outgoing.length()); // add payload length LoRa.print(outgoing); // add payload LoRa.endPacket(); // finish packet and send it msgCount++; // increment message ID } 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 = "";

while (LoRa.available()) { incoming += (char)LoRa.read(); } dl = incoming; if (incomingLength != incoming.length()) { // check length for error Serial.println("error: message length does not match length"); return; // skip rest of function }

// if the recipient isn't this device or broadcast, if (recipient != localAddress && recipient != 0xFF) { Serial.println("This message is not for me."); return; // skip rest of function }

// if message is for this device, or broadcast, print details: Serial.println("Received from: 0x" + String(sender, HEX)); Serial.println("Sent to: 0x" + 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(); a = 1; } void loop() { String message = "node1"; // send a message sendMessage(message); Serial.println("Sending request to " + message); onReceive(LoRa.parsePacket()); if (a == 1){ int pos1 = dl.indexOf('&'); int pos = dl.indexOf(' '); String dl1 = dl.substring(pos1+1,pos); String dl2 = dl.substring(pos+1); t = dl1.toFloat(); h = dl2.toFloat(); Serial.print("temp: "); Serial.print(t); Serial.print("\t"); Serial.print("humi: "); Serial.println(h); a = 0; } delay(10000);

}

here is my end node code:

include // include libraries

include

const int csPin = 5; // LoRa radio chip select const int resetPin = 14; // LoRa radio reset const int irqPin = 2; // change for your board; must be a hardware interrupt pin

String outgoing; // outgoing message float t = 20.5; float h = 70; int a = 0; String request; byte msgCount = 0; // count of outgoing messages byte localAddress = 0xFF; // address of this device byte destination = 0xBB; // destination to send to

void setup() { Serial.begin(9600); // initialize serial while (!Serial);

Serial.println("LoRa Duplex, lora end node");

// override the default CS, reset, and IRQ pins (optional) LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin

if (!LoRa.begin(433E6)) { // initialize ratio at 915 MHz Serial.println("LoRa init failed. Check your connections."); while (true); // if failed, do nothing }

Serial.println("LoRa init succeeded.");

} void sendMessage(String outgoing) { LoRa.beginPacket(); // start packet LoRa.write(destination); // add destination address LoRa.write(localAddress); // add sender address LoRa.write(msgCount); // add message ID LoRa.write(outgoing.length()); // add payload length LoRa.print(outgoing); // add payload LoRa.endPacket(); // finish packet and send it msgCount++; // increment message ID }

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 = "";

while (LoRa.available()) { incoming += (char)LoRa.read(); } request = incoming; if (incomingLength != incoming.length()) { // check length for error Serial.println("error: message length does not match length"); return; // skip rest of function }

// if the recipient isn't this device or broadcast, if (recipient != localAddress && recipient != 0xFF) { Serial.println("This message is not for me."); return; // skip rest of function }

// if message is for this device, or broadcast, print details: Serial.println("Received from: 0x" + String(sender, HEX)); Serial.println("Sent to: 0x" + 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(); a = 1; }

void loop() { onReceive(LoRa.parsePacket()); if (a == 1){ if (request == "node1"){ String message = String(t, 1)+" "+String(h, 1); sendMessage(message); a = 0; } }

}

IoTThinks commented 3 years ago

Use the bellow code for address instead of inserting node1. byte localAddress = 0xBB; // address of this device byte destination = 0xFF; // destination to send to

BTW likely due to your custom code.

If you hit this "message length does not match length", the code should return and doing nothing.

mongtron commented 3 years ago

thanks sir. i will try it