您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python開發板DIY小型家庭氣象站的方法是什么”,在日常操作中,相信很多人在Python開發板DIY小型家庭氣象站的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python開發板DIY小型家庭氣象站的方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
BMP180是一直常見的氣壓傳感器,BMP180是一款高精度、小體積、超低能耗的壓力傳感器,可以應用在移動設備中,它的性能卓越,精度最低可以達到0.03hPa,并且耗電極低,只有3μA;BMP180采用強大的8-pin陶瓷無引線芯片承載(LCC)超薄封裝,可以通過I2C總線直接與各種微處理器相連。
BMP180實物圖
硬件接線圖
TPYBoard v702 | BMP180 |
3.3V | VIN |
GND | GND |
Y9 | SCL |
Y10 | SDA |
效果展示圖
連接完畢后,將font.py,upcd8544.py與bmp180的庫導入,就可以通過以下方法分別讀取溫度、氣壓、海拔高度了。
導入需要的類庫,編輯好main.py,直接運行就ok了,下面是main.py的程序源碼
# main.py -- put your code here! import pyb import upcd8544 from machine import SPI,Pin from ubinascii import hexlify from ubinascii import * from bmp180 import BMP180 bmp=BMP180(2) SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in) #CLK =>SPI(1).SCK 'X6' SPI clock RST = pyb.Pin('X20') CE = pyb.Pin('X19') DC = pyb.Pin('X18') LIGHT = pyb.Pin('X17') lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT) while True: tem=bmp.getTemp() press=bmp.getPress() altitude=bmp.getAltitude() lcd_5110.lcd_write_string('Tem:',0,0) lcd_5110.lcd_write_string(str(tem),0,1) lcd_5110.lcd_write_string('C',65,1) lcd_5110.lcd_write_string('Press:',0,2) lcd_5110.lcd_write_string(str(press),0,3) lcd_5110.lcd_write_string('Pa',65,3) lcd_5110.lcd_write_string('Hight:',0,4) lcd_5110.lcd_write_string(str(altitude),0,5) lcd_5110.lcd_write_string('M',65,5) bmp180.py庫的源碼 import pyb from pyb import I2C BMP180_I2C_ADDR = const(0x77) class BMP180(): def __init__(self, i2c_num): self.i2c = I2C(i2c_num, I2C.MASTER, baudrate = 100000) self.AC1 = self.short(self.get2Reg(0xAA)) self.AC2 = self.short(self.get2Reg(0xAC)) self.AC3 = self.short(self.get2Reg(0xAE)) self.AC4 = self.get2Reg(0xB0) self.AC5 = self.get2Reg(0xB2) self.AC6 = self.get2Reg(0xB4) self.B1 = self.short(self.get2Reg(0xB6)) self.B2 = self.short(self.get2Reg(0xB8)) self.MB = self.short(self.get2Reg(0xBA)) self.MC = self.short(self.get2Reg(0xBC)) self.MD = self.short(self.get2Reg(0xBE)) self.UT = 0 self.UP = 0 self.B3 = 0 self.B4 = 0 self.B5 = 0 self.B6 = 0 self.B7 = 0 self.X1 = 0 self.X2 = 0 self.X3 = 0 def short(self, dat): if dat > 32767: return dat - 65536 else: return dat def setReg(self, dat, reg): buf = bytearray(2) buf[0] = reg buf[1] = dat self.i2c.send(buf, BMP180_I2C_ADDR) def getReg(self, reg): buf = bytearray(1) buf[0] = reg self.i2c.send(buf, BMP180_I2C_ADDR) t = self.i2c.recv(1, BMP180_I2C_ADDR) return t[0] def get2Reg(self, reg): a = self.getReg(reg) b = self.getReg(reg + 1) return a*256 + b def measure(self): self.setReg(0x2E, 0xF4) pyb.delay(5) self.UT = self.get2Reg(0xF6) self.setReg(0x34, 0xF4) pyb.delay(5) self.UP = self.get2Reg(0xF6) def getTemp(self): self.measure() self.X1 = (self.UT - self.AC6) * self.AC5/(1<<15) self.X2 = self.MC * (1<<11) / (self.X1 + self.MD) self.B5 = self.X1 + self.X2 return (self.B5 + 8)/160 def getPress(self): self.getTemp() self.B6 = self.B5 - 4000 self.X1 = (self.B2 * (self.B6*self.B6/(1<<12))) / (1<<11) self.X2 = (self.AC2 * self.B6)/(1<<11) self.X3 = self.X1 + self.X2 self.B3 = ((self.AC1*4+self.X3) + 2)/4 self.X1 = self.AC3 * self.B6 / (1<<13) self.X2 = (self.B1 * (self.B6*self.B6/(1<<12))) / (1<<16) self.X3 = (self.X1 + self.X2 + 2)/4 self.B4 = self.AC4 * (self.X3 + 32768)/(1<<15) self.B7 = (self.UP-self.B3) * 50000 if self.B7 < 0x80000000: p = (self.B7*2)/self.B4 else: p = (self.B7/self.B4) * 2 self.X1 = (p/(1<<8))*(p/(1<<8)) self.X1 = (self.X1 * 3038)/(1<<16) self.X2 = (-7357*p)/(1<<16) p = p + (self.X1 + self.X2 + 3791)/16 return p def getAltitude(self): p = self.getPress() return (44330*(1-(p/101325)**(1/5.255))) def get(self): t = [] t.append(self.getPress()) t.append(self.getAltitude()) t.append(self.getTemp()) return t
到此,關于“Python開發板DIY小型家庭氣象站的方法是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。