Raspberry Pi: Edimax EW-7811UN WiFi Dongle

I recently bought myself an Edimax Wifi dongle to use with my Raspberry Pi. My router is hidden away so I got tired of running a network cable to my Raspberry Pi. The Edimax WiFi dongle was ideal as it is really inexpensive and very small.
It took a while for me to get it set up correctly but eventually I got there. To set up the WiFi connection, you either need a ethernet cable plugged into your Raspberry Pi or you need to connect via the serial port. I connected via the serial port with a FTDI USB to UART converter as this made it much easier to know when the network was connected. 
First of all plug the dongle into one of the USB ports and then reboot.I have not tested this in a USB hub but I am sure it will work the same. To make sure that the dongle has been started, run 

ifconfig

and make sure that wlan0 can be seen.

The next step is to see if your Raspberry Pi can see your wireless network. Run the following command and look for your network.

sudo iwlist wlan0 scan

Under ‘Cell 01’ I can see my network. Take not of your ESSID and you will also need your network password.
Once you have established that your Raspberry Pi can see your wireless network, you will need to edit the network interfaces file.

sudo nano /etc/network/interfaces

You will need to add the following:

  auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid ‘your ESSID’
wpa-psk ‘your password’

Your interfaces file should now look like this. Note that I have set a static IP address so if you still use DHCP then you will not need the static IP address settings like I have.

Restart your network connection by running

sudo /etc/init.d.networking restart

and then run

ifconfig

and you should notice an IP address assigned to wlan0.

Now reboot your Raspberry Pi to make sure that everything is working in order.

sudo reboot

If your connection is up and running after the reboot, then you are all set to go wireless.

Greg

Raspberry PI: Bottlepy and Twitter Bootstrap

I have been using my Raspberry Pi for the last few months quite a lot. I am currently working on a temperature/environment monitor that logs temperature, using a DS18B20, and light conditions using an optical sensor. I also have two LED’s and two buttons to test basic input and output.
I am developing the application in python using the BottlePy framework. I chose the BottlePy framework as it is a single file that you add, it is very small and it works with Python 3. To control my GPIO’s, I am using the Quick2Wire library. To display the temperature data, I am using Google Charts.

This is a picture of the main temperature logging page so far.
However, the purpose of this post is to show how to integrate BottlePy and Bootstrap by Twitter. According to their website, Bootstrap is a “Sleek, intuitive, and powerful front-end framework for faster and easier web development.”
To display this, I am going to use my GPIO code which reads buttons and turns on LED’s through a webpage. 

To start off we need python file that will run as the server. Below is a snippet of code. The @route shows that this particular function will be run when /gpio us entered into the browser. The webpage is broken up into four sections. The first controls LED 1, the second controls LED 2, the third gets the state of both buttons and light sensor and then the fourth runs a .c file to flash the LED’s.
Now we need to have a look at the html code that creates the webpage. By default, Bootstrap works with the user downloading the .css and .js file. These are then statically linked to the html code. When using the BottlePy framework, I haven’t found a way to return a webpage template, AND a static file. Therefore I have found a link where the .css and .js files have been hosted. By using these links, you only have access to the default settings, but they are enough for what I am wanting to do.
You have to link to the .js and .css file and then by using certain classes that have been defined and explained on the Bootstrap website, you can get nice looking buttons, navigation bars, and tables. They have many options to use but these are the three that I used here for my site. 
When a button on the website is pressed, it sends the value back to the python function and the python code will perform the task. With the Flash LED button, the python code calls a .c file that has been compiled and runs when called. 
When requesting the button state, I return hex formatted colours to show the state of the buttons. These the colour in the table. 

The red shows that Button 1 has been pressed, the Green shows that Button 2 is not pressed and the Blue shows that the light level is currently light.
There are many different elements that can be added by using the Bootstrap framework. The documentation is really clear and easy to follow with many examples. 
If you get stuck or need some more explanations give me a shout.
Greg

Raspberry Pi: Changing time zones

By default the time zone on the Raspberry Pi image is set the the UK. As there is no RTC on the Raspberry Pi, when ever it is turned on, the time is synced. To set the time zone to your local area, in my case South Africa, the configuration of the tzdata package needs to be changed.

sudo dpkg-reconfigure tzdata

Select your territory.

Then select your local time zone.

As you can see the time zone has now been changed.

Greg

Raspberry Pi: Using Bottlypy to control GPIO

I have been trying for a few days now to get control over the GPIO pins through a webserver. There are a number of tutorials on the web but having very little web programming experience, I struggled to understand and follow the tutorials. 
After working through a number of examples, and not getting what I want, I came across the bottlepy framework. The tutorial on their website and the todo example show enough ways on how to get data to an html page and how to get data from an HTML page.
I have been super busy at work and also trying to get it to work so haven’t had a chance yet to do a bit of an explanation on what I did.
To download the files I used click here.
You will need to edit your IP address in the index.py file.
Change to the directory on your Raspberry Pi and run $ python3 index.py
In your browser navigate to IPaddress:8080/index then follow the links. Have a look in index.py to see what pins I have used for the inputs and outputs.

Sorry this is so vague but as soon as I get a chance I will update this post with some more explanations.

Greg

Raspberry Pi: GPIO input and output

So I got my Raspberry Pi yesterday and am super excited. I feel like a 10 year old on Christmas morning. I had already loaded the the Raspbian image onto an SD card so as soon as I got it I powered it up and away it ran.
The first thing that I wanted to get working was the GPIO pins. I did a bit of reading and some basic python examples and away I went. Have a look at this website for a very decent crash course in python.
The first thing you need to do is import the GPIO module. This can be installed from here
Then you need to tell python that you are going to use the Raspberry Pi’s pin numbers when referencing the ports. The direction of the port is then set.
Once the setup is complete, you can start your code. In my hardware, I connected a push button switch to pin 11 and pin 18 and an LED to pin 3 and pin 5.
import RPi.GPIO as GPIO

#use the Raspberry Pi pin numbers
GPIO.setmode(GPIO.BOARD)

#set the input with pull up control
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#set the output pins
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)

while 1:
if GPIO.input(11):
GPIO.output(3, False)
else:
GPIO.output(3, True)

if GPIO.input(18):
GPIO.output(5, False)
else:
GPIO.output(5, True)
Above is the python code that I used. If you want to download the file you can get it here.

Once you have the file saved, run it in python and by pressing the buttons, the LED’s will turn on. To stop the program running, press CTRL+z.

Greg