1096  LoRaWAN Simulation Lab

Prerequisites: LoRaWAN Network Architecture | ADR Optimization

This enables: Practice Exercises | Real hardware deployment

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:

  1. Learn protocol mechanics without hardware investment
  2. Visualize packet flow that would be invisible with real radios
  3. Experiment with ADR in seconds instead of hours
  4. 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:

%%{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

Figure 1096.1: LoRaWAN simulation architecture with two end devices and a gateway/network server

1096.5 Embedded Wokwi Simulator

TipHow to Use This Simulator
  1. Click the green Play button to start the simulation
  2. Watch the Serial Monitor for detailed LoRaWAN packet logs
  3. Copy the code from the repository to run the full demonstration
  4. Modify spreading factors and observe the airtime changes
  5. Trigger downlinks by pressing the boot button on end devices

1096.6 Lab Challenges

1096.6.2 Challenge 2: Add Class B Beacon Simulation

Extend the simulation to support Class B scheduled receive windows:

  1. Gateway broadcasts a beacon every 128 seconds
  2. End device synchronizes to beacon and opens ping slots
  3. Implement ping slot scheduling based on DevAddr hash
  4. Compare downlink latency between Class A and Class B

1096.6.3 Challenge 3: Implement Join Procedure (OTAA)

Add the LoRaWAN join procedure:

  1. Create JoinRequest message with DevEUI and AppEUI
  2. Gateway responds with JoinAccept containing DevAddr and session keys
  3. Derive AppSKey and NwkSKey from AppKey and nonces
  4. Transition from join state to data transmission state

1096.6.4 Challenge 4: Multi-Gateway Reception

Simulate multiple gateways receiving the same uplink:

  1. Add a second gateway node
  2. Both gateways forward uplinks to the “network server”
  3. Implement deduplication logic (keep best RSSI)
  4. 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:

  1. Hardware: Get an ESP32 + SX1276/SX1262 module (Heltec WiFi LoRa 32, TTGO LoRa32, or separate module)
  2. Library: Use LMIC (LoRaWAN-in-C) or RadioLib for Arduino
  3. Network: Register with The Things Network (TTN) or Chirpstack
  4. 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