這個項目能夠通過電子郵件或短信通知溫度突然變化或超出定義閾值的設(shè)備。
構(gòu)建項目
第 1 步:將 LM35 連接到螺栓
將 LM35 的 GND 引腳連接到 Bolt 設(shè)備的 GND 引腳。
將 LM35 的模擬輸出引腳連接到 Bolt 設(shè)備的 A0(模擬輸入)引腳。
連接 LM35 的 VCC 引腳連接到 Bolt 設(shè)備的 5v。
第 2 步:配置 Bolt IoT
要為這個項目創(chuàng)建一個產(chǎn)品,首先登錄到BOLT CLOUD
單擊“產(chǎn)品”選項卡,然后創(chuàng)建產(chǎn)品
對于這個項目
我選擇 Input Devices 選項和 GPIO 來收集數(shù)據(jù)
單擊配置此產(chǎn)品以配置產(chǎn)品
單擊螺栓的“A0”引腳并命名變量
現(xiàn)在單擊代碼選項卡并寫下代碼
第 3 步:可視化
現(xiàn)在為了制作預(yù)測圖,我們使用 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’);
現(xiàn)在查看可視化
每 5 分鐘計算一次數(shù)據(jù),因此請等待 20-25 分鐘。
注意:現(xiàn)在您還可以通過單擊 Bolt 設(shè)備名稱來使用您的BOLT IOT android 應(yīng)用程序和 ios 應(yīng)用程序 f或 Visualization
第 4 步:使用 Z 分?jǐn)?shù)分析和通知進(jìn)行異常檢測
對于這個項目,我使用了 Oracle VM Virtual Box,您也可以使用 Digital Ocean。現(xiàn)在制作 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 分析進(jìn)行異常檢測并運(yùn)行它
python3異常檢測.py
Python代碼附在本文下面
如果發(fā)生異常或溫度超過閾值,則會使用Twilio和Mailgun發(fā)送 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)
評論
查看更多