Noduino OpenPlug
(→Get the device IP) |
(→Turn Off) |
||
(未显示1个用户的30个中间版本) | |||
第1行: | 第1行: | ||
== Overview == | == Overview == | ||
+ | === China Version === | ||
− | * AC | + | * AC 110V - 230V input |
− | * ESP8285 inside | + | * ESP8285/ESP8266 inside |
* 250V 10A relay | * 250V 10A relay | ||
* Shell size 53mm x 53mm x 27mm | * Shell size 53mm x 53mm x 27mm | ||
第9行: | 第10行: | ||
[[文件:Open-plug-board-shell.jpg | 600px]] | [[文件:Open-plug-board-shell.jpg | 600px]] | ||
+ | [[文件:Open-plug-board.jpg | 600px]] | ||
+ | <br> | ||
− | [[文件: | + | === US Version === |
+ | |||
+ | * AC 110V - 230V input | ||
+ | * ESP8266/ESP8285 inside | ||
+ | * 250V 10A relay | ||
+ | * 50mm Diameter, 27.5mm Thickness | ||
+ | |||
+ | |||
+ | [[文件:Openplug-us1.jpg | 600px]] | ||
+ | [[文件:Openplug-us2.jpg | 600px]] | ||
+ | |||
+ | |||
+ | [[文件:Noduino-works-with-logo.png | 600px]] | ||
<br><br> | <br><br> | ||
第62行: | 第77行: | ||
var devid = YOUR_DEVICE_ID; | var devid = YOUR_DEVICE_ID; | ||
var mqtt_uname = xxxxxxxx; | var mqtt_uname = xxxxxxxx; | ||
− | + | </source> | |
− | var | + | |
− | var | + | <br><br> |
+ | |||
+ | === Go === | ||
+ | |||
+ | ==== Prepare ==== | ||
+ | |||
+ | Prepare the Go lang: | ||
+ | |||
+ | <source lang=bash> | ||
+ | $ sudo apt-get install golang-go | ||
+ | $ sudo apt-get install golang-eclipse-paho-dev | ||
+ | $ export GOPATH=/usr/share/gocode | ||
+ | </source> | ||
+ | |||
+ | Refer to: | ||
+ | |||
+ | * https://golang.org | ||
+ | * http://www.eclipse.org/paho/clients/golang/ | ||
+ | |||
+ | <br> | ||
+ | |||
+ | ==== Turn On/Off ==== | ||
+ | |||
+ | <source lang=cpp> | ||
+ | package main | ||
+ | |||
+ | import ( | ||
+ | "fmt" | ||
+ | "os" | ||
+ | "strings" | ||
+ | "net/http" | ||
+ | "encoding/json" | ||
+ | MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" | ||
+ | ) | ||
+ | |||
+ | //define a function for the default message handler | ||
+ | var f MQTT.MessageHandler = func(client *MQTT.Client, msg MQTT.Message) { | ||
+ | fmt.Printf("TOPIC: %s\n", msg.Topic()) | ||
+ | fmt.Printf("MSG: %s\n", msg.Payload()) | ||
+ | } | ||
+ | |||
+ | var uid string = "YOUR_mqtt_uname" | ||
+ | |||
+ | func main() { | ||
+ | if len(os.Args[1:]) >= 2 { | ||
+ | |||
+ | devid := os.Args[1] | ||
+ | cmd := os.Args[2] | ||
+ | msg := "" | ||
+ | |||
+ | if cmd == "on" { | ||
+ | msg = "on" | ||
+ | } else if cmd == "off" { | ||
+ | msg = "off" | ||
+ | } else if cmd == "ota" { | ||
+ | msg = "ota" | ||
+ | } | ||
+ | |||
+ | pass := get_token(uid) | ||
+ | send_msg(devid, msg, pass) | ||
+ | |||
+ | } else { | ||
+ | help_msg() | ||
+ | } | ||
+ | } | ||
+ | |||
+ | func help_msg() { | ||
+ | fmt.Println("Supported cmd:") | ||
+ | fmt.Println("\topenplug dev_id on") | ||
+ | fmt.Println("\topenplug dev_id off") | ||
+ | fmt.Println("\topenplug dev_id ota") | ||
+ | } | ||
+ | |||
+ | func send_msg(devid string, msg string, pass string) { | ||
+ | |||
+ | opts := MQTT.NewClientOptions().AddBroker("tcp://mqtt.noduino.org:1883") | ||
+ | opts.SetClientID("cid_" + devid) | ||
+ | opts.SetUsername(devid) | ||
+ | opts.SetPassword(pass) | ||
+ | opts.SetDefaultPublishHandler(f) | ||
+ | |||
+ | c := MQTT.NewClient(opts) | ||
+ | if token := c.Connect(); token.Wait() && token.Error() != nil { | ||
+ | panic(token.Error()) | ||
+ | } | ||
+ | |||
+ | mqtt_tx_topic := "app2dev/" + devid | ||
+ | |||
+ | token := c.Publish(mqtt_tx_topic, 0, false, msg) | ||
+ | token.Wait() | ||
+ | } | ||
+ | |||
+ | func get_token(user_id string) (string) { | ||
+ | url := "http://api.noduino.org/user/token" | ||
+ | |||
+ | type Body struct { | ||
+ | UID string `json:"user_id"` | ||
+ | } | ||
+ | |||
+ | var body = Body { | ||
+ | UID: user_id, | ||
+ | } | ||
+ | |||
+ | data, err := json.Marshal(body) | ||
+ | //fmt.Println(string(data)) | ||
+ | |||
+ | if err == nil { | ||
+ | resp, err := http.Post(url, "application/json", strings.NewReader(string(data))) | ||
+ | if err != nil { | ||
+ | fmt.Println(err) | ||
+ | } | ||
+ | defer resp.Body.Close() | ||
+ | |||
+ | dec := json.NewDecoder(resp.Body) | ||
+ | var v map[string]interface{} | ||
+ | if err := dec.Decode(&v); err != nil { | ||
+ | fmt.Printf("error: %v\n", err) | ||
+ | } else { | ||
+ | if val, ok := v["user_token"].(string); ok { | ||
+ | return val | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return "" | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | <source lang=bash> | ||
+ | $ go build openplug.go | ||
+ | $ ./openplug MKD12343405300 on | ||
+ | $ ./openplug MKD12343405300 off | ||
+ | </source> | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | ==== OTA ==== | ||
+ | |||
+ | <source lang=bash> | ||
+ | $ ./openplug MKD12343405300 ota | ||
</source> | </source> | ||
第88行: | 第243行: | ||
Turn off the plug throught node.js: | Turn off the plug throught node.js: | ||
− | replace the | + | * replace the mqtt_uname in line 6 |
+ | * replace the devid in line 7 | ||
<source lang=javascript> | <source lang=javascript> | ||
第121行: | 第277行: | ||
var http_req = require('/usr/local/lib/node_modules/request'); | var http_req = require('/usr/local/lib/node_modules/request'); | ||
http_req.post( | http_req.post( | ||
− | 'http:// | + | 'http://api.noduino.org/user/token', |
− | { json: {user_id: opt.username | + | { json: {user_id: opt.username} }, |
function (error, response, body) { | function (error, response, body) { | ||
if (!error && response.statusCode == 200) { | if (!error && response.statusCode == 200) { | ||
第131行: | 第287行: | ||
var MQTTClient = require('/usr/local/lib/node_modules/MQTTClient').Client; | var MQTTClient = require('/usr/local/lib/node_modules/MQTTClient').Client; | ||
− | var client = new MQTTClient(' | + | var client = new MQTTClient('mqtt.noduino.org', 1883, opt); |
client.connect(function () { | client.connect(function () { | ||
console.log("connect ok!"); | console.log("connect ok!"); | ||
第228行: | 第384行: | ||
==== Turn off ==== | ==== Turn off ==== | ||
+ | |||
+ | * replace the mqtt_uname in line 10 | ||
+ | * replace the devid in line 11 | ||
<source lang=python> | <source lang=python> | ||
第258行: | 第417行: | ||
sys.exit(-2) | sys.exit(-2) | ||
− | url = 'http:// | + | url = 'http://api.noduino.org/user/token' |
dat = dict ( | dat = dict ( | ||
user_id = mqtt_uname, | user_id = mqtt_uname, | ||
− | |||
− | |||
) | ) | ||
第278行: | 第435行: | ||
mqtt_tx_topic = 'app2dev/' + devid | mqtt_tx_topic = 'app2dev/' + devid | ||
− | mqtt_host = ' | + | mqtt_host = 'mqtt.noduino.org' |
mqtt_port = 1883 | mqtt_port = 1883 | ||
mqtt_msg = set_state | mqtt_msg = set_state | ||
第403行: | 第560行: | ||
== Open API (HTTP in LAN) == | == Open API (HTTP in LAN) == | ||
− | <b style="color:#f00"> | + | <b style="color:#f00">Note: the device security policy using the WiFi router's password, that is: the users who are authorized to connect to the WiFi router, is a trusted user</b> |
− | + | Device start a HTTP service by default. You can send the following HTTP request to turn on or turn off the device. This way independent of MQTT cloud service can be used in local area network when you don't want to make device access Internet. | |
− | + | ||
<br> | <br> | ||
第412行: | 第568行: | ||
=== Get the device IP === | === Get the device IP === | ||
− | You can use the Fing app in iOS to scan the LAN network. The device named 'Espressif' is your OpenPlug device: | + | You can use the Fing app in iOS to scan the LAN network. |
+ | |||
+ | The device named '''Espressif''' is your OpenPlug device: | ||
+ | |||
[[文件:Fing-lan-scan.png | 400px]] | [[文件:Fing-lan-scan.png | 400px]] | ||
第497行: | 第656行: | ||
[[文件:Alexa-openplug-turnoff.png | 500px]] | [[文件:Alexa-openplug-turnoff.png | 500px]] | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Debug === | ||
+ | |||
+ | UPNP discovery: | ||
+ | |||
+ | <source lang=bash> | ||
+ | comcat@jackslab:/work/alexa/test$ cat dis.txt | ||
+ | M-SEARCH * HTTP/1.1 | ||
+ | HOST: 239.255.255.250:1900 | ||
+ | MAN: "ssdp:discover" | ||
+ | ST: urn:Belkin:device:** | ||
+ | MX: 3 | ||
+ | comcat@jackslab:/work/alexa/test$ socat -T1 STDIO UDP4-DATAGRAM:239.255.255.250:1900 < dis.txt | ||
+ | HTTP/1.1 200 OK | ||
+ | EXT: | ||
+ | CACHE-CONTROL: max-age=86400 | ||
+ | SERVER: Noduino/1.0 UPNP/1.1 OpenPlug/2.0 | ||
+ | USN: uuid:38323636-4558-4dda-9188-cda0810b2e50 | ||
+ | ST: urn:Belkin:device:** | ||
+ | LOCATION: http://192.168.1.195:80/setup.xml | ||
+ | </source> | ||
<br><br> | <br><br> | ||
第507行: | 第689行: | ||
<source lang=bash> | <source lang=bash> | ||
− | $ git clone --recursive git://github.com/ | + | $ git clone --recursive git://github.com/noduino/noduino-sdk.git noduino-sdk |
</source> | </source> | ||
第518行: | 第700行: | ||
</source> | </source> | ||
− | Compile Open Plug firmware: | + | |
+ | '''Compile Open Plug US version firmware:''' | ||
+ | |||
+ | <source lang=bash> | ||
+ | $ cd ../sketch/open-plug-us | ||
+ | $ make | ||
+ | </source> | ||
+ | |||
+ | |||
+ | Compile Open Plug (China Version) firmware: | ||
<source lang=bash> | <source lang=bash> | ||
第537行: | 第728行: | ||
==== Serial ==== | ==== Serial ==== | ||
− | [[文件: | + | [[文件:Openplug-fw-upload.jpg | 800px]] |
第613行: | 第804行: | ||
<br><br> | <br><br> | ||
− | == Hardware == | + | == Hardware == |
+ | |||
+ | === China Version === | ||
[[文件:Open-plug-v0.8-sch.png | 900px]] | [[文件:Open-plug-v0.8-sch.png | 900px]] | ||
第620行: | 第813行: | ||
[[文件:Open-plug-layout.png | 600px]] | [[文件:Open-plug-layout.png | 600px]] | ||
+ | |||
+ | <br> | ||
+ | |||
+ | === US Version === | ||
+ | |||
<br><br> | <br><br> | ||
第628行: | 第826行: | ||
* [[Noduino]] | * [[Noduino]] | ||
+ | * Github: https://github.com/noduino/noduino-sdk | ||
+ | |||
<br><br> | <br><br> |
2018年10月4日 (四) 14:24的最后版本
目录 |
[编辑] 1 Overview
[编辑] 1.1 China Version
- AC 110V - 230V input
- ESP8285/ESP8266 inside
- 250V 10A relay
- Shell size 53mm x 53mm x 27mm
[编辑] 1.2 US Version
- AC 110V - 230V input
- ESP8266/ESP8285 inside
- 250V 10A relay
- 50mm Diameter, 27.5mm Thickness
[编辑] 2 Quick Start
- Power on the plug, waiting 8s
- Light is flashing slowly means waiting for network matching
- If there is no flash, please press the button for 10sec.
- Mobile connect to 2.4G WiFi, scan the QR code in WeChat and click "Network Connection"
- Enter WiFi password and click "Connect" then waiting for network connection. (P.S. only 2.4G WiFi is applicable, 5G WiFi and Enterprise - level security certifications are not supportable.)
- Finish WiFi configuration, WeChat will be in LAN devices discovery mode with devices list visible.
- Click the first device then press "Like Device" button to bind the device. (P.S. if the device has been binding, then the button at bottom is "Enter Official Account")
- Finish above steps, please press "Enter Official Account" button then click "Devices" button to find your binding devices list. Click the device to enter control page.
If more people need to control the device, please connect to the same router and scan the same QR code while the device is power on. Click "Device connected and skip" -> "Link Device"->"Enter Official Account" to control smart devices
- Note
- Press the button momentarily, manual switch controller
- Press the button for 10sec, restore the factory settings.
[编辑] 3 Open API
Afer you guys 'Link Device' in WeChat, Enter Official Account:
- click "Devices" to list your binding devices
- select your OpenPlug in the list, enter control page, click upright corner, then Copy URL
- paste the URL into web browser (Chrome or Safari) to access the control page in browser
- check the source code of the page to get the following variables:
var devid = YOUR_DEVICE_ID; var mqtt_uname = xxxxxxxx;
[编辑] 3.1 Go
[编辑] 3.1.1 Prepare
Prepare the Go lang:
$ sudo apt-get install golang-go $ sudo apt-get install golang-eclipse-paho-dev $ export GOPATH=/usr/share/gocode
Refer to:
[编辑] 3.1.2 Turn On/Off
package main import ( "fmt" "os" "strings" "net/http" "encoding/json" MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git" ) //define a function for the default message handler var f MQTT.MessageHandler = func(client *MQTT.Client, msg MQTT.Message) { fmt.Printf("TOPIC: %s\n", msg.Topic()) fmt.Printf("MSG: %s\n", msg.Payload()) } var uid string = "YOUR_mqtt_uname" func main() { if len(os.Args[1:]) >= 2 { devid := os.Args[1] cmd := os.Args[2] msg := "" if cmd == "on" { msg = "on" } else if cmd == "off" { msg = "off" } else if cmd == "ota" { msg = "ota" } pass := get_token(uid) send_msg(devid, msg, pass) } else { help_msg() } } func help_msg() { fmt.Println("Supported cmd:") fmt.Println("\topenplug dev_id on") fmt.Println("\topenplug dev_id off") fmt.Println("\topenplug dev_id ota") } func send_msg(devid string, msg string, pass string) { opts := MQTT.NewClientOptions().AddBroker("tcp://mqtt.noduino.org:1883") opts.SetClientID("cid_" + devid) opts.SetUsername(devid) opts.SetPassword(pass) opts.SetDefaultPublishHandler(f) c := MQTT.NewClient(opts) if token := c.Connect(); token.Wait() && token.Error() != nil { panic(token.Error()) } mqtt_tx_topic := "app2dev/" + devid token := c.Publish(mqtt_tx_topic, 0, false, msg) token.Wait() } func get_token(user_id string) (string) { url := "http://api.noduino.org/user/token" type Body struct { UID string `json:"user_id"` } var body = Body { UID: user_id, } data, err := json.Marshal(body) //fmt.Println(string(data)) if err == nil { resp, err := http.Post(url, "application/json", strings.NewReader(string(data))) if err != nil { fmt.Println(err) } defer resp.Body.Close() dec := json.NewDecoder(resp.Body) var v map[string]interface{} if err := dec.Decode(&v); err != nil { fmt.Printf("error: %v\n", err) } else { if val, ok := v["user_token"].(string); ok { return val } } } return "" }
$ go build openplug.go $ ./openplug MKD12343405300 on $ ./openplug MKD12343405300 off
[编辑] 3.1.3 OTA
$ ./openplug MKD12343405300 ota
[编辑] 3.2 node.js
[编辑] 3.2.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.2.2 Turn off
Turn off the plug throught node.js:
- replace the mqtt_uname in line 6
- replace the devid in line 7
$ cat switch.js #! /usr/bin/env node // replace it to yours //////////////////////////////////////////////// var mqtt_uname = "o7okr76757879RfhBs_XM339"; <---- YOUR_mqtt_uname var devid = "MJD123456789012"; <---- YOUR_devid //////////////////////////////////////////////// var opt = { username: mqtt_uname, password: "" }; var mqtt_tx_topic = 'app2dev/' + devid var args = process.argv.slice(2); var set_state = args[0]; if (set_state != 'on' && set_state != 'off') { console.log("Supported cmd:"); console.log("\tswitch.js on"); console.log("\tswitch.js off"); process.exit(1); } console.log("st = " + set_state); 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; //console.log(opt) var MQTTClient = require('/usr/local/lib/node_modules/MQTTClient').Client; var client = new MQTTClient('mqtt.noduino.org', 1883, opt); client.connect(function () { console.log("connect ok!"); var pubopt = { qos_level: 2 } client.publish(mqtt_tx_topic, set_state, 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); } } ); $ ./switch.js off
[编辑] 3.2.3 Turn On
Turn on the plug:
$ ./switch.js on
[编辑] 3.2.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.3 python
[编辑] 3.3.1 Prepare
$ python3 --version Python 3.5.3rc1 $ pip3 install paho-mqtt Collecting paho-mqtt Downloading paho-mqtt-1.2.tar.gz (49kB) 100% |████████████████████████████████| 51kB 308kB/s Building wheels for collected packages: paho-mqtt Running setup.py bdist_wheel for paho-mqtt ... done Stored in directory: /home/comcat/.cache/pip/wheels/fa/db/fb/b495e37057e2f40534726b3c00ab26a58fc80fb8d17223df07 Successfully built paho-mqtt Installing collected packages: paho-mqtt Successfully installed paho-mqtt-1.2
More infor: https://pypi.python.org/pypi/paho-mqtt
[编辑] 3.3.2 Turn off
- replace the mqtt_uname in line 10
- replace the devid in line 11
$ cat switch.py #!/usr/bin/python3 import json, requests import paho.mqtt.client as mqtt import sys # replace it to your id ########################################### mqtt_uname = 'o7okr76757879RfhBs_XM339' <---- YOUR_mqtt_uname devid = 'MJD123456789012' <---- YOUR_devid ########################################### def help_msg(): print("Supported cmd:") print("\tswitch.py on") print("\tswitch.py off") if len(sys.argv) >= 2: set_state = sys.argv[1] else: help_msg() sys.exit(-2) if set_state != 'on' and set_state != 'off': help_msg() sys.exit(-2) url = 'http://api.noduino.org/user/token' dat = dict ( user_id = mqtt_uname, ) try: resp = requests.post(url = url, json = dat) except Exception as e: print(e) sys.exit(-1) p = resp.json() mqtt_pass = p['user_token'] mqtt_rx_topic = 'dev2app/' + devid mqtt_tx_topic = 'app2dev/' + devid mqtt_host = 'mqtt.noduino.org' mqtt_port = 1883 mqtt_msg = set_state def on_connect(client, u_dat, rc): if rc == 0: print("Connected successfully") else: print("Connection failed. rc = "+str(rc)) def on_publish(client, u_dat, rc): print("Message "+str(rc)+" published.") def on_subscribe(client, u_dat, mid, qos): print("Subscribe with "+str(mid)+" received") def on_message(client, udat, msg): print("Message received on topic "+msg.topic+" and payload "+str(msg.payload)) mqttc = mqtt.Client() mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_message = on_message mqttc.username_pw_set(mqtt_uname, mqtt_pass) mqttc.connect(mqtt_host, mqtt_port) mqttc.publish(mqtt_tx_topic, mqtt_msg) #mqttc.subscribe(mqtt_rx_topic) #mqttc.loop_forever()
$ ./switch.py off
[编辑] 3.3.3 Turn On
Turn on the plug:
$ ./switch.py on
[编辑] 3.4 HTML5
The device control page in WeChat is a H5 page. Using a MQTT client implement in javascript , using the mqttws31.js 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 Open API (HTTP in LAN)
Note: the device security policy using the WiFi router's password, that is: the users who are authorized to connect to the WiFi router, is a trusted user
Device start a HTTP service by default. You can send the following HTTP request to turn on or turn off the device. This way independent of MQTT cloud service can be used in local area network when you don't want to make device access Internet.
[编辑] 4.1 Get the device IP
You can use the Fing app in iOS to scan the LAN network.
The device named Espressif is your OpenPlug device:
[编辑] 4.2 Turn On via HTTP in LAN
192.168.1.75 is the openplug device IP
Turn On:
$ cat plug-on.txt POST /upnp/control/basicevent1 HTTP/1.1 Host: 192.168.1.69 Accept: */* Content-type: text/xml; charset="utf-8" Content-Length: 83 <?xml version="1.0" encoding="utf-8"?><s:Body><BinaryState>1</BinaryState></s:Body> $ cat plug-on.txt | nc 192.168.1.75 80 HTTP/1.0 200 OK Server: lwIP/1.4.0 Content-type: text/plain Content-Length: 0 Connection: close
[编辑] 4.3 Turn Off via HTTP in LAN
192.168.1.75 is the openplug device IP
Turn Off:
$ cat plug-off.txt POST /upnp/control/basicevent1 HTTP/1.1 Accept: */* Content-type: text/xml; charset="utf-8" Content-Length: 83 <?xml version="1.0" encoding="utf-8"?><s:Body><BinaryState>0</BinaryState></s:Body> $ cat plug-off.txt | nc 192.168.1.75 80 HTTP/1.0 200 OK Server: lwIP/1.4.0 Content-type: text/plain Content-Length: 0 Connection: close
[编辑] 5 HomeBridge
To use the Siri to control the OpenPlug :)
[编辑] 6 Alexa
Support Amazon Alexa:
[编辑] 6.1 Discover
[编辑] 6.2 Turn On
[编辑] 6.3 Turn Off
[编辑] 6.4 Debug
UPNP discovery:
comcat@jackslab:/work/alexa/test$ cat dis.txt M-SEARCH * HTTP/1.1 HOST: 239.255.255.250:1900 MAN: "ssdp:discover" ST: urn:Belkin:device:** MX: 3 comcat@jackslab:/work/alexa/test$ socat -T1 STDIO UDP4-DATAGRAM:239.255.255.250:1900 < dis.txt HTTP/1.1 200 OK EXT: CACHE-CONTROL: max-age=86400 SERVER: Noduino/1.0 UPNP/1.1 OpenPlug/2.0 USN: uuid:38323636-4558-4dda-9188-cda0810b2e50 ST: urn:Belkin:device:** LOCATION: http://192.168.1.195:80/setup.xml
[编辑] 7 Hacking
[编辑] 7.1 Prepare firmware
Get noduino-sdk:
$ git clone --recursive git://github.com/noduino/noduino-sdk.git noduino-sdk
Generate toolchain (you need Python 2.7):
$ cd noduino-sdk/toolchain $ ./gen.py
Compile Open Plug US version firmware:
$ cd ../sketch/open-plug-us $ make
Compile Open Plug (China Version) 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
[编辑] 7.2 Upload
[编辑] 7.2.1 Serial
- 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
[编辑] 7.2.2 Online
- 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
- Select your device and the input "ota" in the data aera then click "send data"
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
[编辑] 8 Hardware
[编辑] 8.1 China Version
[编辑] 8.2 US Version
[编辑] 9 Reference
- For more information please refer to