创建有助于实现以下目标的设备:
Capstone 项目的目标如下。
A. 使用 Bolt 和 LM35 传感器构建温度监测系统电路。
B. 在 Bolt Cloud 上创建一个产品,以监控来自 LM35 的数据,并将其链接到您的 Bolt。
C. 编写产品代码,对 Bolt 发送的数据运行多项式回归算法。
带着这个目标,奈杰尔先生成功地满足了政府设定的第一个条件。使用预测数据,只要图表预测温度将保持在 -33 和 -30 摄氏度范围内超过 20 分钟,他就能够及早采取行动。
代码 :
setChartLibrary('google-chart');
setChartTitle('Polynomial Regression');
setChartType('predictionGraph');
setAxisName('time_stamp','temp');
mul(0.0977);
plotChart('time_stamp','temp');
D. 将温度监测电路保持在冰箱内,关闭冰箱门,让系统记录温度读数约 2 小时。
E. 使用您在 2 小时内收到的读数,设置冰箱内温度的界限。
F. 编写一个 Python 代码,每 10 秒获取一次温度数据,如果温度超出您在目标“E”中确定的温度阈值,则发送电子邮件警报。
打开ubuntu服务器。
创建一个文件来存储凭据:
sudo nano email_conf.py
输入以下代码。
MAILGUN_API_KEY = 'This is the private API key which you can find on your Mailgun Dashboard'
SANDBOX_URL= 'You can find this on your Mailgun Dashboard'
SENDER_EMAIL = 'This would be test@your SANDBOX_URL'
RECIPIENT_EMAIL = 'Enter your Email ID Here'
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
创建主代码文件:
sudo nano capstone_project.py
输入以下代码。
import email_conf, json, time, math, statistics
from boltiot import Email, Bolt
max_limit = 52
min_limit = -52
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'])
if sensor_value > max_limit or sensor_value < min_limit:
print("Making request to Mailgun to send an email")
temperature = (100*sensor_value)/1024
response = mailer.send_email("Alert!!", "The temperature of the refrigerator is " +str(temperature))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
except e:
print("There was an error while parsing the response: ",e)
continue
G. 修改 Python 代码,同时进行 Z 分数分析,并在检测到异常时打印“有人打开冰箱门”这一行。
H. 调整 Z-score 分析代码,当有人打开冰箱门时,它会检测到异常。
最终代码:
import email_conf, json, time, math, statistics
from boltiot import Email, Bolt
max_limit = 52
min_limit = -52
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)
mailer = 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 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'])
if sensor_value > max_limit or sensor_value < min_limit:
print("Making request to Mailgun to send an email")
temperature = (100*sensor_value)/1024
response = mailer.send_email("Alert!!", "The temperature of the refrigerator is " +str(temperature))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
except 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] or sensor_value < bound[1]:
print ("Someone has opened the refrigerator door.")
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(10)
运行代码:
sudo python3 capstone_project.py
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !