%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#2C3E50', 'primaryTextColor': '#fff', 'primaryBorderColor': '#16A085', 'lineColor': '#E67E22', 'secondaryColor': '#7F8C8D', 'tertiaryColor': '#ECF0F1', 'fontSize': '14px'}}}%%
graph TB
subgraph EndDevices["End Devices (Class A)"]
ED1["ESP32 #1<br/>Sensor Node<br/>DevAddr: 0x26011234"]
ED2["ESP32 #2<br/>Sensor Node<br/>DevAddr: 0x26015678"]
end
subgraph Gateway["LoRaWAN Gateway"]
GW["ESP32 #3<br/>Gateway Simulator<br/>+ Network Server"]
end
subgraph NetworkFunctions["Network Server Functions"]
ADR["ADR Algorithm"]
DC["Duty Cycle Monitor"]
DL["Downlink Scheduler"]
end
ED1 -->|"Uplink SF7-SF12"| GW
ED2 -->|"Uplink SF7-SF12"| GW
GW -->|"RX1/RX2 Downlink"| ED1
GW -->|"RX1/RX2 Downlink"| ED2
GW --- ADR
GW --- DC
GW --- DL
style ED1 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
style ED2 fill:#16A085,stroke:#2C3E50,stroke-width:2px,color:#fff
style GW fill:#E67E22,stroke:#2C3E50,stroke-width:3px,color:#fff
style ADR fill:#ECF0F1,stroke:#7F8C8D,stroke-width:1px,color:#2C3E50
style DC fill:#ECF0F1,stroke:#7F8C8D,stroke-width:1px,color:#2C3E50
style DL fill:#ECF0F1,stroke:#7F8C8D,stroke-width:1px,color:#2C3E50
1096 LoRaWAN Simulation Lab
1096.1 Learning Objectives
By completing this lab, you will be able to:
- Understand LoRaWAN packet structure: DevAddr, FCnt (frame counter), FPort, and encrypted payload fields
- Implement uplink/downlink communication: Simulate Class A receive windows (RX1 and RX2)
- Observe spreading factor effects: See how SF7-SF12 affects airtime, range simulation, and throughput
- Manage duty cycle compliance: Implement 1% EU868 duty cycle tracking and enforcement
- Experience ADR behavior: Watch the network automatically adjust spreading factors based on link quality
1096.2 Why Simulate LoRaWAN?
Real LoRaWAN requires specialized hardware (SX1276/SX1262 radio modules) and licensed spectrum access. This simulation lets you:
- Learn protocol mechanics without hardware investment
- Visualize packet flow that would be invisible with real radios
- Experiment with ADR in seconds instead of hours
- Test duty cycle scenarios without regulatory concerns
The simulation accurately models packet structure, timing behaviors, and protocol state machines - the same concepts you will apply with real LoRaWAN hardware.
1096.3 LoRaWAN Concepts Demonstrated
| Concept | Real LoRaWAN | This Simulation |
|---|---|---|
| Physical Layer | LoRa CSS modulation | ESP-NOW 2.4 GHz |
| Packet Format | PHYPayload with MHDR, MACPayload, MIC | Identical structure |
| Spreading Factor | SF7-SF12 radio parameter | Simulated airtime delays |
| Duty Cycle | 1% TX time per hour | Tracked and enforced |
| ADR | Network server algorithm | Simplified SNR-based |
| Device Classes | Class A/B/C | Class A implemented |
| Security | AES-128 encryption | Simulated (visible for learning) |
1096.4 Lab Architecture
The simulation creates a LoRaWAN network with three ESP32 devices:
1096.5 Embedded Wokwi Simulator
- Click the green Play button to start the simulation
- Watch the Serial Monitor for detailed LoRaWAN packet logs
- Copy the code from the repository to run the full demonstration
- Modify spreading factors and observe the airtime changes
- Trigger downlinks by pressing the boot button on end devices
1096.6 Lab Challenges
1096.6.1 Challenge 1: Implement Confirmed Uplinks
Modify the end device to send confirmed uplinks and handle ACK responses:
- Change
MTypefrom 2 (Unconfirmed) to 4 (Confirmed) in uplink frames - Implement ACK timeout and retransmission logic
- Track confirmed vs. unconfirmed delivery rates
- Observe how confirmed uplinks affect duty cycle budget
1096.6.2 Challenge 2: Add Class B Beacon Simulation
Extend the simulation to support Class B scheduled receive windows:
- Gateway broadcasts a beacon every 128 seconds
- End device synchronizes to beacon and opens ping slots
- Implement ping slot scheduling based on DevAddr hash
- Compare downlink latency between Class A and Class B
1096.6.3 Challenge 3: Implement Join Procedure (OTAA)
Add the LoRaWAN join procedure:
- Create JoinRequest message with DevEUI and AppEUI
- Gateway responds with JoinAccept containing DevAddr and session keys
- Derive AppSKey and NwkSKey from AppKey and nonces
- Transition from join state to data transmission state
1096.6.4 Challenge 4: Multi-Gateway Reception
Simulate multiple gateways receiving the same uplink:
- Add a second gateway node
- Both gateways forward uplinks to the “network server”
- Implement deduplication logic (keep best RSSI)
- Demonstrate how multi-gateway improves reliability
1096.7 Key Concepts Learned
1096.7.1 Concept: DevAddr and Frame Counters
DevAddr (Device Address) is a 4-byte network-assigned identifier: - Assigned during join (OTAA) or pre-configured (ABP) - Used for routing at the network server - Format: 0x26XXXXXX for private networks
Frame Counters (FCnt) prevent replay attacks: - FCntUp: Incremented by device on each uplink - FCntDown: Incremented by network on each downlink - Network rejects packets with out-of-sequence counters
1096.7.2 Concept: Spreading Factor Trade-offs
| SF | Airtime (20 bytes) | Range | Sensitivity | Battery Impact |
|---|---|---|---|---|
| SF7 | 50 ms | ~2 km | -123 dBm | Lowest (1x) |
| SF8 | 100 ms | ~4 km | -126 dBm | 2x |
| SF9 | 200 ms | ~6 km | -129 dBm | 4x |
| SF10 | 400 ms | ~8 km | -132 dBm | 8x |
| SF11 | 800 ms | ~11 km | -134.5 dBm | 16x |
| SF12 | 1600 ms | ~15 km | -137 dBm | 32x |
1096.7.3 Concept: Duty Cycle Regulations
EU868 Regulations: - Sub-band g (868.0-868.6 MHz): 1% duty cycle = 36 seconds/hour - Sub-band g1 (868.7-869.2 MHz): 0.1% = 3.6 seconds/hour - Sub-band g3 (869.4-869.65 MHz): 10% = 360 seconds/hour (used for downlinks)
US915 Regulations: No duty cycle limit, but: - 400ms dwell time per channel - Frequency hopping required (>50 channels) - Fair Access Policy recommends 30 seconds/day
1096.7.4 Concept: Adaptive Data Rate (ADR)
How ADR Works: 1. Network server collects SNR/RSSI from last 20 uplinks 2. Calculates link margin above sensitivity threshold 3. If margin > 10 dB, recommends lower SF (faster, less power) 4. Sends LinkADRReq MAC command in downlink 5. Device acknowledges and applies new SF
1096.8 Lab Summary
| Exercise | LoRaWAN Concept | What You Learned |
|---|---|---|
| Basic transmission | Packet structure | DevAddr, FCnt, FPort, MIC fields |
| SF modification | Spreading factors | Airtime vs range trade-off |
| Duty cycle test | Regulatory compliance | EU868 1% limit enforcement |
| Multi-device | ADR algorithm | Network-optimized SF selection |
| Challenge 1 | Confirmed messages | Reliability with ACK/retry |
| Challenge 2 | Class B | Beacon-synchronized receive |
| Challenge 3 | OTAA | Secure device activation |
| Challenge 4 | Redundancy | Multi-gateway reception |
1096.9 Real Hardware Next Steps
After completing this simulation lab, you are ready to work with real LoRaWAN hardware:
- Hardware: Get an ESP32 + SX1276/SX1262 module (Heltec WiFi LoRa 32, TTGO LoRa32, or separate module)
- Library: Use LMIC (LoRaWAN-in-C) or RadioLib for Arduino
- Network: Register with The Things Network (TTN) or Chirpstack
- Activation: Use OTAA (more secure) or ABP (simpler for testing)
The packet structures and concepts from this lab translate directly to real deployments - the primary difference is the physical LoRa radio layer.
1096.10 What’s Next
Continue to Practice Exercises to test your understanding of LoRaWAN concepts.
Alternative paths: - LoRaWAN Overview - Return to the chapter index - LoRaWAN Architecture - Deep dive into protocol specifications