Testing RA-02 SX1278 module with STM32L031
I am trying to get this example project working on the STM32L031 development board: https://www.st.com/en/evaluation-tools/nucleo-l031k6.html
I cannot fully understand how am I intended to test this. I have 2 identical STM32L031 devices and connected each STM32L031 to the RA-02 SX1278 lora module.
One is running LoRa transmit code:
while (1)
{
char* send_data;
send_data = "Hello!";
if(LoRa_transmit(&myLoRa, (uint8_t*)send_data, 6, 100) == 1){
printf("Transmit ok \n");
}
else{
printf("Transmit failed \n");
}
HAL_Delay(1500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
The other is running receive code:
LoRa_startReceiving(&myLoRa);
uint8_t received_data[10];
uint8_t packet_size = 0;
while(1){
packet_size = LoRa_receive(&myLoRa, received_data, 10);
if(packet_size > 0 ){
printf("received data = %s \n",received_data);
}
HAL_Delay(500);
}
The logs side by side:
Is that the expected behaviours of this library? How to ensure I receive the correct data? Is it normal that it fails to transmit every now and then?
Hello
Using char* without memory allocation is not recommended.
Please use char* as array and use sprintf and snprintf functions like this:
char send_data[20];
snprintf(send_data, 6, "Hello!");
LoRa_transmit(&myLoRa, (uint8_t*)send_data, 6, 100);
I think this should solve your problem.
@SMotlaq I have updated the transmitter code as you have suggested but im afraid not much changed. I believe issue lies in the receiver code. Something is not right.
The receiver is constantly printing garbage even when I dont have transmitter powered.
Please send me your project and codes [email protected]
@SMotlaq I can share my project through here. You access it via my github: https://github.com/krupis/Lora-STM32
All you need to do is to update :
// 0 - receiver mode
// 1 - transmitter mode
#define MODA_LORA 0
depending on which mode you want to program the device
Additionally, I have put a scope on the DIO0 pin on the receiver device. It is never triggered but on the console it prints the received garbage data. Does your receiver code utilize DIO0 rx interrupt?. I was under the impression that data received should only be printed when the RX interrupt is triggered.
On my STM32, the DIO0 is configured as input pin and is pulled down. (You can check my CubeMX configuration)
Have you checked what value LoRa_init() function returns?
Have you checked what value LoRa_init() function returns?
Yes. I have added the following to my code:
LoRa_reset(&myLoRa);
uint16_t lora_ret = LoRa_init(&myLoRa);
printf("Lora return value = %u \n",lora_ret);
and the result:
Lora return value = 200
Starting RX mode
If it would be helpful, I can connect logic analyzer to SPI pins and check what happens
1- Please reset the buffer first, using memset to check that we get any packet or not:
uint8_t received_data[10];
memset(received_data, NULL, 10);
2- Also please send a variable data from sender. For example "Hello 1", "Hello 2", ...
3- Print the exact values of the received_data. Not the character or ASCII.
@SMotlaq I have modified my receiver code as following:
if(MODA_LORA == 0){
printf("Starting RX mode \n");
LoRa_startReceiving(&myLoRa);
uint8_t received_data[10];
memset(received_data, NULL, 10);
uint8_t packet_size = 0;
while(1){
packet_size = LoRa_receive(&myLoRa, received_data, 10);
if(packet_size > 0 ){
for(int i = 0; i<packet_size;i++){
printf("received data[i] = %u \n",i,received_data[i]);
}
}
memset(received_data, NULL, 10); // Clear the received data buffer after it is received
packet_size = 0; // set packet size to 0 after receiving data.
HAL_Delay(500);
}
}
and my transmitter code as following:
else if(MODA_LORA == 1){
printf("Starting TX mode \n");
while (1)
{
char send_data[20];
for (int i = 0; i < 100; i++)
{
snprintf(send_data, 20, "test%d", i); // puts string into buffer
printf("String to send = %s and length =%u \n", send_data,strlen(send_data)); // outputs so you can see it
if(LoRa_transmit(&myLoRa, (uint8_t*)send_data, 6, 100) == 1){
printf("Transmitting %s successful \n",send_data);
}
else{
printf("Transmit failed \n");
}
HAL_Delay(2000);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
The logs side by side:
As you can see lora receiver receives strange data. This data is being does not have anything to do with my lora transmitter since it will keep receiving:
received data[i] = 0
received data[i] = 1
received data[i] = 2
received data[i] = 3
received data[i] = 4
received data[i] = 5
received data[i] = 6
received data[i] = 7
received data[i] = 8
received data[i] = 9
Even when my transmsitter is powered OFF.
I have updated my git repository for this project if you want to have a look at the latest code.
I will run your code on my hardware as soon as possible and tell you the result.
I will run your code on my hardware as soon as possible and tell you the result.
Sounds good. Please let me know if you are able to reproduce the issue at all. :)
@SMotlaq
I have recently watched your Youtube video regarding this library.
I have noticed a few comments:
Perhaps my issue is related to this?
In my case, NVIC settings are as following
@SMotlaq Additionally, I do not fully understand whether DIO0 signal is set high or low when message is received? Could you confirm whether it is active LOW or active HIGH? I havent been able to find this information
When the module receives a valid LoRa packet, it changes the DIO0 from LOW to HIGH (I don't know how long this pin stays HIGH). You can enable an EXTI in the rising edge mode to detect arriving packets.
You didn't use interrupts in your code, so I don't think that is the reason of your problem. But you can test it. The polling mode is not recommended. It's better than using interrupts.
@SMotlaq
I will try to receive using EXT interrupt and post back the results