A tag-aware, energy-conscious routing protocol that dynamically balances latency and battery life in IoT networks โ simulated in NS-3.
IoT networks face a fundamental trade-off: speed vs. battery life. A fire-alarm packet cannot wait in queue behind bulk sensor logs, yet routing everything at maximum speed drains resource-constrained nodes within hours.
PCER solves this by tagging every data packet with an urgency level and feeding it into a weighted cost function that selects the optimal next-hop. Critical alerts get the fastest path; bulk data takes the most energy-efficient route; standard traffic strikes a balance โ all while avoiding nodes whose batteries are about to die.
| Feature | Description |
|---|---|
| Priority Tagging | Packets are classified as Critical (0), Standard (1), or Bulk (2) at the source |
| Weighted 5-Component Cost | Incorporates Delay, Energy (Sigmoid), Trust, ETX, and Queue Load |
| Bi-directional Backpressure | L3 signals L7 to throttle injection when queue occupancy exceeds 60% |
| Dynamic Route Switching | The protocol re-evaluates every hop in real time, guarded by Hysteresis |
| Standalone Demo | A self-contained C++ binary demonstrates the routing logic without NS-3 |
| Traffic Generator | Python script simulates realistic IoT traffic distributions |
| Automated Analysis | Matplotlib scripts produce publication-ready latency & network-life graphs |
Open index.html in any browser โ no server or dependencies needed. The interactive demo features:
- 5-node network with animated packets color-coded by priority (๐ด Critical, ๐ก Standard, ๐ต Bulk)
- Live battery drain on Node 2 with automatic route-switch when it drops below 5 %
- Real-time cost calculator panel showing the exact
CalculateCost()math per hop - Playback controls โ Play / Pause / Speed / Reset + manual "Send Packet" button
- 5-phase auto-play that tells the complete PCER story in 12 seconds
You can deploy this entire simulation to your own Vercel account with one click:
- Push this code to your GitHub.
- Connect your repository to Vercel.
- Vercel will automatically detect the
index.htmland deploy it as a static site. - Access different versions at:
/-> Latest (index.html)/v2-> Version 2/v3-> PCER-T v4 (Advanced)
โโโโโโโโโโโโ Tag 0 (Critical) โโโโโโโโโโโโโโโโโโโโโโโโ
โ Source โ โโโโโโโโโโโโโโโโโโโโโโโบ โ Fastest Path (low โ
โ Node โ โ delay, ignore energy)โ
โ โ Tag 1 (Standard) โโโโโโโโโโโโโโโโโโโโโโโโค
โ โ โโโโโโโโโโโโโโโโโโโโโโโบ โ Balanced Path โ
โ โ โ (delay โ energy) โ
โ โ Tag 2 (Bulk) โโโโโโโโโโโโโโโโโโโโโโโโค
โ โ โโโโโโโโโโโโโโโโโโโโโโโบ โ Energy-Optimal Path โ
โโโโโโโโโโโโ โ (ignore delay) โ
โโโโโโโโโโโโโโโโโโโโโโโโ
The heart of PCER lives in CalculateCost():
Cost = (wโ ร delay) + (wโ ร 1/energy)
| Tag | Traffic Type | wโ (Delay) | wโ (Energy) | Behaviour |
|---|---|---|---|---|
| 0 | Critical | 100 | 0 | Pure speed โ pick the fastest neighbour |
| 1 | Standard | 1 | 1 | Balanced โ weigh both factors equally |
| 2 | Bulk | 0 | 100 | Pure efficiency โ pick the highest-battery neighbour |
If a neighbour's battery drops below 5 %, its cost is set to โ regardless of tag. This prevents critical packets from being routed through a node that might die mid-transmission.
The standalone demo shows this in action over 10 simulated seconds:
Node 2: 5 ms delay, starts at 10 % battery (preferred for speed)
Node 3: 50 ms delay, starts at 100 % battery (backup)
As Node 2 drains below 5 %, PCER automatically switches all traffic โ including Critical โ to Node 3, trading latency for reliability.
Dynamic_routing_in_blockChain/
โ
โโโ traffic_generator.py # Generates IoT traffic trace (100 events)
โโโ traffic_trace.txt # Sample output: [Time Src Dst Size Tag]
โ
โโโ ns3/ # NS-3 implementation files
โ โโโ pcer_tag.h # PcErTag class โ urgency tag (0/1/2)
โ โโโ pcer-routing-protocol.h # Protocol header โ NeighborInfo, API
โ โโโ pcer-routing-protocol.cc # โ
Core logic โ CalculateCost(), RouteOutput()
โ โโโ pcer-helper.h # NS-3 helper to install protocol on nodes
โ โโโ pcer_sim.cc # Full NS-3 simulation script (5 nodes, UDP)
โ โโโ pcer_routing.cc # Auxiliary routing utilities
โ
โโโ pcer_demo.cpp # Standalone demo (no NS-3 required)
โโโ pcer_demo # Pre-compiled demo binary
โ
โโโ plot_results.py # Matplotlib analysis script
โโโ pcer_results.csv # Baseline vs PCER comparison data
โ
โโโ latency_comparison.png # Graph: Critical traffic latency
โโโ network_life_comparison.png # Graph: Battery remaining
โ
โโโ index.html # โ
Interactive animated demo (open in browser)
โโโ final_documentation.md # Step-by-step setup guide
โโโ README.md # โ You are here
| Tool | Version | Purpose |
|---|---|---|
| NS-3 | โฅ 3.35 | Network simulation (only for full sim) |
| g++ | C++17 capable | Compiling standalone demo |
| Python 3 | โฅ 3.8 | Traffic generation & plotting |
| matplotlib | any | Generating result graphs |
The fastest way to see PCER in action:
# Compile
g++ -std=c++17 -o pcer_demo pcer_demo.cpp
# Run
./pcer_demoExpected output:
=== PCER DYNAMIC SIMULATION (STANDALONE) ===
Scenario: Sending Critical Data (Tag 0).
Node 2: Delay 5ms (Preferred)
Node 3: Delay 50ms (Backup)
-----------------------------------------------------------------
| Time | Node 2 Bat | Cost (N2) | Cost (N3) | Routing Decision |
-----------------------------------------------------------------
| 0s | 10% | 500 | 5000 | Node 2 (Fast) |
| 1s | 9% | 500 | 5000 | Node 2 (Fast) |
...
| 6s | 4% | INF | 5000 | -> SWITCH -> Node 3 |
...
-----------------------------------------------------------------
At t = 6 s, Node 2's battery hits 4 % โ the survival threshold kicks in โ PCER switches to Node 3.
python3 traffic_generator.py
# โ Creates traffic_trace.txt (100 IoT events with mixed tags)Copy files into your NS-3 workspace:
# Copy simulation script
cp ns3/pcer_sim.cc <NS3_DIR>/scratch/pcer_sim.cc
# Copy protocol files (easiest: put everything in scratch/)
cp ns3/pcer_tag.h <NS3_DIR>/scratch/
cp ns3/pcer-routing-protocol.h <NS3_DIR>/scratch/
cp ns3/pcer-routing-protocol.cc <NS3_DIR>/scratch/
cp ns3/pcer-helper.h <NS3_DIR>/scratch/
# Copy traffic trace
cp traffic_trace.txt <NS3_DIR>/cd <NS3_DIR>
./waf --run scratch/pcer_sim
# โ Generates pcer_results_real.csvpython3 plot_results.py
# โ Generates latency_comparison.png & network_life_comparison.png| Method | Tag | Avg Latency (ms) | Network Life (%) |
|---|---|---|---|
| Baseline | Critical | 150.5 | 80 |
| Baseline | Standard | 120.0 | 80 |
| Baseline | Bulk | 200.0 | 80 |
| PCER | Critical | 45.2 | 95 |
| PCER | Standard | 110.0 | 95 |
| PCER | Bulk | 210.0 | 95 |
Key takeaway: PCER reduces critical-traffic latency by 70 % (150.5 โ 45.2 ms) while increasing overall network lifetime by 19 % (80 โ 95 %).
The core routing decision is made in pcer-routing-protocol.cc:
double PcerRoutingProtocol::CalculateCost(uint8_t tag, const NeighborInfo &neighbor) {
double w1_delay = 1.0, w2_energy = 1.0;
if (tag == 0) { // CRITICAL โ pure speed
w1_delay = 100.0;
w2_energy = 0.0;
} else if (tag == 2) { // BULK โ pure efficiency
w1_delay = 0.0;
w2_energy = 100.0;
}
// STANDARD keeps w1 = w2 = 1.0 (balanced)
// Survival Threshold: dying nodes are avoided entirely
if (neighbor.energy < 0.05)
return std::numeric_limits<double>::max();
double energy_cost = (neighbor.energy > 0.0001) ? (1.0 / neighbor.energy) : 10000.0;
return (w1_delay * neighbor.delay) + (w2_energy * energy_cost);
}Tuning guide: Adjust the weight constants (100.0, 0.0, 1.0) and the survival threshold (0.05) to match your specific network characteristics.
- Multi-hop path computation โ extend beyond direct-neighbour routing to full shortest-path trees
- Multi-Agent Reinforcement Learning (MARL) โ replace static multi-component weights with adaptive RL layers
- Hardware testbed โ validate on real IoT boards (ESP32 / Raspberry Pi mesh)
This project is open-source and available under the MIT License.

