» » » Weather Station using Raspberry Pi with BME280 in Python

Weather Station using Raspberry Pi with BME280 in Python

posted in: Others | 0

With global warming and climate change issues, the global weather pattern is becoming unpredictable across our world leading to a number of weather-related natural disasters (droughts, extreme temperatures, floods, storms, and wildfires), a weather station seems to be a necessary evil at home. You can make your own weather station using BME280 Sensor and Raspberry pi at home. It’s pretty easy to set up and in no time you can have it.


Step 1: Hardware Required

1. A Raspberry Pi

Get your hands on a Raspberry Pi board. Raspberry Pi is a Linux powered single board computer. The Raspberry Pi is really cheap, tiny and versatile built of an accessible and functional computer for learners to exercise basics of programming and software development.

Raspberry Pi
Raspberry Pi 3 Model B

2. I2C Shield for Raspberry Pi

The I2C adapter is a non-invasive I2C adapter complete with level shifter providing you with a 5V I2C port for use with any of our I2C devices.

I2C Connecting Shield for Raspberry PI

3. BME280 Digital Humidity, Pressure and Temperature Sensor

The BME280 is a combined humidity, pressure and temperature sensor that allows for two devices per I2C port. It is a humidity, pressure and temperature sensor that has fast response time and high overall accuracy.

BME280 I2C Sensor
BME280 Humidity, Pressure and Temperature I2C Sensor

4. I2C Connecting Cable

We also designed the I2C connecting cable which is available here.

I2C Cable
I2C Connecting Cable

5. Micro USB cable

The micro USB cable Power supply is an ideal choice for powering the Raspberry Pi.

6 . Interpret Internet Access via Ethernet Cable/WiFi Adapter

Internet access can be enabled through an Ethernet cable connected to a local network and the internet. Alternatively, you can connect to a wireless network using a USB wireless dongle, which will require configuration.

7. HDMI Cable (Display & connectivity cable)

Any HDMI/DVI monitor and any TV should work as a display for the Pi. Alternatively, you can remote access the Pi through SSH negating the need for a monitor (advanced users only)


Step 2: Hardware Connections

In general, the connections are very simple. Keep calm and follow the instructions and images, and you should have no problems. While learning, we got thoroughly with the basics of electronics regarding hardware and software knowledge. We wanted to draw up a simple electronics schematic for this project.

Connection of the Raspberry Pi and I2C Shield

First, take the Raspberry Pi and place the I2C Shield on it. Press the Shield gently and we are done with this step as easy as pie (see the picture below).

pi-adapter-side

Connection of the Sensor and Raspberry Pi

Take the sensor and Connect the I2C cable with it. Make sure that I2C Output ALWAYS connects to the I2C Input. The same has to be followed for the Raspberry Pi with the I2C shield mounted over it the GPIO pins.
We recommend the use of the I2C cables as it negates the need for reading pinouts, soldering, and malaise caused by even the slightest slip-up. With this simple plug and play cable, you can install, swap out boards, or add more boards to an application with ease.

BME280 Final Connections
bme280-final-connections-with-raspberry-pi

Note: The brown wire should always follow the Ground (GND) connection between the output of one device and the input of another device.

Internet Connectivity is key

You have two choices here. Either You can connect the Raspberry Pi to the network using an ethernet cable or use a USB to WiFi Adapter for WIFI Connectivity. Either way, as long as it is connected to the internet you’re covered.

Powering up the Circuit

Plug in the Micro USB cable into the power jack of Raspberry Pi.

Connection to Screen

We can either have the HDMI cable connected to a monitor or to a TV. Additionally, we can access a Raspberry Pi without connecting it to a monitor using remote access. SSH is a handy tool for secure remote access. You can also use the PUTTY software for that.


Step 3: Code

The Python Code for the Raspberry Pi and BME280 Sensor. It’s available in our Github repository.

Before going on to the code, make sure you read the instructions given in the Readme file and Setup your Raspberry Pi according to it. Just a little time will get you ready for setup.
A weather station is a facility, either on land or sea, with instruments and equipment for measuring atmospheric conditions to provide information for weather forecasts and to study the weather and climate.

The code is clearly in front of you and it’s in the simplest form that you can imagine of and you should have no problems.

You can copy the working Python code for this sensor from here as well.

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# BME280 address, 0x76(118)
# Read data back from 0x88(136), 24 bytes
b1 = bus.read_i2c_block_data(0x76, 0x88, 24)

# Convert the data
# Temp coefficients
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

# Pressure coefficients
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

# BME280 address, 0x76(118)
# Read data back from 0xA1(161), 1 byte
dig_H1 = bus.read_byte_data(0x76, 0xA1)

# BME280 address, 0x76(118)
# Read data back from 0xE1(225), 7 bytes
b1 = bus.read_i2c_block_data(0x76, 0xE1, 7)

# Convert the data
# Humidity coefficients
dig_H2 = b1[1] * 256 + b1[0]
if dig_H2 > 32767 :
    dig_H2 -= 65536
dig_H3 = (b1[2] &  0xFF)
dig_H4 = (b1[3] * 16) + (b1[4] & 0xF)
if dig_H4 > 32767 :
    dig_H4 -= 65536
