๐ŸŽ„โ›„โ„๏ธ๐ŸŽ It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! ๐ŸŽ„โ›„โ„๏ธ๐ŸŽ
0

BME280 Raspberry Pi Adafruit IO Altitude accuracy
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 4:03 am

Hello I have

a BME280 with a raspberry pi collecting sensor data and sending it to Adafruit IO

I want to know how to modify my code to get the altitude readings in the Adafruit IO site to show 2 decimal points

This is my code

Code: Select all | TOGGLE FULL SIZE

#bme280 on rasperry pi debian buster oct2019
import time
import board
import busio
import adafruit_bme280

from Adafruit_IO import Client, Feed, RequestError
LOOP_DELAY = 35
ADAFRUIT_IO_KEY = 'TNW Launch Codes'
ADAFRUIT_IO_USERNAME = 'WOPR'
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

try: # if we already have the feeds, assign them.
    temperature_feed = aio.feeds('temperature2')
    humidity_feed = aio.feeds('humidity2')
    pressure_feed = aio.feeds('pressure2')
    altitude_feed = aio.feeds('altitude2')
except RequestError: # if we don't, create and assign them.
    temperature_feed = aio.create_feed(Feed(name='temperature2'))
    humidity_feed = aio.create_feed(Feed(name='humidity2'))
    pressure_feed = aio.create_feed(Feed(name='pressure2'))
    altitude_feed = aio.create_feed(Feed(name='altitude2'))

i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
bme280.sea_level_pressure = 1016.11
while True:
    print('Reading sensors...')
    temp_data = bme280.temperature
    # convert temperature (C->F)
    temp_data = int(temp_data) * 1.8 + 32
    humid_data = bme280.humidity
    pressure_data = bme280.pressure
    alt_data = bme280.altitude

    print('sending data to adafruit io...')
    # Send BME280 Data to Adafruit IO.
    print('Temperature: %0.1f F' % temp_data)
    aio.send(temperature_feed.key, temp_data)
    print("Humidity: %0.1f %%" % humid_data)
    aio.send(humidity_feed.key, int(humid_data))
    time.sleep(2)
    print("Pressure: %0.1f hPa" % pressure_data)
    aio.send(pressure_feed.key, int(pressure_data))
    print("Altitude = %0.2f meters" % alt_data)
    aio.send(altitude_feed.key, int(alt_data))

    time.sleep(LOOP_DELAY * 1.2)





Any help would be cool

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by millercommamatt on Mon Oct 14, 2019 9:23 am

The first step to getting accurate altitude is to set an accurate sea level pressure. This value will be specific to your location and changes with time.

All your adafruit.io send commands are casting your values to integers. Don't do that if you want higher precision.

For example, turn this:
aio.send(altitude_feed.key, int(alt_data))
into this:
aio.send(altitude_feed.key, alt_data)

millercommamatt
 
Posts: 508
Joined: Tue Jul 31, 2018 4:57 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by brubell on Mon Oct 14, 2019 10:18 am

Your pressure data is most likely being converted from a floating point value, to an integer value due to the line below:
Code: Select all | TOGGLE FULL SIZE
aio.send(pressure_feed.key, int(alt_data))


To check what the sensor is actually reading, place this line in your code:
Code: Select all | TOGGLE FULL SIZE
print("Altitude Sensor: ", alt_data)


To send the altitude data with two decimals of precision, you'll want to use the following snippet of code (where 0.2f = 2 decimal points):
Code: Select all | TOGGLE FULL SIZE
aio.send(pressure_feed.key, '{0:.2f}'.format(alt_data))

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 1:54 pm

Brubel Thank you for the help

millercommamatt Thank you for the help

I did adjust my sea level to reflect my current elevation and todays conditions . I will be attaching a 50 foot line to a small weather balloon to check out the Altimeter on the BME280 and comparing it to the BMP388


Code: Select all | TOGGLE FULL SIZE
 
#Re edited by KFLMIAMI420
#Licensed under the MIT license.
#All text above must be included in any redistribution.

# BME280 on rasperry pi zero w running debian buster October, 2019
# Take readings from a BME280 Sensor attached to a Raspberry Pi Zero W
# Print readings to Terminal Window
# Upload readings to Adafruit IO IOT Portal
# Convert Raw readings from C to F
# Add Decimal to Altitude upload to IO Portal
# Add Humidity correction for Sensor Error

import time
import board
import busio
import adafruit_bme280

from Adafruit_IO import Client, Feed, RequestError
LOOP_DELAY = 35
ADAFRUIT_IO_KEY = 'TNW Codes!'
ADAFRUIT_IO_USERNAME = 'wopr'
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

try: # if we already have the feeds, assign them.
    temperature_feed = aio.feeds('temperature2')
    humidity_feed = aio.feeds('humidity2')
    pressure_feed = aio.feeds('pressure2')
    altitude_feed = aio.feeds('altitude2')
