-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_read.py
33 lines (26 loc) · 919 Bytes
/
weather_read.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
import smbus
import time
class SHT31:
def sht31(self):
# Get I2C bus
bus = smbus.SMBus(1)
# SHT31 address, 0x44(68)
# Send measurement command, 0x2C(44)
# 0x06(06) High repeatability measurement
bus.write_i2c_block_data(0x44, 0x2C, [0x06])
time.sleep(0.5)
# SHT31 address, 0x44(68)
# Read data back from 0x00(00), 6 bytes
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x44, 0x00, 6)
# Convert the data
temp = data[0] * 256 + data[1]
cTemp = -45 + (175 * temp / 65535.0)
fTemp = -49 + (315 * temp / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
return cTemp, humidity
# Output data to screen
#device_weather = SHT31()
#temp, moist = device_weather.sht31()
#print (temp)
#print (moist)