Category Archives: Web

HTTP GET Parameters with Arduino

For a while now Kevin B has been working on writing a library for communication using the NRF24l01 radio with the Arduino’s. The idea is to use them for home automation purposes. But of course that’s only one piece of the puzzle… what good is home automation unless it’s also internet ready?

I took it upon myself to start looking into that portion, since it’s right up my alley (between server-side scripting such as PHP along with a database such as MySQL, and the API needing to working with Android of course).

Kevin’s thought was to use RS-232 between the Arduino and a Raspberry Pi or Seagate Dockstar. At first it seemed like a great idea until I started reading up on different methods to make this work.

Using Python + PHP

This seems to be the most popular recommendation, but I’ve never touched Python before. The idea was to have Python and LAMP running on a Raspberry Pi while connected to RS-232 and send/receive commands that way. Seemed simple enough, but boy was I wrong. Python boasts being extremely easy to learn for people new to programming, but after seeing a handful of examples, I didn’t even want to touch it. Maybe it’s from years of Java and over a decade of PHP, but my brain just didn’t want to even look at Python never mind understand it. NEXT!

PHP with Serial Library

My next thought was that there must be a library to do RS-232 communication through PHP alone. There were a few limitations that bothered me though. First, PHP is a server-side scripting language. It’s not really meant to run in loops forever waiting for serial data to be received in the same way an Arduino runs in a loop forever. Most web servers (such as Apache) come default with a 30 second max execution time, so if your script runs for more than 30 seconds the server will just terminate it anyway. There is also the issue that the Arduino resets every time you open a serial connection with it, which means even if you don’t loop the PHP script and just call it when needed, the serial data will be reset as soon as the script runs… not good. Kevin pointed out that this is due to DTR signal on RS-232 pulling the reset pin to ground, which isn’t an issue if using a Pro Mini without DTR connected, but in my case I had planned to use one of my Arduino Duemilanove boards as the base station (USB is built on board, so “it is what it is”).

Too Many Layers

Lastly since RS-232 doesn’t do any form of acknowledgements it means there would have to be another software layer in between to control and confirm data. At this point just to get the Arduino to talk to a web server, there was waaay too many points of failure for me:

The Simple Solution

Then it hit me… the route I should have been going all along. An ethernet shield. This will take care of nearly all the problems. The Arduino can act as a web server, and with a bit of programming we can have it serving data or receiving and processing data. Using HTTP response headers, we already have a form of acknowledgements built-in. And lastly, later down the road I could actually remove the Pi from the picture completely if I wanted and instead use my VPS located off site, or even control multiple houses from a single server without the need of separate Pis everywhere or being limited by the processing power of the Pi.

So I plugged my Ethernet shield into my Arduino, along with a battery source for power (my USB ports weren’t giving enough current to keep it powered on by itself), and off to the races I went.

HTTP GET Parameters with Arduino

While not secure by any means, HTTP GET provides an easy solution. The idea is simple. Commands can be sent from the Pi by a GET parameter in the URL to the Arduino, the Arduino can then parse this data and do whatever needs to be done and then output the result (eg. Success or Fail). The Pi can wait for the output/result, and then process whatever it needs to do (eg. informing the user of the result).

First we need to set up the network information for the shield. The Ethernet library does support DHCP, but its easier to use a static IP for testing.

// Setup the static network info for you arduino
byte ip[] = { 192, 168, 1, 177 };   // IP Address
byte subnet[] = { 255, 255, 255, 0 }; // Subnet Mask
byte gateway[] = { 192, 168, 1, 1 }; // Gateway
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address

Next we setup the ethernet server on port 80 (default HTTP port) as well as a string to store the GET parameter in.

EthernetServer server = EthernetServer(80); // Port 80
String HTTPget = "";

The setup() function is pretty straight forward. Remember we can’t use pins 10 through 13 for any thing else as they are used for Ethernet. Unfortunately the NRF also needs some of those pins though, so I will have to look into using a SoftSPI library or figuring out how to use the SS pin (slave select) down the road.

void setup() { // pin 10, 11, 12 and 13 are used by the ethernet shield Ethernet.begin(mac, ip, gateway, subnet); // setup ethernet with params from above server.begin(); } continue reading

Disable Directory Listing on apache2

As some of you may (or may not) know, by default apache2 allows something called “Directory Listing”. Essentially what this means is that when you visit your web server, if you browse to a directory within your www folder that does not contain an index page (index.php, index.htm, index.html, etc.), apache2 will actually spit out (or list) the entire contents of that folder. This should be a concern for anyone running a production server, and unless there is a specific need for directory listing its always a good idea to disable it. There is tonnes of different opinions on whether or not its a security issue to leave it enabled, and to this I have to say to each their own. Personally I feel it is a security risk, but at best disabling it is simply security by obscurity. continue reading

Expanding content area of WordPress TwentyFourteen theme

I stumbled upon a post on a blog run by David Lehman (NowhereLAN) the other day that caught my attention. It was regarding changing the page content area in WordPress’ TwentyFourteen theme.

Like David, I used TwentyFourteen as the base for my theme, and have been editing it to suit my needs. One of the things that bothered me was the excessive padding around the content area. As David points out in his post, its not so much a padding issue as it is the max-width set in the css file. continue reading

Force secure connection (SSL/HTTPS) using .htaccess and mod_rewrite

It recently came up where I wanted to force users visiting the IdleDev page to use a secure connection (https) domain wide (mostly for the development of Zero Day to ensure a secure connection is used at all times).

Unfortunately this was something that did not appear to be directly supported by my host. Luckily they do allow .htaccess and mod_rewrite, so from there it was easy. continue reading