Elektor_Mag_-_January-February_2021

([email protected]) #1

58 January & February 2021 http://www.elektormagazine.com


Once you have assembled the circuit, continue to the next chapter
where you will implement the Python script that handles the nRF24
communications.

How to install the Python nRF24 modules
on the Raspberry Pi
In this chapter I will show you how to compile and install the
C-language drivers and relevant Python wrappers for the nRF24
module. There are two modules involved: the main RF24 module,
and RF24Network sub-module. There are several forks of the RF24
project, but the one you will be using is maintained by TMRh20
[15]. This is optimized for the Raspberry Pi.

You can obtain the source code for the two modules from their
respective Github repositories:

> RF24 repository: https://github.com/nRF24/RF24;
> RF24Network repository: https://github.com/nRF24/
RF24Network;

For each module, the installation process includes these steps:

> download the source code of the module from Github;
> compile and install the C driver;
> compile and install the Python wrapper.

Before you begin, ensure the SPI interface on your Raspberry Pi
is enabled. The nRF24 module uses SPI to communicate with
the Raspberry Pi. To check this (and enable SPI if not already
enabled), log in to your Raspberry Pi and type the following on
the command line:

sudo raspi-config

Use the keyboard arrows and Enter key to navigate to Interfacing
Options, and then SPI. Enable SPI if necessary, and exit raspi-con-
fig. Continue by updating Raspbian:

sudo apt-get update
sudo apt-get upgrade

You will download the source code of the modules in your applica-
tion directory. You also want to compile the Python wrappers with
your application Python virtual environment activated so that they
are available to your application Python scripts.

Prepare your work with these commands:
$ sudo su
# cd /var/www/lab_app/
#. bin/activate
# mkdir rf24libs
# cd rf24libs

You are now ready to begin the compilation and installation of
the two modules.

RF24 compilation and installation
Create a clone of the RF24 source code, from the git at
https://github.com/tmrh20/RF24.

the header of the message and pass it to the header variable.
Remember in the Arduino sketch, we created a 12-byte
payload that looks like this: 51.23,23.09.



On the next two lines, I use print to print out the payload to
the console. This was useful as I was working to decode the
payload into numbers in the try block that follows.
The payload variable contains a string, that looks like this:
51.23,23.09. The script uses decode() [11] to convert it
into a UTF-8 encoding, and the split() function [12] to slip
the temperature and humidity values into an array of two
substrings, using the ‘,’ as the delimiter. The two values are
stored in the values variable (an array of strings).
In the try block, the script uses the float() function [13] to
extract the numbers stored in the payload character array
and convert them into a floating point number. The float()
function receives a string, and if it can be converted into a
number, it returns it.
With float(values[1][0:5]), the script takes the first
5 characters of the string stored in index 1 of the values array,
and uses the float() function to convert it into a number.
This is the numerical value stored in the temperature
variable.
The same happens with the humidity value using
float(values[0][0:5]).
If any of these two conversions fail, the try block exits and an
error message is printed on the console. A conversion can fail
if the payload string is not formatted properly by the Arduino,
or perhaps the payload becomes corrupted during trans-
mission and receipt. Interference may be the cause of such
corruption.



Take as much time as you need to study this code, so you are
comfortable with it. When you are ready, copy it into your
application directory. Use Vim to create a new file titled ‘rf24_
receiver.py’. In the Vim buffer, copy the code from the project
repository [14].


Before you can test the RF24 communications, you need to
compile the RF24 and RF24Network C language drivers and
Python wrappers. You will do this in the next chapter. But
before you do this, I want to show you what the script you just
learned looks like when running (Figure 4). I have annotated
this screenshot so you can see the printouts embedded in the
script. Let’s continue to the next chapter with the setup of the
RF24 and RF24Network drivers.


Figure 4: Example RF24 receiver script output.

Free download pdf