Web interface like Cups, Router etc.

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Web interface like Cups, Router etc.

Postby purplepenguin » Sun Jan 05, 2014 2:48 pm

Hi all

Bear with me I'm not sure of the correct technical terms of what I'm asking.

Over the last year or so I have been playing with Arduino and Rpi. Little automation projects. Here's an example. I have an Arduino, ultrasonic sensor and relay attached to my desk lamp and screens. When I sit at my desk the Arduino turns my screens and desk lamp on. Then when I leave my desk the Aduino turns the screens and light off after 60 secs.

I would like to create a simple web interface that I can access from any browser (Computer, tab or phone), Like the ones Routers and other programs such as CUPS use.

I know how to write a static web page using HTML and CSS. I know that I would need a web server such as Apache running to serve the page. What I don't know is how do I get the HTML to send or execute a command to the program.

I know if I use an Ethernet shield the Aduino can check the packet header for a particular string and then act accordingly. Is that the same process that say CUPS would use. It seams a bit basic?

Lets say that I have a basic web page with a button to toggle the light. How would I get the web page to send a command when I click the button. I know there is a need for some logic to check the state of the light. i.e.

When button click
if (light var == 0);
light var =1;
else light var =0;

and the command to the Arduino is sent via the serial com or ethernet.

I've been researching this for a while. But the google results generated from my search always point to web apps for iPhones and Android. I don't want to create an App I want a web page. I've managed to work out that I need something like PHP/Java? to perform the function. Am I right in thinking PHP/java is what I need?

I do hope that makes sense :)

TIA

PP
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 133
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line

Postby guy » Sun Jan 05, 2014 7:27 pm

You need something a little more programmable on the web server than a plain http daemon. The classic Linux based solution is the LAMP stack - Linux, Apache, MySQL, Perl/Python/PHP. Ruby is sometimes used as an honorary "P" while you probably don't need a database but can get away with a script or two.

Apache is not the only good web server. It does have all the bell-and-whistle options such as PHP in case you need them, depending on whether you are more desperate to install them or to avoid installing stuff.

Check out those "P"s and choose your weapon. Or, (Gulp!) get into Java, maybe via JSP or whatever. Since it's an Arduino I wouldn't recommend Java as it can bloat your system.

You can use HTML Forms (and/or javascript if you want a fancy user interface) in your web page to send your selected option/s back to the server. You may/will need to set up Apache to pass your HTTP requests to the correct script - there are a lot of options here and I am v. rusty, so ask around. Probably the simplest is to create say a PHP script which can both process your requests and masquerade as index.html (once you have told Apache to default to it), in fact that may well be all you need.

Further elaboration might be to capture the Arduino status and use it to update the web display.

HTML forms: use the <input ...> element to create clickable widgets, with one along the lines of say <input type="submit" name="Enter" value="send "> to create an "Enter" button (sorry, v. rusty again on the exact code).
"Klinger, do you know how many zoots were killed to make that one suit?" — BJ Hunnicutt
User avatar
guy
LXF regular
 
Posts: 1097
Joined: Thu Apr 07, 2005 12:07 pm
Location: Worcestershire

Postby purplepenguin » Sun Jan 05, 2014 8:17 pm

Thanks Guy.

I've seen the HTML forms being used on other Arduino projects is that the same as HTML post/get?

I'm glad you said one of the Ps. As I've been working through the PHP track on Code Academy it seems fairly easy to pick up. I've not dabbled with Python yet but plan to soon. I'm hoping to avoid databases and Java for now. Something else that popped up is a MQTT server, which looks really interesting maybe a bit over kill for now though. I've got an Apache LAMP server installed on a VM. Although I've not really used it.

I think the HTML forms need further investigation.

Going back to PHP for a moment. If I have a basic bash script that writes the output of
Code: Select all
ls
to a txt file. Could I use PHP to call the script directly? Or is there another mechanism between the PHP and the script? That seems to be something I could get my head around, I like to take small steps and build up to the big stuff.

Thanks for your reply.
PP
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 133
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line

Postby Dutch_Master » Sun Jan 05, 2014 8:52 pm

You may well stick to Bash scripts:
http://httpd.apache.org/docs/2.2/howto/cgi.html

Nice 'n easy :)

