LoRa Network - Structured Messages

date
Jun 6, 2024
type
Post
AI summary
slug
lora-network-structured-messages
status
Published
tags
LoRa
ESP32
summary
Explore how to send and receive structured messages using LoRa with ESP32 devices. Instead of sending simple strings, we use structs to include a MAC address for identification, extraction instructions, and a payload. By using an enum to declare message types and identifying the message type with the first byte (msgType), we eliminate the need to include message length in the transmission.
We have already discussed how to send and receive messages using LoRa, but those examples were limited to sending strings. What if you want to send structured messages that include a MAC address for identification, information on how to extract data, and a payload? To achieve this, we can send structs.
Below, I will explain how to send and receive structs using LoRa. We will identify the message struct type by the first byte msgType. An enum will be used to declare different message types, eliminating the need to include the message length in the message.

Define the Structs and Enum

First, define the structs you want to send and the enum to identify the message types.

Sending the Structs

When sending a message, populate the struct with data and send it using LoRa.write.

Receiving the Structs

On the receiving side, read the message, check the msgType to determine the struct type, and then handle the message accordingly.

Putting It All Together

Ensure your setup function initializes the LoRa module and registers the onReceive callback.
By using structs and an enum for msgType, you can easily send and receive structured messages with LoRa, ensuring that both the sender and receiver understand the message format without needing to include the length in the message. This approach simplifies communication and enhances the flexibility of your LoRa network.

Unlocking New Possibilities with LoRa

What can we do with this? Imagine you add multiple switch statements to both sender and receiver, enabling them to handle all kinds of logic, just like a REST API! If you already have a web server built out, you can implement the same logic on the LoRa node and deploy it in areas without WiFi or cell signal.

Conclusion

How exciting! Now you can send and receive virtually any kind of messages. At the end of the day, all information can be packed into byte-sized messages, right? However, there's one caveat: the Arduino LoRa library only supports a payload size of 250 bytes. What if you need to send and receive larger data? That's right, we need to discuss how to send files or data in chunks and reassemble them on the receiver side!

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