一、购买设备,这里的设备应该有笔记本散热器,因为是usb通电的,需要一个充电器,由于充电器如果长期通电会影响寿命,于是我想通过智能开关进行控制,我查了一些设备,发现pdd上有一款15元的智能插座可以使用python控制,主要是搜索博联插座,选择价格便宜的。
二、python控制broadlink插座,一开始是将插座连接手机的正常做法设置好,然后进入wifi路由查找ip地址,根据ip地址获取插座的信息。代码如下:
import broadlink
# #显示设备的信息
# devices = broadlink.discover(discover_ip_address='192.168.1.36')
# print(devices)
# #显示设备的信息(简单)
# device = broadlink.hello('192.168.1.36')
# print(device)
device = broadlink.sp4(host=('192.168.1.36',80), mac=bytearray.fromhex('E81656CA934B'), devtype=32021)
device.auth()
state = device.check_power()
print(state)
# 关的代码
# if state == False:
# print( "OFF")
# elif state == True:
# device.set_power(False)
# 开的代码
if state == True:
print( "ON")
elif state == False:
device.set_power(True)
这个E81656CA934B是去掉:的mac地址,其中重要的一个devtype=32021是通过显示设备信息的代码找出来的,用sp4是我探索出来的,因为显示的代码是(忘记了),我用的是sp mini3,有sp2和sp4可以用,一开始用sp2不能控制开关,后来我修改为sp4,就能用了。笔记本用的wifi和智能插座是同个路由器2.4G。
三、获取cpu温度,写入数据库,按每10秒写入一个cpu温度的数据,考虑到智能插座不能频繁开关,我考虑用15分钟去判断cpu的温度,如果温度大于58.5度就开,这个温度是笔记本运行一段时间后用sql语句去分析的,即每90组去求一个平均值,这个平均价大于某个问题就在另外一列显示1,否则显示0,sql代码如下:
;WITH RankedData AS (
SELECT
cput, -- 替换成你想要计算平均值的列名
(ROW_NUMBER() OVER (ORDER BY id) - 1) / 90 AS GroupNum -- 假设SomeOrderColumn是排序依据
FROM
cpu -- 替换成你的表名
),
GroupAverages AS (
SELECT
GroupNum,
AVG(cput) AS AverageValue
FROM
RankedData
GROUP BY
GroupNum
)
SELECT
AverageValue,CASE WHEN AverageValue > 58.5 THEN 1 ELSE 0 END AS openfen
FROM
GroupAverages
ORDER BY
GroupNum; -- 如果你需要按组顺序显示结果
我的表是:
四、python的全程序代码:
# -*- coding: utf-8 -*-
'''
Created on Tue Jun 4 09:04:11 2024
@author: YBK
'''
import clr
import time
import pymssql
import broadlink
from apscheduler.schedulers.blocking import BlockingScheduler
clr.AddReference(r'e:/.spyder-py3/OpenHardwareMonitorLib.dll')
from OpenHardwareMonitor.Hardware import Computer
# e.g. clr.AddReference(r'OpenHardwareMonitor/OpenHardwareMonitorLib'), without .dll
#修改成OpenHardwareMonitorLib.dll文件的实际路径。
def insertdb(sj,cput):
conn = pymssql.connect(server='localhost', user='sa', password='1', database='gr',charset='utf8')
cursor = conn.cursor()
insert_query = "INSERT INTO cpu(sj,cput) VALUES(%s,%s)"
insert_data = (sj,cput)
cursor.execute(insert_query,insert_data)
conn.commit()
cursor.close()
conn.close
def getcput():
c = Computer()
c.CPUEnabled = True # get the Info about CPU
c.GPUEnabled = True # get the Info about GPU
c.Open()
cputs = 0
j = 0
for a in range(0, len(c.Hardware[0].Sensors)):
#print(c.Hardware[0].Sensors[a].Identifier)
if "/temperature" in str(c.Hardware[0].Sensors[a].Identifier):
#print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), c.Hardware[0].Sensors[a].get_Value())
cputs = cputs + c.Hardware[0].Sensors[a].get_Value()
j = j + 1
c.Hardware[0].Update()
insertdb(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), int(cputs/j))
# print(int(cputs/j))
def openfen():
conn = pymssql.connect(server='localhost', user='sa', password='1', database='gr',charset='utf8')
cursor = conn.cursor()
sqlget = "select avg(cput) from cpu where id in (select top 90 id from cpu order by id desc)"
cursor.execute(sqlget)
row = cursor.fetchone()
temp = row[0]
cursor.close()
conn.close
device = broadlink.sp4(host=('192.168.1.36',80), mac=bytearray.fromhex('E81656CA934B'), devtype=32021)
device.auth()
state = device.check_power()
print(round(temp,2))
if temp > 58.5:#温度大于58.5时打开风扇
if state == True:
print("ON pass")
elif state == False:
device.set_power(True)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " ON")
else:
if state == False:
print("OFF pass")
elif state == True:
device.set_power(False)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " OFF")
getcput()
openfen()
sched = BlockingScheduler()
sched.add_job(getcput, 'interval', seconds=10,)
sched.add_job(openfen, 'interval', minutes=15,)
sched.start()
运行的情况: