Monday, May 27, 2013

Raspberry pi: nRF24L01 and TCP

This time I will describe how I got a Raspberry pi (Wheezy) to work as a Python server that controls a RF-transmitter (nRF24L01). The server takes commands either from a TCP connection (internet) or via the keyboard, and broadcasts the data through the RF-transmitter to the receivers i have set up in my home automation system. See my other posts covering how to use the nRF with an AVR, and the construction of a  multi-functional remote control.

        

Why use a Raspberry pi when i already have a working TCP-server on my PC?

Easy answered:
  • I do not like to have my PC up and running 24-7 due to the facts that it's noisy (stationed in my bedroom)
  • The RPi takes much less energy (runs on a 1000mA cellphone charger)
  • I don't need any other parts other than a RPi, a nRF24L01(+) and cables, since the RPi has a 3,3V power supply!

Setting up SPI on RPi

First of all, you must have a working copy of raspbian, I use Wheezy (made a small tutorial in how to set it up). The RPi has a built in hardware SPI, which we first has to get up and running according to this guide, and here is how i did it:

Start a terminal like the LXTerminal and follow these commands:



To check if the SPI is working (optional) you can connect the mosi-pin to the miso-pin (GPIO10 and 9, see picture), and run the commands in a terminal:




Quick2Wire

Quick2Wire is a tool that makes it possible to control the GPIO and the SPI via python. We need to install quick2wire, download the quick2wire python plugin (API) and gain root access to the spi.
Start by downloading the plugin from there website. Extract the zip-file by right clicking and unzip. Now open a terminal window and change directory so that you are in the extracted folder called "quick2wire-gpio-admin-master" then enter the following commands:


Now log out from the RPi and back in again. The power button at the bottom right quorner has the option to log out. Log back in by typing your username "pi"/enter, then your code "raspberry"/enter...

To gain root access for the SPI so that you dont have to use the "sudo" command in front of every bit of code, I followed this guide, which tells you to do this:


Finnish by remove and reattach the power cable to restart.

Python 3

Now it's time to install python3 which is needed to run the code. Open a terminal and run these commands:


Now to use quick2wire with python, you need the python API which can be downloaded from this site. Download it, and unpack it to a location on you SD-card (no installation required!)

Setting up the hardware

The setup is very straight forward. Connect the wires like this (see red circles in the picture):
RPi GPIO9     (Pin 21)    to RF  ( MISO )
RPi GPIO10   (Pin 19)    to RF  ( MOSI )
RPi GPIO11   (Pin 23)    to RF  ( SCK )
RPi GPIO8     (Pin 24)    to RF  ( CSN )
RPi GPIO18   (Pin 12)    to RF  ( CE )
RPI 3.3V        (Pin 17)    to RF  ( VCC/3.3V )
RPi Gnd         (Pin 25)    to RF  (GND)
(IRQ-pin on nRF is not used in this example)

nRF24L02+ (top view)


nRF24L01 (only one of each VCC and GND-pin has to be connected!)


I think the CE-pin should work on a different pin (it would be convenient to put it on GPIO25) since it doesn't have anything to do with the SPI, but I haven't tried that yet...

The python programs

I got the basic nrf python code from this program (plus a lot of help with the steps above) from Jussi Kinnunen, all creds to him!
Download  my version of the program, as well as my TCP-server program. which is well commented to make it easy to understand. You see the codes below:

nRF24L01p.py


TCP_Server.py



To run the program, create a third file, and name it to something like "a.sh", the ending "sh" means shell file, which is runnable. Fill the file with these commands:
(change the path "/home/pi/" to where you have the quick2wire-folder!)

This will make sure the path is imported every time the script is run! If anyone knows how to permanently add the path, please tell me in the comment field!

To run the program, all you have to do now is to set the working path in the terminal to where you store the a.sh-file with the cd-command and type


This is a screenshot from the code running in the terminal:































As you can see, the program starts by asking if you want to run it as "rx" (Receiver) or "tx" (transmitter). I chose transmitter by typing "tx"/enter.
Then it prints out all the registers that the code changes (or sets) and then starts a server loop in a background thread (TCP).
When this is done the program waits for either the user to type in data to send (3 bytes in my example), or for a command from the TCP-server.
In the picture above, you can see that i started by typing "123" on the keyboard, which was sent to the nRF and broadcasted to any nRF-receiver with address 0x12. As you can see the STATUS register after the broadcast tells me that the transmission failed (0x1E), since i don't have a receiver running at the moment... a working transmission would give the result "0x2E" (when the EN_AA is turned on)!

The second thing I did was to send the command "300" to the TCP-server from my home-made android application (can be sent form any TCP-client with the right port number). The TCP-server then calls the nRF24L01p.py and transmits the data like it was inputted with the keyboard.

If a (large) error shows up when you try to run the code, which tells you something about "cannot export the pin number 18 because it is already exported", run the following command in the terminal:



If an error shoes up that tells you that the address is occupied, the TCP-server has not closed yet, and you have to sit down and wait for up to 1min before you try again (I know you can change this timing, but haven't come so far)


If you have any questions or just want to tell me what you think of the blog, just give me a comment in the comment field underneath...
/Kalle

Setting up Raspbian on Raspberry pi

This is step by step how i installed Raspbian on my Raspberry pi:

  1. On a PC (or mac/linux, i will cover the PC-version) download the file "Raspbian Wheesy" from the website, using either a torrent program like utorrent or by the direct downloading-link.
  2. Extract the files to a folder on the computer, remember the location!
  3. Insert an SD-card in the computer, and make sure it contains nothing of value (since you will wipe all of its data later)
  4. (windows users) Download the program "Win32Diskmanager" from this site (green download button).
    1.  Extract the files to a folder, and open the file "Win32Diskmanager"
    2. In the program, press the blue folder-button and brows to the file you downloaded and extracted in step 1-2 (something like: "2013-05-12-wheeay-raspbian")
    3. Make sure it is the right SD-card in the "Device" tab, and press the "Write" button. (only takes a couple of minutes. 
  5. Move the SD-card to the Raspberry pi and connect:
    1. A mouse + keyboard
    2. HDMI-cable (to screen) 
    3. Network cable or wifi adapter
    4. Finnish by inserting the usb power adapter (at least 750mA)
  6. If you are having problems with the HDMI connection, unplug the USB-devices and restart the RPi (happens if you have a weak power supply!)
  7. The "Raspi-config" box shows up: 
  8. Start by arrow down to "expand_rootfs" and press enter
    1. Accept by pressing enter again. This setting makes sure the whole SD-card gets used, otherwise only 2GB will be visible to the system.
  9. Configure_keyboard (takes a while to load)
    1. Try to find a match in the list/enter
    2. Chose "other" at the very bottom/enter
    3. page down to your language/enter
    4. choose your language/enter
    5. Choose "The default for the keyboard layout"/enter
    6. choose "No compose key"/enter
    7. change this one to "<yes>"/enter (to enable the ctr+alt+backspace (like ctrl+alt+delete on windows))
  10.   Change_pass
    1. Pres "Ok" on window that pops up, (Change the standard password from "raspberry" to something else)
    2. Type new password/enter
    3. retype the same password/enter
  11. Change_local
    1. scroll down (or "page down") to your local (mine is "[] sv_SE.UTF-8 UTF-8") and press SPACE-key to put an "*" in the "[*]", finish with enter
    2. Choose your local in the list/enter (takes a while)
  12. Change_timezone
    1. Go to your timezone in the list (Europe)/enter
    2. Find a city nearby (Stockholm)/enter
  13. Overclock
    1. If you want to boost the RPi, its here! 
    2. I choose "modest", and it seem to work fine!
  14. Boot_behaviour
    1. confirm with yes (to skip the login-thing every time you boot up)
  15. Finnish by pressing right key twise to get to "<Finish>" and press Enter
    1. Press "Yes" to reboot
Now you should boot straight into Raspbian Wheesy, if not type your username ("pi"/enter) and your password ("raspberry"/enter if not changed), now type "startx"/enter to get to the desktop!
To get to the raspi-config again, open a terminal ("LXTerminal") and type "sudo raspi-config"/enter