Create your own adjustable delay powerstrips - AVS Forum | Home Theater Discussions And Reviews
Forum Jump: 
 
Thread Tools
post #1 of 1 Old 11-25-2019, 03:01 PM - Thread Starter
Advanced Member
 
harlekin's Avatar
 
Join Date: May 2007
Posts: 741
Mentioned: 69 Post(s)
Tagged: 0 Thread(s)
Quoted: 317 Post(s)
Liked: 151
Create your own adjustable delay extension leads

(This is for people who can navigate their own home WIFI router and assign static IP addresses to the devices in their home network.)

Today I came across my first instance, when powering on one item in my AV chain with a set delay turned out to be necessary (I had to delay a power on process by just a few minutes). After three hours of googling - I found that timed delay power sockets (with a 'countup' of less than 30 minutes) are considered "professional gear", and that you are supposed to pay premium bucks for those.

So I crafted my own custom solution, using a Raspberry Pi that was already connected to the TV and a few WLAN power sockets (I already had lying around as well.. ).


If you ever run into that problem as well - here is how you tackle it without paying 500 USD for "professional adjustable delay extension cords":

Buy a master/slave multiple sockets power lead. (20 USD)
Connect TV to master.

Buy a few normal multiple socket outlets (powerstrips). (5 USD x2)

Buy a few TP-Link HS100 WLAN power sockets. (20 USD per unit)

Connect those to your WLAN and set them up with their app on your smartphone. You dont have to connect them to the internet,
you dont have to create an account. As long as turning them on and off via your local WLAN works, you are golden.

Assign them static IP adresses in your router and write them down.

Buy a Raspberry Pi (and a case, and a power brick/cable for it ), a 32GB micro sd card, a network cable, and create a Raspberry Pi Pattern generator. (60 USD)
https://www.lightillusion.com/pgenerator.html

Set it up, connect it to your powercord (slave), your network (wired) (and the internet (for the setup stages)), let it boot - then ssh into it using a PC and
Code:
ssh [email protected]
... assuming that that is the IP adress your Raspberry Pi got assigned to by your router.

Password is: PGenerator!!$
--

When master switches on the powerstrip, all WLAN power sockets will switch to previous state the WLAN power socket
was left in (which usually is on).

A script on our Pi will run at the boot of the Pi (which will be triggered by the powerstrip getting power (master/slave)),
and first switch all WLAN power sockets off, then turn them on with the delays you specify.
--

Once you are ssh'ed into your Pi (type ls to see if your see the content of the home directory) do the following:


write
Code:
pkg --check_for_install=netcat --install
press enter

This installs netcat. (For this your Pi has to be online.)

write
Code:
pkg --check_for_install=nano --install
press enter

This installs nano. (For this your Pi has to be online.)

write
Code:
reboot
press enter

To reboot your Pi.

Wait for it to boot up, ssh into it again, then

write
Code:
cd ~
press enter

write
Code:
mkdir delaypower
press enter

write
Code:
cd delaypower
press enter

Create on and off scripts for every WLAN power socket you want to control.

write
Code:
nano 01on.sh
press enter

then copy/paste this into the file
Code:
/root/delaypower/hs100.sh 192.168.1.51 9999 on
press ctrl+x, then y, then enter

(Assuming that 192.168.1.51 is the actual IP address your first WLAN socket has in your home WLAN network.
Replace this with your actual IP for the first WLAN power socket)

write
Code:
chmod +x 01on.sh
press enter

write
Code:
nano 01off.sh
press enter

then copy/paste this into the file
Code:
/root/delaypower/hs100.sh 192.168.1.51 9999 off
press ctrl+x, then y, then enter

(Assuming that 192.168.1.51 is the actual IP address your first WLAN socket has in your home WLAN network.
Replace this with your actual IP for the first WLAN power socket)

write
Code:
chmod +x 01off.sh
press enter

(optional, repeat for 02on.sh/02off.sh scripts for your second WLAN power socket, 03on.sh/03off.sh scripts for your third,
and so on and so forth)

Create the main script.
Curtesy to ggeorgovassilis on github, see:
https://github.com/ggeorgovassilis/l...tplug/hs100.sh

write
Code:
nano hs100.sh
press enter

then copy/paste this into the file
Code:
#!/bin/bash

##
#  Switch the TP-LINK HS100 wlan smart plug on and off, query for status
#  Tested with firmware 1.0.8
#
#  Credits to Thomas Baust for the query/status/emeter commands
#
#  Author George Georgovassilis, https://github.com/ggeorgovassilis/linuxscripts

ip=$1
port=$2
cmd=$3

check_binaries() {
  command -v nc >/dev/null 2>&1 || { echo >&2 "The nc programme for sending data over the network isn't in the path, communication with the plug will fail"; exit 2; }
  command -v base64 >/dev/null 2>&1 || { echo >&2 "The base64 programme for decoding base64 encoded strings isn't in the path, decoding of payloads will fail"; exit 2; }
  command -v od >/dev/null 2>&1 || { echo >&2 "The od programme for converting binary data to numbers isn't in the path, the status and emeter commands will fail";}
  command -v read >/dev/null 2>&1 || { echo >&2 "The read programme for splitting text into tokens isn't in the path, the status and emeter commands will fail";}
  command -v printf >/dev/null 2>&1 || { echo >&2 "The printf programme for converting numbers into binary isn't in the path, the status and emeter commands will fail";}
}

# base64 encoded data to send to the plug to switch it on 
payload_on="AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog=="

# base64 encoded data to send to the plug to switch it off
payload_off="AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu3qPeow=="

# base64 encoded data to send to the plug to query it
payload_query="AAAAI9Dw0qHYq9+61/XPtJS20bTAn+yV5o/hh+jK8J7rh+vLtpbr"

# base64 encoded data to query emeter - hs100 doesn't seem to support this in hardware, but the API seems to be there...
payload_emeter="AAAAJNDw0rfav8uu3P7Ev5+92r/LlOaD4o76k/6buYPtmPSYuMXlmA=="

usage() {
 echo Usage:
 echo $0 ip port on/off/check/status/emeter
 exit 1
}

checkarg() {
 name="$1"
 value="$2"

 if [ -z "$value" ]; then
    echo "missing argument $name"
    usage
 fi
}

checkargs() {
  checkarg "ip" $ip
  checkarg "port" $port
  checkarg "command" $cmd
}

sendtoplug() {
  ip="$1"
  port="$2"
  payload="$3"
  echo -n "$payload" | base64 -d | nc $ip $port  || echo couldn''t connect to $ip:$port, nc failed with exit code $?
}

check(){
  output=`sendtoplug $ip $port "$payload_query" | base64`
  if [[ $output == AAACJ* ]] ;
  then
     echo OFF
  fi
  if [[ $output == AAACK* ]] ;
  then
     echo ON
  fi
}

status(){
  payload="$1"
  code=171
  offset=4
  input_num=`sendtoplug $ip $port "$payload" | od --skip-bytes=$offset --address-radix=n -t u1 --width=9999`
  IFS=' ' read -r -a array <<< "$input_num"
  for element in "${array[@]}"
  do
    output=$(( $element ^ $code ))
    printf "\x$(printf %x $output)"
    code=$element
  done
}

##
#  Main programme
##
check_binaries
checkargs
case "$cmd" in
  on)
  sendtoplug $ip $port "$payload_on" > /dev/null
  ;;
  off)
  sendtoplug $ip $port "$payload_off" > /dev/null
  ;;
  check)
  check
  ;; 
  status)
  status "$payload_query"
  ;;
  emeter)
  status "$payload_emeter"
  ;;
  *)
  usage
  ;;
esac
exit 0
press ctrl+x, then y, then enter

write
Code:
chmod +x hs100.sh
press enter

write
Code:
cd /etc/init.d/
press enter

write
Code:
nano delayedon
press enter

then copy/paste this into the file
Code:
#!/bin/sh
PROGRAM_NAME="delayedon"
INIT_NAME="delayedon"
PROGRAM_DAEMON="/bin/sleep"

case "$1" in
    start)
        echo -n "Starting $PROGRAM_NAME..."
        sleep 10 && /root/delaypower/01off.sh && /root/delaypower/02off.sh && sleep 10 && /root/delaypower/01off.sh && /root/delaypower/02off.sh && sleep 5 && /root/delaypower/01on.sh && sleep 2 && /root/delaypower/02on.sh
        echo "done"
    ;;

    stop)
        echo -n "Stopping $PROGRAM_NAME..."
        echo "done"
    ;;
    
    restart)
        $0 stop
        $0 start
     ;;

    *)
        echo "Usage: /etc/init.d/$INIT_NAME {start|stop|restart}" >&2
        exit 1
    ;;
esac
press ctrl+x, then y, then enter

(You can add more WLAN power sockets in the line above, make sure commands are connected with && and or play with the delay
times in seconds in the script above.

sleep 10 - btw means, that the Pi will wait for 10 seconds until it executes the next command (connected with &&), so this is how we get our delays. Also sleep 110 or even higher second values are possible as well. I have one such value in my actual script. )

write
Code:
chmod +x delayedon
press enter

write
Code:
rcset
press enter

select delayedon, press enter, enter 99, press enter, exit rcset with arrow keys and enter

write
Code:
reboot
press enter

And thats it - upon reboot you should hear your relays click at the timeintervalls you've set before.

Thanks go to Riccardo Biasiotto for creating the RaspberryPi Pattern Generator, and for helping me out today in getting familiar with init.d scripting.

And of course George Georgovassilis for the initial HS100 shell script to control those WLAN power sockets from a command line.

Last edited by harlekin; 11-26-2019 at 02:11 AM.
harlekin is offline  
Sponsored Links
Advertisement
 
Reply Display Calibration

Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off