ã¯ããã«
XBee 㯠ZigBee è¦æ ¼ã§éä¿¡ãè¡ãªã£ã¦ãããå°åã®ç¡ç·éä¿¡ã¢ã¸ã¥ã¼ã«ã§ããååï¼boost::asio::serial_port で ZigBee と会話してみた - 凹みTipsï¼ã¯ C++ 㧠ZigBee ãæ±ãã¾ããããä»å㯠Node.js ã§ã®å¤ã®åå¾ããã£ã¦ã¿ããã¨æãã¾ãã
åºæ¥ãããã«ãªããã¨
- JavaScript ããé¨å±ã®æ¸©åº¦ãç¡ç·ã§åå¾
ç°å¢ / æ§æ
node-serialport ã®å°å ¥
Node.js ç¨ã®ã¢ããªã³ã¨ã㦠node-xbee ãå ¬éããã¦ãã¾ãã®ã§ãããã使ãã¾ãã
Readme ãèªãã§ã¿ãã¨ãnode-serialport ãå©ç¨ãã¦ã·ãªã¢ã«éä¿¡ãè¡ãããã§ããä¸ã¤æ³¨æããªãã¦ã¯ãªããªãã®ã¯ãnode-serialport ã®ææ°çã§ã¯åããã0.7.3 ãç¨ããç¹ã§ãã
npm install [email protected] npm install xbee
ãã ã0.7.3 ã®å¤ã node-serialport ã§ã¯ããã¤ãã£ãã¢ã¸ã¥ã¼ã«ã®ä½æã« node-waf ãç¨ãã¦ãããnode-waf ã¯ææ°ã® Node.jsï¼0.9.2@2012/10/15ç¾å¨ï¼ã§ã¯åãé¤ããã¦ãã¾ãã¾ããããã㧠node-waf ã§æ¸ãã¦ããé¨åã node-gyp ç¨ã«æ¸ãæãã¾ããã¢ã¸ã¥ã¼ã«ã®ãã£ã¬ã¯ããªã¯ä»¥ä¸ã«ãªãã¾ãã
$ cd node_modules/serialport
ã¾ããnode-gyp ç¨ã« binding.gyp ãä½ãã¾ãã
serialport_native/binding.gyp
{ 'targets': [ { 'target_name' : 'serialport_native', 'sources' : [ 'serialport_native.cc' ], }, ], }
次ã«ãC++ ã®ã½ã¼ã¹ã³ã¼ãã«ãï¼è¡è¿½å ãã¾ãã
serialport_native/serialport_native.ccï¼362è¡ç®ããï¼
extern "C" void init (Handle<Object> target) { HandleScope scope; SerialPort::Initialize(target); } NODE_MODULE(serialport_native, init); // <-- ãããè¿½è¨ }
ä¸åä¸ã®é層ã«ä¸ãããserialport.js ã®ä¸ã§ Node.js v0.9.x ããå»æ¢ã«ãªã£ã IOWatcher ã®è¡ãåãé¤ãã¾ãã
serialport.jsï¼12è¡ç®ï¼
// var IOWatcher = process.binding('io_watcher').IOWatcher; <-- ã³ã¡ã³ãã¢ã¦ã
æå¾ã« Makefile ãæ¸ãæãã¾ãã
Makefile
setup : cd serialport_native;node-gyp configure build;cd ..; # <-- node-waf ãã node-gyp ã« cp serialport_native/build/Debug/serialport_native.node .ã# ç°å¢ã«åãã㦠Release ãã Debug ã«
ã§ãMake ããã° OK ã§ãã
$ make
温度ãåå¾
node-xbee 㯠node-serialport ã®ã¢ããªã³çãªä½ç½®ã¥ãã§æ¸ããã¦ãã¾ããAT ã³ãã³ãã® IS 㧠1 ç§ããã« XBee ãããã¼ã¿ãåãåã£ã¦æ¸©åº¦ã表示ããã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
temp.js
var SerialPort = require("serialport").SerialPort , xbee = require("xbee") ; var serial_xbee = new SerialPort("/dev/tty.usbserial-A501DG57", { parser: xbee.packetParser() }); serial_xbee.on("data", function(data) { // console.log('xbee data received:', data); <-- ããã§ä½ãå ¥ã£ã¦ãããè¦ãã¾ã var ain3hi = data.bytes[19]; var ain3lo = data.bytes[20]; var voltage = 1.2 * (ain3hi * 0x0100 + ain3lo) / 0x03ff; var temperature = (voltage - 0.6) * 100; console.log(temperature); }); var atc = new xbee.RemoteATCommand(); atc.setCommand('IS'); atc.destination64 = [0x00,0x13,0xa2,0x00,0x40,0x89,0x9d,0x84]; // XBee ã®ã¢ãã¬ã¹ atc.destination16 = [0xff,0xfe]; setInterval(function() { serial_xbee.write( atc.getBytes() ); }, 1000);
å®è¡
$ node temp 27.859237536656888 27.74193548387097 ï¼ä»¥ä¸ç¥ï¼
SerialPort 㯠Ubuntu ãªãã/dev/ttyUSB0ããWindows ãªããCOM3ãã¨ãã£ãå½¢ã«ãªãã¾ãï¼æ°åã¯å¤ããã¾ãï¼ãMac ã®å ´åã¯ä»¥ä¸ã®ãµã¤ããã FTDI ã®ãã©ã¤ããã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã¾ãã
温度è¨ç®ã®é¨åã«ã¤ãã¦ã¯ã LM 61 ã¨ãã温度ã»ã³ãµãç¨ãã¦æ¸©åº¦ã測å®ããã®ã§ãã®é»å§ - 温度å¤æå¼ã«æºæ ããå½¢ã«ãªã£ã¦ãã¾ãã
ãããã«
ããã«ãã®éä½æãã iRemocon ã® Node.js ã¢ã¸ã¥ã¼ã«ï¼node-iRemocon つくった - 凹みTipsï¼ãçµã¿åãããã°ãæå®ãã温度ã«ã¨ã¢ã³ã³ãè¨å®ãã¦ãããé¨å±ãç°¡åã«ã§ãã¾ãï¼
node-xbee ã® Readme ã®æã¿ãããªä¸çã«ã¾ã§åºæ¥ãã¨æ¥½ãããªãããã§ããã
node-xbee ã®åºæï¼å¼ç¨ï¼
frontdoor.on("open", function() { if (alarm.state == "on") { alarm.sound(); hounds.release(); } else { lights.switchOn(); voice.speak("Welcome home"); } });