forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtempLampControl.py
executable file
·50 lines (40 loc) · 1.29 KB
/
tempLampControl.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
import spidev
import time
import os
import RPi.GPIO as GPIO
lampPin = 21
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.OUT)
# Create a spidev object and open /dev/spidev(0,1)
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
def lampControl(state):
if state == "OFF":
GPIO.output(lampPin, 0)
elif state == "ON":
GPIO.output(lampPin, 1)
if __name__ == "__main__":
lamp = "OFF"
while(1):
getTempReading(0)
print "Toggling lamp"
# if lamp == "OFF":
# lampControl("ON")
# malp = "ON"
# else:
# lampControl("OFF")
# lamp = "OFF"
time.sleep(1)