Elektor_Mag_-_January-February_2021

([email protected]) #1
lektor January & February 2021 57

this function by studying the driver’s source code [5].
> Once the radio object is created and initialized, the script
creates the network object using the RF24Network construc-
tor. This is the object that makes it possible for the Raspberry
Pi to receive a transmission from the Arduino node. The only
parameter needed to create the network object is the radio
object we created on the previous line. You can learn more
about the RF24Network constructor in the source code of
RF24Network.h [6], on line 373.
> On the next line, with octlit = lambda n: int(n, 8) we
create a lambda function which we use later to convert a
decimal number into an octal number. We do this because
the RF24 Network driver uses the octal system for the node
addresses. In Python, a lambda function [7] is similar to a
regular function (where you use the def keyword) but has
no name. They are convenient to use when you want to do
things like evaluate a single expression, as is the case here: we
pass a decimal number to octlit, and the lambda function
will return its octal-base equivalent using the Python int()
function [8].
> In this_node = octlit("00"), we use the octlit lambda to
get the octal-base equivalent of 00 , and store the value in the
this_node variable. This is the RF24 network address of the
Raspberry Pi.
> In the next six lines, until the while block, the script will:


  • start the RF24 radio;

  • wait for 0.1 seconds for the radio to become ready;

  • start the network at channel 90;

  • print the radio and network configuration to the console;

  • reset the packets_sent counter to zero;

  • and reset the last_sent counter to zero.

    Now the radio and network are ready, the script enters an
    infinite loop during which it waits for a transition from an
    Arduino node.
    At the start of each loop, it calls the update() function [9] of
    the network object. This checks for your messages.
    If there is no new message, the script goes to sleep for
    1 second. After this the loop restarts.
    If there is a new message, the script uses the read() function
    [10] to read the 12 bytes (the payload) of the message, and pass
    them the payload variable. The read function will also get





connected in earlier parts of this project. If you are working your
way through the project, the only new components are the nRF24
transceiver and its bypass capacitor.


Parts needed:



one DHT22;
one nRF24 breakout;
two resistors, 10 kΩ;
two resistors, 330 Ω;
one LED, red (power indicator);
one LED, blue (activity indicator);
one electrolytic capacitor, 220 μF or similar.



The Raspberry Pi nRF24 receiver script
In this chapter, I will explain the functionality of the Python receiver
script that runs on the Raspberry Pi and takes care of the nRF24
communications. Note this script will not work ‘out of the box’.
It depends on the RF24 and RF24Network C-language drivers and
the Python wrappers that I will show you how to set up in the next
chapter. For now, let’s concentrate on the Python receiver script. You
can download and then view the full source code of this script in the
project repository [4]. I have written this script so that eventually
it can run as a background process controlled by systemd (similar
to the way you already set up the web application script earlier in
this project). I will show you how to do this later, as first we must
be sure the script runs properly on the command line.


The content of the function log_values() is an almost perfect
copy of the same function in script env_log.py that is already set
to run based on a Cron schedule. This function will simply receive
sensor values as parameters and store them in the local database
and Google Sheet on the Cloud.


Below I list and discuss selected elements of the script, in particular
those that relate to the RPI-nRF24 communications.



Right after the definition of the log_values() function, the
script sets up the nRF24 module. It first initializes the radio
variable using the RF24 constructor. This constructor is part of
the RF24 Python wrapper library that allows us to use the RF24
C driver from within our Python script. You can learn about



Figure 3: The schematic diagram of the Raspberry Pi HAT for the nRF24 and DHT22 breakout.

Free download pdf