User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Control and communication via internet

Wed Jan 11, 2012 2:09 pm

Hi Everyone,

I've not read anything similar but forgive me if not (not quite sure what I'd be searching for anyway, since I really don't know much about this).

Basically, wondering what ways there are to send simple messages to the R-Pi to control various programs (and ideally hardware) through the internet (not something like VNC or a terminal connection, but web based single command messages type thing).

For example, you have a web page with various buttons on which when pressed connect to the R-Pi and cause an LED to light.  On the R-Pi side of things it receives a message from the web-page with a code which is interpreted by whatever programming language you are using to switch on the GPIO output as required.  Similarly data can be sent back to the webpage about inputs too.

Now I'm sure there are 101 ways to do this, but any thoughts about what the easiest ways are?  Would some kind of web-server running on the R-Pi be needed or are there other ways to do it without that.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

loldma
Posts: 2
Joined: Tue Nov 01, 2011 3:25 pm

Re: Control and communication via internet

Wed Jan 11, 2012 4:57 pm

Maybe a Webserver? PHP has the ability to execute commands. http://php.net/manual/en/funct.....l-exec.php

So it's pretty simple, I guess:
-A HTML site, with the buttons, to a controller php.
-The controller php, it is just some if // case, and shell_exec.
-Some bash/python/etc scripts, what contains the predefined actions. You can run different scripts, or with shell_exec you can call the same script with different parameters.

I think its an easy-go. The harder part is the security, I want to take the rPi into my car as a BoardComp, but its not so good if a hacker can break into it

Tomo2k
Posts: 127
Joined: Mon Dec 19, 2011 10:00 pm

Re: Control and communication via internet

Wed Jan 11, 2012 5:09 pm

To have a web page requires a webserver running on the RPi.

It is very easy for a webserver to be running server-side code that does "real stuff" - in fact, the forum we are using now does exactly that in order to store our inane ramblings.

Once you are running your own software on the server, you can make it do anything. Anything at all!

- For example, my set-top box has a webserver running on it that lets me schedule recordings and do other useful things.

Look up "LAMP stack" in Google for some idea - the P would probably stand for Python on the RPi.

roelfrenkema
Posts: 105
Joined: Sat Jan 07, 2012 5:17 pm

Re: Control and communication via internet

Wed Jan 11, 2012 5:22 pm

loldma said:


Maybe a Webserver? PHP has the ability to execute commands. http://php.net/manual/en/funct.....l-exec.php

So it's pretty simple, I guess:
-A HTML site, with the buttons, to a controller php.
-The controller php, it is just some if // case, and shell_exec.
-Some bash/python/etc scripts, what contains the predefined actions. You can run different scripts, or with shell_exec you can call the same script with different parameters.

I think its an easy-go. The harder part is the security, I want to take the rPi into my car as a BoardComp, but its not so good if a hacker can break into it



PHP doesn't need a webserver really it can read socks. you could telnet ssh or netcat right into it if you want too,

willlim
Posts: 27
Joined: Tue Nov 08, 2011 12:16 pm

Re: Control and communication via internet

Wed Jan 11, 2012 5:41 pm

You could make a program that controls than you want and make a php or any other kind of script that creates the html, is very commom the use of   shell_exec on php to call for commands in shell mode, but is this need some precautions for reasons on security.
the ideial is make a program that do everting you want and call then with the php.
annother idea if you dont need a realtime response, is possible the php store some kind of data in a database or file and configure the cron to execute the program/script that read the data,from time to time

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Control and communication via internet

Wed Jan 11, 2012 5:54 pm

The standard Python library includes three HTTP server implementations, one of which allows CGI scripts to be used.

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Control and communication via internet

Wed Jan 11, 2012 7:41 pm

I have written a Python module AuthRPC that will work on a RPi:

http://pypi.python.org/pypi/AuthRPC/

It is based around JSONRPC with some of my own non-standard additions to make it more secure and more flexible.

To use it, you create a Python program (server) to run on the RPi and another Python program that runs somewhere else (client).  The Rpi server program could do anything you want, for example GPIO (inputs and outputs).

I am in the process of writing a client for Android as well so you can control your RPi from an Android phone or tablet.

Edit: It would not be too difficult to  write a client library in JavaScript as well

User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Control and communication via internet

Wed Jan 11, 2012 8:22 pm

Some excellent answers thank you people!

Hopefully I can work out how to use some of the methods mentioned, great to see that it is possible though.  Cheers.

Edit: I think I'll ask Liam the R-Pi tutorial guy if he will do a tutorial on it, since it will be useful for a lot of people, and probably a little beyond my current skills at the moment.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

liamfraser280
Posts: 354
Joined: Tue Oct 04, 2011 6:53 pm
Contact: Website

Re: Control and communication via internet

Fri Jan 13, 2012 4:02 pm

Hi all,

I'm Liam the tutorial guy

Anyone have any specific ideas about what this could be used to do a tutorial on? As in a scenario it would tie in well with.

Cheers

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Control and communication via internet

Fri Jan 13, 2012 4:45 pm

I've been having great results playing around with the C-code found here:

http://www.linuxhowtos.org/C_C.....socket.htm

The article deals with opening a socket in a client and a server application. Both TCP and UDP examples are given.
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Control and communication via internet

Tue Jan 17, 2012 7:19 pm

Thanks for the help. Will have a good look into the links.

@Liam
As for a particular example, I"m mainly thinking to control and monitor hardware. But it could equally be applied to controlling software, such as playing pre-set sounds to perhaps controlling a media player.

I guess I have some experimenting to do...
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

gjs
Posts: 148
Joined: Fri Dec 02, 2011 8:04 pm

Re: Control and communication via internet

Wed Jan 25, 2012 11:44 pm

+1 for a Python tutorial on controlling stuff via a web interface, and controlling a media player sounds like a great idea.  I've just been reading about MPD media server and MPC command line player and these seem like ideal candidadtes for web based control.

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Control and communication via internet

Sat Jan 28, 2012 11:15 pm

+1 for tutorial on web interface for Gertboard. Say a web page with row of buttons to control LEDs on gertboard and indicators to show position of switches.

Doogle
Posts: 16
Joined: Sun Jan 29, 2012 5:55 am

Re: Control and communication via internet

Sun Jan 29, 2012 8:46 am

@Liam

My pennyworth:

I think that a basic tutorial on Asynchronous TCP /IP bi-directional communications between the Pi and another computer would be useful. (eg Buffering data, example of an Application Protocol etc.) What happens at either end once messages have been sent and received is really down to the purpose of the application itself. eg It could wobble a bit or two on the Parallel port on a PC to control some hardware, or, as I have done, with a Blackberry, control a PowerPoint presentation via WiFi (press "F" on the Blackberry and the next slide is displayed, press "B" and the previous slide is displayed).

Another possible scenario might be a "Panic Button"; press something connected to the Pi which then sends a message to a server on the Internet which raises an alarm and send a message back once the alarm has been acknowledged. (I built a similar system for Dad-in-law but had to use a UHF Transmitter and Receiver "cause I couldn"t find a suitable small enough WiFi enabled device.)




liamfraser280
Posts: 354
Joined: Tue Oct 04, 2011 6:53 pm
Contact: Website

Re: Control and communication via internet

Sun Jan 29, 2012 10:06 am

@gjs

There are already quite a few MPD web clients so I don't think there would be much point re inventing that.

Gertboard is a good idea though and also the Panic Button idea. How about a pressure pad under the doormat connected to a Pi that triggers a notification on your main pc?

Any other ideas ?

Cheers guys,

Liam.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Control and communication via internet

Sun Jan 29, 2012 11:29 am

I've just come back to this thread, and find myself wondering why we were all recommending web servers on the RPi. That is not the way to go about it.

What you want to do is to write an application on the RPi that listens for a message on a  socket, and an application on the remote PC that sends the message (or indeed vice versa.) There is no need for a webserver anywhere. The only thing a webserver does is make it look pretty, and it makes it harder to implement anything more interesting than hand-toggling switches on a browser.

However you do it, if you want it accessible from the Internet, you'll have to poke holes in your firewall, and implement some security.

liamfraser280
Posts: 354
Joined: Tue Oct 04, 2011 6:53 pm
Contact: Website

Re: Control and communication via internet

Sun Jan 29, 2012 11:34 am

