[How To] Add barometric pressure and temperature sensor to Openwrt

This is the How To connect a I2C BMP085 barometric pressure and temperature sensor to Openwrt.


For Python, I2C installation and I2C hardware configuration tips, please read my previous post here:

[[How To] Add 20x4 LCD (HD44780) to a OpenWrt router via I2C](http://[How To] Add 20x4 LCD (HD44780) to a OpenWrt router via I2C)


NOTE: this device operates at 3.3V and does not need a voltage level shifter, so you can connect SDA and SCL pins of this device directly to the correspondent Gpio pins of the router :grinning:
You can connect many I2c devices to the same bus (SDA and SCL), just wire them in parallel, but pay attention to the total current consumption and operating voltage of each device, you cannot connnect in parallel devices operating at different voltages!

Connections:

3V0 pin = 3.3V of the router
SDA = SDA GPIO the router
SCL = SCL GPIO of the router
GND = GND of the router


lets start:

to check if the system detects the device and to check the I2C address (it should be 77), do the following:

i2cdetect -y 0

test if everything worked. A device should be displayed. For me it looks like this:

0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77

BMP180 Software

The easiest way to address the sensor is to take an existing library that already has all the functions. Adafruit offers such a library for Raspberry but luckily it also works for OpenWrt :grinning:

We need to clone the repository of Adafruit (git must be installed).

opkg update
opkg install git git-http
git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code/
git checkout 9ff733d59242a02f7ccd0222001ce80f6090a978
cd Adafruit_BMP085

Then we can do the first test:

python Adafruit_BMP085_example.py

The script will provide an output similar to this one:

Temperature: 25.70 C
Pressure: 1007.08 hPa
Altitude: 50.83


Credits: http://49v.com/viewtopic.php?f=24&t=230&p=515#p515

2 Likes

Did you try using pip to install the required libraries in a venv, or did you go direct into the root install of Python?

I use the BME280, BME680, MAX30205, MCP9808, and HDC1010 in various applications and the basic approach would be the same for any of them. I do tend to write my own drivers, as the Adafruit Python drivers have been somewhat abandoned in favor of their own "flavor" of Python.

I go direct into the root install of Python

@ pilovis Could you please add targeted hardware to subject line e.g. Raspberry if there is some place left?

Why do you want to restrict this to Raspi? Every device where I2C is accessible should do.

You are right, I couldn't imagine someone is poking around at a standard router without proper pin header for I²C ;- )

Does "all" routers have an I²C bus? Estimated around %?

I'm not sure, but the inexpensive GL.iNet devices have a header for I2C called out. At under US$20 in a case, they're an interesting platform.

Hi Jeff,
yes I took a short look at https://www.gl-inet.com/ and indeed nearly all are marked with "GPIO header" although I don't ckecked if this also means I2C bus.

As a "normal" usr my first thoughts where centered around standard consumer routers. I superficial ckecked for R7800 and found nothing so probably it's a niche for enthusiasts, nevertheless not uninteresting e.g. for some people to add a LCD matrix display for router status.

What surely would have a heavy commercial impact would be some kind of controllable RGB lightning for routers ;- )

I agree that many consumer devices can be "challenging" to find the right pads, if they exist at all. I'll be checking my Archer C7's unpopulated header one of these days.

On the GL.iNet, for example, https://openwrt.org/toh/gl.inet/gl.inet_gl-ar300m-lite shows two GPIOs "tagged" as I2C and two "tagged" for serial.

1 Like

People are doing the strangest things if they are only desperate enough... :slight_smile:

3 Likes

For this project I used the TP-Link TL WDR4300 (TL-WDR3600 is almost the same) and OpenWrt 18.06.1

Any Router that supports OpenWrt 18.06.1 and has at least two free GPIO pins availabe and 32 Mbytes or more of ram, is OK for this project.

if you don't want to open your router and solder any pin inside, simply buy an USB to I2C dongle and use kmod-i2c-tiny-usb :grinning:

3 Likes

@pilovis
many thanks for this and other tutorials
Do you know how to read the data using sysfs, i.e. without python?

I’m familiar with these devices and you need to access them with “raw” I2C commands. There also are some complex calculations required to convert the data read from the registers to meaningful values. If you wanted “turn-key” access from OpenWrt, a dedicated microcontroller with a USB interface would be my recommendation.

There is another option - with kmod-iio-bmp280-i2c driver the ready-to-use data are available through sysfs. While I was able to read the sensor data, I'm also getting some weird error messages in the log. That's why I was curios if anyone else tried that way.

echo "bmp180 0x77" > /sys/bus/i2c/devices/i2c-0/new_device

# cat /sys/bus/iio/devices/iio\:device0/in_temp_input 
19500

log:
[ 2118.066798] bmp280 0-0077: non-rising trigger given for EOC interrupt, trying to enforce it
[ 2118.075672] i2c i2c-0: new_device: Instantiated device bmp180 at 0x77
[ 2185.639232] bmp280 0-0077: timeout waiting for completion

There is also a kmod-bmp085-i2c driver, but I have no idea on how to use it.

1 Like

OK, sorted out with another sensor - BME280.
Initial configuration:

insmod i2c-gpio-custom bus0=0,2,3
modprobe bmp280
modprobe bmp280-i2c
echo "bme280 0x76" > /sys/bus/i2c/devices/i2c-0/new_device

reading temperature:

# cat /sys/bus/iio/devices/iio\:device0/in_temp_input
20560

Here is a log:

Custom GPIO-based I2C driver version 0.1.1
i2c-gpio i2c-gpio.0: using pins 2 (SDA) and 3 (SCL)
i2c i2c-0: new_device: Instantiated device bme280 at 0x76