LoRa Network - The Delicacy of LoRa.onReceive()

date
Jun 5, 2024
type
Post
AI summary
slug
lora-delicacy-of-onreceive
status
Published
tags
LoRa
ESP32
summary
When developing with the ESP32 or Arduino, using the LoRa.onReceive function requires careful handling due to its operation within an interrupt context. Interrupt Service Routines (ISRs) in the ESP32 have stringent requirements and limitations. Performing complex or time-consuming tasks within an ISR can lead to various issues, including crashes and reboots.
When developing with the ESP32 or Arduino, using the LoRa.onReceive function requires careful handling due to its operation within an interrupt context. Interrupt Service Routines (ISRs) in the ESP32 have stringent requirements and limitations. Performing complex or time-consuming tasks within an ISR can lead to various issues, including crashes and reboots.
I would recommend you read these, but you are lazy then let me just sum it up, just so we can get it to work!

Why Does It Crash?

When working with the ESP32, particularly using the LoRa.onReceive function, it is crucial to keep the operations within this callback minimal. The primary reason for this is that onReceive is called in an interrupt context. Interrupt Service Routines (ISRs) in the ESP32 have very stringent requirements and limitations. Performing complex or time-consuming tasks within an ISR can lead to various issues, including crashes and reboots.
Typical symptoms of a onReceive failure
Typical symptoms of a onReceive failure
Interrupt Context Limitations:
  • ISRs are designed to be executed quickly. They should perform only the most essential tasks to avoid delaying the main program execution. Prolonged operations within an ISR can prevent other critical tasks from running, leading to instability.
Restricted Operations:
  • Certain functions are not safe to call from within an ISR. For instance, functions like printf, memory allocation (e.g., malloc), and blocking operations (e.g., delay functions) are not suitable for ISRs. Attempting to use these functions can cause the system to crash or reboot.

Coming Up with a Solution

Crash Prevention Strategy (Use Flags):
  • To handle received data properly, use the ISR to set a flag or send a simple notification (e.g., setting a boolean variable or posting to a queue). Then, in your main loop or a dedicated task, check this flag and perform the necessary processing. This approach ensures that the intensive processing happens outside the ISR context, maintaining system stability.

Example Adjustment

Here is how you can adjust your onReceive function to follow these best practices:
By adhering to these guidelines, you can ensure your ESP32 project runs smoothly without encountering crashes related to interrupt handling. For more detailed information, you can refer to discussions on the ESP32 forum.

About Me

Hi, I'm Qiwei Mao, a geotechnical engineer with a passion for IoT systems. I'm exploring low-power microcontrollers and LoRa communication systems to enable both hobbyist remote monitoring solutions and industrial-grade monitoring or control systems.
Qiwei Mao
Qiwei Mao

© Qiwei Mao 2024