forked from Divyansh12/IOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempalert.py
52 lines (39 loc) · 2.37 KB
/
tempalert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import conf,json,time
from boltiot import Sms,Bolt
config = {
"consumer_key" : conf.consumer_key,
"consumer_secret" : conf.consumer_secret,
"access_token" : conf.access_token,
"access_token_secret" : conf.access_token_secret
}
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.API_KEY,conf.DEVICE_ID)
sms=Sms(conf.SSID,conf.AUTH_TOKEN,conf.TO_NUMBER,conf.FROM_NUMBER)
mailerone=Email(email_conf.MAILGUN_API_KEY,email_conf.SANDBOX_URL,email_conf.SENDER_EMAIL,email_conf.RECIPIENT_EMAIL)
mailersec=Email(email_conf.MAILGUN_API_KEY,email_conf.SANDBOX_URL,email_conf.SENDER_EMAIL,email_conf.RECIPIENT_EMAIL_SEC)
#To set the threshold to 25 deg celcius (since the Lm35 sensor gives a analog value it must be divided by 10.24 to convert to deg celcius.)
temperature_threshold = 100*256/1024
while True:
response=mybolt.analogRead('A0')
data=json.loads(response)
print(data['value'])
try:
sensor_value=int(data['value'])
Temperature=(100*sensor_value)/1024
print(sensor_value)
if temperature > temperature_threshold:
print ("Temperature has crossed the threshold of "+str(temperature_threshold)+"in deg Celcius and It is now : " + str(temperature)+"in deg celcius")
api_object=get_api_object(config)
tweet = "Temperature has crossed the threshold of " + str(temperature_threshold)+"in deg Celcius and It is now :"+ str(temperature)+"in deg celcius"
status = api_object.update_status(status=tweet)
response=mailerone.send_email("Alert! - Warning!","The Current temperature sensor value is " + str(sensor_value) +" And in Deg Celius is " + str(Temperature))
response=mailersec.send_email("Alert! - Warning!","The Current temperature sensor value is " + str(sensor_value) +" And in Deg Celcius is " + str(Temperature))
response = sms.send_sms("The Current temperature sensor value is " +str(sensor_value))
except Exception as e:
print("An error ocurred ", e)
time.sleep(10)