Send narrow, time-bound commands (e.g., "reboot", "change interval", "enable sensor X") via presigned URLs so field devices can act without long-lived credentials.
You have field devices that need to receive occasional commands: reboot, change sampling interval, enable/disable sensors, or update configuration. Storing long-lived credentials on devices is risky. Cloud IoT platforms are expensive and complex.
You want to send time-limited, single-use command links that devices can execute without holding credentials.
Use presigned URLs to create time-limited command signals. Devices poll for commands (public read). Control systems generate presigned URLs that update command values, expiring after 15 minutes. No credentials on devices.
Create a stash for device commands:
# Create command stash
curl -X POST https://wrendb.com/api/stash
{
"stash_id": "commands789...",
"master_token": "token-remote123...",
"message": "Save this token securely..."
}
# Initialize with "none" (no pending commands)
curl -X POST https://wrendb.com/api/item/commands789.../device-01-cmd \
-d "none"
Lightweight device firmware:
import requests
import time
import os
STASH_ID = "commands789..."
DEVICE_ID = "device-01"
POLL_INTERVAL = 60 # seconds
def check_for_command():
"""Poll for pending commands (public read - no auth)"""
try:
response = requests.get(
f"https://wrendb.com/api/item/{STASH_ID}/{DEVICE_ID}-cmd",
timeout=10
)
return response.text if response.status_code == 200 else "none"
except:
return "none"
def execute_command(cmd):
"""Execute the received command"""
if cmd == "reboot":
print("๐ Rebooting device...")
os.system("sudo reboot")
elif cmd.startswith("interval:"):
interval = int(cmd.split(":")[1])
print(f"โฑ๏ธ Changing poll interval to {interval}s")
global POLL_INTERVAL
POLL_INTERVAL = interval
elif cmd.startswith("enable:"):
sensor = cmd.split(":")[1]
print(f"โ
Enabling sensor: {sensor}")
# Enable sensor logic...
elif cmd == "none":
pass # No command
else:
print(f"โ Unknown command: {cmd}")
def main_loop():
print(f"Device {DEVICE_ID} started, polling every {POLL_INTERVAL}s")
while True:
cmd = check_for_command()
if cmd != "none":
print(f"๐ฅ Received command: {cmd}")
execute_command(cmd)
time.sleep(POLL_INTERVAL)
if __name__ == "__main__":
main_loop()
Generate presigned command URLs:
#!/bin/bash
STASH_ID="commands789..."
TOKEN="$REMOTE_CONTROL_TOKEN"
DEVICE_ID="$1"
COMMAND="$2"
if [ -z "$DEVICE_ID" ] || [ -z "$COMMAND" ]; then
echo "Usage: send_command.sh DEVICE_ID COMMAND"
echo "Examples:"
echo " send_command.sh device-01 reboot"
echo " send_command.sh device-01 interval:120"
echo " send_command.sh device-01 enable:temp-sensor"
exit 1
fi
# Generate presigned URL that sets the command
PRESIGNED_URL=$(curl -X POST \
"https://wrendb.com/api/item/$STASH_ID/$DEVICE_ID-cmd/presign" \
-H "Authorization: Bearer $TOKEN" \
-d "$COMMAND" | jq -r '.url')
echo "๐ก Command ready for $DEVICE_ID: $COMMAND"
echo "URL: $PRESIGNED_URL"
echo "(Expires in 15 minutes)"
# Or directly set the command (requires token)
curl -X PUT "https://wrendb.com/api/item/$STASH_ID/$DEVICE_ID-cmd" \
-H "Authorization: Bearer $TOKEN" \
-d "$COMMAND"
echo "โ
Command sent to $DEVICE_ID"