IoT devices push/pull small state (e.g., "armed", "idle"); presigned updates for time-limited commands.
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.
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.
Set up a stash for each device or device group:
# 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"
Simple device firmware:
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()
Generate time-limited mode change URLs:
#!/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