I've developed a communication stack based on the OSI model for nRF24 radios over the past number of years, and it is fairly complete and fully functional. It supports quite a wide range of communication scenarios, with a mesh layer that allows wireless nodes to find communication paths through the network automatically when a given connection fails and uses other nodes to bridge the distance between points in the network.
This is one of the more advanced scenarios on how to use it.
Basically what I am doing is using the internet connected Raspberry Pi as a gateway for all of my Arduino devices, which will run their own IP stack and communicate directly with the Raspberry Pi using the MQTT protocol. This allows me to send and receive information to and from the Arduinos using any device/software that supports MQTT, including mobile devices. All of this is done wireless, using nrf24l01 radios.
These instructions assume users have working radios wired up and ready to go. There are numerous tutorials on setting up and transmitting simple messages.
The first steps are to install the RF24 libraries. On the Arduino, the easiest way is to open the Arduino Library Manager, and select the RF24Ethernet library. It should install all the required RF24 libraries. For linux devices, an install script has been written, the instructions for which can be found here. In this scenario we will select to install all of the RF24 libraries including the ncurses library, and use the SPIDEV driver when prompted. If running RPi OS Lite, you may need to sudo apt-get install pigpio
If there are any issues with the installer or the libraries, please report them at https://github.com/nRF24
Once installed, on the Raspberry Pi, we need to navigate to the example file and build it:
1. cd rf24libs/RF24Gateway/examples/ncurses
2. If you have used the default pin connections to wire up your nrf24 module to the RPi, then just run make
to build the example file. Else edit the file per step 3.
3. Run nano RF24Gateway_ncurses.cpp
to edit the file and change the line RF24 radio(22, 0);
to suit your chosen pin connections. Then run make per step 2.
Scroll down at https://nrf24.github.io/RF24/index.html for wiring information.
4. RF24Gateway was designed to be run in screen
. This allows you to run it in the background while still being able to access the GUI as desired. Use sudo apt-get install screen
to install. Then just run screen
. Use key combination ctrl+A+D
to detatch from the screen and the command screen -r
to resume the screen.
5. Run the following commands to configure the interface manually. (Automatic configuration requires running the gateway as root) If your username is something other than 'pi' edit the command to specify it after 'user'.
sudo ip tuntap add dev tun_nrf24 mode tun user pi multi_queue
sudo ifconfig tun_nrf24 10.10.2.2/24
6. If not enabled, enable the SPI interface on the RPi by running sudo raspi-config
7. Once built on the Raspberry Pi, we can run ./RF24Gateway_ncurses
to execute the example.
Almost ready to go! It should look something like the following:
The next step on the RPi is to install mosquitto, an MQTT broker:
8. Run sudo apt-get install mosquitto
9. Run nano /etc/mosquitto/mosquitto.conf
and add two lines:
a: allow_anonymous true
b: listener 1883
10. Run sudo service mosquitto restart
to reload the configuration
All done on the Raspberry Pi!
Arduino Steps:
1. Open the Arduino library manager and search for RF24Ethernet. Install the library. We also need the MQTT library just labelled "MQTT" by Joel Gaehwiler.
2. Open one of the examples: File -> Examples -> RF24Ethernet -> MQTT -> mqtt_basic
3. If not using pins 7 and 8 for CE and CS respectively, set up your pin connections in RF24 radio(7, 8);
4. Upload to your Arduino
Mobile Device Steps:
On my iOS device, I installed MQTTool and it is fairly nice to use. As long as the Raspberry Pi is connected to the same LAN as your mobile device, you should now be able to send and receive messages to and from your Arduino.
1. On the Raspberry Pi, type in ifconfig
to get the IP address. Usually the eth0 or wlan0 interface depending on if you are connected via WiFi or cable.
2. This IP from step 1 is what you want to punch into the app on your phone, in the connection area.
3. Once connected, you can publish any message to inTopic, and the MQTT server will send it to your Arduino which will print it out over the serial port.
4. You can also subscribe to 'outTopic' and your mobile device should start receiving messages from the connected node.
To add more devices, just change the IP address in the example sketch from 10.10.2.4 to use any last octet from 3-254. ie: 10.10.2.5
Expanding on these Principles:That should get you started with RF24Gateway/RF24Ethernet and MQTT. The hard part is getting the first device working. From here it is a matter of customizing the MQTT topics and the information you send/receive.
To expand on this, users can install software such as Node Red on the Raspberry Pi, and create charts and graphs, design input/output scenarios like on/off switches for devices etc. as in a home automation system.
The RPi radio interface behaves just as it were a standard network interface, so a wide range of tools protocols and software can communicate with Arduino nodes running RF24Ethernet. This can be further extended by installing a vpn on the RPi to make the controls, charts etc accessible from anywhere.
See http://tmrh20.blogspot.com for further information and https://github.com/nRF24 for support.
Note:
This configuration sets up an IP enabled wireless interface on your Raspberry Pi, so suitable firewall rules and strong passwords should be used to prevent unwelcome access.
Comments