-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasap3.0.py
More file actions
127 lines (112 loc) · 3.21 KB
/
asap3.0.py
File metadata and controls
127 lines (112 loc) · 3.21 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import serial
import time
import os
import requests
import sys
import json
import random
import string
import requests.packages.urllib3
def request(sensorValue, dirValue, name):
print 'Uploading ' + name + ' Data Sensor ...'
requests.packages.urllib3.disable_warnings()
result = requests.put(firebase_url + '/' + dirValue + '/.json' + '?auth=' + auth_token, data=json.dumps(sensorValue))
print 'Record inserted. Result Code = ' + str(result.status_code)
print '------------------------------------------\n'
if (name=='FAN'):
print 'All Data Sensor has been uploaded, dude!\n'
time.sleep(1)
os.system('cls')
#Setup a loop to send Temperature values at fixed intervals
fixed_interval = 1
start_interval = 2
#Init Firebase
# firebase = firebase.FirebaseApplication('https://ramean.firebaseio.com/', None)
firebase_url = 'https://test-be923.firebaseio.com/'
auth_token = 'c2d8015d350440fca438f41a4784030f'
def main():
#Connect to Serial Port for communication
count = 0
isDone = 0
try:
ser = serial.Serial('COM5', 9600, timeout=0)
os.system('cls')
print '=========================='
print 'Welcome to ASAP-Monitoring'
print 'Getting Started ...'
print '==========================\n'
time.sleep(start_interval)
read_out = False
while 1:
isDone = 0
print count
print isDone
#read Sensor
if read_out==False: #jangan lupa diganti True
# temp = ser.readline()
dht = ser.readline()
mq2 = ser.readline()
#out2 = ser.readline()
mq7 = ser.readline()
mq135 = ser.readline()
#out7 = ser.readline()
#fan = ser.readline()
#simulation here
#temp = "27"
# in2 = "300\r\n"
# out2 = "55\r\n"
# in7 = "20\r\n"
# out7 = "80\r\n"
# fan = "1\r\n"
#print value
print '============='
print 'Data Sensor\n'
print 'Suhu : ' + dht + 'mq2 : ' + mq2 + 'MQ 7 : ' + mq7 + 'mq135 : ' + mq135 + '=============\n'
read_sensor = True
else:
print 'System cannot read the sensor'
break
#Request-ing Data Sensor
i=1
while (i<=4):
if i == 1:
strTemp = str(int(dht))
dataSensor = {'value':strTemp}
dirSensor = 'deviceTemp'
namaSensor = 'dht'
elif i == 2:
#strSmokeIn = str(int(mq2))
dataSensor = {'value':strSmokeIn}
dirSensor = 'deviceSmokeIn'
namaSensor = 'mq2'
elif i == 3:
#strSmokeOut = str(int(mq7))
dataSensor = {'value':strSmokeOut}
dirSensor = 'deviceSmokeOut'
namaSensor = 'mq7'
else:
#strCOIn = str(int(mq135))
dataSensor = {'value':strCOIn}
dirSensor = 'deviceCOIn'
namaSensor = 'mq135'
isDone = 1
request(dataSensor, dirSensor, namaSensor)
i = i + 1
if (isDone==1):
print 'Uploaded, dude! Sounds Great'
else:
print 'Oh no, Something went wrong'
ser.write(str(isDone))
count = count + 1
time.sleep(fixed_interval)
except serial.SerialException:
print '\nSerial communication cannot established, dude'
print '----------------------------------------------'
except IOError:
print('Error! Something went wrong.')
except KeyboardInterrupt:
print('\n\nOPERATION HAS BEEN CANCELED!\n')
sys.exit()
print 'System has been exit'
sys.exit()
if __name__ == '__main__':main()