亲,“电路城”已合并升级到更全、更大、更强的「新与非网」。点击查看「新与非网」

本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页

基于物联网的室内光监控系统

发布时间:2021-05-08
分享到:

基于物联网的室内光监控系统

发布时间:2021-05-08
分享到:

该系统可得到房间内光的强度值,且当房间的灯关了时,有人未经你的允许就进入你的房间,该系统就会向你的手机上发送警报。

原理:

一整天,房间里的光线会随着太阳的升起和落下而变化。这种变化将是缓慢的,并且系统的界限将改变以匹配这种变化。但是当有人打开或关闭房间里的灯时,房间里的光强会突然变化。因此,系统会检测到异常,并迅速提醒您有人在您的房间里。

所需硬件:

  • 物联网模块
  • 微型USB电缆
  • LDR(顶部带有红色波形盘的双腿设备)
  • 330Ω电阻器(橙色、棕色、金色)

 连接:

将LDR的一根导线插入螺栓模块的3v3引脚,将LDR的第二根导线插入A0引脚。

将330Ω电阻器的一个引脚插入GND引脚,并将电阻器的第二个引脚也插入A0引脚。

(3.3V和GND引脚或从它们出来的导线会相互接触。如果在没有电阻器的情况下将电源短路到地,即使是偶然,所汲取的电流也可能高到破坏物联网模块)

编码:

1)为了监控光强,将使用Python语言,打开python编辑器,输入以下配置参数

需要在Twilio和Bolt Cloud上创建一个帐户。

用新的凭据替换上述所有值。可以在Twilio仪表板中找到前四个值,在Bolt Cloud仪表板中找到后两个值。

可以将FRMAE_SIZE设置为10,将MUL_FACTOR设置为6。完成后,可以通过按“CTRL+x”来保存配置文件。

2)再创建一个文件。该文件将包含主要代码

import conf, json, time, math, statistics
from boltiot import Sms, Bolt
def compute_bounds(history_data,frame_size,factor):
    if len(history_data)<frame_size :
        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(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[]

while True:
    response = mybolt.analogRead('A0')
    data = json.loads(response)
    if data['success'] != 1:
        print("There was an error while retriving 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 e:
        print("There was an error while parsing the response: ",e)
        continue

    bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR)
    if not bound:
        required_data_count=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 ("The light level increased suddenly. Sending an SMS.")
            response = sms.send_sms("Someone turned on the lights")
            print("This is the response ",response)
        elif sensor_value < bound[1]:
            print ("The light level decreased suddenly. Sending an SMS.")
            response = sms.send_sms("Someone turned off the lights")
            print("This is the response ",response)
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

主代码概述:

1)从螺栓装置获取最新的传感器值。

2)将传感器值存储在列表中,该列表将用于计算z分数。

3)计算正常和异常读数的z分数以及阈值上限和下限。

4)检查传感器读数是否在正常读数范围内。

5)如果不在范围内,发送短信。

6)等待10秒。

7)从步骤1开始重复。

该系统使用z-score算法来动态更改手机号码上发送短信提醒的界限,当慢慢地移动光源靠近或远离LDR时,界限也开始慢慢地改变。但是把光源移动到离LDR很近或很远的地方时,边界的变化不够快,系统会检测到异常并发出短信提醒。

加入微信技术交流群

技术交流,职业进阶

关注与非网服务号

获取电子工程师福利

加入电路城 QQ 交流群

与技术大牛交朋友

讨论