Back to Docs
📻

LoRaWAN Integration

LoRaWAN's limited payload size makes compression critical. ALEC enables 4x more data per transmission while extending battery life.

Why ALEC + LoRaWAN?

📦

4x More Data

Pack 4x more sensor readings into each LoRa frame with delta encoding.

🔋

Extended Battery

Fewer transmissions = longer battery life. Critical for remote deployments.

Priority Alerts

P1/P2 messages can request confirmed uplinks for guaranteed delivery.

Device Code (Rust)

use alec::{Encoder, Context, Priority, Config};

// LoRaWAN payload limits by spreading factor
const SF7_PAYLOAD: usize = 222;   // bytes
const SF12_PAYLOAD: usize = 51;   // bytes

fn main() {
    // Configure ALEC for constrained payloads
    let config = Config::builder()
        .max_message_size(SF12_PAYLOAD)  // Worst case
        .prefer_delta(true)               // Maximize compression
        .build();
    
    let mut ctx = Context::new();
    let encoder = Encoder::with_config(&mut ctx, config);
    
    // Batch multiple readings into one LoRa frame
    let readings = collect_sensor_readings();  // e.g., 10 values
    let msg = encoder.encode_batch(&readings, Priority::Auto);
    
    // msg.len() typically 5-15 bytes for 10 readings
    lora_send(msg.as_bytes());
}

TTN Payload Decoder

Add this decoder to The Things Network console to parse ALEC messages:

// The Things Network decoder (JavaScript)
function decodeUplink(input) {
    // ALEC header byte
    const header = input.bytes[0];
    const mode = (header >> 6) & 0x03;
    const priority = (header >> 3) & 0x07;
    
    let value;
    switch (mode) {
        case 0: // Raw
            value = decodeFloat32(input.bytes.slice(1, 5));
            break;
        case 1: // Delta8
            value = lastValue + input.bytes[1] - 128;
            break;
        case 2: // Delta16
            value = lastValue + decodeInt16(input.bytes.slice(1, 3));
            break;
        case 3: // Repeated
            value = lastValue;
            break;
    }
    
    return {
        data: { value: value, priority: priority },
        warnings: priority <= 2 ? ["High priority alert"] : []
    };
}

Payload Efficiency

SF Max Payload Raw Values With ALEC Gain
SF7 222 bytes 55 floats ~200 readings 4x
SF9 115 bytes 28 floats ~100 readings 4x
SF12 51 bytes 12 floats ~45 readings 4x

⚠️ Fair Use Policy

Remember LoRaWAN duty cycle limits (typically 1%). Even with ALEC compression, plan your transmission schedule accordingly.