Internet of Things Asked by metichi on August 6, 2020
I am trying to send messages from my arduino to my raspberry pi. But I don’t understand why the pi isn’t receiving the messages.
On the arduino side Im using an arduino UNO with a Dragino Lora/GPS shield, using the Radiohead library.
On the Pi side, the Upotronics LoRa hat for the pi zero and a pi zero, which is based on a RFM95 chip. And on the software side I’m using the Raspi Lora library.
I wrote a couple of simple programs just to test the connection, and I can send messages from the raspberry to the arduino, but not the other way around.
Am I making a mistake in the code on either sending the data from the arduino or receiving it on the raspberry that prevents this?
Code for the arduino: This one sends data it gets from serial port and awaits radio input when nothing is available.
#include <SPI.h>
#include <RH_RF95.h>
RH_RF95 rf95;
void setup(){
Serial.begin(9600);
if (rf95.init()){
Serial.println("Init Success");
} else {
Serial.println("Init Failed");
}
if (rf95.setFrequency(868)) Serial.println("Freq set for 868Mhz");
if (!rf95.setModemConfig(RH_RF95::Bw125Cr45Sf128)) Serial.println("Invalid modem");
Serial.println("Send serial data to echo it through radio");
}
void loop(){
uint8_t data[100];
uint8_t len;
if (Serial.available()){
delay(20);
int i = 0;
while (Serial.available() && i < sizeof(data)-1){
data[i] = Serial.read();
i++;
data[i] = 0;
}
rf95.setModeTx();
if(rf95.send(data,i))
{
Serial.print("Message sent: ");
Serial.println((char *) data);
Serial.println(i);
} else {
Serial.println("Failure to send");
}
}
if (rf95.available())
{
if (rf95.recv((uint8_t *)data,&len)){
Serial.println("Got it");
Serial.println((char *)data);
Serial.println(len);
}
}
}
Raspberry pi code: This one just initializes the lora instance, I run it on the interactive console and in theory it should print data, but as much as I tried I haven’t managed to catch an IRQ on any of the pins when I send a message.
from raspi_lora import LoRa, ModemConfig
# This is our callback function that runs when a message is received
def on_recv(payload):
print("From:", payload.header_from)
print("Received:", payload.message)
print("RSSI: {}; SNR: {}".format(payload.rssi, payload.snr))
lora = LoRa(0, 25, 2,freq=868, receive_all=True, modem_config=ModemConfig.Bw125Cr45Sf128)
lora.on_recv = on_recv
lora.set_mode_rx()
I figured it out.
Turns out there was a bug in the raspi_lora
library I used for my python code.
It is so that, if you are not specifically sending to the device address or have receive_all=True
, it will do nothing with the messages.
If you plan to use the raspi_lora library you should replace line 268 in the lora.py file with
if (self._this_address != header_to) and ((header_to != BROADCAST_ADDRESS) or (self._receive_all is False)):
Correct answer by metichi on August 6, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP