Tutorial: rtl_433 start at boot, with hotplug

Tutorial to get a RTL2823 radio to start at boot, run as non privileged user.
Rtl_433 starts at boot and at USB insertion/resinsertion

Create non-root user and group:

echo 'rtl_433:x:120:120::/var/run/rtl_433:/bin/false' >> /etc/passwd
echo 'rtl_433:x:120:rtl_433' >> /etc/group

Download rtl_433 software:
opkg update && opkg install rtl_433

Create rtl_433.config file. This is the basic setting used for reporting every reading to a MQTT server. There are a lot of settings, so check out the rtl_433 github repo and the example config file located at /etc/rtl_433/rtl_433.example.conf

echo 'output mqtt://<MQTT-IP>:<PORT>,user=<USERNAME>,pass=<PASSWORD>,devices=rtl_433[/id],retain=0' >> /etc/rtl_433/rtl_433.conf

Create a init script that starts the program in the background at boot time and lets the program be started and stopped with the service rtl_433 start/stop command. Create the file /etc/init.d/rtl_433.conf and edit with text editor.
nano /etc/init.d/rtl_433.conf

Enter the following script, taken and modified from OpenWrt wiki page about procd init scripts. There are more to these scripts that you may want to check out.

#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

start_service() {
         procd_open_instance rtl_433
         procd_set_param command /usr/bin/rtl_433 # location of rtl_433 executable
         procd_append_param command -c /etc/rtl_433/rtl_433.conf # command of pointing rtl_433 to a config file using the -c flag
         procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
         procd_set_param file /etc/rtl_433/rtl_433.conf # restarts the service automatically if the config file changes
         procd_set_param stdout 1 # forward stdout of the command to logd
         procd_set_param stderr 1 # same for stderr
         procd_set_param user rtl_433 # run service as user rtl_433
         procd_set_param pidfile /var/run/rtl_433.pid # write a pid file on instance start and remove it on stop
         procd_set_param term_timeout 60 # wait before sending SIGKILL
         procd_close_instance
}

Set execute permissions:
chmod +x /etc/init.d/rtl_433.conf

Create a hotplug rule that sets permissions for the USB device, sets a symbolic link (optional) and starts/stops the service upon USB insertion/removal. Create a directory and a file with your text editor of choice:

mkdir -p /etc/hotplug.d/usb
nano /etc/hotplug.d/usb/99-rtl_sdr

Insert the following script. The first line is an environment variable that sets the device ID. You get the USB ID from running lsusb. Ignore the leading zero as in the example.

Example:
Bus 003 Device 006: ID 0bda:2838 Realtek RTL2838UHIDIR
Exchange the bda/2838 below to your device ID.

RTL433_PRODID="bda/2838/100" # Enter your RTL_SDR USB deice ID
SYMLINK="rtl433"
M-
if [ "${PRODUCT}" = "${RTL433_PRODID}" ]; then
    if [ "${ACTION}" = "add" ]; then
        ln -s /dev/${DEVNAME} /dev/${SYMLINK}
        chown rtl_433:rtl_433 /dev/${DEVNAME} /dev/${SYMLINK}
        service rtl_433 start
        logger -t hotplug "Symlink from /dev/${DEVNAME} to /dev/${SYMLINK} created"
    fi
fi

if [ "${PRODUCT}" = "${RTL433_PRODID}" ]; then
    if [ "${ACTION}" = "remove" ]; then
        service rtl_433 stop
        rm /dev/${SYMLINK}
        logger -t hotplug "Symlink /dev/${SYMLINK} removed"
    fi
fi

Enable and start the service:

service rtl_433 enable
service rtl_433 start