3

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!

Rob
  • 11,492
  • 14
  • 59
  • 94

3 Answers3

2

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

Community
  • 1
  • 1
user3159253
  • 16,836
  • 3
  • 30
  • 56
1

Here a full & working RealLife™ perl's example

...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;

Here another full & working RealLife™ example using

<?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).

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

To open an http api server, you must either install & configure some tools (such as nginx, dancer, or php), or write a python/perl script by yourself. Both of them takes time.

But if you want to put less effort to get a just-working solution, you can simply use netcat:

while true; do
  echo SFRUUC8xLjEgMjAwIE9LDQpDb250ZW50LUxlbmd0aDogMw0KQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluDQpDb25uZWN0aW9uOiBDbG9zZWQNCg0KbmlsDQo= | nc -l 8881 | grep GET
  # The output looks like: GET /switch?device=1&action=on HTTP/1.1
  # parse the received string and run your tdtool
done

If someone access http://127.0.0.1:8881/switch?device=1&action=on, nc will print it out and run your script.
The HTTP request will fail, but who cares? Your tdtool command has been launched.

recolic
  • 554
  • 4
  • 18