Skip to content

Instantly share code, notes, and snippets.

@chak10
Last active June 27, 2018 13:55
Show Gist options
  • Save chak10/69c4b4268e79056feeee0e3252684656 to your computer and use it in GitHub Desktop.
Save chak10/69c4b4268e79056feeee0e3252684656 to your computer and use it in GitHub Desktop.
BMP280 + example
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
from bmp280 import *
geo = "" #coordin sea
d_api = "" #api darksky
def internet_on():
url = 'http://www.google.com/'
for timeout in [1, 2, 5, 10]:
try:
requests.get(url, timeout=timeout)
return True
except requests.ConnectionError:
pass
return False
if __name__ == "__main__":
if internet_on():
forecast = requests.get(
"https://api.darksky.net/forecast/{0}/{1}?exclude=minutely,hourly,daily,alerts,flags".format(d_api, geo))
forecast = forecast.json()
press_sea = forecast['currently']['pressure']
sensor = BMP280()
d = sensor.read_temp_press()
altitude = 44330 * (1.0 - pow(d["p"] / press_sea, 0.1903))
print(altitude)
else:
print("Lost Connection...")
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import smbus, time
class BMP280:
c_temp = []
c_press = []
def __init__(self, bus=0, add=0x76):
self.N_ADD = add
# Get I2C bus
self.bus = smbus.SMBus(bus)
b1 = self.read_cf()
self.con_cf_temp(b1)
self.con_cf_press(b1)
self.select_register()
def read_cf(self):
# BMP280 address, 0x76(118)
# Read data back from 0x88(136), 24 bytes
r = 0
try:
r = self.bus.read_i2c_block_data(self.N_ADD, 0x88, 24)
except Exception as e:
print('Error reading serial port {0}: {1}.'.format(type(e).__name__, e))
return r
def con_cf_temp(self, b1):
# Temp coefficents
dig_T1 = b1[1] * 256 + b1[0]
dig_T2 = b1[3] * 256 + b1[2]
if dig_T2 > 32767:
dig_T2 -= 65536
dig_T3 = b1[5] * 256 + b1[4]
if dig_T3 > 32767:
dig_T3 -= 65536
self.c_temp = [dig_T1, dig_T2, dig_T3]
def con_cf_press(self, b1):
# Pressure coefficents
dig_P1 = b1[7] * 256 + b1[6]
dig_P2 = b1[9] * 256 + b1[8]
if dig_P2 > 32767:
dig_P2 -= 65536
dig_P3 = b1[11] * 256 + b1[10]
if dig_P3 > 32767:
dig_P3 -= 65536
dig_P4 = b1[13] * 256 + b1[12]
if dig_P4 > 32767:
dig_P4 -= 65536
dig_P5 = b1[15] * 256 + b1[14]
if dig_P5 > 32767:
dig_P5 -= 65536
dig_P6 = b1[17] * 256 + b1[16]
if dig_P6 > 32767:
dig_P6 -= 65536
dig_P7 = b1[19] * 256 + b1[18]
if dig_P7 > 32767:
dig_P7 -= 65536
dig_P8 = b1[21] * 256 + b1[20]
if dig_P8 > 32767:
dig_P8 -= 65536
dig_P9 = b1[23] * 256 + b1[22]
if dig_P9 > 32767:
dig_P9 -= 65536
self.c_press = [dig_P1, dig_P2, dig_P3, dig_P4,
dig_P5, dig_P6, dig_P7, dig_P8, dig_P9]
def set_rate(self):
# Select Control measurement register, 0xF4(244)
# 0x27(39) Pressure and Temperature Oversampling rate = 1
# Normal mode
self.bus.write_byte_data(self.N_ADD, 0xF4, 0x27)
def select_register(self):
# Select Configuration register, 0xF5(245)
# 0xA0(00) Stand_by time = 1000 ms
self.bus.write_byte_data(self.N_ADD, 0xF5, 0xA0)
def read_raw_data(self):
self.set_rate()
time.sleep(0.25)
# Read data back from 0xF7(247), 8 bytes
# Pressure MSB, Pressure LSB, Pressure xLSB, Temperature MSB, Temperature LSB
# Temperature xLSB, Humidity MSB, Humidity LSB
r = 0
try:
r = self.bus.read_i2c_block_data(self.N_ADD, 0xF7, 8)
except Exception as e:
print('Error reading serial port {0}: {1}.'.format(type(e).__name__, e))
return r
def to_19bits(self, data):
# Convert temperature[0] and pressure[1] data to 19-bits
return [((data[3] * 65536) + (data[4] * 256) + (data[5] & 0xF0)) / 16,
((data[0] * 65536) + (data[1] * 256) + (data[2] & 0xF0)) / 16]
def read_temp_press(self):
data = self.read_raw_data()
adc = self.to_19bits(data)
temp = self.t_calibration(adc, self.c_temp)
press = self.p_calibration(adc, self.c_press, temp)
temp = temp / 5120.0
return {"t": temp, "p": press}
def t_calibration(self, adc, cf):
# Temperature offset calculations
return ((((adc[0]) / 16384.0 - (cf[0]) / 1024.0) * (cf[1])) + (
(((adc[0]) / 131072.0 - (cf[0]) / 8192.0) * ((adc[0]) / 131072.0 - (cf[0]) / 8192.0)) * (cf[2])))
def p_calibration(self, adc, cf, t_fine):
# Pressure offset calculations
var1 = (t_fine / 2.0) - 64000.0
var2 = var1 * var1 * (cf[5]) / 32768.0
var2 = var2 + var1 * (cf[4]) * 2.0
var2 = (var2 / 4.0) + ((cf[3]) * 65536.0)
var1 = ((cf[2]) * var1 * var1 / 524288.0 + (cf[1]) * var1) / 524288.0
var1 = (1.0 + var1 / 32768.0) * (cf[0])
p = 1048576.0 - adc[1]
p = (p - (var2 / 4096.0)) * 6250.0 / var1
var1 = (cf[8]) * p * p / 2147483648.0
var2 = p * (cf[7]) / 32768.0
return (p + (var1 + var2 + (cf[6])) / 16.0) / 100
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
from bmp280 import*
def main():
sensor = BMP280()
while True:
d = sensor.read_temp_press()
# Output data to screen
print("Temperature : {0:.3f} °C | Pressure : {1:.3f} hPa".format(d["t"], d["p"]))
time.sleep(1)
if __name__ == "__main__":
main()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from bmp280 import *
alt_m = 10 # altitude
if __name__ == "__main__":
sensor = BMP280()
d = sensor.read_temp_press()
p0 = d["p"] / pow(1.0 - alt_m / 44330.0, 5.255)
print(p0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment