这个项目能够通过电子邮件或短信通知温度突然变化或超出定义阈值的设备。
构建项目
第 1 步:将 LM35 连接到螺栓
将 LM35 的 GND 引脚连接到 Bolt 设备的 GND 引脚。
将 LM35 的vwin 输出引脚连接到 Bolt 设备的 A0(模拟输入)引脚。
连接 LM35 的 VCC 引脚连接到 Bolt 设备的 5v。
第 2 步:配置 Bolt IoT
要为这个项目创建一个产品,首先登录到BOLT CLOUD
单击“产品”选项卡,然后创建产品
对于这个项目
我选择 Input Devices 选项和 GPIO 来收集数据
单击配置此产品以配置产品
单击螺栓的“A0”引脚并命名变量
现在单击代码选项卡并写下代码
第 3 步:可视化
现在为了制作预测图,我们使用 Google Chart 在 Bolt Cloud 中编写 JavaScript 代码
setChartLibrary(‘google-chart’);
setChartTitle(‘Temperature Prediction For Pharmaceuticals’);
setChartType(‘predictionGraph’);
setAxisName(‘Time’,‘Temperature’);
mul(0.0977);
plotChart(‘time_stamp’,‘temp’);
现在查看可视化
每 5 分钟计算一次数据,因此请等待 20-25 分钟。
注意:现在您还可以通过单击 Bolt 设备名称来使用您的BOLT IOT android 应用程序和 ios 应用程序 f或 Visualization
第 4 步:使用 Z 分数分析和通知进行异常检测
对于这个项目,我使用了 Oracle VM Virtual Box,您也可以使用 Digital Ocean。现在制作 conf.py ,其中包含Twilio(登录并使用 API)和Bolt Cloud帐户的信息,例如
SSID = "You can find SSID in your Twilio Dashboard "
AUTH_TOKEN = "You can find on your Twilio Dashboard"
FROM_NUMBER = "This is the no. generated by Twilio. You can find this on your Twilio Dashboard"
TO_NUMBER = "This is your number. Make sure you are adding +91 in beginning"
API_KEY = "This is your Bolt Cloud account API key"
DEVICE_ID = "This is the ID of your Bolt device"
FRAME_SIZE = 10
MUL_FACTOR = 6
同样对于Mailgun电子邮件,制作email_conf.py
MAILGUN_API_KEY = "your Mailgun private API"
SANDBOX_URL = "your sandbox url"
SENDER_EMAIL = "test@your sandbox url"
RECIPIENT_EMAIL = "your mail id"
API_KEY = "Bolt API key"
DEVICE_ID = "Your Device ID"
FRAME_SIZE = 10
MUL_FACTOR = 6
制作 anomaly_detection.py,我们在其中编写 python 代码以使用 z-score 分析进行异常检测并运行它
python3异常检测.py
Python代码附在本文下面
如果发生异常或温度超过阈值,则会使用Twilio和Mailgun发送 SMS 和邮件警报。
Twilio 的短信和 Mailgun 的电子邮件截图
屏幕截图
anomaly_detection.py:
import conf, email_conf, json, time, math, statistics
from boltiot import Sms, Email, 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]
minimum_limit = 7 #can be change
maximum_limit = 40 #can be change
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)
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("The sensor value is"+(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 > maximum_limit or sensor_value < minimum_limit:
response = mailer.send_email("Alert", "The Current temperature is beyond the threshold ")
if sensor_value > bound[0]:
print("The temperature increased suddenly.Sending Sms.")
response = sms.send_sms("Someone opened the chamber")
print("This is the response", response)
elif sensor_value < bound[1]:
print("The temperature decreased suddenly. Sending an email.")
response = mailer.send_email("Someone opened the chamber")
print("This is the response", response)
history_data.append(sensor_value)
except Exception as e:
print("Error", e)
time.sleep(10)
评论
查看更多