forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtempUpdater.py
executable file
·142 lines (121 loc) · 4.52 KB
/
tempUpdater.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<<<<<<< HEAD
import spidev
import time
import os
from socketIO_client import SocketIO, LoggingNamespace
spi = spidev.SpiDev()
spi.open(0,1)
def getTempReading(channel):
# We read the sensor voltage through ADC
adcValue = spi.xfer2([1, (8+channel) << 4, 0])
# spidev writes back the data onto same buffer used to send data
# We have 10-bit resoultion, so 2 bits from first byte and then 8 bits from last
reading = ((adcValue[1]&3) << 8) + adcValue[2]
print "Adc reading : ",reading
# We have put reference voltage as 5V from RPi
analogVoltage = (reading * 5) / float(1023)
print "Voltage reading : ",analogVoltage
tempinC = ((analogVoltage*1000) - 500) / 10
print "Temperature in C : ",tempinC
return tempinC
def getDoorStatus(channel):
# We read doorPin voltage through ADC
adcValue = spi.xfer2([1, (8+channel) << 4, 0])
# spidev writes back the data onto same buffer used to send data
# We have 10-bit resoultion, so 2 bits from first byte and then 8 bits from last
reading = ((adcValue[1]&3) << 8) + adcValue[2]
print "Door Adc reading : ",reading
status = ""
# If voltage is high, nothing is in proximity / door closed else open
if(reading < 100):
print "Door OPENED"
status = "OPENED"
else:
print "Door CLOSED"
status = "CLOSED"
return status
def main():
currTemp = getTempReading(0)
# round it to two decimals
currTemp = float("{0:.2f}".format(currTemp))
# Send it to server
with SocketIO('192.168.43.121', 5000, LoggingNamespace) as socketIO:
socketIO.emit('tempReading', currTemp)
door = getDoorStatus(1)
with SocketIO('192.168.43.121', 5000, LoggingNamespace) as socketIO:
socketIO.emit('doorReading', door)
if __name__ == '__main__':
while True:
main()
time.sleep(2)
=======
import spidev
import time
import os
from socketIO_client import SocketIO, LoggingNamespace
spi = spidev.SpiDev()
spi.open(0,1)
def getTempReading(channel):
# We read the sensor voltage through ADC
adcValue = spi.xfer2([1, (8+channel) << 4, 0])
# spidev writes back the data onto same buffer used to send data
# We have 10-bit resoultion, so 2 bits from first byte and then 8 bits from last
reading = ((adcValue[1]&3) << 8) + adcValue[2]
print "Adc reading : ",reading
# We have put reference voltage as 5V from RPi
analogVoltage = (reading * 5) / float(1023)
print "Voltage reading : ",analogVoltage
tempinC = ((analogVoltage*1000) - 500) / 10
print "Temperature in C : ",tempinC
return tempinC
def getDoorStatus(channel):
# We read doorPin voltage through ADC
adcValue = spi.xfer2([1, (8+channel) << 4, 0])
# spidev writes back the data onto same buffer used to send data
# We have 10-bit resoultion, so 2 bits from first byte and then 8 bits from last
reading = ((adcValue[1]&3) << 8) + adcValue[2]
print "Door Adc reading : ",reading
status = ""
# If voltage is high, nothing is in proximity / door closed else open
if(reading < 100):
print "Door OPENED"
status = "OPENED"
else:
print "Door CLOSED"
status = "CLOSED"
return status
def getPhotoRes(channel):
# We read doorPin voltage through ADC
adcValue = spi.xfer2([1, (8+channel) << 4, 0])
# spidev writes back the data onto same buffer used to send data
# We have 10-bit resoultion, so 2 bits from first byte and then 8 bits from last
reading = ((adcValue[1]&3) << 8) + adcValue[2]
print "Photo-resistor reading : ",reading
lux_value = 16 * 255 / reading;
status = ""
# If voltage is high, nothing is in proximity / door closed else open
if(lux_value < 8):
print "DULL"
status = "DULL"
else:
print "BRIGHT"
status = "BRIGHT"
return status
def main():
currTemp = getTempReading(0)
# round it to two decimals
currTemp = float("{0:.2f}".format(currTemp))
# Send it to server
with SocketIO('192.168.43.210', 5000, LoggingNamespace) as socketIO:
socketIO.emit('tempReading', currTemp)
door = getDoorStatus(1)
with SocketIO('192.168.43.210', 5000, LoggingNamespace) as socketIO:
socketIO.emit('doorReading', door)
photores = getPhotoRes(2)
with SocketIO('192.168.43.210', 5000, LoggingNamespace) as socketIO:
socketIO.emit('PhotoReading', photores)
if __name__ == '__main__':
while True:
main()
time.sleep(2)
>>>>>>> develop