-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureSensor.py
More file actions
34 lines (24 loc) · 1010 Bytes
/
TemperatureSensor.py
File metadata and controls
34 lines (24 loc) · 1010 Bytes
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
import smbus
class TemperatureSensor:
def __init__(self):
self.bus = smbus.SMBus(1)
self.config = [0x00, 0x00]
self.bus.write_i2c_block_data(0x18, 0x01, self.config)
self.bus.write_byte_data(0x18, 0x08, 0x03)
self.data = self.bus.read_i2c_block_data(0x18, 0x05, 2)
#returns double representing the temperature in Fahrenheit
def getTemperatureInFahrenheit(self):
self.updateData()
ctemp = ((self.data[0] & 0x1F) * 256) + self.data[1]
if ctemp > 4095 :
ctemp -= 8192
return ctemp * 0.0625 * 1.8 + 32
def updateData(self):
self.data = self.bus.read_i2c_block_data(0x18, 0x05, 2)
#returns double representing the temperature in Celsius
def getTemperatureInCelsius(self):
self.updateData()
ctemp = ((self.data[0] & 0x1F) * 256) + self.data[1]
if ctemp > 4095 :
ctemp -= 8192
return ctemp * 0.0625