Gunjan yadav
Published © GPL3+

Temperature Sensor Using LM35 Sensor and Bolt WiFi Module

This project uses an LM35 sensor and Bolt WiFi module to measure the temperature change in a refrigerator.

IntermediateFull instructions provided1,954
Temperature Sensor Using LM35 Sensor and Bolt WiFi Module

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LM35 sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Buzzer
Buzzer
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
Any power bank can also be used.
×1

Software apps and online services

Bolt IoT Android App
Bolt IoT Android App
VMware Ubuntu

Story

Read more

Schematics

Temperature_sensor.pdf

Code

anomaly_prediction.py

Python
import conf_anomaly, json, time, math, statistics
from boltiot import Sms, Bolt, Email
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_anomaly.AUTH_TOKEN, conf_anomaly.TO_NUMBER, conf.FROM_NUMBER)
mailer = Email(conf_anomaly.MAILGUN_API_KEY,conf_anomaly.SANDBOXURL,conf_anomaly.SENDER_EMAIL,conf_anomaly.RECIPIENT_EMAIL)
history_data=[]
  
  
while True:    
    response = mybolt.analogRead('A0')    
    data = json.loads(response)    
    if data['success'] != '1':        
        print("There was an error while retrieving 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 Exception as 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 ("Someone has opened fridge. Sending an SMS and email.")
            response = sms.send_sms("Someone has opened the Fridge")            
            response = mail.send_email("Someone has opened Fridge")
            conf_anomaly.call_me()
            conf_anomaly.call_tweet()
            print("This is the response ",response)       
         history_data.append(sensor_value)    
    except Exception as e:        
        print ("Error",e)    
    time.sleep(10)

conf_anomaly.py

Python
import boltiot import Bolt
import time,json, 
import tweepy


#You can find SSID in your Twilio Dashboard
SSID = 'AC7ae69b9370XXXXXXXXXX6978d3ce4ce8'
#You can find AUTH TOKEN on your Twilio Dashboard
AUTH_TOKEN = 'eb4563dfdXXXXXXXXXXadde03ae817bb'
#FROM_NUMBER,This is the no. generated by Twilio. 
FROM_NUMBER = '+13XXXXXXX80'
#TO_NUMBER,This is your number
TO_NUMBER = '+91XXXXXXXX6
3'#This is the private API key which you can find on your Mailgun Dashboard
MAILGUN_API_KEY = '4beed33869c0a7de8b97XXXXXXXXXXXX-060550c6-e591c150'
#SANDBOX_URL,You can find this on your Mailgun Dashboard
SANDBOX_URL= 'sandbox12b9e8227bf34c118XXXXXXXXXXXXXXX.mailgun.org'
#SENDER_EMAIL,This would be test@your, email from where we'll receive mail
SANDBOX_URLSENDER_EMAIL = 'test@sandboxXXXXXXXXXXXXXXX18ccec4718337b3f6.mailgun.org'
#RECIPIENT_EMAIL,Enter your Email ID Here
RECIPIENT_EMAIL = 'cbXXXXXX@gmail.com'
#tweeter consumer key
consumer_key="yTXXXXXXXXXXXXXXXXXXX9vI"
#consumer secret twitter
consumer_secret="aksXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXhng"
access_token="10786XXXXXXXXXXXXXX-BNXXXXXXXXXXXXXXXXXXXXXX"
access_token_secret="DqkXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
#API_KEY,This is your Bolt Cloud account API key
API_KEY = '499d00d0-81f6-XXXX-XXXX-7caaecca6e93'
#DEVICE_ID,This is the ID of your Bolt device
DEVICE_ID = 'BOLTXXXXXXX6'
FRAME_SIZE = 10
MUL_FACTOR = 3

mybolt=Bolt(API_KEY, DEVICE_ID)
#function to trigger the buzzer
def call_me():
    response=mybolt.analogWrite('1','1023')
    time.sleep(5)
    response=mybolt.analogWrite('1','0')

# Dictionary to store credentials as key-value pairs.
config = {
"consumer_key"    : consumer_key,
"consumer_secret"     : consumer_secret,
"access_token"        : access_token,
"access_token_secret" : access_token_secret
}
# Method to authenticate user via Tweepy and return API object
def get_api_object(cfg):
   auth =tweepy.OAuthHandler(cfg['consumer_key'],
                                 cfg['consumer_secret'])
   auth.set_access_token(cfg['access_token'],
                             cfg['access_token_secret'])
   return tweepy.API(auth)
mybolt = Bolt(conf.bolt_cloud_api_key, conf.device_id)

#function to call the tweet on our dashboard
def call_tweet():
    temperature_threshold = 59
    counter=random.random()
    tweet=1
    while tweet:
       response = mybolt.analogRead('A0')
       data = json.loads(response)
       print ("The twitter value is "+data['value'])
       try:
           sensor_value = int(data['value'])
           if sensor_value > temperature_threshold:
               print ("Temperature has crossed the threshold.")
           # Call get_api_object to authenticate user and get the API object
           api_object = get_api_object(config)
           # Store the tweet message in the variable
           tweet = ("Temperature has crossed the threshold "+str(counter))
           # Post the tweet on your Twitter account using the update_status method.
           status = api_object.update_status(status=tweet)
       except Exception as e:
           print ("An error occurred ", e)
       tweet=0

Credits

Gunjan yadav

Gunjan yadav

1 project • 1 follower

Comments