How to control a switch by audio

Copy the following files into a directory (e.g. /opt/audio-switch)


rm rx.log

while true
        rm in.wav
        rec in.wav silence 1 0.1 3% 1 3.0 3%
        rx=$(./dtmf2num in.wav | tail -n 1 | cut -c 18-)
        echo $rx >> rx.log
        if [ "$rx" == "1" ]; then
                echo switch ON >> rx.log
                curl http://localhost:8080/on
        elif [ "$rx" == "D" ]; then
                echo switch OFF >> rx.log
                curl http://localhost:8080/off
                echo unknown command >> rx.log
        sleep 1

Python dependencies: pyserial,

import serial
import time
import json
import web

# /status       -> return the status of the switch (on/off)
# /on           -> turn ON the switch and return the status (on/off)
# /off          -> turn OFF the switch and return the status (on/off)
urls = (
    '/(.*)', 'ActionController'

app = web.application(urls, globals())

ser = serial.Serial('/dev/ttyUSB0')

class ActionController:
    def GET(self, action):
        status = ""
        if action == 'status':
            print "status"
            status = ser.dsr
        elif action == 'on':
            print "on"
            ser.dtr = True
            status = ser.dsr
        elif action == 'off':
            print "off"
            ser.dtr = False
            status = ser.dsr
            print "unknown command"
            status = ser.dsr
        return json.dumps(status)
if __name__ == "__main__":


pushd /opt/audio-switch

# start usb-switch-service
python > /tmp/usb-switch-service.log &
sleep 5
curl http://localhost:8080/off

# start rx-audio-adapter
rm in.wav
nohup ./ > /dev/null 2>&1 &





then compile it (make).


DTMF tones

The DTMF tone files can be generated from here:

The rx-audio-adapter can be tested with the following script:


while true
        aplay audiocheck.net_dtmf_1.wav
        sleep 10
        aplay audiocheck.net_dtmf_D.wav
        sleep 10


How to install ratox

In order to install ratox, we need first to install toxcore:

then we can install ratox:

Note: if you get a compile error about librt, add “-lrt” in the Makefile as indicated here:


        @echo LD $@
        @$(LD) -lrt -o $@ $< util.a $(LDFLAGS)


Here you can find information about the file structure and how to use it:


DTMF generator and decoder