Controlling the Raspberry Pi from your mobile

Today I'm going to put a post of the ones I like, They are very simple and look very cool! Well, if we have a Raspberry Pi and we have a command that we want to be easily executed from the mobile such as turning on a house light, or change a TV channel on our Kodi, This is your post! Easy, Simple and family-friendly!

 

Manage-Raspberry-by-Web-00-Bujarra

What I said, if we have a script out there and we want to be able to run it from any mobile device (ios, Android…) or from any browser… the first thing will be to have Apache2 installed and create a PHP file, a JS and upload a PNG image for the icon. When you press the ON button from the browser that will appear on the web it will execute the file .sh we say, What we'll do is 'Add to Home Screen'’ said shortcut to that URL and the mobile either the iPhone/iPad/Android will create an app with the icon, that when pressed will run our program on the Pi.

 

I leave you some screenshots of my mobile phone of how you can control any TV channel, any device we have, How to turn the heating on/off, raise/lower blinds, Turn on/off/dim the lights in the house, Turn on/off the alarm…

 

What I said, We will install the requirements first:

[SourceCode]sudo apt-get install apache2 php5 libapache2-mod-php5[/SourceCode]

 

We'll change the user that will run apache2 (optional, I seem to remember that I did it for commands that carry audio), We edit the file '/etc/apache2/envvars', Correcting the user:

[SourceCode]export APACHE_RUN_USER=pi
export APACHE_RUN_GROUP=pi[/SourceCode]

 

And we restarted Apache:

[SourceCode]sudo /etc/init.d/apache2 restart[/SourceCode]

 

Here are a couple of files, a PHP and a JS, Now that I document them, I also see lines that are left over, if you want you can correct 😉 them if you don't, It doesn't matter that it will work for you too! Este archivo PHP es un formulario que al pulsar el boton “On” ejecutará el script ‘/home/pi/la6_enciende.sh’ que veréis más adelante que pone La Sexta en mi TV. All this, obviamente en ‘/var/www/’:

la6.php

[SourceCode]
<HTML>
<Head>
<title>Domotica casa</title>
<link rel="stylesheet" type="text/css" href="./css/style.css" />
<link rel="apple-touch-icon" href="la6.png">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="./amcharts/amcharts.js" type="text/javascript"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="la6.js"></script>
</Head>
<Bodysuit>
<form id="relay" action="remote_relay.php" method="GET">
<input type="hidden" name="total" id="total" value="">
<input type="button" id="La6submitOn" style="border:1px solid #000; font-size:40Px;" value="On" onClick="La6relayOn()"/>
</Form>
<?php

$state = $_GET["total"];

if ($state == "La6on"){
Exec("/home/pi/la6_enciende.sh");
}
?>
<script type=’text/javascript’>
self.close();
</script>
</Bodysuit>
</HTML>
[/SourceCode]

la6.js

[SourceCode]window.onload = function(){
buttonOff = document.getElementById(‘La6submitOn’);
buttonOff.onClick = La6relayOn;
}

function La6relayOn(){

hidden = document.getElementById("total");
hidden.value="La6on";

form = document.getElementById("Relay");
form.method="GET";
form.action="la6.php";
form.submit();
}[/SourceCode]

GOOD, with this we already have the possibility to execute any script of our Raspberrys from any mobile in a pleasant way for the rest of the family 😉 Remember to put the PNG file with the icon you want to be seen!

One thing, the PHP website has 3 lines that you will have to comment on the first time! Since the idea of the PHP website is that when you click from the mobile phone and open it, run the command and then the website closes automatically so as not to be left residual in the mobile browser. And if we don't comment on it, we will not be able to add it as an app to the 'Desktop’ of the mobile since it will close and will not give us time! So: Discussed > We pin it to the Home screen > We uncomment. Lines in question:

[SourceCode]#&Lt;script type=’text/javascript’&Gt;
# self.close();
#&Lt;/script&Gt;[/SourceCode]

 

What do we have left? Something interesting, if we have Kodi and have remote access enabled, we will know that we can handle it with applications such as: Kodi Remote, Yacht, Kore… we can manage it with JSON in the same way from the shell of our Pi, and create a script that executes what we want, such as switching to a TV channel, change the TV language, record TV…

 

Manage-Raspberry-by-Web-01-Bujarra

The first thing will be to know what our iOS/Android app is sending to Kodi, for this, from a PC we will hear the port 9090 from Kodi, to see what our yacht sends him, Kodi Remote… So we executed:

[SourceCode]nc DIRECCION_IP_RASPBERRY 9090[/SourceCode]

And we hope…

 

Manage-Raspberry-by-Web-02-Bujarra

Having netcat to listen, We will be able to tap the channel we want from our mobile app and on the screen, The NC will get the command that we will need to execute later, or at least an aid to know the ID of each TV channel!

In my case,, to put La Sexta I'll have to run the following command:

[SourceCode]echo '{"id":1,"jsonrpc":"2.0","method":"Player.Open","Params":{"item":{"channelid":93}}}’ | Nc 192.168.1.200 9090[/SourceCode]

 

Example of stopping TV:

[SourceCode]echo '{ "id": 1, "jsonrpc": "2.0", "method": "Player.Stop", "Params": { "PlayerID": 1 } }’ | Nc 192.168.1.200 9090[/SourceCode]

Example to change TV audio so that we practice English!

[SourceCode]echo '{"jsonrpc": "2.0", "method": "Player.SetAudioStream", "Params": { "PlayerID": 1, "stream": "next"}, "id": 1}’ | Nc 192.168.1.200 9090[/SourceCode]

 

Etc, etc… we save this command in a .sh file that we can execute when we open the PHP website from our mobile! Enjoy!

 

Recommended Posts

Author

nheobug@bujarra.com
Autor del blog Bujarra.com Cualquier necesidad que tengas, Do not hesitate to contact me, I will try to help you whenever I can, Sharing is living ;) . Enjoy documents!!!