Hello. I've been trying to communicate two RFM69HCW (433 MHz) to each other while connecting it to Arduino Nano 33 BLE however, I've been getting 'LoRa initialization failed' error. Here's my connections:
Arduino Nano 33 ---------- RFM69HCW
3.3 V ---------- 3.3 V
GND ---------- GND
D13 ---------- SCK
D12(CIPO) ---------- MOSI
D11 (COPI) --------- MISO
D10 --------- CS (Chip Select)
D9 --------- RST
D2 --------- DI0
Here's the code I'm using that I got from the internet.
#include <SPI.h> // include libraries
#include <LoRa.h>
const int csPin = 10; // LoRa radio chip select
const int resetPin = 9; // LoRa radio reset
const int irqPin = 2; // change for your board; must be a hardware interrupt pin
String outgoing; // outgoing message
String message; // input message
byte msgCount = 0; // count of outgoing messages
byte localAddress = 0x01; // address of this device
byte destination = 0x02; // destination to send to
long lastSendTime = 10000; // last send time
int interval = 2000; // interval between sends
void setup() {
Serial.begin(9600); // initialize serial
while (!Serial);
Serial.println("LoRa RFM9x Radio - Transmitter");
// override the default CS, reset, and IRQ pins (optional)
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
if (!LoRa.begin(434E6)) { // initialize ratio at 915 MHz
Serial.println("LoRa initialization failed. Check your connections.");
while (true); // if failed, do nothing
}
Serial.println("LoRa initialization succeeded.");
delay (1000);
Serial.println("Please enter your message:");
delay (1000);
}
void loop() {
if (millis() - lastSendTime > interval) {
while(Serial.available())
{
Serial.print("Sending message: ");
message = Serial.readString();
sendMessage(message);
//Serial.print("Sending: " + message);
Serial.print(message);
}
lastSendTime = millis(); // timestamp the message
interval = random(10) + 1000; // 2-3 seconds
}
// parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket());
}
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();
}
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();
Serial.println("You have received a data!");
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();
}