Skip to content

Commit

Permalink
Add HTTP cmd API examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Feb 1, 2024
1 parent 98b4b7c commit 2122cc2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
68 changes: 68 additions & 0 deletions examples/rtl_433_http_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3

"""Custom hop controller example for rtl_433's HTTP cmd API."""

# Start rtl_433 (`rtl_433 -F http`), then this script.
# Needs the Requests package to be installed.

import requests
import json
from time import sleep

# You can run rtl_433 and this script on different machines,
# start rtl_433 with `-F http:0.0.0.0`, and change
# to e.g. `HTTP_HOST = "192.168.1.100"` (use your server ip) below.
HTTP_HOST = "127.0.0.1"
HTTP_PORT = 8433


def set_freq(freq):
return send_cmd({'cmd': 'center_frequency', 'val': freq})


def set_rate(rate):
return send_cmd({'cmd': 'sample_rate', 'val': rate})


def send_cmd(params):
url = f'http://{HTTP_HOST}:{HTTP_PORT}/cmd'
headers = {'Accept': 'application/json'}

# You will receive JSON events, one per line terminated with CRLF.
# Use GET
response = requests.get(url, params=params, headers=headers, timeout=70, stream=True)
# or POST
# response = requests.post(url, data=params, headers=headers, timeout=70, stream=True)
print(f'Sending {params} to {url}')

# Answer is lines of JSON
return response.text


def rtl_433_control():
"""Simple timed control of rtl_433 in a loop forever."""

# Loop forever
while True:
try:
# Set first hop
sleep(10)
print(set_freq(433920000))
print(set_rate(250000))

# Set second hop
sleep(10)
print(set_freq(868000000))
print(set_rate(1024000))

except requests.ConnectionError:
print('Connection failed, retrying in 60s...')
sleep(60)


if __name__ == "__main__":
try:
rtl_433_control()
except KeyboardInterrupt:
print('\nExiting.')
pass
15 changes: 15 additions & 0 deletions examples/rtl_433_http_cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

# Custom hop controller example for rtl_433's HTTP cmd API.

# Start rtl_433 (`rtl_433 -F http`), then this script.
# Needs the xh tool installed (or httpie and change `xh` to `http`)

while : ; do
sleep 10
xh :8433/cmd cmd==center_frequency val==433920000
xh :8433/cmd cmd==sample_rate val==250000
sleep 10
xh :8433/cmd cmd==center_frequency val==868000000
xh :8433/cmd cmd==sample_rate val==1024000
done

0 comments on commit 2122cc2

Please sign in to comment.