Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
低成本气压式高度计制作教程(代码与接线部分)
bmp.py
from micropython import const from machine import I2C BMP280_I2C_ADDR = const(0x76) class BMP280(): def __init__(self, i2c): self.i2c = i2c self.tb = bytearray(1) self.rb = bytearray(1) self.dig_T1 = self.get2Reg(0x88) self.dig_T2 = self.short(self.get2Reg(0x8A)) self.dig_T3 = self.short(self.get2Reg(0x8C)) self.dig_P1 = self.get2Reg(0x8E) self.dig_P2 = self.short(self.get2Reg(0x90)) self.dig_P3 = self.short(self.get2Reg(0x92)) self.dig_P4 = self.short(self.get2Reg(0x94)) self.dig_P5 = self.short(self.get2Reg(0x96)) self.dig_P6 = self.short(self.get2Reg(0x98)) self.dig_P7 = self.short(self.get2Reg(0x9A)) self.dig_P8 = self.short(self.get2Reg(0x9C)) self.dig_P9 = self.short(self.get2Reg(0x9E)) self.mode = 3 self.osrs_p = 3 self.osrs_t = 1 self.setReg(0xF4, 0x2F) self.setReg(0xF5, 0x0C) self.filter = 3 self.T = 0 self.P = 0 self.version = '1.0' def short(self, dat): if dat > 32767: return dat - 65536 else: return dat # set reg def setReg(self, reg, dat): self.tb[0] = dat self.i2c.writeto_mem(BMP280_I2C_ADDR, reg, self.tb) # get reg def getReg(self, reg): self.i2c.readfrom_mem_into(BMP280_I2C_ADDR, reg, self.rb) return self.rb[0] # get two reg def get2Reg(self, reg): return self.getReg(reg) + self.getReg(reg+1) * 256 def get(self): adc_T = (self.getReg(0xFA)<<12) + (self.getReg(0xFB)<>4) var1 = (((adc_T>>3)-(self.dig_T1<>11 var2 = (((((adc_T>>4)-self.dig_T1)*((adc_T>>4) - self.dig_T1))>>12)*self.dig_T3)>>14 t = var1+var2 self.T = ((t * 5 + 128) >> 8)/100 var1 = (t>>1) - 64000 var2 = (((var1>>2) * (var1>>2)) >> 11 ) * self.dig_P6 var2 = var2 + ((var1*self.dig_P5)<>2)+(self.dig_P4<>2)*(var1>>2))>>13)>>3) + (((self.dig_P2) * var1)>>1))>>18 var1 = ((32768+var1)*self.dig_P1)>>15 if var1 == 0: return # avoid exception caused by division by zero adc_P = (self.getReg(0xF7)<<12) + (self.getReg(0xF8)<>4) p=((1048576-adc_P)-(var2>>12))*3125 if p < 0x80000000: p = (p << 1) // var1 else: p = (p // var1) * 2 var1 = (self.dig_P9 * (((p>>3)*(p>>3))>>13))>>12 var2 = (((p>>2)) * self.dig_P8)>>13 self.P = p + ((var1 + var2 + self.dig_P7) >> 4) return [self.T, self.P] # get Temperature in Celsius def getTemp(self): self.get() return self.T # get Pressure in Pa def getPress(self): self.get() return self.P # Calculating absolute altitude def getAltitude(self): return 44330*(1-(self.getPress()/101325)**(1/5.255)) # sleep mode def poweroff(self): self.setReg(0xF4, 0) # normal mode def poweron(self): self.setReg(0xF4, 0x2F)
main.py
import time from machine import Pin,I2C import _thread import bmp280 def run(): i2c = I2C(sda=Pin(6), scl=Pin(7)) b = bmp280.BMP280(i2c) da = 0 t=0 try: while(1): da=da+1 f = open("date"+str(da)+".txt",'r') f.close() except: f = open("date"+str(da)+".txt",'a') f.write('气压计数据记录\n') f.write("记录时间: 原始温度和气压: 高度:\n") f.close() while(True): time.sleep_ms(100) f = open("date"+str(da)+".txt",'a') t=t+1 y = b.get() p = b.getPress() h = (101325-p)*9/100 dat = (str(t/10)+" "+str(y)+" "+str(h)+"\n") f.write(dat) #print(h) f.close() pboot = Pin(9,Pin.IN) def bled(): led4 = Pin(12,Pin.OUT) led5 = Pin(13,Pin.OUT) while(True): led4.on() time.sleep_ms(500) led4.off() led5.on() time.sleep_ms(500) led5.off() #_thread.start_new_thread(bled,()) while(1): time.sleep_ms(100) if(pboot.value()==0): _thread.start_new_thread(bled,()) run() break
接线:
接线图
SDA=IO06 SCL=IO07 VCC=3.3V GND=GND
========== ========== ========== ==========
电池3.7/4.2V 正极=+5v 负极=GND
========== ========== ========== ==========
最后模块接到usb的5伏电源或者
把正5伏和gnd接到一块满电1s电池上
切记其他电池(2s-6s)只能插平衡头来获取1s电压
否则直接看烟花
作者:洋洋柚Y
https://www.bilibili.com/read/cv24948670?spm_id_from=333.999.0.0
出处:bilibili
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0092909336090088 seconds