Friday, 23 August 2013

Running a web server on a Raspberry Pi

I've been quite frustrated in my attempts to get anything connected to my Raspberry Pi, mainly because it usually involves using a programming language that I only know the basics of, or some obscure application library that isn't very well documented.
As always, back to basics! I've decided to install a web server with PHP and MYSQL, then take it from there - these are tools that I am very proficient in and it will removes the unknowns from the equation. As I expected, this is very easy but I will take it slowly and assume that you are inexperienced. Please feel free to ask questions, point out errors or just generally comment.
Apache is a very popular web server that will let your Raspberry Pi run as a web server which means you will be able to access web pages that have been written and are stored on the Pi.
The web pages are usually written in HTML, but you can add a lot of functionality to the web server by adding another popular programming language called PHP.
Its also good to be able to use a database to store information about any systems that you create on the server.

Step 1 Install Raspbian on your Raspberry Pi.

I arent going to document this as there are hundreds of guides and I will only echo what has already been said. For the record, I used NOOBS (New out of the box software) to install it, then allowed SSH access and used MobaXTerm to access the graphical interface. (This isnt important, you can just as easily do this straight from a normal monitor view)

Step 2 Install Apache, PHP and MYSQL

On the desktop of Raspbian you will see an icon saying "LX Terminal". Click on that and then type this:

sudo apt-get update

The command "sudo" means "run this instruction as an administrator," which in linux is a user called "root".
The apt-get update part tells Raspbian to make sure that all its records of available software are up to date. You will see it trundle off and check lots of things, just leave it alone until you see a coloured command prompt starting "Pi@..."
The next command to type is:

sudo apt-get install apache2 php5 mysql-server

This instructs Raspbian to install the web server (Apache2), PHP version 5 and mysql-server. This will take five minutes or more, its fascinating to read the things that flash by on your screen, all very clever but once you have seen it a couple of dozen times, the delay is just an inconvenience!
You will be asked to set a password for mysql. Make this a secure, complex password and also make sure that you remember it!

Step 3 - Install phpmyadmin

You will now have a web server running (we will check it later) and we could start to add web pages. I've always used a great utility called phpmyadmin which lets you look after any databases running on Apache very easily.
Type this command:

sudo apt-get install phpmyadmin

Again, this takes a while. You will be asked for the MySQL password (the one you specified in step 2) and also for another password which you will use to log on to phpmyadmin once it is installed.

I usually install a GUI interface that is great for controlling a web server, its called webmin. This guide will tell you how to do it, but it isn't essential by any means!

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=63&t=6096

Step 4 - check it all works.

Type this command:

ifconfig

Into the terminal window, and Raspbian will tell you lots of information about how it connects to the network.
Look for  a line that starts "inet addr", followed by an IP address such as 192.168.1.210
This is the IP address of your Raspberry Pi. On any other computer on your network, open a browser and type this IP address into the main URL box. You should see a page that says "It works" - well done!
You can also use "http://192.168.1.200/phpmyadmin" to open the phpmyadmin window. It will ask you for the password that you entered in Step3 (nb, not step 2!)

Any web pages for your web server are stored on your raspberry Pi at /var/www
You should see a file there already called "index,html"

If you have come this far, well done! You will now need to work out how you can edit pages on your server and make it do something useful - watch our for future updates when I will be detailing various methods.

Tuesday, 20 August 2013

Frustrations and good luck

I dont really want to get distracted today - I need a solid day of coding development to work out how I can make the Raspberry Pi do the things I need it to do to control Pondnet.
Last night, after a successful day of re-jigging existing electrical systems to run by remote control, I went to turn the lights on but they wouldn't work. After checking wiring this morning I found that the power supply unit wasn't working. Its a sealed unit, all the components are sealed in resin and its a chunky "brick". As you can see there is an ominous crack on the side of the case so its either let water in or some connection inside has moved and is no longer connected.
Its rated at 12v 30va so I had a look around my spares box for a replacement unit. The only one that I found was a Netgear one rated at 15v but needs must! It won't fit in the box very easily but at least it doesn't involve any more money!
And then I remembered an old set of lights we had bought for our garden seat and stopped using. Luckily it uses exactly the same connector and is 12v 30va so is perfect. Unlike the pond light unit, it isn't a resin filled unit so I will need to enclose it somehow, probably just by keeping it in the distribution box.
Great, now I can devote some day to coding - pass the coffee and turn the Chemical Brothers up loud!