I agree with what your saying. I think people want web interfaces because they are portable and can work on anything with a browser.

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Control and communication via internet

Sun Jan 29, 2012 9:51 pm

The AuthRPC Python package I mentioned earlier in this thread works using HTTP on port 80 and can be run as a standalone program just as ruwin described. You can get it to work with Apache for heavy duty applications with virtually no change in your code. The other advantage is that you can work through corporate firewalls where your only internet access is through an HTTP proxy.

By the way, I have just started a Python module to control GPIO in Python.

Edit: you can still use a web browser running some javascript for the client, keeping your options open.

liamfraser280
Posts: 354
Joined: Tue Oct 04, 2011 6:53 pm
Contact: Website

Re: Control and communication via internet

Sun Jan 29, 2012 9:55 pm

Never thought about the proxy thing, good point!

Sounds good

Cheers,

Liam.

grahamed
Posts: 277
Joined: Mon Jan 30, 2012 7:01 pm

Re: Control and communication via internet

Mon Jan 30, 2012 8:24 pm

Hi

First post.

I have been reading this with interest, particularly comments against web servers.

I propose to use an R-Pi as a remote control extender mainly for the Sky+ box. I envisage a web page accessed by, say, an iPhone which triggers a flashing IR LED. So far I have a PIC 16F628 flashing a LED at 36kHz modulated by the RC6 protocol Sky use - all working well though the code is disgraceful - 100's of NOPs; timing accuracy trumps elegance I afraid. Sky+ seems to be very picky, much more so than the Sony (RC5) TV stood next to it.  At present the particular command byte transmitted is hard-coded into the program. Next is to get the required command via the on-chip UART - tomorrow probably.

I have used IIS (vbscript and asp) for all the web coding I have done but I am pretty sure I can do the Linux webery  when I know what web server, etc I am going to use. Bearing in mind - the memory restrictions, the very restricted number of users, the lack of need for any kind of data (other than the commands for the Sky and a few others), all static stuff in fact - can anyone advise what is the lightest web server available?  I do feel a web server is the way to go. Seems to work for routers and such. LAMP (where my P would be python) seems obvious but maybe OTT and is there memory space anyway?

At present I am using a VM so I think my progress will come to a halt after the visuals of the web page until I can get hold of a real R-Pi, I know python can send serial but from within a VM?  We shall see, maybe.

So what server stack?

Graham

liamfraser280
Posts: 354
Joined: Tue Oct 04, 2011 6:53 pm
Contact: Website

Re: Control and communication via internet

Mon Jan 30, 2012 8:34 pm

Hi Graham.

I'd reccomend lighttpd as the web server. I have the daemon running now and it's using 808kb just serving a couple of files. (Putty and VNC for windows for if I want to connect to my machine from somewhere).

As far as I know, It can do much the same as Apache but has less support so I wouldn't reccomend in a commercial environment.

Hope this helps you out.

Cheers,

Liam.

grahamed
Posts: 277
Joined: Mon Jan 30, 2012 7:01 pm

Re: Control and communication via internet

Mon Jan 30, 2012 8:57 pm

Hi Liam

Thanks for that - as little as 808k -  no problem then as my VM currently has 101/256M free.

I enjoyed the tutorials.  Thanks again.

Graham

stef_aap
Posts: 1
Joined: Thu Mar 08, 2012 9:42 pm

Re: Control and communication via internet

Thu Mar 08, 2012 10:35 pm

isn't execnet a possible (easy) way to go ?

sanders
Posts: 6
Joined: Sun Mar 11, 2012 7:38 am

Re: Control and communication via internet

Sun Mar 11, 2012 8:25 am

Hi all,

First post. (Being a c# programmer and hardware hobbyist, trying to convert to apache/python and all other open source stuff ).

I have the following components :

1. Wind/light/temperature sensor communication with RS485.

2. Motorized venetian blinds communicating also with RS485 communication.

Using the Pi I want to connect both lines to the gpio of the Pi.

Using a web interface I want to have buttons to open or close the venetian blinds. Also the sensor values need to be displayed.

Depending on control logic the Pi also needs to take action on the sensor values by closing or opening the blinds...

Conceptually I know what needs to be done. I have no real idea on which components/framework etc is best to use.... A tutorial would be great !

Sander.

Return to “Other projects”