|
【KW41z】公网访问Thread网络终端去撸灯
[复制链接]
本帖最后由 ljj3166 于 2017-7-5 11:26 编辑
用外网去访问Thread网络
是楼主设想中非常重要的一部分
Thread网络的终端,获取到的都是IPv6地址
在天朝的网络环境中,想获取到IPv6地址比获得一个公网IP估计还要困难
目前貌似只有教育网有IPv6的部署环境
虽然有各种4to6,6to4的管道
但是实在是麻烦,还跑题
so,直接想办法搞事
前面两贴
基本上完成了在树莓派上跑边界路由器的功能
所以考虑通过现网IPv4来访问路由,从而与Thread网络通信
有了RPI做平台,可玩度比较高了
原本是想在RPI上做个web服务器,然后使用DDNS进行外网访问
后来想到各种html、各种XML的煎熬
果断放弃
不是还有各种IOT云平台吗?
度娘了一下,yeelink和乐为互联用得比较多啊
尤其是yeelink,大把树莓派在上面跑
可是这几天yeelink死活登不上,是不是要倒闭了?(貌似现在又能登上了)
直接用乐联网吧
找了半天,居然找不到有人把RPI撸上乐联网
我类个去,自己写吧
上python码
- #!/usr/bin/python
- import socket
- import time
- import threading
- import os
- from threading import Timer
- HOST='tcp.lewei50.com'
- PORT=9960
- IO_Status=0x00
- cmd_device='{"method": "update","gatewayNo": "01","userkey": "e7cf2e663c8648cb9b82e2cc619934d5"}&^!'
- cmd_confirm='{"f":"message","p1":"ok"}&^!'
- cmd_device_check='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"getAllSensors"}&^!'
- cmd_device_check_confirm='{"method": "response","result": {"successful": true,"message": "device1 on","data":[{"id":"01","value":"1"},{"id":"02","value":"1"},{"id":"03","value":"1"},{"id":"04","value":"1"},{"id":"05","value":"1"}]}}&^!'
- cmd_device1_on='{"method": "response","result": {"successful": true,"message": "device1 on","data":[{"id":"01","value":"1"}]}}&^!'
- cmd_device1_off='{"method": "response","result": {"successful": true,"message": "device1 off","data":[{"id":"01","value":"0"}]}}&^!'
- cmd_device1_control_on='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"01","p2":"1"}&^!'
- cmd_device1_control_off='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"01","p2":"0"}&^!'
- cmd_device2_on='{"method": "response","result": {"successful": true,"message": "device2 on","data":[{"id":"02","value":"1"}]}}&^!'
- cmd_device2_off='{"method": "response","result": {"successful": true,"message": "device2 off","data":[{"id":"02","value":"0"}]}}&^!'
- cmd_device2_control_on='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"02","p2":"1"}&^!'
- cmd_device2_control_off='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"02","p2":"0"}&^!'
- cmd_device3_on='{"method": "response","result": {"successful": true,"message": "device3 on","data":[{"id":"03","value":"1"}]}}&^!'
- cmd_device3_off='{"method": "response","result": {"successful": true,"message": "device3 off","data":[{"id":"03","value":"0"}]}}&^!'
- cmd_device3_control_on='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"03","p2":"1"}&^!'
- cmd_device3_control_off='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"03","p2":"0"}&^!'
- cmd_device4_on='{"method": "response","result": {"successful": true,"message": "device4 on","data":[{"id":"04","value":"1"}]}}&^!'
- cmd_device4_off='{"method": "response","result": {"successful": true,"message": "device4 off","data":[{"id":"04","value":"0"}]}}&^!'
- cmd_device4_control_on='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"04","p2":"1"}&^!'
- cmd_device4_control_off='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"04","p2":"0"}&^!'
- cmd_device5_on='{"method": "response","result": {"successful": true,"message": "device5 on","data":[{"id":"05","value":"1"}]}}&^!'
- cmd_device5_off='{"method": "response","result": {"successful": true,"message": "device5 off","data":[{"id":"05","value":"0"}]}}&^!'
- cmd_device5_control_on='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"05","p2":"1"}&^!'
- cmd_device5_control_off='{"method":"send","gatewayNo":"01","userkey":"e7cf2e663c8648cb9b82e2cc619934d5","f":"updateSensor","p1":"05","p2":"0"}&^!'
- def keepalive():
- s.sendall(cmd_device)
- print("keepalive")
- t = Timer(15, keepalive)
- t.start()
- def CMD_SET():
- global IO_Status
- global cmd_device_check_confirm
- cmd_device_check_confirm='{"method": "response","result": {"successful": true,"message": "device1 on","data":['
- if IO_Status&0x01:
- cmd_device_check_confirm='{"method": "response","result": {"successful": true,"message": "device1 on","data":['+'{"id":"01","value":"1"},'
- else:
- cmd_device_check_confirm='{"method": "response","result": {"successful": true,"message": "device1 on","data":['+'{"id":"01","value":"0"},'
- if IO_Status&0x02:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"02","value":"1"},'
- else:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"02","value":"0"},'
- if IO_Status&0x04:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"03","value":"1"},'
- else:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"03","value":"0"},'
- if IO_Status&0x08:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"04","value":"1"},'
- else:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"04","value":"0"},'
- if IO_Status&0x10:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"05","value":"1"}]}}&^!'
- else:
- cmd_device_check_confirm=cmd_device_check_confirm+'{"id":"05","value":"0"}]}}&^!'
- def data_proc():
- global IO_Status
- global t
- data=s.recv(1024)
- if data==cmd_confirm:
- print("device confirm")
- if data==cmd_device_check:
- s.sendall(cmd_device_check_confirm)
- print("devices status check")
- if data==cmd_device1_control_on:
- IO_Status=IO_Status|0x01
- s.sendall(cmd_device1_on)
- print("set device1 on")
- if data==cmd_device1_control_off:
- IO_Status=IO_Status&0xFE
- s.sendall(cmd_device1_off)
- print("set device1 off")
- if data==cmd_device2_control_on:
- IO_Status=IO_Status|0x02
- s.sendall(cmd_device2_on)
- print("set device2 on")
- if data==cmd_device2_control_off:
- IO_Status=IO_Status&0xFD
- s.sendall(cmd_device2_off)
- print("set device2 off")
- if data==cmd_device3_control_on:
- IO_Status=IO_Status|0x04
- s.sendall(cmd_device3_on)
- print("set device3 on")
- if data==cmd_device3_control_off:
- IO_Status=IO_Status&0xFB
- s.sendall(cmd_device3_off)
- print("set device3 off")
- if data==cmd_device4_control_on:
- IO_Status=IO_Status|0x08
- s.sendall(cmd_device4_on)
- print("set device4 on")
- if data==cmd_device4_control_off:
- IO_Status=IO_Status&0xF7
- s.sendall(cmd_device4_off)
- print("set device4 off")
- if data==cmd_device5_control_on:
- IO_Status=IO_Status|0x10
- s.sendall(cmd_device5_on)
- print("set device5 on")
- if data==cmd_device5_control_off:
- IO_Status=IO_Status&0xEF
- s.sendall(cmd_device5_off)
- print("set device5 off")
- s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- s.connect((HOST,PORT))
- s.sendall(cmd_device)
- keepalive()
- while 1:
- CMD_SET()
- data_proc()
- s.close()
复制代码
怪不得在乐联网上没人撸RPI
不是直接POST、GET啊
要自己撸一个TCP/IP Client
然后根据协议去撸码通信
需要注意的是,要保持一个协议层的心跳,否则会被平台踢下线
先看看整个系统框架吧
乐为IOT平台--------------- 以太网-------------树莓派--- usb---KW41z Platform ))))))) 咻咻咻)))))))) KW41z Platform
上视频
视频中,一块KW41z USB连接至树莓派,组成一个伪边界路由器,其实是把数据echo到tty设备
高手来鄙视楼主吧
另一块通过树莓派进行供电
两块KW41z一块create网络,一块作为终端join
通过乐为IOT平台反向控制对终端进行撸灯
暂时先写这么多
此内容由EEWORLD论坛网友ljj3166原创,如需转载或用于商业用途需征得作者同意并注明出处
|
|