插座介绍http://www.reco4life.com/bbs/portal.php 自己看去或者百度
代码有点乱,根据论坛里的脚本和官方的API修改拼凑来
代码: 全选
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#ECO 智能插座PYTHON控制
from socket import *
import time
import datetime
import urllib
import urllib2
import re
#Domoticz服务器
domoticzserver = "127.0.0.1:8084"
#定义设备idx值
Current_idx = "32"
Voltage_idx = "33"
Electric_idx = "34"
#此方法向Domoticz服务器发送请求
def domoticzrequest (url):
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read()
HOST = '192.168.100.108'
PORT = 8899
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
data2 = "AT+YZOUT" + '\r' + '\n'
tcpCliSock.send(data2)
data1 = tcpCliSock.recv(BUFSIZ)
#print data1
tcpCliSock.close()
#截取返回值从第5位开始
data1 = data1[4:]
#去除返回值尾部无用数据
data1 = data1.strip()
#把返回以,区分 存入序列
list_data1 = data1.split(',')
#读取序列
num1 = list_data1[0]
num2 = list_data1[1]
num3 = list_data1[2]
num4 = list_data1[3]
num5 = list_data1[4]
num6 = list_data1[6]
#print('list:{0},num1:{1}'.format(list_data1,num1))
dianliu = float(num1) / 100
print "当前电流为:%.2f" %dianliu + "A"
#改变变量类型
Current = str(dianliu)
print Current
dianya = float(num2) / 100
print "当前电压为:%.2f" %dianya + "V"
Voltage = str(dianya)
print Voltage
pinlv = float(num3) / 100
print "当前频率为:%.2f" %pinlv + "Hz"
gonglv = float(num4) / 10
Electric = str(gonglv)
print "当前功率为:%.2f" %gonglv + "W"
print Electric
nengliang = float(num6) / 1
print "当前功率为:%.2f" %nengliang + "WH"
#通过-连接字符串
#str_list = ('-').join(list_data1)
#print('str_list:{0}'.format(str_list))
domoticzrequest("http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+Current_idx+"&nvalue=0&svalue="+Current)
domoticzrequest("http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+Voltage_idx+"&nvalue=0&svalue="+Voltage)
domoticzrequest("http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+Electric_idx+"&nvalue=0&svalue="+Electric)
后面这个是开关控制:
代码: 全选
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#RECO 智能插座PYTHON控制
from socket import *
import time
import datetime
HOST = '192.168.100.108'
PORT = 8899
BUFSIZ = 1024
ADDR = (HOST, PORT)
nowtime = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
data2 = "AT+YZSWITCH=1,ON," + nowtime
#关闭的话把ON改OFFdata2 = "AT+YZSWITCH=1,OFF," + nowtime
tcpCliSock.send(data2)
data1 = tcpCliSock.recv(BUFSIZ)
print data1
tcpCliSock.close()