基于阿里云端SDK-python的开发,控制设备的LED
基于阿里云端SDK-python的开发
这里是配合上一批文章,在上文中ESP8266作为设备,订阅了DATA这个topic,本文章将通过实现通过该topic发送数据给ESP8266 来控制LED的熄灭!
SDK的开发及下载参考https://help.aliyun.com/document_detail/42700.html?spm=a2c4g.11186623.6.697.59a036dbksZ9ok
主要提供4个版本的,这里我们使用python来开发该云端。
代码如下,添加了2个按键,来控制LED的熄灭,实际操作的时候会看到ESP8266上的LED对应的状态切换
from aliyunsdkcore import client
from aliyunsdkiot.request.v20180120 import RegisterDeviceRequest
from aliyunsdkiot.request.v20180120 import PubRequest
import base64
import time
import Tkinter
import tkMessageBox
#init sdk
accessKeyId = 'yourselfkeyid' #换成自己的
accessKeySecret = 'yourselfsecret' #换成自己的
clt = client.AcsClient(accessKeyId, accessKeySecret, 'cn-shanghai')
LED_ON=base64.encodestring('LED_ON') #your send out data here
LED_OFF=base64.encodestring('LED_OFF') #your send out data here
##request set
request = PubRequest.PubRequest()
request.set_accept_format('json')
request.set_ProductKey('a19bI5MaWV6') #换成自己的productkey
request.set_TopicFullName('/a19bI5MaWV6/ESP01/DATA') #换成自己的topic
request.set_Qos(0)
#FROUNT PANEL
top = Tkinter.Tk()
def LED_ON_Back():
tkMessageBox.showinfo( "1", "LED_ON")
request.set_MessageContent(LED_ON)
result = clt.do_action_with_exception(request)
print 'result : ' + result
def LED_OFF_Back():
tkMessageBox.showinfo( "1", "LED_OFF")
request.set_MessageContent(LED_OFF)
result = clt.do_action_with_exception(request)
print 'result : ' + result
B = Tkinter.Button(top, text ="LED_ON", command = LED_ON_Back)
C = Tkinter.Button(top, text ="LED_OFF", command = LED_OFF_Back)
B.pack()
C.pack()
top.mainloop()
运行之后界面如下