RAPTI CHAUDHURI
Published © GPL3+

Anomaly Detection through Sms, Email, Whatsapp and PhoneCall

LM35 sensor records the temperature inside the fridge and if an anomaly occurs, an alert sms, call, mail, whatsapp is immediately received.

IntermediateProtip4 hours1,030
Anomaly Detection through Sms, Email, Whatsapp and PhoneCall

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LM35 Temperature Sensor
×1
Jumper Wires Male to Female
×1

Software apps and online services

Digital Ocean
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Code

anomaly_detection.py

Python
It is the code for Z-score analysis. When anomaly is detected it prints an alert message and an SMS as well as an alert email is also received.
import conf, json, time, math, statistics
from boltiot import Sms, Bolt, Email
from twilio.rest import Client
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)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOXURL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)
history_data=[]
client = Client(conf.SSID, conf.AUTH_TOKEN)
  
  while True:
    rtextsms = mybolt.analogRead('A0')
    remail = mybolt.analogRead('A0')
    data = json.loads(rtextsms)
    if data['success'] != '1':
        print("There was an error while retriving the data.")
        print("This is the error:"+data['value'])
        time.sleep(5)
        continue
    sensor_value1 = int(data['value'])
    sensor_value1 = sensor_value1/10.24
    print ("The current temperature of your Refrigerator is "+str(sensor_value1) + " degree celsius" And the sensor Value is "+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(5)
        continue
    try:
        if sensor_value > bound[0] :
            celsius_value = sensor_value/10.24
            print ("Anomaly Detected! Someone definitely opened the fridge door. We are sending SMS, Mail, Whatsapp message and arranging a phone call")
            rtextsms = sms.send_sms("Anomaly Detected! Someone definitely opened the fridge door. The current temperature is " + str(celsius_value) + "degree celsius")
            message = client.messages.create(
                              body = 'Anomaly Detected! Someone definitely opened the fridge door. The current temperature is' + str(celsius_value)+ ' degree celsius',
                              from = 'whatsapp:+14155238886'
                              to = 'whatsapp:+917005507703'
                       )
             print(message.sid)
           call = client.calls.create(
                        url = 'http://139.59.42.205/voice.xml',
                        to = '+917005507703'
                        from ='+16672225480'
                   )
             print(call.sid)
  
            remail = mailer.send_email("Anomaly Detected!", "Someone opened the fridge door. The temperature of your refrigerator hasbeen increased suddenly. The current temperature is" +str(celsius_value) " degree celsius")
            print("This is the response for SMS ", rtextsms)
            print("This is the response for EMAIL",remail)
        history_data.append(sensor_value)
     except Exception as e:
        print ("Error",e)
     time.sleep(5)

conf.py

Python
It is the code for SMS configuration.
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'
MAILGUN_API_KEY = 'You can find in your Mailgun dashboard'
SANDBOX_URL = 'You can find in your Mailgun dashboard'
SENDER_MAIL = 'Mail address of Mailgun. You can find in your dashboard'
RECIPIENT_MAIL = 'Write Your mail'
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

voice.py

XML
We use this code for voice alert text to speech line
<Response>
<Say voice="alice">
Anomaly Detected! Someone definitely Opened the Fridge Door. The current temperature is increasing. Please take remedial step quickly.
</Say>
</Response>

Credits

RAPTI CHAUDHURI
1 project • 2 followers

Comments