This project provides a C++ implementation of the LoRaWAN protocol using the CH341 USB-to-SPI converter to interface with a RFM95/SX1276 LoRa radio module.
Documentation: Full API documentation is available at https://serper.github.io/LoRaWANCH341
- Full LoRaWAN 1.0.3 protocol implementation
- Support for Class A and Class C devices
- OTAA and ABP activation methods
- Support for multiple regions (EU868, US915, etc.)
- MAC commands implementation
- Adaptive Data Rate (ADR) support
- Channel management
- Duty cycle management
- Confirmed/unconfirmed messages
- Linux and Windows support via CH341 USB interface
- CH341A/CH341F USB adapter
- RFM95W, SX1276, or compatible LoRa module
- Any USB-capable device running Linux or Windows
You can use this library with a reference board design that is available at: https://oshwlab.com/serper/lora-usb-adapter
This design provides a complete USB-to-LoRa adapter using the CH341 chip and RFM95 module, allowing for easy integration with this software library.
- libusb-1.0
- OpenSSL (for AES encryption)
- cJSON (for configuration and session management)
- C++17 compatible compiler
The project currently supports the following LoRaWAN features:
-
Network Activation
- Over-the-Air Activation (OTAA)
- Activation By Personalization (ABP)
-
Message Handling
- Uplink and downlink communication
- Class A and Class C device operation
-
Network Management
- Regional channel and power restrictions
- Adaptive Data Rate (ADR) with performance statistics
- Duty cycle management
The implementation adheres to LoRaWAN protocol specifications and continues to evolve with additional features.
The project uses a JSON configuration file (config.json
) to set up device parameters and connection settings. A sample configuration file (config.json.sample
) is provided as reference.
{
"device": {
"devEUI": "Device Extended Unique Identifier",
"appEUI": "Application Extended Unique Identifier",
"appKey": "Application Key"
},
"connection": {
"spi_type": "ch341",
"device_index": 0,
"spi_device": "/dev/spidev0.0",
"spi_speed": 1000000
},
"options": {
"force_reset": false,
"send_interval": 30,
"verbose": false
}
}
// Create SPI interface using CH341
auto spi_interface = SPIFactory::createCH341SPI(device_index, true);
LoRaWAN lorawan(std::move(spi_interface));
or more easy
LoRaWAN lorawan();
// Create SPI interface using native Linux SPI
auto spi_interface = SPIFactory::createLinuxSPI(spi_device, spi_speed);
LoRaWAN lorawan(std::move(spi_interface));
The SPIFactory
class provides factory methods to create appropriate SPI interfaces based on your hardware configuration. Choose the implementation that matches your setup.
devEUI
: Device Extended Unique Identifier (16 characters)appEUI
: Application Extended Unique Identifier (16 characters)appKey
: Application Key (32 characters)
spi_type
: SPI interface type (currently supports "ch341")device_index
: Device index numberspi_device
: SPI device pathspi_speed
: SPI communication speed in Hz
force_reset
: Enable/disable force resetsend_interval
: Message sending interval in secondsverbose
: Enable/disable verbose logging
- Copy
config.json.sample
toconfig.json
- Update the configuration with your device credentials
- Configure the connection settings according to your setup
- Adjust options as needed
- CH341 compatible hardware or native linux SPI
- Access to LoRaWAN network
- Proper device registration with network server
- libusb-dev (for CH341 communication)
- libjson-cpp-dev (for JSON configuration parsing)
- libspi-dev (for SPI communication)
- libopenssl
- C++17 compatible compiler
- CMake build system (version 3.10 or higher)
Complete API documentation is generated using Doxygen and available online at https://serper.github.io/LoRaWANCH341.
The documentation includes:
- Detailed class and function descriptions
- Code examples
- Implementation details
- UML diagrams of class relationships
- Call graphs for complex functions
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.