Arduino Wi-Fi, Part 3 — Getting high

Smartphones are jam-packed with sensors — everything from gyroscopes to accelerometers to magnetometers. They can even measure barometric pressure.

If you’re into weather observations (like I am), the three key environmental parameters you want to record are temperature, relative humidity and barometric pressure.

New digital pressure sensors hitting the market are so accurate, your phone can even measure elevation with accuracy to within 17cm up to an altitude of 9km. What’s even more awesome, you can buy these sensors and add them to an Arduino.

Wi-Fi Weather

This tiny BMP180 pressure module is available online for under $5.

In Part 2, we created a simple Wi-Fi Weather Station with an Arduino Uno, DHT22 environment sensor and ESP8266 802.11n Wi-Fi module, readable from any browser on your home network.

Now, we’re adding a digital pressure sensor you can buy online for under $5. This is no dinky ‘hobby’ sensor, however — it’s a commercial-grade sensor with accuracy I mentioned earlier.

The BMP180 is made by German industrial giant Bosch, who know a thing or two about putting pressure sensors into phones — Samsung chose this exact sensor for 60-odd million Galaxy S4 smartphones.

Sensor module

 This overlay diagram shows how to build our Wi-Fi Weather Station.

This overlay diagram shows how to build our Wi-Fi Weather Station.

The BMP180 is available as a four- or five-pin module with the standard 0.1-inch pin pitch. It only needs four connections: Vcc (3.3V), GND (0V), SCL (serial bus clock input), SDA (serial bus data output).

The module is tiny, the size of a MicroSD card, but the sensor itself is not much more than a breadcrumb at 3mm x 3mm. The only trick with this module is you must power the Vcc pin with 3.3VDC, not 5VDC, otherwise, you’ll blow it up.

The SCL and SDA lines are 5VDC-tolerant, thanks to internal pull-up resistors. Connect up the power connections, point the SDA pin to Arduino pin A4, the SCL pin to Arduino pin A5 and you’re done.

Follow the overlay diagram and pics to build your own version using an Arduino Uno and the small DIY prototype shield.

Source code & libraries

You also need a code library to link the BMP180 into the Arduino IDE and, thankfully, there are several available online. We’ve chosen the Sparkfun library because it’s small and as easy to use as these things can be.

Grab our source code from the website, unzip the archive and copy the contents of the libraries subfolder into the same of your Arduino IDE.

If the IDE is already running, close all instances and restart it so the new libraries will take effect.

There are two key libraries — the SFE_BMP180 library and the Arduino Wire library. The SFE_BMP180 library directly handles the pressure sensor, but the Wire library is a special library included with the Arduino IDE for handling I2C communications.

Inter-integrated circuit

Long before USB came along, Philips developed a low-power serial bus called I2C for this very task — connecting low-power digital components together. Intel’s old System Management Bus (SMBus) is based on I2C.

Arduino doesn’t have an I2C bus itself, so the Wire Library creates a virtual one using pins A4 and A5 (these are normally analog input pins, but they can also be configured as digital I/O, as we’ll see here).

Measuring altitude from air pressure?

The formula for calculating altitude from relative air pressure readings.

The formula for calculating altitude from relative air pressure readings.

In normal atmosphere, the fall in air pressure as you rise in altitude can be calculated by mathematical formula.

Here, ‘p’ represents the pressure measured by the digital sensor in SI units called hectopascals (hPa or 100 Pascals) and ‘p0’ is the air pressure at sea level, which is defined as 1013.25hPa.

We’ve shown the formula here, but put simply, a fall of 1hPa equals a rise in altitude of 8.43 metres at sea level.

You can read more about this from the Bosch Sensortec BMP180 datasheet datasheet (page 16).

How it works

To code this into our Wi-Fi Weather Station, we’ll just look at the code to make the BMP180 work.

The first thing we do is create an SFE_BMP180 data object imaginatively called ‘BMP180’. When barometric pressure is shown on synoptic charts in nightly news weather reports on TV, that pressure is scaled relative to sea-level.

Since pressure changes with altitude, scaling relative to sea-level takes out altitude as a factor in the measurements, ensuring any change in pressure is due to weather conditions alone.

That means to get accurate readings, you need to know your location elevation above sea-level. In Australia, town elevations are usually based on the local Post Office or Automatic Weather Station (AWS).

The Bureau of Meteorology has a decent list of GPS coordinates and elevation for many Australian towns.

We’ve put all of this gory detail into a simplified function called getPressure(). To get an accurate pressure reading, the sensor must first take the current temperature (BMP180.startTemperature).

Once that reading is recorded in variable ‘tempC’, the pressure reading is then taken (BMP180.startPressure). The ‘3’ parameter tells the sensor to capture using maximum oversampling to reduce atmosphere noise and increase accuracy.

When that’s completed, the BMP180.getPressure function uses the temperature to get the absolute pressure level, stored in the ‘pressureCurrent’ variable.

Finally, the BMP180.sealevel function takes that absolute pressure reading, along with the current altitude and converts it to a relative-to-sea-level reading, storing the result in the BMPbar variable.

Posting to your network

The ESP8266 Wi-Fi sensor serves weather data as a simple HTML page.

The ESP8266 Wi-Fi sensor serves weather data as a simple HTML page.

From last time, the ESP8266 is coded to send HTML code when it receives a client request to its virtual access point IP address.

Here, we just add the new Barometer readings from the BMPbar variable into the ‘webpage’ string variable holding all of the HTML code.

The code displays temperature, relative humidity and barometric pressure relative to sea level.

All you need now is a few hundred of these projects around the country and you have your own weather service.

Learn about your environment

Seriously, though, this project is a great way to learn about sensors, Wi-Fi and the environment.

You can power it off-grid using a phone Power Bank and with modifications, change the record period and store the results to MicroSD card. From there, you can do some basic data analysis and learn the relationship between these three weather factors.

The ESP8266 Wi-Fi module we’ve been looking at over the last couple of articles is the perfect tool for this kind of application. It’s easy to use, incredibly low-cost and small enough to hide in almost anything.