Recently, I started experimenting with radio communication with low cost nrf24L01 modules. These modules are very cheap compared to XBee modules, which I used earlier. With these nrf24 modules, we can enable wireless communication between Arduinos and Raspberry Pis very effectively and economically. For my experiment, I used two nrf24 modules, one connected to an Arduino Uno and another to a Raspberry Pi 1. Here are the pin connection details

Seq

NRF24L01

RPi

Arduino Uno

1

GND

25 (GND)

GND

2

VCC

17 (3.3v)

3.3v

3

CE

15

7

4

CSN

24

8

5

SCK

23

13

6

MOSI

19

11

7

MISO

21

12

8

IRQ

–

–

To the communication, I used the RF24Network library, which is very good and has good documentation. Also, it comes with examples for both Arduino and RPi. I didn’t write any code — I just used the examples and was able to see the communication working. Initially, I had some trouble, but by the end, everything worked well. I could see the data coming from the Arduino to my Pi.

My intention was to use these modules in my RPi and write code in Node.js. Unfortunately, there is no Node.js support for this library. So last night, I decided to write a Node.js addon for this C/C++ library. I didn’t have any experience in writing a Node.js addon, I just spent an hour understanding the Nan and creating very simple addons. Then I started writing the addon for RF24Network. This was the hard part — definitely harder than a simple hello world addon.

Node-gyp kept on failing when it tried to compile the RFNetwork modules. In my searches, I realized that node-gyp uses the make utility, and I needed to add the C/C++ files from that library. In the end, I could compile the node addon. See the binding.gyp file

I should say that I am just a beginner at node-gyp and this binding.gyp might need some improvements. Anyway, with this GYP file, the compilation succeeded.

Next was creating the addon file. Here, I had to learn more about the data types of Nan and callbacks. I started with simple functions and compiled again, then moved onto the next one. I took more time in understanding callbacks, which allows the addon to call JavaScript callback functions. I also spent a lot of time understanding threading and creating a module for continuous listening of incoming messages and triggering the callback function — so that Node.js can process those incoming messages. I use libuv for threading. It seems easier to understand than Async worker modules in Nan.

I spent that whole night learning and writing and refactoring the addon. I finished the module by early morning. By that time, I could write a Node.js app and could listen to incoming messages.

Here is the sample code in Node.js to listen to and acknowledge the message back to the sender.