I little bit confuse with onReceive handler in arduino software.. I using LoRa to communicate with my arduino. Here is my simple program to read data from LoRa :
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex");
// 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
}
LoRa.setTxPower(20);
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125E3);
LoRa.onReceive(onReceive);
LoRa.receive();
Serial.println("LoRa init succeeded.");
}
void loop() {
// put your main code here, to run repeatedly:
// send a message
readSerialtoSend();
if(getKWH == true)
{
Serial.println("test");
getKWH = false;
}
// parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket());
}
void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return
else
{
Serial.println("Packet Size: " + String(packetSize));
}
// read packet header bytes:
byte master = LoRa.read(); // master address
byte incomingLength = LoRa.read(); // payload length
String incoming = "";
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
}
// if the recipient isn't this device or broadcast,
if (master != masterAddress) {
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("Message: " + incoming);
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println();
getKWH = true;
}
My Question is why i just can read my LoRa data with serial monitor from arduino software, but i cannot read my data From LoRa to other Serial Terminal software?
My Question is why i just can read my LoRa data with serial monitor from arduino software, but i cannot read my data From LoRa to other Serial Terminal software?
I'm sure it doesn't depend on the terminal software used but I may have misinterpreted your question. As you don't describe in detail what problems you have such a misinterpretation is quite probable.
void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return
else
{
Serial.println("Packet Size: " + String(packetSize));
}
onReceive() is called in interrupt context (at least if activated by the LoRa.onReceive() method) and therefor must not call any write method of the Serial object because that depends on interrupts to work which isn't the case in interrupt context (interrupts are disabled during the execution of an interrupt handler).
If Serial Monitor works then any other terminal emulator connected to the same USB virtual serial port should also work. What happens when you try? Have you tried writing a simple sketch for the Arduino to just send some text to see if your terminal emulator works with that?
i don't have problem to send serial data to other terminal software when in normal condition. In setup, i can send data correctly but i cannot send to terminal software in Onreceive handler.. It's really making me confuse.
The real problem comes when i send data to Terminal software in onReceive handler. It never send any data.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex");
// 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."); // can be send
while (true); // if failed, do nothing
}
LoRa.setTxPower(20);
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125E3);
LoRa.onReceive(onReceive);
LoRa.receive();
Serial.println("LoRa init succeeded."); // can be send
}
void loop() {
// put your main code here, to run repeatedly:
// send a message
readSerialtoSend();
if(getKWH == true)
{
Serial.println("test");
getKWH = false;
}
// parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket());
}
void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return
else
{
Serial.println("Packet Size: " + String(packetSize)); // cannot be send
}
// read packet header bytes:
byte master = LoRa.read(); // master address
byte incomingLength = LoRa.read(); // payload length
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
if (incomingLength != incoming.length()) { // check length for error
Serial.println("error: message length does not match length"); // cannot be send
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (master != masterAddress) {
Serial.println("This message is not for me."); // cannot be send
return; // skip rest of function
}
// if message is for this device, or broadcast, print details:
Serial.println("Message: " + incoming); // cannot be send
Serial.println("RSSI: " + String(LoRa.packetRssi())); // cannot be send
Serial.println("Snr: " + String(LoRa.packetSnr())); // cannot be send
Serial.println(); // cannot be send
getKWH = true;
}
i hope this can make it clear. Thank you for your reply.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa Duplex");
// 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
}
//LoRa.setTxPower(20);
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125E3);
//LoRa.onReceive(onReceive);
//LoRa.receive();
Serial.println("LoRa init succeeded.");
}
void loop() {
// put your main code here, to run repeatedly:
// send a message
readSerialtoSend();
// parse for a packet, and call onReceive with the result:
//LoRa.receive();
// onReceive(LoRa.parsePacket());
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
incoming = "";
//Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
// print RSSI of packet
//Serial.print("' with RSSI ");
//Serial.println(LoRa.packetRssi());
}
Serial.println(incoming);
delay(500);
}
This is my last code, i have tried. But it doesn't solve the problem. But i found interesting thing. When i use serial monitor, i can get data from other LoRa transmitter, but when i use other terminal software, it doesn't receive anything. Does LoRa.parsePacket has a connection with serial communication?