(mind, Apache, although very powerful, is really overkill for your application. See if any of the lighter webservers available will work too, thus saving on resources)
Dutch_Master
LXF regular
 
Posts: 2459
Joined: Tue Mar 27, 2007 1:49 am

Postby nelz » Sun Jan 05, 2014 8:52 pm

MQTT is so minimal it can't be overkill for anything, evan saying "Hello World", but I don't se how it would fit in with what you are doing. MQTT is a messaging protocol, not a web service.

Hmm, maybe an MQTT tutorial is called for, I don't think that's been covered n LXF before.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8549
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby purplepenguin » Sun Jan 05, 2014 9:56 pm

Hi Nelz

I heard about MQTT on a site called superhouse.tv. The chap on there described it as a sort of drop in replacement for HTTP post. He said that although HTTP post is suitable it's a bit heavyweight for this sort of thing.

This is what I understood from his video.
MQTT is a subscribe/publish service. If my web UI has two buttons one for light on and one for light off. When one of the buttons is clicked Apache would publish to the channel either "light on" or "light off" accordingly. The Arduino/Rpi which is subscribed to the same channel would see the publish and take action.

Here's a link to the video. He start talking about MQTT at around 10min 40sec.
http://www.superhouse.tv/episode/home-automation-system-architecture

I think a tutorial would be a great idea.

@DutchMaster. Thanks for your link I'm going to have a look now. :)
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 133
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line

Postby guy » Sun Jan 05, 2014 10:39 pm

purplepenguin wrote:I've seen the HTML forms being used on other Arduino projects is that the same as HTML post/get?

More or less. Broadly, when you request a web page or data you forward an HTML get but when you send form data you forward an HTML post. Your browser knows the difference and you can set Apache how to respond to each kind if you need to.

If I have a basic bash script that writes the output of
Code: Select all
ls
to a txt file. Could I use PHP to call the script directly? Or is there another mechanism between the PHP and the script?

I'm no expert in PHP but I'm pretty sure it can. Worst case would be PHP passing it to Apache to run the script. Alternatively, as suggested, you could just run a bash script directly from Apache. But getting the script to generate and serve your updated web page might be more of a challenge than giving that task to PHP.

On MQTT, it is a box-to-box protocol and nothing to do with the web. Its relevance depends on where you install the web server. Stick a small one on the Arduino and MQTT is redundant because your PC-to-Arduino protocol is then HTTP. Stick say Apache on your PC and you might find MQTT a good way for Apache to talk to the Arduino - see Apache ActiveMQ for the kind of support built into Apache.
"Klinger, do you know how many zoots were killed to make that one suit?" — BJ Hunnicutt
User avatar
guy
LXF regular
 
Posts: 1097
Joined: Thu Apr 07, 2005 12:07 pm
Location: Worcestershire

Postby purplepenguin » Mon Jan 06, 2014 11:20 am

Thank you to all who replied. I now know what I should be looking into. It looks as though I have a few options.

To summarise my understanding.

I think the first thing I need to do is decide how the Web server will communicate with the Arduino. Serial interface or Ethernet.

If I use the Serial Interface. I could use CGI to call a script that can read or write to the Arduino's serial interface. I'm really interested in this CGI. I could write a script in PHP, Bash, C, Perl etc. This doesn't require me to learn a new language.

Using Ethernet would kill two birds with one stone. I could use HTTP forms to communicate with the Arduino and Power over Ethernet meaning I don't have to worry about power for the devices. Freetronics make an Arduino compatible board with Ethernet and PoE built in. I could then splice an Ethernet and Power cable together or buy a mid-span injector.

With regards to web servers. I'm going to stick with Apache for now as there are a lot of tutorials on the web. Once I get it all up and running. I'll look into lighter servers such as Nginx.

Nelz pointed out that MQTT isn't really what I want. Although I think it requires further investigation for future projects/builds.

I'm going to do some more reading and have a little play around. I'm sure I'll have a few more questions so I'll hold off on marking this one as solved for now.

Many thanks
PP
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 133
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line

Postby nelz » Mon Jan 06, 2014 8:34 pm

MQTT is definitely an interesting technology, I use it a fair bit. But if you are using a web interface, you should be using HTTP.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8549
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests