arduino-LoRa
arduino-LoRa copied to clipboard
Using HSPI ESP32 for LoRa
i configure my lora set on HSPI on ESP32 and use LoRaDuplexCallback function but only transmit but not receive data from node. Someone help me please! My code:
#define lora_sck 14
#define lora_miso 12
#define lora_mosi 13
#define lora_cs 15
#define lora_rst 4
#define lora_irq 16
#define nodeFourth_addr 0x04
#define fre 433E6
SPIClass hspi(HSPI) ;
LoRaClass LoRa2;
void lora_Config();
void sendRequest(byte addr_destination, String message);
void onReceive1(int packetSize);
void lora_Config()
{
// pinMode(lora_cs, OUTPUT);
hspi.begin(lora_sck, lora_miso, lora_mosi);
LoRa2.setSPI(hspi);
LoRa2.setPins(lora_cs, lora_rst, lora_irq);
// override the default CS, reset, and IRQ pins (optional)
if (!LoRa2.begin(fre)) { // initialize ratio at 915 MHz
Serial.println("LoRa init failed. Check your connections.");
delay(5000);
ESP.restart();
}
LoRa2.setTxPower(20);
LoRa2.onReceive(onReceive1);
LoRa2.receive();
LoRa2.enableCrc();
Serial.println("LoRa init succeeded.");
}
void sendRequest(byte addr_destination, String message)
{
uint8_t data_send[3];
data_send[0] = addr_destination;
data_send[1] = gattway_addr;
data_send[2] = message.length();
uint8_t crc=crc8(data_send, 3);
LoRa2.beginPacket();
LoRa2.write(data_send[0]);
LoRa2.write(data_send[1]);
LoRa2.write(data_send[2]);
LoRa2.write(crc);
LoRa2.print(message);
LoRa2.endPacket();
// state_request=false;
}
void onReceive1(int packetSize)
{
packetSize = LoRa2.parsePacket();
byte data_rec[4];
String LoRaData="";
if (packetSize == 0) return;
data_rec[0]=LoRa2.read(); //Serial.println(data_rec[0]);//addr gatt
data_rec[1]=LoRa2.read();//Serial.println(data_rec[1]);// addr node
data_rec[2]=LoRa2.read(); //Serial.println(data_rec[2]);
data_rec[3]=LoRa2.read();// Serial.println(data_rec[2]);
while (LoRa2.available()) LoRaData = LoRa2.readString(); Serial.println(LoRaData);
if ((data_rec[0] != gattway_addr)|| (data_rec[1]!= destination_addr )){ Serial.println("not for me."); return ;}
if(data_rec[3] != crc8(data_rec, 3)){ Serial.println("crc faile"); return;}
if(LoRaData.length() != data_rec[2]){ Serial.println("data length faile"); return;}
decode_data(data_rec[1],LoRaData);
}
void setup() {
Serial.begin(115200);
lora_Config();
xTaskCreate(task_lora_data,"lora",5000,NULL,0,&TaskHandle_1);
}
unsigned long t1=0;
int i=0;
int rd=0;
static bool task_status=false;
static bool set_status=false;
static void task_lora_data(void *pvParameters)
{
for(;;)
{
if ((unsigned long)(millis() - t1) >(1000-rd))
{
t1 = millis();
if(state_request && !task_status )
{
state_request=false;
destination_addr =nodeFourth_addr;
cmd =cmdR;
sendRequest(destination_addr,cmd);
LoRa2.receive();
if(state_request==false) {state_request =true;i+=1;if(i>3) i =0;}
}
}
vTaskDelay(10);
}
}
Hello, Is this still a problem?
I am also using ESPs HSPI and it is working for me.
I think you should remove this line: hspi.begin(lora_sck, lora_miso, lora_mosi);
Here is how I use it:
SPIClass hspi = NULL;
void setup() {
hspi = SPIClass(HSPI);
LoRa.setSPI(hspi);
LoRa.setPins(RF_CS, RF_RST, RF_IRQ);
...