Use Node.js DDP Client on Arduino Yun to Access Meteor Server
Use Node.js DDP Client on Arduino Yun to Access Meteor Server
概述
在Arduino Yun上安装 Node.js, 并測试与 Meteor 通过 DDP Client 通讯。
所用硬件
- Arduino Yun
- SD Card(至少 2GB,我使用的是 8G )
所用软件
PC 软件:
- Arduino IDE
- SSH Terminal(Putty / Screen)
升级 Arduino Yun固件
从站点上下载 Arduino Yun 最新的 openWRT 固件, 当前升级包的版本号是 YunSysupgradeImage_v1.5.3.zip。
选用 MS-DOS 方式格式化 SD Card,将所下载的 ZIP 文件展开出的 bin 文件复制到 SD Card 的根文件夹下。
将 SD Card 插入 Arduino Yun, 并重新启动 Arduino Yun。正常情况下,应该能够在 Web Page 点按 “RESET” 按钮运行升级。
raw=true" alt="RESET" title="">
也能够用 SSH 登陆后,先检查一下 /mnt/sda1 文件夹下是否有升级文件,并运行下面命令。
# ls /mnt/sda1
openwrt-ar71xx-generic-yun-16M-squashfs-sysupgrade.bin
# run-sysupgrade /mnt/sda1/openwrt-ar71xx-generic-yun-16M-squashfs-sysupgrade.bin
- 升级完毕后。选择 ArduinoYun 的初始 WIFI。并通过 Web 页面訪问 192.168.240.1。设置 Arduino Yun 的网络连接。
利用 SD Card 扩展 Arduino Yun 空间
Arduino Yun 的板上存储仅有 16M,要想安装很多其它地东西就必须利用 SD Card 扩展磁盘空间。參考链接
重新启动后,通过本地局域网。SSH 到 Arduino Yun上,检查现有的分区情况。
# df
Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 7104 364 6740 5% /
/dev/root 7680 7680 0 100% /rom
tmpfs 30560 148 30412 0% /tmp
tmpfs 512 0 512 0% /dev
/dev/mtdblock3 7104 364 6740 5% /overlay
overlayfs:/overlay 7104 364 6740 5% /
/dev/sda1 7746772 1008 7745764 0% /mnt/sda1
通过 USB 连接 Arduino Yun,打开 Arduino IDE 上传此 ino 文件, 然后打开 Serial Monitor。在屏幕右下方选择 “Newline”, 并依照提示进行操作。下面的操作记录了我为分区方式,供參考。须要注意的是,此处输入的 4096 的意思是,将 4G 的空间保留为 /mnt/sda1 下的数据存储,剩下的空间留给根文件夹。
整个过程须要等待几分钟。
Open the Serial Monitor and double check the dropdown menu "Newline" has been selected. This sketch will format your micro SD card and use it as additional disk space for your Arduino Yun.
Please ensure you have ONLY your micro SD card plugged in: no pen drives, hard drives or whatever.
Do you wish to proceed (yes/no)? yes Starting Bridge... Ready to install utility software. Please ensure your Arduino Yun is connected to internet.
Ready to proceed (yes/no)? yes
Updating software list...
Software list updated. Installing software (this will take a while)...
e2fsprogs mkdosfs fdisk rsync installed Proceed with partitioning micro SD card (yes/no)? yes
Enter the size of the data partition in MB: 4096
Partitioning (this will take a while)...
Micro SD card correctly partitioned Creating 'arduino' folder structure...
Copying files from Arduino Yun flash to micro SD card...
Enabling micro SD as additional disk space... enabled We are done! Yeah! Now press the YUN RST button to apply the changes.
按下 YUN RST 后等待系统全然重新启动,再次检查分区情况,能够看到,Root 文件夹已经被放置在 /dev/sda2 下了,大小已经被扩大。
# df
Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 3556712 120920 3257464 4% /
/dev/root 7680 7680 0 100% /rom
tmpfs 30560 100 30460 0% /tmp
tmpfs 512 0 512 0% /dev
/dev/sda2 3556712 120920 3257464 4% /overlay
overlayfs:/overlay 3556712 120920 3257464 4% /
/dev/sda1 4186104 12 4186092 0% /mnt/sda1
安装 Node.js
确认 Arduino Yun 能够正常联网,然后运行下面安装 Node.js。 原文參考链接
# opkg update
# opkg install node
安装完毕之后,能够用下面命令检查 Node.js 成功安装
# node -e "console.log('Hello_Yun')"
Hello_Yun
Arduino Yun 可用的全部 Package 能够在这里找到。
您还能够依据须要,安装很多其它的 Node.js 基础包,比如:
# opkg install node-serialport
# opkg install node-noble
设置 Swap 区
Arduino Yun 提供的 Node.js 包非常有限。还须要通过 npm 安装很多其它可扩展包。可是由于 npm 所需的内存比較大,我们必须打开 Swap 才干够顺利运行,否则会报内存不足。
检查现有的 Swap 设置
# free -m
total used free shared buffers
Mem: 61116 31712 29404 0 8364
-/+ buffers: 23348 37768
Swap: 0 0 0
SSH 登陆后。创建 Swap 分区。这一步要等比較长的时间。
# mkdir /swap # dd if=/dev/zero of=/swap/yunswapfile bs=1M count=1024
1024+0 records in
1024+0 records out
设置 Swapon。此时已经能够看到 Swap 区正确创建了。
# mkswap /swap/yunswapfile
# swapon /swap/yunswapfile # free -m
total used free shared buffers
Mem: 61116 59704 1412 0 7676
-/+ buffers: 52028 9088
Swap: 1048572 0 1048572
设置启动时自己主动载入 Swap。重新启动 Arduino Yun 后。Swap 区应该正常。
# uci add fstab swap
# uci set fstab.@swap[0].device=/swap/yunswapfile
# uci set fstab.@swap[0].enabled=1
# uci set fstab.@swap[0].fstype=swap
# uci set fstab.@swap[0].options=default
# uci set fstab.@swap[0].enabled_fsck=0
# uci commit
安装 DDP Client
此时,我们再使用 npm 安装 DDP Client。
我使用的 DDP Client 是 https://www.npmjs.com/package/ddp
# npm install ddp
# npm install underscore
# npm list
/root
├─┬ ddp@0.11.0
│ ├── ddp-ejson@0.8.1-3
│ ├── ddp-underscore-patched@0.8.1-2
│ ├─┬ faye-websocket@0.9.4
│ │ └─┬ websocket-driver@0.5.4
│ │ └── websocket-extensions@0.1.1
│ └─┬ request@2.53.0
│ ├── aws-sign2@0.5.0
│ ├─┬ bl@0.9.4
│ │ └─┬ readable-stream@1.0.33
│ │ ├── core-util-is@1.0.1
│ │ ├── inherits@2.0.1
│ │ ├── isarray@0.0.1
│ │ └── string_decoder@0.10.31
│ ├── caseless@0.9.0
│ ├─┬ combined-stream@0.0.7
│ │ └── delayed-stream@0.0.5
│ ├── forever-agent@0.5.2
│ ├─┬ form-data@0.2.0
│ │ └── async@0.9.2
│ ├─┬ hawk@2.3.1
│ │ ├── boom@2.7.2
│ │ ├── cryptiles@2.0.4
│ │ ├── hoek@2.14.0
│ │ └── sntp@1.0.9
│ ├─┬ http-signature@0.10.1
│ │ ├── asn1@0.1.11
│ │ ├── assert-plus@0.1.5
│ │ └── ctype@0.5.3
│ ├── isstream@0.1.2
│ ├── json-stringify-safe@5.0.1
│ ├─┬ mime-types@2.0.13
│ │ └── mime-db@1.11.0
│ ├── node-uuid@1.4.3
│ ├── oauth-sign@0.6.0
│ ├── qs@2.3.3
│ ├── stringstream@0.0.4
│ ├── tough-cookie@1.2.0
│ └── tunnel-agent@0.4.0
└── underscore@1.8.3
使用 DDP Client 訪问 Meteor
下面的參考实现展示了怎样用 DDP Client 与 Meteor 交互。
var DDPClient = require("ddp"),
_ = require('underscore');
var ddpclient = new DDPClient({
// url: 'ws://192.168.199.240:3000/websocket'
url: 'ws://mcotton-01.chinacloudapp.cn/websocket'
});
var useremail = "iasc@163.com";
var pwd = "123456";
var user_id = null, token = null;
var appkit_weather_station, myappkit_weather_station, my_app_kit_id;
var data_observer, control_observer;
/*
* Connect to the Meteor Server
*/
ddpclient.connect(function (error, wasReconnect) {
// If autoReconnect is true, this callback will be invoked each time
// a server connection is re-established
if (error) {
console.log("DDP connection error!");
return;
}
if (wasReconnect) {
console.log("Reestablishment of a connection.");
}
console.log("connected!");
ddpclient.call("login", [
{user: {email: useremail}, password: pwd}
], function (err, result) {
console.log(result);
user_id = result.id;
token = result.token;
if (token) {
console.log("Logined!", user_id, token);
//var observer = ddpclient.observe("appkits");
/*
* Subscribe to a Meteor Collection
*/
ddpclient.subscribe(
"appkits", // name of Meteor Publish function to subscribe to
[], // any parameters used by the Publish function
function () { // callback when the subscription is complete
console.log("appkits all ==> ", ddpclient.collections.appkits);
appkit_weather_station = _(ddpclient.collections.appkits).findWhere({name: 'Weather Station'});
console.log("appkits weather_station ==> ", appkit_weather_station);
console.log("appkits weather_station id ==> ", appkit_weather_station._id);
// Create myAppKit
var myAppKit = ddpclient.call(
'myAppKitInsert',
[{
name: 'My Weather Station',
app_kit_id: appkit_weather_station._id
}],
function (err, result) {
console.log('myAppKitInsert, error: ' + error);
console.log('myAppKitInsert, result: ' + result._id);
my_app_kit_id = result._id;
/*
* Subscribe to a Meteor Collection
*/
ddpclient.subscribe(
"myappkits", // name of Meteor Publish function to subscribe to
[user_id], // any parameters used by the Publish function
function () { // callback when the subscription is complete
console.log("myappkits all ==> ", ddpclient.collections.myappkits);
console.log("appkit_weather_station id ==> ", appkit_weather_station._id);
myappkit_weather_station = _(ddpclient.collections.myappkits).filter({_id: my_app_kit_id});
console.log("myappkits weather_station ==> ", myappkit_weather_station);
}
);
ddpclient.subscribe(
"dataevents", // name of Meteor Publish function to subscribe to
[user_id], // any parameters used by the Publish function
function () { // callback when the subscription is complete
console.log("dataevents all ==> ", ddpclient.collections.dataevents);
}
);
ddpclient.subscribe(
"controlevents", // name of Meteor Publish function to subscribe to
[user_id], // any parameters used by the Publish function
function () { // callback when the subscription is complete
console.log("controlevents all ==> ", ddpclient.collections.controlevents);
}
);
/*
* observe DataEvents
*/
data_observer = ddpclient.observe("dataevents");
data_observer.added = function (id) {
console.log("[ADDED] to " + data_observer.name + ": " + id);
var event = _(ddpclient.collections.dataevents).findWhere({_id: id});
console.log("[ADDED] dataevents ", event)
};
/*
* observe ControlEvents
*/
control_observer = ddpclient.observe("controlevents");
control_observer.added = function (id) {
console.log("[ADDED] to " + control_observer.name + ": " + id);
var event = _(ddpclient.collections.controlevents).findWhere({_id: id});
console.log("[ADDED] controlevents ", event)
};
/*
* Send new Control Event to Server
* */
var newControlEvent = ddpclient.call(
'controlEventInsert',
[{
my_app_kit_id: my_app_kit_id,
control_name: "Status",
control_value: "true"
}],
function (err, result) {
console.log('controlEventInsert, error: ' + error);
console.log('controlEventInsert, result: ' + result._id);
});
}
);
}
);
}
});
////Debug information
//
//ddpclient.on('message', function (msg) {
// console.log("ddp message: " + msg);
//});
/*
ddpclient.on('socket-close', function (code, message) {
console.log("Close: %s %s", code, message);
});
ddpclient.on('socket-error', function (error) {
console.log("Error: %j", error);
});
*/
// close
setTimeout(function () {
// observer.stop();
ddpclient.close();
}, 5000);
});
代码地址
https://github.com/iascchen/arduino_study/tree/master/src/yun_ddp
转载请注明出处
Author : iascchen(at)gmail(dot)com
Date : 2015-6-3
Github : https://github.com/iascchen/arduino_study
新浪微博 : @问天鼓
Use Node.js DDP Client on Arduino Yun to Access Meteor Server的更多相关文章
- 《深入浅出Node.js》第7章 网络编程
@by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...
- Node.js Web 开发框架大全《中间件篇》
这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...
- TCP Socket Programming in Node.js
TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...
- Node.js 给前端带来了什么
在软件开发领域,前端工程师曾经是一个比较纠结的职业.在Web技术真正发展起来之前的相当长一段时间里,由于技术门槛很低,前端工程师行业一直是鱼龙混杂的状态.其中很多号称是Web开发者的人实际上并没有什么 ...
- Node.js模块
每一个Node.js都是一个Node.js模块,包括JavaScript文件(.js).JSON文本文件(.json)和二进制模块文件(.node). mymodul.js function Hell ...
- node.js 基础学习笔记3 -http
http模块,其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端 http.server是一个基于事件的HTTP服务器 http.request则是一个HTTP客户端工具,用户向服务器发送请 ...
- Node.js学习笔记(一)基础介绍
什么是Node.js 官网介绍: Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js us ...
- Node.js 基础介绍
什么是Node.js 官网介绍: Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js us ...
- 忽略node.js服务中favicon.icon的请求
场景 一个最简单的node.js的http服务 const http = require('http'); const server = http.createServer(function(req, ...
随机推荐
- ubutun 下webalizer 分析Apache日志
http://www.webalizer.org/ 配置Webalizer 我们可以通过命令行配置Webalizer,也可以通过配置文件进行配置.下面将重点介绍使用配置文件进行配置,该方法使用形式比 ...
- Sicily-1050 深度优先搜索
一. 题意 给出5个数和4则运算,看能不能算出目标值出来,如果算不出来就算出比目标值小的最大值.深搜:每一步选两个数做运算,然后算出的结果作为下一步的其中一个操作数.每一步选数有C(5,2) ...
- hdoj 3549 Flow Problem(最大网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 思路分析:该问题为裸的最大网络流问题,数据量不大,使用EdmondsKarp算法求解即可:需要注 ...
- nodejs学习笔记_nodejs和PHP在基础架构上的差别--共享状态的并发
绝大多数对于Node.js的讨论都把关注点放在了处理高并发能力上,做开发的时候一定要明确node内部做出的权衡,以及node应用性能好的原因. node 为javascript引入了一个复杂的概念,: ...
- Python经常使用第三方工具、库、骨架
Python ImagingLibrary(PIL):它提供强大的图形处理的能力,并提供广泛的图形文件格式支持.该库能进行图形格式的转换.打印和显示.还能进行一些图形效果的处理,如图形的放大.缩小和旋 ...
- 关于js闭包是否真的会造成内存泄漏(转载)
闭包是一个非常强大的特性,但人们对其也有诸多无解.一种危言耸听的说法是闭包会造成内存泄露. 局部变量本来应该在函数退出的时候被解除引用,但如果局部变量被封闭在闭包形成的环境中,那么这个局部变量就能一直 ...
- js中使用this的一些注意事项
先看看这段代码 var x =9; var fobj ={ x:1, test:function(callback){ var x= 2; callback(); } } function pp(){ ...
- 子请求执行失败。有关更多信息,请检查 InnerException。
异常:子请求执行失败.有关更多信息,请检查 InnerException. 错误:程序请求异常 可能原因: 1.可以检查是否引用了分布视图,而分布视图中发生错误 2.可以检查是否引用了分布视图,而分布 ...
- F - 蜘蛛牌(深度搜索)
Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...
- 解决虚拟内存不够导致Eclipse is not responding
安装目录下eclipse.ini中: 修改参数至必要大小. e.g. -vmargs-Djava.net.preferIPv4Stack=true-Dosgi.requiredJavaVersion= ...