Noduino OpenPlug CN

来自Jack's Lab
(版本间的差异)
跳转到: 导航, 搜索
(node.js)
(node.js)
第65行: 第65行:
 
=== node.js ===
 
=== node.js ===
  
Using the node.js to switch off the plug:
+
==== Prepare ====
 +
 
 +
Download the node-v6.9.1-xxx.tgz from http://nodejs.org
  
 
<source lang=bash>
 
<source lang=bash>
# download the node-v6.9.1-xxx.tgz from http://nodejs.org
 
 
$ tar xf node-v6.9.1-*.tar.xz
 
$ tar xf node-v6.9.1-*.tar.xz
 
$ sudo cp -a node-v6.9.1*/*  /usr/local/
 
$ sudo cp -a node-v6.9.1*/*  /usr/local/
 
$ sudo npm install -g MQTTClient
 
$ sudo npm install -g MQTTClient
 
$ sudo npm install -g request
 
$ sudo npm install -g request
 +
</source>
 +
 +
<br><br>
 +
 +
==== Turn off ====
 +
 +
Turn off the plug throught node.js:
 +
 +
<source lang=javascript>
 
$ cat switch-off.js
 
$ cat switch-off.js
 
#! /usr/bin/env node
 
#! /usr/bin/env node
第116行: 第126行:
 
</source>
 
</source>
  
 +
<br><br>
 +
 +
==== Turn On ====
  
 
Turn on the plug:
 
Turn on the plug:
  
<source lang=bash>
+
<source lang=javascript>
 
$ cat switch-on.js
 
$ cat switch-on.js
 
#! /usr/bin/env node
 
#! /usr/bin/env node
第163行: 第176行:
 
</source>
 
</source>
  
If you get the following error:
+
<br><br>
 +
 
 +
==== Debug ====
 +
 
 +
<font color=red><b>If you get the following error:</b></font>
  
 
<source lang=bash>
 
<source lang=bash>

2016年11月14日 (一) 12:03的版本

目录

1 Overview

  • AC 85V - 250V Smart Plug
  • ESP8285 inside
  • 250V 10A relay
  • Shell size 53mm x 53mm x 27mm


Open-plug-board-shell.jpg


Open-plug-board.jpg



2 Quick Start

Maike-wifi-ctrl-2233-all.jpg


  • 插座通电,等 6 s 左右,指示灯 1 秒左右慢闪,表示在等待配网
  • 如果不在慢闪,长按按钮 10 秒
  • 手机连接 2.4G WiFi ,微信扫描二维码,点 "配置设备上网"
  • 按提示输入 WiFi 密码,点 “连接”,等待配网完成(注意,设备目前只支持 2.4G,不支持 5G WiFi);此外企业级安全认证也暂不支持)
  • WiFi 配置完成后,微信自动进入局域网发现设备模式,其会列出设备列表
  • 点击第一个设备,进入页面,在页面最下面会有 “绑定设备” 按钮 (如果已经绑定过改设备,最下面的按钮为“进入公众号”),点击按钮,完成设备绑定
  • 完成后,点“进入公众号”,在公众号菜单的 “智能设备”,即可列出你绑定的所有设备,点一个设备,即可进入设备控制页


其他人需要控制设备,需要先连接到同样的路由器,在设备通电的情形下,扫描同样的二维码,点“设备已联网,跳过此步”,直接“发现”、“绑定设备” 即可在公众号菜单“智能设备里控制


其他说明:
  • 短按按钮,手动开关控制器
  • 长按 10 秒按钮,恢复出厂设置




3 Open API

编程控制起一个MQTT客户端,给设备发消息即可


微信绑定设备后,进入公众号,公众号下面的菜单 “智能设备”列出您所绑定的所有设备,选择您的插座,进入设备控制页,点右上角,在弹出的菜单里选“复制链接”,粘贴后查看 URL 即可获得您的插座的关键控制参数:

var devid = YOUR_DEVICE_ID;
var mqtt_uname = xxxxxxxx;
var mqtt_pass = TTTTTTTTTTTTTTTT;                           < ---- 3600s life time
var mqtt_server = xxx.xxx.xxx.xxx;
var mqtt_port = xxx;



3.1 node.js

3.1.1 Prepare

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



3.1.2 Turn off

Turn off the plug throught node.js:

$ cat switch-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://apiiot.mjyun.com/vendor/user_login',
    { 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;
            //console.log(opt)

            var MQTTClient = require('MQTTClient').Client;
            var client = new MQTTClient('101.200.241.60', 1883, opt);
            client.connect(function () {
                console.log("connect ok!");

                var pubopt = {
                    qos_level:  2
                }

                client.publish('app2dev/YOUR_DEVICE_ID', 'off', pubopt, function (message_id) {    <-------- Send the 'off' to device
                    console.log("public ok! message_id = " + message_id);
                    process.exit(0);
                });
            });
        } else {
            console.log("Request the user token failed");
            process.exit(1);
        }
    }
);

$ ./switch-off.js



3.1.3 Turn On

Turn on the plug:

$ cat switch-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://apiiot.mjyun.com/vendor/user_login',
    { 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;
            //console.log(opt)

            var MQTTClient = require('MQTTClient').Client;
            var client = new MQTTClient('101.200.241.60', 1883, opt);
            client.connect(function () {
                console.log("connect ok!");

                var pubopt = {
                    qos_level:  2
                }

                client.publish('app2dev/YOUR_DEVICE_ID', 'on', pubopt, function (message_id) {    <-------- Send the 'on' to device
                    console.log("public ok! message_id = " + message_id);
                    process.exit(0);
                });
            });
        } else {
            console.log("Request the user token failed");
            process.exit(1);
        }
    }
);

