Introduction: Weather Station Using Raspberry Pi With BME280 in Python

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

is maith an scéalaí an aimsir(The Weather is a Good Storyteller)

With global warming and climate change issues, the global weather pattern is becoming erratic 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 learn a lot about basic electronics from a weather station project using a bunch of cheap parts and sensors. It’s pretty easy to set up and in no time you can have it.

Step 1: Imperative Equipment Bill

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.

2. I2C Shield for Raspberry Pi

The INPI2(I2C adapter) provides the Raspberry Pi 2/3 an I²C port for use with multiple I2C devices. It’s available on DCUBE Store.

3. Digital Humidity, Pressure and Temperature Sensor, BME280

The BME280 is a humidity, pressure and temperature sensor that has fast response time and high overall accuracy. We purchased this sensor from DCUBE Store.

4. I2C Connecting Cable

We used the I²C cable available here DCUBE Store.

5. Micro USB cable

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

6 . Interpret Internet Access via EthernetCable/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 for Circuit

Make the circuit as per the schematic shown.
In general, the connections are very simple. Keep calm and Follow the instructions and images above, 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. Electronic schematics are like blueprints. Draw up a blueprint and follow the design carefully. A few basic concepts of electronics might be useful here !

Connection of the Raspberry Pi and I2C Shield

First, take the Raspberry Pi and place the I²C Shield on it. Press the Shield gently and we are done with this step as easy as pie(see the pic).

Connection of the Sensor and Raspberry Pi

Take the sensor and Connect the I²C cable with it. Make sure that I²C Output ALWAYS connects to the I²C Input. The same has to be followed for the Raspberry Pi with the I²C shield mounted over it the GPIO pins.
We recommend the use of the I²C 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.

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. Punch up and voila! Our squad is information.

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. This option is for advanced users so we won't be covering it in detail here.

It's an economical method if you don’t want to spend much.

Step 3: Raspberry Pi Programming in Python

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. Still ask if any(Even if you know a thousand things, still ask someone who knows).

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

<p># Distributed with a free-will license.<br># Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# BME280
# This code is designed to work with the BME280_I2CS I2C Mini Module available from
#</p><p>import smbus
import time</p><p># Get I2C bus
bus = smbus.SMBus(1)</p><p># BME280 address, 0x76(118)
# Read data back from 0x88(136), 24 bytes
b1 = bus.read_i2c_block_data(0x76, 0x88, 24)</p><p># 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</p><p># 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</p><p># BME280 address, 0x76(118)
# Read data back from 0xA1(161), 1 byte
dig_H1 = bus.read_byte_data(0x76, 0xA1)</p><p># BME280 address, 0x76(118)
# Read data back from 0xE1(225), 7 bytes
b1 = bus.read_i2c_block_data(0x76, 0xE1, 7)</p><p># 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</p><p># 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)</p><p>time.sleep(0.5)</p><p># 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)</p><p># 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</p><p># Convert the humidity data
adc_h = data[6] * 256 + data[7]</p><p># 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</p><p># 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</p><p># 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</p><p># 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</p>

Step 4: The Running 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.

Step 5: Utilization in Practical World

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 I²C 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! Please let us know what you think of this instructable. We would love to make some improvements if necessary.