給樹莓派安裝SHT30
由于買來的SHT30已經(jīng)接好電路,直接用杜邦線連接樹莓派即可。
完美
樹莓派開啟i2c
參考?https://blog.csdn.net/panwen1111/article/details/81044428 這里不多贅述裕膀。
檢查是否開啟成功 在命令行輸入 lsmod 出現(xiàn)如下說明加載成功氮趋。
安裝i2c-tools
sudo apt-get install i2c-tools
簡單介紹一下:
i2c-tools僅有四條命令
1.i2c-tool查詢i2c設(shè)備:
sudo i2cdetect -y 1? ?(樹莓派1用 -y 0? 樹莓派2用 -y 1? ?參考https://blog.csdn.net/xukai871105/article/details/15029843)
地址為0x44的就是我們的SHT30荧缘。
2.所有寄存器內(nèi)容導(dǎo)出
i2cdump -y 1 0x44
? ?-y? ? ? ? ?代表取消用戶交互過程,直接執(zhí)行指令珊皿;
????1? ? ?????代表I2C總線編號(hào);
? ? 0x44? ? 代表I2C設(shè)備從機(jī)地址巨税,
3.寄存器內(nèi)容寫入
i2cset -y 1 0x44 0x00 0x13
?-y? ??????代表曲線用戶交互過程蟋定,直接執(zhí)行指令
1? ? ?????代表I2C總線編號(hào)
0x44? ? 代表I2C設(shè)備地址
0x00? ? 代表存儲(chǔ)器地址
0x13? ? 代表存入的數(shù)據(jù)
4.寄存器內(nèi)容讀取
i2cget -y 1 0x44 0x00
?-y? ??????代表曲線用戶交互過程,直接執(zhí)行指令
1? ? ?????代表I2C總線編號(hào)
0x44? ? 代表I2C設(shè)備地址
0x00? ? 代表存儲(chǔ)器地址
輸出溫濕度
根據(jù)SHT30手冊(cè)草添,我們?cè)诩拇嫫髦凶x取的數(shù)值并不是真實(shí)的溫濕度數(shù)值驶兜。數(shù)據(jù)需要經(jīng)行轉(zhuǎn)換。
python代碼實(shí)現(xiàn)
實(shí)現(xiàn)一(根據(jù)SHT31修改 十分完美 基于python3):
import smbus
import time
i2c = smbus.SMBus(1)
addr=0x44
i2c.write_byte_data(addr,0x23,0x34)
time.sleep(0.5)
while 1:
? ? i2c.write_byte_data(addr,0xe0,0x0)
? ? data = i2c.read_i2c_block_data(addr,0x0,6)
? ? rawT = ((data[0]) << 8) | (data[1])
? ? rawR = ((data[3]) << 8) | (data[4])
? ? temp = -45 + rawT * 175 / 65535
? ? print (str(temp) +"C")
? ? RH = 100 * rawR / 65535
? ? print (str(RH) +"%")
? ? time.sleep(1)
? ? print ("*************")
實(shí)現(xiàn)二(根據(jù)SHT20修改 半成品 基于python2):
import commands
import time
while 1:
? ? status_temp,temp_reg=commands.getstatusoutput('i2cget -f -y 1 0x44 0x00')
? ? temp_int = int(temp_reg,16)
? ? temp = (temp_int<<8)|temp_int
? ? T=-45+temp*175/65535
? ? print "Current Temperature=",T,"℃"
? ? time.sleep(0.5)
? ? status_hump,hump_reg=commands.getstatusoutput('i2cget -f -y 1 0x44 0x30')
? ? hump_int=int(hump_reg,16)
? ? hump=(hump_int<<8)|hump_int
? ? H= 100 * hump / 65535
? ? print"Current Hump=",H,'%'
? ? print "--------------------------------------------------------"
? ? time.sleep(0.5)
ps:這份代碼存在以下問題:
1.最主要的問題:無法確定濕度數(shù)據(jù)的寄存器地址远寸,寄存器中經(jīng)常沒有值抄淑。查詢到的0x44i2c設(shè)備地址應(yīng)該無誤(如果地址錯(cuò)誤會(huì)全是xx),但是寄存器中的數(shù)據(jù)全為0x00驰后。溫度的地址為0x00肆资,但是濕度的地址一直在變化甚至沒有濕度的數(shù)據(jù)。
2.使用commands.getstatusoutput庫時(shí)灶芝,如果把溫度和濕度command命令放在一起迅耘,則會(huì)出現(xiàn)以下問題
? 其解決辦法就是使用time庫延時(shí)0.5s 即可贱枣。
? 剛?cè)腴T實(shí)屬小白一枚 歡迎大家指點(diǎn)幫助!!!