dig_H5 = (b1[4] / 16) + (b1[5] * 16)
if dig_H5 > 32767 :
    dig_H5 -= 65536
dig_H6 = b1[6]
if dig_H6 > 127 :
    dig_H6 -= 256

# BME280 address, 0x76(118)
# Select control humidity register, 0xF2(242)
#		0x01(01)	Humidity Oversampling = 1
bus.write_byte_data(0x76, 0xF2, 0x01)
# BME280 address, 0x76(118)
# Select Control measurement register, 0xF4(244)
#		0x27(39)	Pressure and Temperature Oversampling rate = 1
#					Normal mode
bus.write_byte_data(0x76, 0xF4, 0x27)
# BME280 address, 0x76(118)
# Select Configuration register, 0xF5(245)
#		0xA0(00)	Stand_by time = 1000 ms
bus.write_byte_data(0x76, 0xF5, 0xA0)

time.sleep(0.5)

# BME280 address, 0x76(118)
# Read data back from 0xF7(247), 8 bytes
# Pressure MSB, Pressure LSB, Pressure xLSB, Temperature MSB, Temperature LSB
# Temperature xLSB, Humidity MSB, Humidity LSB
data = bus.read_i2c_block_data(0x76, 0xF7, 8)

# Convert pressure and temperature data to 19-bits
adc_p = ((data[0] * 65536) + (data[1] * 256) + (data[2] & 0xF0)) / 16
adc_t = ((data[3] * 65536) + (data[4] * 256) + (data[5] & 0xF0)) / 16

# Convert the humidity data
adc_h = data[6] * 256 + data[7]

# Temperature offset calculations
var1 = ((adc_t) / 16384.0 - (dig_T1) / 1024.0) * (dig_T2)
var2 = (((adc_t) / 131072.0 - (dig_T1) / 8192.0) * ((adc_t)/131072.0 - (dig_T1)/8192.0)) * (dig_T3)
t_fine = (var1 + var2)
cTemp = (var1 + var2) / 5120.0
fTemp = cTemp * 1.8 + 32

# Pressure offset calculations
var1 = (t_fine / 2.0) - 64000.0
var2 = var1 * var1 * (dig_P6) / 32768.0
var2 = var2 + var1 * (dig_P5) * 2.0
var2 = (var2 / 4.0) + ((dig_P4) * 65536.0)
var1 = ((dig_P3) * var1 * var1 / 524288.0 + ( dig_P2) * var1) / 524288.0
var1 = (1.0 + var1 / 32768.0) * (dig_P1)
p = 1048576.0 - adc_p
p = (p - (var2 / 4096.0)) * 6250.0 / var1
var1 = (dig_P9) * p * p / 2147483648.0
var2 = p * (dig_P8) / 32768.0
pressure = (p + (var1 + var2 + (dig_P7)) / 16.0) / 100

# Humidity offset calculations
var_H = ((t_fine) - 76800.0)
var_H = (adc_h - (dig_H4 * 64.0 + dig_H5 / 16384.0 * var_H)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * var_H * (1.0 + dig_H3 / 67108864.0 * var_H)))
humidity = var_H * (1.0 -  dig_H1 * var_H / 524288.0)
if humidity > 100.0 :
    humidity = 100.0
elif humidity < 0.0 :
    humidity = 0.0

# Output data to screen
print "Temperature in Celsius : %.2f C" %cTemp
print "Temperature in Fahrenheit : %.2f F" %fTemp
print "Pressure : %.2f hPa " %pressure
print "Relative Humidity : %.2f %%" %humidity

 


Step 4: Working of the BME280 Code

Now, download (or git pull) the code and open it in the Raspberry Pi.

Run the commands to Compile and Upload the code on the terminal and see the output on display. After few seconds, it will display all the parameters. After making sure that everything works great, you can develop some more interesting ones.

BME280 Output
BME280 Python Code Output

Step 5: Applications & Features

The BME280 achieves high performance in all applications requiring humidity and pressure measurement. These emerging applications are Context awareness, e.g. Skin Detection, Room Change Detection, Fitness Monitoring / Well-Being, Warning Regarding Dryness or High Temperatures, Measurement of Volume and Air Flow, Home Automation Control, Control Heating, Ventilation, Air Conditioning (HVAC), Internet of Things(IoT), GPS Enhancement (e.g. Time-to-First-Fix Improvement, Dead Reckoning, Slope Detection), Indoor Navigation (Change of Floor Detection, Elevator Detection), Outdoor Navigation, Leisure & Sports Applications, Weather Forecast And Vertical Velocity Indication (Rise/Sink Speed).


Step 6: Conclusion

Hope this project inspires further experimentation. Making a more sophisticated weather station can involve some more sensors like Rain Gauge, Light sensor, anemometer(wind speed) etc. You can add them and amend the code. We have a video tutorial on YouTube having the basic functioning of the I2C sensor with Rasp Pi. It’s really amazing to see the results and working of the I²C communications. Check it as well. Have fun building and learning!

Leave a Reply