I have a Raspberry Pi that I use as a multi-purpose 24/7 device for DLNA, CIFS, VPN etc. Now I bought a TellStick, that is a USB device that can send 433MHz radio commands to wireless power switches, dimmers etc. The manufacturer offers sources and tools for linux, which is really great, btw.
Using a special command (named tdtool) I can send commands to my power switches, e.g.
tdtool --on 1
This switches on device 1. This works very well and stable, so that I want to get away from shell commands in order to make the handling easier. My idea is to set up a very simple web server that only needs to be able to receive GET or POST requests and triggers some action like running the command "tdtool --off 3". So the web server does not even need to serve pages, it just needs to listen to requests.
I want to create a HTTP-based solution because that would allow me to use my smartphone as a remote control. There is an Android app named "Tasker" that is awesome on its own, but it also allows sending customized HTTP requests based on certain conditions, so that I could make my lights go bright when I come home (and Tasker recognizes a connection to my WIFI network or similar).
As the Raspberry is not the most powerful piece of hardware, I'd like to keep things as simple as possible. Basically, I need this:
A HTTP get request comes in, for example:
/switch?device=1&action=on
According to this request, the server should translate that somehow into this:
tdtool --on 1
I am sure that I would find a way to build something like that with Apache and PHP, but I think that would be somewhat overdressed in my case. What would you recommend? Is there some cool python magic that could make this happen? Or some fancy mini webserver with a CGI script? Any thoughts and code samples are greatly appreciated, thanks in advance!
While your question is too "opinion-like", there's an almost instant solution:
nginx - How to run a shell script on every request?
But since you're talking about R-Pi, maybe you will find Python builtin CGIHTTPServer (Python 2) or http.server (Python 3) modules be more suitable for the task of executing a shell command
...using Dancer
# cpan Dancer
$ dancer -a MyApp
$ cd MyApp
$ cat ./lib/MyApp.pm # need to be edited, see bellow
$ bin/app.pl
Now you can call the URL
http://127.0.0.1:3000/switch?device=1&action=on
$cmd will be now executed.
The ./lib/MyApp.pm :
package MyApp;
use Dancer ':syntax';
our $VERSION = '0.1';
get '/switch' => sub {
my $var = params;
my $device = $var->{device};
my $action = "--" . $var->{action};
# building custom system command
my $cmd = "tdtool $action $device";
# running the command
`$cmd`;
return "$cmd\nexecuted\n";
};
true;
<?php
header("HTTP/1.1 200 OK");
if (isset($_REQUEST['action'], $_REQUEST['device'])) {
$device = $_REQUEST['device'];
$action = '--' . $_REQUEST['action'];
$cmd = "tdtool $action $device";
system("$cmd");
echo "Command<br>$cmd<br>executed...";
}
?>
The url is :
http://127.0.0.1/switch.php?device=1&action=on
This require a HTTP server binding on port 80 and the script switch.php to be on the top of your DocumentRoot (for this example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With