How setting the TZ environment variable avoids thousands of system calls

https://blog.packagecloud.io/eng/2017/02/21/set-environment-variable-save-thousands-of-system-calls/

Advertisements

How to control a switch by audio

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

rx-audio-adapter.sh

#!/bin/bash

rm rx.log

while true
do
        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
        else
                echo unknown command >> rx.log
        fi
        sleep 1
done

 

usb-switch-service.py

Python dependencies: pyserial, web.py

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
            time.sleep(0.1)
            status = ser.dsr
        elif action == 'off':
            print "off"
            ser.dtr = False
            time.sleep(0.1)
            status = ser.dsr
        else:
            print "unknown command"
            status = ser.dsr
        return json.dumps(status)
      
if __name__ == "__main__":
    app.run()

 

start-switch.sh

#!/bin/bash

pushd /opt/audio-switch

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

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

popd

 

dtmf2num

wget http://aluigi.altervista.org/mytoolz/dtmf2num.zip

then compile it (make).

 

DTMF tones

The DTMF tone files can be generated from here:

http://www.audiocheck.net/audiocheck_dtmf.php

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

loop.sh

#!/bin/bash

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

 

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:

...

.o:
        @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

Script to restart network connection, if it fails

# cat check-network.sh

#!/bin/bash
# replace wlan0 with your device name
# as given by ip addr or ifconfig
while true 
do
    # keep checking if we have ip address and can ping outside  
    wifi_info=$(ip -4 -o addr  show wlan0 )
    ping -w 30 baidu.com
    #ping -w 10 google.com  # test bad case
    ping_result=$(echo $?)
    echo $ping_result
    
    while [ -n "$wifi_info" ] && [ $ping_result -eq 0 ];
    do
       sleep 180    # 180 sec = 3 minutes
       wifi_info=$(ip -4 -o addr  show wlan0 )
       ping -w 30 baidu.com
       #ping -w 10 google.com  # test bad case
       ping_result=$(echo $?)
       echo $ping_result
    done

    # We get here only if IP address is lost
    # which means we're off-line
    # restart wifi 
    nmcli radio wifi off
    sleep 10
    nmcli radio wifi on
    sleep 20  # wait that wifi restarts and connects
done

 

# crontab -l

...
# Run at the boot
@reboot /root/scripts/check-network.sh > /dev/null &