查看Noduino OpenLight的源代码
←
Noduino OpenLight
跳转到:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您刚才请求的操作只有这个用户组中的用户才能使用:
用户
您可以查看并复制此页面的源代码:
=== node.js === Prepare the node.js: <source lang=bash> # download the node-v6.9.1-xxx.tgz from http://nodejs.org $ tar xf node-v6.9.1-*.tar.xz $ sudo cp -a node-v6.9.1*/* /usr/local/ $ sudo npm install -g MQTTClient $ sudo npm install -g request </source> <br><br> ==== Turn Off ==== Using the node.js to switch off the OpenLigh: <source lang=bash> $ cat light-off.js #! /usr/bin/env node var opt = { username: "YOUR_mqtt_uname", password: "" }; var http_req = require('/usr/local/lib/node_modules/request'); http_req.post( 'http://api.noduino.org/user/token', { json: {user_id: opt.username} }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body); opt.password = body.user_token; var MQTTClient = require('MQTTClient').Client; var client = new MQTTClient('mqtt.noduino.org', 1883, opt); client.connect(function () { console.log("connect ok!"); var pubopt = { qos_level: 2 }; var state = { on: 0 <---- off }; var data = '{"m":"set", "d":{' + '"s":' + state.on + '}}'; client.publish('app2dev/YOUR_DEVICE_ID', data, pubopt, function (message_id) { console.log("public ok! message_id = " + message_id); process.exit(0); }); }); } else { console.log("Request the user token failed"); process.exit(1); } } ); $ ./light-off.js </source> <br><br> ==== Turn On ==== Turn on the OpenLight: <source lang=bash> $ cat light-on.js #! /usr/bin/env node var opt = { username: "YOUR_mqtt_uname", password: "" }; var http_req = require('/usr/local/lib/node_modules/request'); http_req.post( 'http://api.noduino.org/user/token', { json: {user_id: opt.username, app_id:"mj2030064278", app_key:"0077d4829f52b1b2d668f8a82c5f5ded"} }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body); opt.password = body.user_token; var MQTTClient = require('MQTTClient').Client; var client = new MQTTClient('mqtt.noduino.org', 1883, opt); client.connect(function () { console.log("connect ok!"); var pubopt = { qos_level: 2 }; var state = { on: 1 <---- on }; var data = '{"m":"set", "d":{' + '"s":' + state.on + '}}'; client.publish('app2dev/YOUR_DEVICE_ID', data, pubopt, function (message_id) { console.log("public ok! message_id = " + message_id); process.exit(0); }); }); } else { console.log("Request the user token failed"); process.exit(1); } } ); $ ./light-on.js </source> <br><br> ==== Set Color ==== Change the color of the OpenLight: <source lang=bash> $ cat light-color.js #! /usr/bin/env node var opt = { username: "YOUR_mqtt_uname", password: "" }; var http_req = require('/usr/local/lib/node_modules/request'); http_req.post( 'http://api.noduino.org/user/token', { json: {user_id: opt.username, app_id:"mj2030064278", app_key:"0077d4829f52b1b2d668f8a82c5f5ded"} }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body); opt.password = body.user_token; var MQTTClient = require('MQTTClient').Client; var client = new MQTTClient('mqtt.noduino.org', 1883, opt); client.connect(function () { console.log("connect ok!"); var pubopt = { qos_level: 2 }; var state = { red: 0, green: 0, blue: 255, <--------- outputting full power of the blue channel white: 0, on: 1 }; data = '{"m":"set", "d":{' + '"r":' + state.red + ',"g":' + state.green + ',"b":' + state.blue + ',"w":' + state.white + ',"s":' + state.on + '}}'; client.publish('app2dev/YOUR_DEVICE_ID', data, pubopt, function (message_id) { console.log("public ok! message_id = " + message_id); process.exit(0); }); }); } else { console.log("Request the user token failed"); process.exit(1); } } ); $ ./light-color.js </source> <br><br> ==== FAQ ==== <font color=red><b>If you get the following error:</b></font> <source lang=bash> /usr/local/lib/node_modules/MQTTClient/client.js:56 throw Error('user names are kept to 12 characters or fewer'); ^ Error: user names are kept to 12 characters or fewer </source> Please patch the /usr/local/lib/node_modules/MQTTClient/client.js to remove the line of checking the length of username and password: <source lang=bash> --- a/client.js +++ b/client.js @@ -52,10 +52,10 @@ var Client = exports.Client = function (host, port, options) { options.alive_timer = 30; options.ping_timer = parseInt(options.alive_timer * 0.6 * 1000); // 用户名和密码 - if (typeof options.username == 'string' && options.username.length > 12) - throw Error('user names are kept to 12 characters or fewer'); - if (typeof options.password == 'string' && options.password.length > 12) - throw Error('passwords are kept to 12 characters or fewer'); + //if (typeof options.username == 'string' && options.username.length > 12) + // throw Error('user names are kept to 12 characters or fewer'); + //if (typeof options.password == 'string' && options.password.length > 12) + // throw Error('passwords are kept to 12 characters or fewer'); // Will flag if (options.will_flag && (typeof options.will_topic != 'string' || typeof options.will_message != 'string')) throw Error('missing will_topic or will_message when will_flag is set'); </source> <br><br>
返回到
Noduino OpenLight
。
个人工具
登录
名字空间
页面
讨论
变换
查看
阅读
查看源代码
查看历史
操作
搜索
导航
首页
社区专页
新闻动态
最近更改
随机页面
帮助
工具箱
链入页面
相关更改
特殊页面