$ ./switch-on.js



3.1.4 Debug

If you get the following error:

/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

Please patch the /usr/local/lib/node_modules/MQTTClient/client.js to remove the line of checking the length of username and password:

--- 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');



3.2 HTML5

微信控制页是一个 H5 页,里面起了一个 Javascript 的 MQTT 客户端,Javascript example, using the mqttws31.min.js mqtt library:

var client_id = parseInt(Math.random() * 10000, 10) + '_' + mqtt_uname;
var client = new Paho.MQTT.Client(mqtt_server, mqtt_port, "/mqtt", client_id);
var state = 0;
function failConnect(e) {
	console.log("connect failed");
	console.log(e);
	console.log("reconnecting ...");
	client.connect({userName: mqtt_uname, password: mqtt_pass, onSuccess:onConnect, onFailure: failConnect, mqttVersion:3});
}
function onConnect() {
	console.log("onConnect OK!");
	subscribe('dev2app/' + devid);
}
function subscribe(topic) {
	client.subscribe(topic);
}
function onMessageArrived (message) {
	// MQTT message from device example: {"m":"status","d":"on","t":"2015-12-30T00:00:00+08:00"}
	console.log("Arrived Message: [", message.payloadString, "]");
	try {
		job = JSON.parse(message.payloadString);
		state = job.d;
	} catch(e) {
		console.log("JSON object error!");
		alert("JSON error, RX data is: " + message.payloadString);
		return;
	}
	if(state == 'on') {
		$('#switch').removeClass('btn_off');
		$('#switch').addClass('btn_on');
	} else if (state == 'off') {
		$('#switch').removeClass('btn_on');
		$('#switch').addClass('btn_off');
	}
}
function onDisConnect() {
	console.log("reconnecting ...");
	client.connect({userName: mqtt_uname, password: mqtt_pass, onSuccess:onConnect, onFailure: failConnect, mqttVersion:3});
}
function publish(topic, msg){
	message = new Paho.MQTT.Message(msg);
	message.destinationName = topic;
	client.send(message);
	console.log("publish message ok!");
}
function init() {
	client.connect({userName: mqtt_uname, password: mqtt_pass, onSuccess:onConnect, onFailure: failConnect, mqttVersion:3});
	client.onMessageArrived = onMessageArrived;
	client.onConnectionLost = onDisConnect;
}
function toggle()
{
	//console.log("toggle_devid = " + devid);
	var p_topic = "app2dev/" + devid;
	if($('#switch').hasClass('btn_off')) {
		//console.log("toggle off, topic = " + p_topic);
		publish(p_topic, "on");
	} else if ($('#switch').hasClass('btn_on')) {
		//console.log("toggle off, topic = " + p_topic);
		publish(p_topic, "off");
	}
}



4 HomeBridge



5 Hacking

5.1 Prepare firmware

Get noduino-sdk:

$ git clone --recursive git://github.com/icamgo/noduino-sdk.git noduino-sdk


Generate toolchain (you need Python 2.7):

$ cd noduino-sdk/toolchain
$ ./gen.py

Compile Open Plug firmware:

$ cd ../sketch/open-plug
$ make


The generated firmware is located in build/ dir named user1.bin annnd user2.bin


Window environment please refer to Getting Started with Noduino SDK on Windows, you can get how to setup the basic developmennt environment



5.2 Upload

5.2.1 Serial

5pin.jpg


Ft232.jpg


  • USB2UART_GND ------> SmartNode_GPIO0
  • USB2UAR_GND -----> SmartNode_GND
  • USB2UAR_RXD -----> SmartNode_TX
  • USB2UAR_TXD -----> SmartNode_RX


Connect USB2UAR_VCC3.3 -----> SmartNode_VCC at last

ESP8285 will be enter upload mode, we can upload the compiled firmware through serial using following commands in Linux:

$ cd /path/to/noduino-sdk/sketch/open-plug
$ make produce ESPPORT=/dev/ttyUSB1


In windows:

$ make produce ESPPORT=COM7

COM7 is your USB2UART device


In MAC OS, maybe it's:

$ make produce ESPPORT=/dev/cu.SLAB_USBtoUART

/dev/cu.SLAB_USBtoUART is your USB2UART device



5.2.2 Online

Access
http://dev.noduino.org/openonoff


Login
  • Username: noduino
  • password: noduino1234


Click the "Add files", select the user1.bin and user2.bin located in /path/to/noduino-sdk/sketch/open-plug/build/


Then click "Start upload" to upload the user1.bin and user2.bin into the server temporaily


Enter Official Account, then click "Add" menu, enter "Debug" page

Maike-upload-online-1.jpg


Select your device and the input "ota" in the data aera then click "send data"


Maike-upload-online-2.jpg


Device will download your user1.bin or user2.bin and write it into flash when the device received the "ota" message

You will see the device online message in the Debug page when the uploading is finished




6 Hardware

Open-plug-v0.8-sch.png


Open-plug-layout.png



7 参考

更多信息访问:











个人工具
名字空间

变换
操作
导航
工具箱