LoRa Network - File Transfer

date
Jun 7, 2024
type
Post
AI summary
slug
lora-network-file-transfer
status
Published
tags
LoRa
ESP32
summary
In this post, we explore a rough protocol for transferring large files over LoRa using ESP32 devices. The protocol involves sending metadata, file chunks, and an end-of-transfer message, with acknowledgment handling to ensure reliable transmission.
Transferring large data files over LoRa requires a well-defined protocol to ensure reliable transmission and reassembly. Here, we'll outline an effective protocol and provide a detailed explanation of the architecture, followed by code snippets to implement the solution.

Protocol Overview

To transfer files over LoRa, we need to break the file into manageable chunks and send these chunks sequentially. The protocol involves three main types of messages:
  1. Metadata Message: This message contains the filename and the total size of the file. It is sent first to inform the receiver about the upcoming file transfer.
  1. File Chunk Message: These messages carry chunks of the file data. Each chunk includes a portion of the file's data and its size.
  1. End-of-Transfer Message: This message signifies the end of the file transfer, indicating that all chunks have been sent.
Each message type is identified by a msgType field. The receiver acknowledges each message with an ACK (acknowledgment) or REJ (rejection) message to ensure data integrity.

Architecture

Sender Workflow:
  1. Send Metadata: The sender begins by sending a metadata message with details about the file (filename and size).
  1. Send File Chunks: The file is read in chunks, and each chunk is sent with a delay to avoid congestion. The sender waits for an ACK before sending the next chunk.
  1. Send End-of-Transfer: Once all chunks are sent, an end-of-transfer message is sent to finalize the transfer.
Receiver Workflow:
  1. Receive Metadata: The receiver starts by receiving the metadata message to prepare for the incoming file.
  1. Receive File Chunks: As chunks are received, they are written to the file on the receiver's end. Each chunk receipt is acknowledged.
  1. Receive End-of-Transfer: The end-of-transfer message signals the completion of the file transfer.

Implementation

Sending a File

The sender’s code involves sending metadata, file chunks, and an end-of-transfer message with appropriate acknowledgment handling.

Sending Metadata

The metadata message includes the filename and the file size. It is sent with a retry mechanism to ensure delivery.

Sending File Chunks

Each file chunk is sent with a similar retry mechanism.

Sending End-of-Transfer

Finally, the end-of-transfer message is sent.

Receiving The File

The receiver handles different types of messages based on the msgType.

What Could Go Wrong?

Obviously, the protocol outlined here is just a rough sketch. How do you decide when to send a rejection? How do you keep track of the file transfer initiator’s name? What if a rogue node starts sending unsolicited file chunks? You'll need mechanisms to handle these scenarios, which will be covered in another blog post. This will involve using mutexes with RTOS for effective handling. Stay tuned!

Conclusion

With this architecture, you can reliably transfer files over a LoRa network by breaking them into manageable chunks, sending each chunk with acknowledgment, and reassembling the file on the receiver side. This method ensures that larger data can be transmitted even with the payload size limitation of the Arduino LoRa library. In the next section, we will discuss how to implement more advanced features and optimizations to further enhance this protocol.

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