未加星标

RF Communication using nrf24L01 and Nodejs addon

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Recently I started experimenting with radio communication with low cost nrf24L01 modules. These modules are very cheap compare to XBee modules which I usedearlier. With these nrf24 modules we could enable wireless communication between Arduinos and Raspberry pi 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 is 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

For testing the communication, I used the RF24Network library, which is very good and has good documentation. Also it comes with e.g for both Arduino and RPi. So I didn’t write any single code just used the e.g and able to see the communication working, initially I had some troubles and at the end every thing worked well, I can see the data coming from Arduino in RPi.

My intention is to use these modules in RPi and write code in nodejs. Unfortunately there is no nodejs support for this library. So last night I decided to write a nodejs addon for this C/C++ library. I didn’t had any experience in writing a nodejs addon, I spend an hour understanding the Nan and creating very simple addons. Then I started writing the addon for RF24Network, this task was very hard than trying with simple hello world addons.

Node-gyp was keep on failing when it tries to compile the RFNetwork modules. In my searches I realized that node-gyp uses make utility and I need to add the C/C++ files of this library. At the end I could compile the node addon. See the binding.gyp file

{ " targets " : [ { " target_name " : " nrf24Node " , " sources " : [ " nrf24Node.cc " , " RF24/RF24.cpp " , " RF24/utility/RPi/spi.cpp " , " RF24/utility/RPi/bcm2835.c " , " RF24/utility/RPi/interrupt.cpp " , " RF24Network/RF24Network.cpp " , " RF24Network/Sync.cpp " ], " include_dirs " : [ " <!(node -e \"require('nan')\") " , " RF24Network " , " RF24 " ], " link_settings " : { " libraries " : [ " -RF24 " , " -RFNetwork " ] } } ] }

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

Next is to create the addon file. Here I had to learn more about the data types of Nan and Callbacks. I started simple functions to begin with and compile again, then moved on to next. I took more time in understanding callbacks which allows the addon to call javascript callback functions. Also spend a lot of time in understanding threading and creating a module to continuous listening of incoming messages and trigger the callback function, so that nodejs can process those incoming messages. I use libuv for threading, it seems more easy to understand than Async worker modules in Nan.

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

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

var rf24 = require( ' ./build/Release/nrf24Node.node ' ); rf24.begin( 90 , 00 ); rf24.printDetails(); rf24.write( 1 , " Ack " ); rf24.readAsync( function (from, data){ console.log(from); console.log(data); rf24.write(from, " Ack " ); }); process.on( ' SIGINT ' , exitHandler); function exitHandler() { process.exit(); rf24.close(); }

Here is the complete addon. The code is uploaded to github , with the steps to compile and use it your own nodejs applications.

#include < nan.h > #include < v8.h > #include <

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: ArduinoC++CSN
分页:12
转载请注明
本文标题:RF Communication using nrf24L01 and Nodejs addon
本站链接:http://www.codesec.net/view/529874.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(63)