Automation/IoT with Arduino, RPi, NRF24L01+ and MQTT
Control and monitor Arduino remotely with mobile devices
Please see Arduino Project Hub for an up to date version of this tutorial
Preconfigured kits available at https://tmrautomation.myshopify.com/
This is a quick tutorial on how to wirelessly control or monitor Arduino devices using nothing more than NRF24L01+ radio modules, a Raspberry Pi and a mobile device. I have found MQTT to be one of the best methods of controlling these devices wirelessly, in the case of simple input/output scenarios like monitoring sensor values or sending RGB values to wireless lighting systems.
This assumes users are familiar with the core RF24 library as well as installing libraries from the Arduino IDE, and building and running programs on the RPi.
Raspberry Pi with NRF24l01+ device attached
One or more Arduino devices with NRF24L01+ device attached
The RPi should be connected to the same network as your mobile device
Ensure all devices are functional with RF24 core examples before attempting.
Install RF24 libraries on RPi from your home directory:
1. wget https://raw.githubusercontent.com/TMRh20/tmrh20.github.io/master/RF24Installer/RPi/install.sh
2. chmod +x install.sh
4. Follow the command prompts and install the RF24, RF24Network, RF24Mesh, and RF24Gateway libraries
Install RF24 libraries on Arduino:
1. From Arduino IDE select Sketch > Include Libraries > Library Manager
2. Install the RF24, RF24Network, RF24Mesh and RF24Ethernet libraries
Now that the correct libraries are installed, we need to install an MQTT broker on the Raspberry Pi. Setup is as easy as running the following command:
sudo apt-get install mosquitto
Note: As of mosquitto 2.0 you need to add listener 1883 and allow_anonymous true in mosquitto.conf.
Then we need to start the Gateway on the RPi, so it will pass traffic from the connected sensor nodes etc, to the MQTT server.
The RF24Gateway library is installed in ~/rf24libs/RF24Gateway/
In this example, we will use the ncurses example, located at ~/rf24libs/RF24Gateway/examples/ncurses
Edit the file RF24Gateway_ncurses.cpp and modify the radio constructor to suit your pin connections: RF24 radio(22,0); is the default, then type 'make -B' to build the example.
To run the example, type ' sudo ./RF24Gateway_ncurses ', and it should pop up asking for an IP and subnet mask to use. This is mainly arbitrary, and any suitable private IP/Mask can be selected.
Once the gateway is running, we can switch our attention to the Arduino.
1. Open the MQTT example: File > Examples > RF24Ethernet > MQTT > mqtt_basic
2. Edit the radio constructor to suit your chosen CE/CS pins: RF24 radio(7,8); is default.
3. Edit the IP address of the device to match the range chosen when configuring the RPi.
4. Edit the IP of the gateway to match exactly the IP of the RPi
5. Edit the IP of the server to match exactly the IP of the RPI
6. Upload the sketch to the Arduino.
Note: If using an external MQTT server, the RPI must be configured to forward packets and perform NAT, see the forwarding and routing section here.
You should see serial output like the following:
Attempting MQTT connection...connected
If not, something has gone wrong. Ensure your device is connecting to the mesh and showing up in the address list. If not, troubleshoot using examples from RF24 or RF24Mesh to diagnose the connectivity issues.
Nodes running the MQTT example will publish their NodeID every second to the MQTT server, at outTopic and will receive all messages published to the inTopic.
Setup on iPhone etc:
On my iPhone, I chose to use the program MQTTTerminal (MQTTool is also good with no ads). Setup is easy, just input the external IP address of the MQTT server (the ip of your RPi) and the port (1883). Then set the Publish Topic to inTopic and the Subscribe Topic to outTopic.
You should see messages incoming from the device(s) and can send data to the devices from the app.
From here it is a simple matter of customizing the topics and messages to suit your needs, whether controlling a simple LED on the Arduino, or reporting temperature and humidity from sensors.
This can be expanded using programs like Node Red to create an open-source home automation system. See my more recent blog post.
If problems are encountered etc, feel free to open an issue at https://github.com/nRF24/RF24Ethernet/issues