Pondnet is born!

A bit more progress, had a few things to do so it's been slower than I wished. 
I made a complete hash of fitting the solenoid valve, I managed to not only fit it backwards but also the wrong way round so the box lid would have to face the wall. After some head scratching, I had another go! The valve is a spare part from a washing machine and costs less than a fiver, but does run on mains voltage so has to be used with great care. 
I fitted it inside a ip66 container which means it is certified to remain waterproof when used outside. Its a simple case of connecting a washing machine hose as the input and a hose to the pond as the output. Control is via the two spade contacts. Ive fitted everything inside mains conduit, including the pipes so it's safe and quite neat. 

All the mains devices are now terminated with plugs which go into a distribution board inside an ip50 box (the lower number is because the water proof rating is different mainly due to the large cable entry hole). The pump, ultraviolet, air pump, pond lights and water valve can now all be remote controlled. There is enough room inside the box for my raspberry Pi, power supply and possibly an arduino. 

Tomorrow's job will be to get the Pi to control these remote control sockets via a 433mhz transmitter, then I can think about how the web interface can run. Ive ordered a temperature sensor, which runs on from a single gpio line, and an Ultrasonic Sensor which I can get to measure the water level, it needs two gpio lines.

Saturday, 3 August 2013

Setting up a Raspberry Pi to run "Headless"

On my last post I described how frustrating I had found the whole process of setting up my Raspberry Pi. I have had quite a few questions about the method I eventually used, so I am going to go through the eventual method I used in a bit more detail.

Initial Set Up.

This was much harder for me because of the issues in getting my monitor to work.
Plug in a USB keyboard, USB mouse, Ethernet cable and an SD card that has been freshly formatted and has a new uncompressed copy of NOOBS on it.
The two issues I faced here were that I had used a power supply that wasn't compatible (no idea why!) and I couldn't get the display to work. The green light closest to the sound jack socket was flashing in a steady on/off pattern, which I think means that there isn't enough power being delivered (I cant find out if this is the case though, if you have a link to any descriptions, please email me)

Im using a fairly standard VGA-HDMI lead as shown here.
The instructions for this lead mention that you may have to edit a config.txt file to get it to work. With the NOOBS software, this wasn't necessary, plugging a normal VGA lead (With ALL the pins in place, some have pin 9 missing which just adds extra problems)
The problem for me was that when I used a full VGA lead, my monitor displayed a message that I couldn't turn off saying that the resolution was incorrect. When I used a VGA lead with pin 9 missing, the monitor turned itself off after a short while. I spent quite a few hours trying to get this to work properly, in hindsight this was a waste of time!
The NOOBS software lets you choose which OS to install, I picked Raspbian.
After the OS has installed and the Raspberry Pi has rebooted, you will see a configuration menu. Choose the option to enable SSH access and remember to set this as the default. (Ive seen this option in a couple of different places, so I arent going to be precise - its always clearly marked)
Next, we need to get the IP address of the Raspberry Pi. Simply type:
ifconfig
This will display a lot of information, but on the first few lines it will say what the "Inet Addr" is. It will be something like 192.168.1.85
On your PC, download and install Putty and XMing. Double click on Xming to set it running and then open Putty.
Type in the IP address for your Raspberry and then click "Open".
You will be asked for a username which is "Pi" and a password which is "Raspberry"
Next we have to start the graphical interface for Raspbian, which we do by typing:
startlxde
Hopefully, your PC will now open a window in XMing that shows the graphical interface for the Raspberry Pi.
Once you have set this up, you can unplug the keyboard, mouse and monitor from the Raspberry and access it directly from your PC which is I have found to be much simpler.
Good luck, persevere and don't hesitate to get in touch with me if you have any problems.