在这个项目中,我尝试在房间灯熄灭时打开 LED。我使用 Python 控制 LED 并从螺栓云中检索数据。机器学习技术,z 分数分析,已用于计算数据的上限和下限。如果传感器值超出范围,则会向我的邮箱发送一封电子邮件,并根据情况打开或关闭 LED。
Z 分数分析:
在 z 分数分析中,上限和下限会随着数据不断变化,因此即使异常值低于先前记录的值,也可以检测到异常。它具有三个因素:
Mn = r 值的平均值
Zn=z分数
Tn=阈值
硬件连接:
面包板分为两半,一半中的所有点都相互连接,两条不同的线或不同的一半上的两个点彼此不连接。在面包板上,LDR 与 10k电阻串联。LDR和电阻之间的公共支路通过黄色线连接到螺栓的“A0”引脚。LDR的另一支路连接到 3v3通过绿线引脚。电阻器通过蓝线连接到 LED 。电阻器和 LED 之间的公共线点连接到螺栓的接地引脚。另一条腿(较长的腿)连接到“0”GPIO螺栓销。
该软件分为两个python程序文件。
1)email_conf.-它包含我的 mailgun 帐户的详细信息、我的螺栓设备名称、api 密钥和乘数以及用于 z 分数分析的帧大小。
API_KEY="XXXXX"
DEVICE_ID="BOLTXXXX"
MAILGUN_API_KEY="XXXXX'
SANDBOX_URL="XXXX"
SENDER_EMAIL="XXXX"
RECIPIENT_EMAIL="XXXX"
MUL_FACTOR=2
FRAME_SIZE=10
2)led_control_by_sensor:这是实现项目的主要代码,即控制LED,获取传感器数据并在传感器值越界时发送电子邮件。
import email_conf,json,time,math,statistics
from boltiot import Bolt,Email
def compute_bounds(history_data,frame_size,factor):
if len(history_data)
return None
if len(history_data)>frame_size :
del history_data[0:len(history_data)-frame_size]
Mn=statistics.mean(history_data)
Variance=0
for data in history_data :
Variance += math.pow((data-Mn),2)
Zn = factor * math.sqrt(Variance / frame_size)
High_bound = history_data[frame_size-1]+Zn
Low_bound = history_data[frame_size-1]-Zn
return [High_bound,Low_bound]
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
email=Email(email_conf.MAILGUN_API_KEY,email_conf.SANDBOX_URL,email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)
history_data=[]
while True:
response = mybolt.analogRead('A0')
data = json.loads(response)
if data['success'] != 1:
print("There was an error while getting the data.")
print("This is the error:"+data['value'])
time.sleep(10)
continue
print ("This is the value "+data['value'])
sensor_value=0
try:
sensor_value = int(data['value'])
except Exception as e:
print("There was an error while parsing the response: ",e)
continue
bound = compute_bounds(history_data,email_conf.FRAME_SIZE,email_conf.MUL_FACTOR)
if not bound:
required_data_count=email_conf.FRAME_SIZE-len(history_data)
print("Not enough data to compute Z-score. Need ",required_data_count," more data points")
history_data.append(int(data['value']))
time.sleep(10)
continue
try:
if sensor_value > bound[0] :
print ("light level increased suddenly. sending email.")
status=mybolt.analogWrite('0','0')
print("status of led is :",status)
response=email.send_email("alert","someone turned on the lights")
response=json.loads(response.text)
print("this is the response from mailgun"+str(response['message']))
elif sensor_value < bound[1] :
print ("light level decreased suddenly. sending email.")
status=mybolt.analogWrite('0','20')
print("status of led is :",status)
response=email.send_email("alert","someone turned off the lights")
response=json.loads(response.text)
print("this is the response from mailgun"+str(response['message']))
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(10)
在上面的程序中,我们包括以下内容:
time-time.sleep() 函数暂停程序 10 秒
json-json.loads() 函数来转换来自 boltiot 和 mailgun 的数据
statistics-statistics.mean() 函数计算均值
math-math.pow() 函数求平方
Email-创建一个它的对象来发送邮件
螺栓连接到设备
在程序中,函数 compute_bounds 计算阈值并返回。它采用以下参数:传感器数据列表、帧大小和倍增因子。如果列表的长度小于帧大小,则返回无,因为所需的点数不可用。它保持列表的大小为10 通过删除多余的起始元素。
然后我创建了 Bolt 和 Email 的对象。运行一个无限循环,我们获取数据,如果发生错误,则显示 它。然后通过调用 compute_bounds 函数获取边界。如果传感器值越过边界,则 LED 会打开或根据情况关闭并发送电子邮件并显示 mailgun 的响应。如果值在范围内,则将其简单地添加到列表中。最后,程序在每次循环后暂停 10 秒。
工作模式:
下面的图片
显示工作项目。
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !