Page 1 of 1

Script to turn off/on a power switch for a mural tablet

Posted: Friday 05 January 2018 17:17
by manuloup
Hi,

Chorminator and myself were trying to automate the power of our mural android tablet depending on the battery level.

We finally succeeded, here is our method. For this you need :
- a power switch (rfx, zawe...)
- an android tablet

First we tried with ADB but the daemon on the tablet is very unstable. So we did it with a ssh server.
The original thread in french : https://easydomoticz.com/forum/viewtopic.php?f=7&t=4402

Here are the steps :

1) create a device for your power switch and a dummy device for the battery level.

2) install that simple sshd application on your android tablet : https://play.google.com/store/apps/deta ... ander.sshd
It is free and does not need root.
After the installation, try to connect on your tablet with the ip, default port is 2222

Code: Select all

ssh -p 2222 root@IP_of_domoticz
3) create a ssh key to be able to login on your tablet without a password.

Code: Select all

ssh-keygen -t rsa
Press enter when the passphrase is asked, do not enter any password !
Then copy the content of the id_rsa.pub file to the tablet in the default dir /sdcard/ssh/authorized_keys

You should now be able to connect to your tablet without any password :
root@raspberrypi2:/home/pi# ssh -p 2222 192.168.1.18
user@TU_1491:/storage/emulated/legacy/ssh $


Now you can check if you are able to get the battery level :

Code: Select all

root@raspberrypi2:/home/pi/domoticz/scripts/shell# export TABLETTE_IP="192.168.1.18"
root@raspberrypi2:/home/pi/domoticz/scripts/shell# ssh -p 2222 ${TABLETTE_IP} 'cat /sys/class/power_supply/battery/capacity'
53
Fine ! Now you just have to copy my script and change the parameters with your settings :

Code: Select all

#!/bin/bash
###
# Envoie niveau de batterie a  Domoticz
# Script par manuloup https://easydomoticz.com/forum/viewtopic.php?f=7&t=4402#p38502
# 2017-12-31 : Chrominator / Remontée des messages liés à l'exécution dans le log Domoticz
# 2018-01-04 : manuloup / Utilisation d un serveur SSH sur la tablette 
###
#
###
# YOU NEED TO CHANGE VALUE FOR YOUR CONFIGURATION
# Domoticz server
DOMOTICZ_SERVER="192.168.1.5:8080"
TABLETTE_IP="192.168.1.18"
#
# Device de la tablette
BATTERY_TABLETTE_IDX="378"
#
# Devices de la wallplug
WALLPLUG_TABLETTE_IDX="380"
#
# END CONFIGURATION VALUE
###
cURL="/usr/bin/curl"
#
voir_les_logs () {
   msg="tablette: $1"
   echo $msg
   # Encode le caractère % en html: %25
   msghtml=`echo $msg | sed 's/%/%25/g'`
   # Encode le caractère espace en html: %20
   msghtml=`echo $msghtml | sed 's/ /%20/g'`
   # Les autres caractères même accentués devraient passer
   $cURL -i -H  "Accept: application/json" "http://${DOMOTICZ_SERVER}/json.htm?type=command&param=addlogmessage&message=$msghtml"
}
#
# Get the battery value for the tablet
BATTERY_LEVEL=$(ssh -p 2222 ${TABLETTE_IP} 'cat /sys/class/power_supply/battery/capacity')
#BATTERY_LEVEL=$(echo "${BATTERY_LEVEL}" | tr -d '[:space:]')
voir_les_logs "Niveau de la batterie :${BATTERY_LEVEL} %"
# Set Battery level in domoticz
# Send data to Domoticz
curl -i -H  "Accept: application/json" "http://${DOMOTICZ_SERVER}/json.htm?type=command&param=udevice&idx=${BATTERY_TABLETTE_IDX}&nvalue=0&svalue=${BATTERY_LEVEL}"
#
if [ -z $BATTERY_LEVEL   ] ;
then
   voir_les_logs "Pas de valeur pour la charge de la tablette ! "
   voir_les_logs "On rallume la prise de la tablette "
   curl -s -i -H  "Accept: application/json" "http://${DOMOTICZ_SERVER}/json.htm?type=command&param=switchlight&idx=${WALLPLUG_TABLETTE_IDX}&switchcmd=On"
else
# Test de la valeur de la batterie. On eteind si c'est suerieur a 10 sinon on allume
   if [ $(expr $BATTERY_LEVEL '<' 15 ) = 1 ] ;
   then
      voir_les_logs "Charge batterie inferieure a 15 %, on allume ! "
      curl -s -i -H  "Accept: application/json" "http://${DOMOTICZ_SERVER}/json.htm?type=command&param=switchlight&idx=${WALLPLUG_TABLETTE_IDX}&switchcmd=On"
   else
      voir_les_logs "charge de la batterie superieure a 15%"
   fi
   if [ $(expr $BATTERY_LEVEL '=' 100 ) = 1 ] ;
   then
      voir_les_logs "batterie=100%"
      curl -s -i -H  "Accept: application/json" "http://${DOMOTICZ_SERVER}/json.htm?type=command&param=switchlight&idx=${WALLPLUG_TABLETTE_IDX}&switchcmd=Off"
   else
      voir_les_logs "batterie=${BATTERY_LEVEL}%"
   fi
fi
Do not forget to chmod +x your script and put it in the crontab !

Let me know if you tried it and use it :)

Cheers.

Manu

Re: Script to turn off/on a power switch for a mural tablet

Posted: Thursday 27 February 2020 18:15
by Alfagek
don't know what i'm doing wrong.

But when i copy the file to the tablet key directory and i will login i get the next error back:

[email protected]: Permission denied (publickey).

What i'm doing wrong?

reagards,
Marco

Re: Script to turn off/on a power switch for a mural tablet

Posted: Thursday 27 February 2020 18:18
by manuloup
Alfagek wrote: Thursday 27 February 2020 18:15 don't know what i'm doing wrong.

But when i copy the file to the tablet key directory and i will login i get the next error back:

[email protected]: Permission denied (publickey).

What i'm doing wrong?

reagards,
Marco
Hi Alfagek,

I think you have a problem with the ssh key part. Make sure you can connect with ssh on your tablet without any password.

Re: Script to turn off/on a power switch for a mural tablet

Posted: Thursday 27 February 2020 20:17
by Alfagek
I found the problem. I did make the keys in a directory "domonticz", because is was easier to copy the file with Winscp.

But now i did not do that, en copy it with

Code: Select all

scp -P 2222 /home/user/.ssh/id_rsa.pub 192.168.1.2:/data/data/org.galexander.sshd/files/authorized_keys
to the tablet, it all is running now

Re: Script to turn off/on a power switch for a mural tablet

Posted: Thursday 27 February 2020 20:41
by manuloup
Perfect, I'm glad you use my script :D