Hello,
I have Cubecell HTTC-AB01 connected to PC1(The Sender), and I want to transmitter to a second Cubecell HTTC-AB01 connected to PC2(Receiver), the sender side work perfect, but in the arduino receiver code, I always have this error: Radio.Init failed!
Here is the code:
#include “LoRaWan_APP.h”
// LoRa P2P parameters (matched to sender)
#define RF_FREQUENCY 433000000 // 433 MHz
#define TX_OUTPUT_POWER 14 // 14 dBm
#define LORA_BANDWIDTH 0 // 0: 125 kHz
#define LORA_SPREADING_FACTOR 7 // SF7
#define LORA_CODINGRATE 1 // 4/5
#define LORA_PREAMBLE_LENGTH 8 // Standard preamble
#define LORA_PAYLOAD_CRC true // Enable CRC
#define LORA_FIX_LENGTH_PAYLOAD false
#define LORA_IQ_INVERSION false
#define MAX_IMAGE_SIZE 1024 // Supports up to 1024 bytes
uint8_t image[MAX_IMAGE_SIZE];
uint16_t imageSize = 0;
uint16_t bytesReceived = 0;
bool receiving = false;
// Log buffer
char logBuffer[1024] = {0};
int logIndex = 0;
void appendLog(const char* msg) {
int len = strlen(msg);
if (logIndex + len + 1 < sizeof(logBuffer)) {
strcpy(&logBuffer[logIndex], msg);
logIndex += len;
logBuffer[logIndex++] = '\n';
}
}
void appendLog(String msg) {
appendLog(msg.c_str());
}
// Callback for received packets
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
String msg = “RxDone: size=” + String(size) + “, rssi=” + String(rssi) + “, snr=” + String(snr);
appendLog(msg);
Serial.println(msg);
// Packet format: [packetID (1 byte), length (1 byte), data (up to 48 bytes)]
if (size < 2) {
String msg = "Invalid packet size: " + String(size);
appendLog(msg);
Serial.println(msg);
Radio.Rx(0); // Continue receiving
return;
}
uint8_t packetID = payload[0];
uint8_t len = payload[1];
if (len > 48 || size != len + 2) {
String msg = "Invalid packet: ID=" + String(packetID) + ", len=" + String(len);
appendLog(msg);
Serial.println(msg);
Radio.Rx(0);
return;
}
if (!receiving) {
receiving = true;
imageSize = 0;
bytesReceived = 0;
appendLog("Started receiving image");
Serial.println("Started receiving image");
}
// Calculate expected offset
uint16_t expectedOffset = packetID * 48;
if (expectedOffset != bytesReceived) {
String msg = "Packet out of order: ID=" + String(packetID) + ", expected offset=" + String(bytesReceived);
appendLog(msg);
Serial.println(msg);
Radio.Rx(0);
return;
}
// Copy data to image buffer
memcpy(&image[bytesReceived], &payload[2], len);
bytesReceived += len;
String pktMsg = "Received packet " + String(packetID) + “, len=” + String(len);
appendLog(pktMsg);
Serial.println(pktMsg);
// Estimate image size from first packet
if (imageSize == 0 && bytesReceived >= 2) {
imageSize = image[0] | (image[1] << 8);
if (imageSize > MAX_IMAGE_SIZE || imageSize == 0) {
String msg = "Invalid estimated image size: " + String(imageSize);
appendLog(msg);
Serial.println(msg);
receiving = false;
imageSize = 0;
bytesReceived = 0;
Radio.Rx(0);
return;
}
String msg = "Estimated image size: " + String(imageSize);
appendLog(msg);
Serial.println(msg);
}
// Check if image is complete
if (bytesReceived >= imageSize && imageSize > 0) {
appendLog("Image received: " + String(bytesReceived) + " bytes");
Serial.println("Image received: " + String(bytesReceived) + " bytes");
// Send image size and data to serial
Serial.write((uint8_t)(bytesReceived & 0xFF)); // Low byte
Serial.write((uint8_t)(bytesReceived >> 8)); // High byte
Serial.write(image, bytesReceived);
Serial.flush();
appendLog("Image sent to serial");
Serial.println("Image sent to serial");
// Print logs and reset
Serial.print(logBuffer);
logIndex = 0;
memset(logBuffer, 0, sizeof(logBuffer));
receiving = false;
imageSize = 0;
bytesReceived = 0;
}
Radio.Rx(0); // Continue receiving
}
// Radio events structure
RadioEvents_t RadioEvents;
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println(“Setup started”);
appendLog(“Setup started”);
Serial.println(“Initializing LoRa…”);
appendLog(“Initializing LoRa…”);
RadioEvents.TxDone = NULL;
RadioEvents.RxDone = OnRxDone;
RadioEvents.TxTimeout = NULL;
RadioEvents.RxTimeout = NULL;
RadioEvents.RxError = NULL;
if (!Radio.Init(&RadioEvents)) {
Serial.println("Radio.Init failed!");
appendLog("Radio.Init failed!");
while (1); // Halt for debugging
}
Serial.println(“Radio.Init succeeded”);
appendLog(“Radio.Init succeeded”);
Radio.SetChannel(RF_FREQUENCY);
Serial.println(“Channel set to 433 MHz”);
appendLog(“Channel set to 433 MHz”);
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_FIX_LENGTH_PAYLOAD, LORA_PAYLOAD_CRC,
0, 0, LORA_IQ_INVERSION, 0, 0, 3000);
Serial.println(“LoRa RX configured”);
appendLog(“LoRa RX configured”);
Radio.Rx(0);
Serial.println(“LoRa in receive mode”);
appendLog(“LoRa in receive mode”);
}
void loop() {
// Clear serial buffer
while (Serial.available()) {
String msg = "Clearing residual byte: 0x" + String(Serial.read(), HEX);
appendLog(msg);
Serial.println(msg);
}
// Process LoRa interrupts
Radio.IrqProcess();
}