1 LED灯具自动化-德赢Vwin官网 网
×

LED灯具自动化

消耗积分:0 | 格式:zip | 大小:0.07 MB | 2022-12-05

王璐

分享资料个

描述

介绍:

在这个项目中,我尝试在房间灯熄灭时打开 LED。我使用 Python 控制 LED 并从螺栓云中检索数据。机器学习技术,z 分数分析,已用于计算数据的上限和下限。如果传感器值超出范围,则会向我的邮箱发送一封电子邮件,并根据情况打开或关闭 LED。

Z 分数分析:

在 z 分数分析中,上限和下限会随着数据不断变化,因此即使异常值低于先前记录的值,也可以检测到异常。它具有三个因素:

 

poYBAGN6xyiAHzcvAAAiM086aww357.png
 

Mn = r 值的平均值

Zn=z分数

Tn=阈值

硬件连接

 
 
 
pYYBAGOIMkSAZD0sAAobs_nge1E169.jpg
 
1 / 3面包板连接
 

 

面包板分为两半,一半中的所有点都相互连接,两条不同的线或不同的一半上的两个点彼此不连接。在面包板上,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 秒。

工作模式:

下面的图片

 
 
 
poYBAGOIMmGAFMQ-AEI-6ixGfAg158.jpg
 
1 / 7传感器初始读数
 

显示工作项目。

 


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表德赢Vwin官网 网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !

'+ '

'+ '

'+ ''+ '
'+ ''+ ''+ '
'+ ''+ '' ); $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code ==5){ $(pop_this).attr('href',"//www.hzfubeitong.com/m/login/index.html"); return false } if(data.code == 2){ //跳转到VIP升级页面 window.location.href="https://m.elecfans.com/vip/index?aid=" + webid return false } //是会员 if (data.code > 0) { $('body').append(htmlSetNormalDownload); var getWidth=$("#poplayer").width(); $("#poplayer").css("margin-left","-"+getWidth/2+"px"); $('#tips').html(data.msg) $('.download_confirm').click(function(){ $('#dialog').remove(); }) } else { var down_url = $('#vipdownload').attr('data-url'); isBindAnalysisForm(pop_this, down_url, 1) } }); }); //是否开通VIP $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code == 2 || data.code ==5){ //跳转到VIP升级页面 $('#vipdownload>span').text("开通VIP 免费下载") return false }else{ // 待续费 if(data.code == 3) { vipExpiredInfo.ifVipExpired = true vipExpiredInfo.vipExpiredDate = data.data.endoftime } $('#vipdownload .icon-vip-tips').remove() $('#vipdownload>span').text("VIP免积分下载") } }); }).on("click",".download_cancel",function(){ $('#dialog').remove(); }) var setWeixinShare={};//定义默认的微信分享信息,页面如果要自定义分享,直接更改此变量即可 if(window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'){ var d={ title:'LED灯具自动化',//标题 desc:$('[name=description]').attr("content"), //描述 imgUrl:'https://'+location.host+'/static/images/ele-logo.png',// 分享图标,默认是logo link:'',//链接 type:'',// 分享类型,music、video或link,不填默认为link dataUrl:'',//如果type是music或video,则要提供数据链接,默认为空 success:'', // 用户确认分享后执行的回调函数 cancel:''// 用户取消分享后执行的回调函数 } setWeixinShare=$.extend(d,setWeixinShare); $.ajax({ url:"//www.hzfubeitong.com/app/wechat/index.php?s=Home/ShareConfig/index", data:"share_url="+encodeURIComponent(location.href)+"&format=jsonp&domain=m", type:'get', dataType:'jsonp', success:function(res){ if(res.status!="successed"){ return false; } $.getScript('https://res.wx.qq.com/open/js/jweixin-1.0.0.js',function(result,status){ if(status!="success"){ return false; } var getWxCfg=res.data; wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId:getWxCfg.appId, // 必填,公众号的唯一标识 timestamp:getWxCfg.timestamp, // 必填,生成签名的时间戳 nonceStr:getWxCfg.nonceStr, // 必填,生成签名的随机串 signature:getWxCfg.signature,// 必填,签名,见附录1 jsApiList:['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function(){ //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 wx.onMenuShareTimeline({ title: setWeixinShare.title, // 分享标题 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享给朋友”按钮点击状态及自定义分享内容接口 wx.onMenuShareAppMessage({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 type: setWeixinShare.type, // 分享类型,music、video或link,不填默认为link dataUrl: setWeixinShare.dataUrl, // 如果type是music或video,则要提供数据链接,默认为空 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ”按钮点击状态及自定义分享内容接口 wx.onMenuShareQQ({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口 wx.onMenuShareWeibo({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口 wx.onMenuShareQZone({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); }); }); } }); } function openX_ad(posterid, htmlid, width, height) { if ($(htmlid).length > 0) { var randomnumber = Math.random(); var now_url = encodeURIComponent(window.location.href); var ga = document.createElement('iframe'); ga.src = 'https://www1.elecfans.com/www/delivery/myafr.php?target=_blank&cb=' + randomnumber + '&zoneid=' + posterid+'&prefer='+now_url; ga.width = width; ga.height = height; ga.frameBorder = 0; ga.scrolling = 'no'; var s = $(htmlid).append(ga); } } openX_ad(828, '#berry-300', 300, 250);