I'm using a LoRa Module on a Moteino with a helical antenna and the RFM95 Radiohead Library, trying out manually selecting a frequency or channel using a rotary DIP switch. Here's the relevant code:
/*calculates the channel based on the dip switch*/
int getChannel() {
uint8_t dip = 0;
if (!digitalRead(DIP_0_PIN)) dip+=1;
if (!digitalRead(DIP_1_PIN)) dip+=2;
if (!digitalRead(DIP_2_PIN)) dip+=4;
if (!digitalRead(DIP_3_PIN)) dip+=8;
return dip;
}
/* Sets Tx/Rx Channel and frequency */
void setChannel(){
if (getChannel() != channel){
/*need a delay so it doesn't try to read
the channel between clicks of the dip switch*/
delay(100);
/*Set channel based on the dip switch*/
channel = getChannel();
Serial.print("Channel selected: ");
Serial.println(channel);
/*Set operating frequency based on the channel selected*/
if (!radio.setFrequency(902.2 + (channel * 1.6)))
{
Serial.println("Selected frequency not supported by this radio.");
while (1);
}
Serial.print("Transmitting and receiving at ");
Serial.print(902.2 + (channel * 1.6));
Serial.println(" mhz...");
}
}
I call the setChannel() function in the main loop for both the Rx and Tx units. This all seems to work fine when both the Tx and Rx unit are set to the same frequency on boot up. And if I change the frequency of the Tx unit, predictably the Rx unit stops receiving until I change the frequency back to match.
HOWEVER, if I change the frequency of the Rx unit and change it back to match, it no longer receives messages from the Tx... even though they are seemingly set to the same frequency. When I reset the Rx unit entirely, it goes back to receiving fine. And sometimes, but not always, after the Rx frequency has changed, if I change the Tx frequency and change it back they reconnect again.
I have no idea where to begin diagnosing this, so any guidance would be appreciated. Also, if anyone knows if it's possible to confirm that a radio is set to a particular frequency after setting it, that might be helpful.
Thank you!