except RequestError: # if we don't, create and assign them.
    temperature_feed = aio.create_feed(Feed(name='temperature2'))
    humidity_feed = aio.create_feed(Feed(name='humidity2'))
    pressure_feed = aio.create_feed(Feed(name='pressure2'))
    altitude_feed = aio.create_feed(Feed(name='altitude2'))

i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
bme280.sea_level_pressure = 1017.21
while True:
    print('Reading sensors...')
    temperature_data = bme280.temperature
    # convert temperature (C->F)
    temperature_data = int(temperature_data) * 1.8 + 32
    humid_data = bme280.humidity
    # humidity correction for sensor error. for better  readings get readings from DHT22 humidity on this hat
    humid_data = int(humid_data) + 6 
    pressure_data = bme280.pressure
    alt_data = bme280.altitude

    print('sending BME280 data to adafruit io...')
    print('Temperature: %0.1f F' % temperature_data)
    aio.send(temperature_feed.key, '{0:.2f} F'.format(temperature_data))
    print("Humidity: %0.1f %%" % humid_data)
    aio.send(humidity_feed.key, '{0:.2f} Percent '.format(humid_data))
   #time.sleep(2)
    print("Pressure: %0.1f hPa" % pressure_data)
    aio.send(pressure_feed.key, '{0:.2f} Mbar'.format(pressure_data))
    print("Altitude = %0.2f meters" % alt_data)
    aio.send(altitude_feed.key, '{0:.2f} meters'.format(alt_data))


    time.sleep(LOOP_DELAY * 1.2)

 
Last edited by kflmiami420 on Mon Oct 14, 2019 2:39 pm, edited 1 time in total.

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by brubell on Mon Oct 14, 2019 2:07 pm

Your updated code looks good, I removed your IO Key (careful - don't post those publicly).

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 2:41 pm

Thanks I missed that

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 2:48 pm

BRubell

Can I ask you a question ? the picture from the adafruit site shows a temp dht22 sensor attached to a 128x32 oled and a board

Do you have any build details on this ? a tutorial ? I want to create one of my own
Attachments
brentrubel.jpg
brentrubel.jpg (183.1 KiB) Viewed 159 times

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by brubell on Mon Oct 14, 2019 3:18 pm

Can I ask you a question ? the picture from the adafruit site shows a temp dht22 sensor attached to a 128x32 oled and a board

Do you have any build details on this ? a tutorial ? I want to create one of my own


Sure! That's a Feather Huzzah ESP32 (https://www.adafruit.com/product/3405) and an OLED FeatherWing (https://www.adafruit.com/product/2900), snapped into a FeatherWing Doubler (https://www.adafruit.com/product/2890).

I presume the DHT22 is soldered directly to the proto-plate portion of the FeatherWing Doubler. You could also get a Proto Feather-Wing and install a BME280 (along with other sensors on it), like this

microcontrollers_IMG_6134.jpg
microcontrollers_IMG_6134.jpg (73.34 KiB) Viewed 152 times




I did that in _this_ guide: https://learn.adafruit.com/adafruit-io- ... d-assembly

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 4:36 pm

Thank you , I just got a pyportal and a bme280 and the anemometer (Just waiting for other sensors to be available ), so I guess I already own a ESP32, lol

I am a raspberry pi platform person , but that setup interest me because of the power consumption
Attachments
pyportal3.jpg
pyportal3.jpg (56.18 KiB) Viewed 143 times
pyportal1.jpg
pyportal1.jpg (57.81 KiB) Viewed 143 times

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by brubell on Mon Oct 14, 2019 4:42 pm

You could use a Feather M4 + AirLift FeatherWing to achieve a similar form factor. It'll likely consume less power than the PyPortal in your picture (neat usb/ drill battery btw!).

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 4:59 pm

The makita battery is very useful I have the drill and was given this usb power adapter by family member . when wanting to make something portable quick and quickly rechargeable its a must.
Attachments
maki3.jpg
maki3.jpg (64.93 KiB) Viewed 139 times
maki2.jpg
maki2.jpg (55.72 KiB) Viewed 139 times
maki1.jpg
maki1.jpg (65.66 KiB) Viewed 139 times

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 5:01 pm

I has two USB outlets and an On/Off switch . and with an extra pcb board from another weather project bolted to the py portal the belt clip slides perfectly to hold the pyportal to the power source .
Attachments
maki5.jpg
maki5.jpg (65.75 KiB) Viewed 139 times
maki4.jpg
maki4.jpg (67.23 KiB) Viewed 139 times

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Re: BME280 Raspberry Pi Adafruit IO Altitude accuracy

by kflmiami420 on Mon Oct 14, 2019 10:53 pm

I used a small proto board to solder the wires to the 4 pin jst plug
Attachments
pyportal7.jpg
pyportal7.jpg (65.74 KiB) Viewed 108 times
pyportal6.jpg
pyportal6.jpg (67.95 KiB) Viewed 108 times
pyportal5.jpg
pyportal5.jpg (49.38 KiB) Viewed 108 times

kflmiami420
 
Posts: 122
Joined: Tue Aug 21, 2018 9:43 pm

Please be positive and constructive with your questions and comments.