โ† Back to Use Cases
EDGE, IOT, AND APPS

Device Mode Switches

IoT devices push/pull small state (e.g., "armed", "idle"); presigned updates for time-limited commands.

The Problem

Your IoT devices need to report their current mode (armed, idle, maintenance) and receive mode change commands. You need a simple, lightweight state store that works over HTTP without complex IoT platform setup.

Devices shouldn't hold long-lived credentials. Commands should be time-limited for security.

The WrenDB Solution

Devices report state by POSTing to their stash (no auth). They poll for mode changes (public read). Control systems send time-limited command URLs via presigned URLs. Simple HTTP, no MQTT broker needed.

Step-by-Step Guide

1

Create device stash

Set up a stash for each device or device group:

Terminal
# Create stash for device
curl -X POST https://wrendb.com/api/stash

{
  "stash_id": "device123...",
  "master_token": "token-control456...",
  "message": "Save this token securely..."
}

# Initialize device state
curl -X POST https://wrendb.com/api/item/device123.../mode \
  -d "idle"

curl -X POST https://wrendb.com/api/item/device123.../last-heartbeat \
  -d "2025-01-15T10:00:00Z"
2

Device reports and polls state

Simple device firmware:

device.py
import requests
import time
from datetime import datetime, timezone

STASH_ID = "device123..."
DEVICE_ID = "sensor-01"

def get_command():
    """Check for mode change command (public read)"""
    try:
        response = requests.get(
            f"https://wrendb.com/api/item/{STASH_ID}/mode",
            timeout=5
        )
        return response.text if response.status_code == 200 else "idle"
    except:
        return "idle"  # Safe default

def send_heartbeat():
    """Report device is alive (no auth)"""
    requests.post(
        f"https://wrendb.com/api/item/{STASH_ID}/last-heartbeat",
        data=datetime.now(timezone.utc).isoformat()
    )

def main_loop():
    current_mode = "idle"

    while True:
        # Check for mode change
        new_mode = get_command()
        if new_mode != current_mode:
            print(f"Mode change: {current_mode} โ†’ {new_mode}")
            current_mode = new_mode

        # Act based on mode
        if current_mode == "armed":
            print("๐Ÿ”’ Device armed - monitoring sensors")
            # Monitor sensors...
        elif current_mode == "idle":
            print("๐Ÿ’ค Device idle")

        # Send heartbeat
        send_heartbeat()

        time.sleep(30)  # Poll every 30 seconds

if __name__ == "__main__":
    main_loop()
3

Control system sends commands

Generate time-limited mode change URLs:

control.sh
#!/bin/bash

STASH_ID="device123..."
TOKEN="$DEVICE_CONTROL_TOKEN"

case "$1" in
  arm)
    # Change mode to armed
    curl -X PUT "https://wrendb.com/api/item/$STASH_ID/mode" \
      -H "Authorization: Bearer $TOKEN" \
      -d "armed"
    echo "โœ… Device set to ARMED mode"
    ;;

  disarm)
    curl -X PUT "https://wrendb.com/api/item/$STASH_ID/mode" \
      -H "Authorization: Bearer $TOKEN" \
      -d "idle"
    echo "โœ… Device set to IDLE mode"
    ;;

  status)
    echo "Current mode: $(curl -s https://wrendb.com/api/item/$STASH_ID/mode)"
    echo "Last heartbeat: $(curl -s https://wrendb.com/api/item/$STASH_ID/last-heartbeat)"
    ;;

  *)
    echo "Usage: control.sh {arm|disarm|status}"
    ;;
esac

Why This Works

  • Simple HTTP - No MQTT broker or complex IoT platform
  • Minimal credentials - Devices read publicly, control requires token
  • Lightweight - Perfect for resource-constrained devices
  • Public monitoring - Dashboards can check device status without auth

Related Use Cases

Remote Control Signals - Send time-bound commands to devices Maintenance Switches - Operational flags and state