Build a Apple HomeKit Temperature Sensor (BME280) Using a RaspberryPI and a BME280

20K3569

Intro: Build a Apple HomeKit Temperature Sensor (BME280) Using a RaspberryPI and a BME280

I have been playing around around with IOT devices for the last few months, and have deployed around 10 different sensors to monitor conditions around my house and cottage. And I had originally started using the AOSONG DHT22 temperate humidity sensor, but found that after a few months the values from the humidity sensor where off majorly. I would be looking at the humidity and it would be showing 40% or more higher than the actual conditions. So I looked around and found that the Bosch BME280 Temperature/Pressure/Humidity sensor had a very good reputation for accuracy ( http://www.kandrsmith.org/RJS/Misc/Hygrometers/ca... ). So in this instructable we will connecting a Bosch BME280 to Raspberry PI Model 2, and making the information available to Apple HomeKit via Homebridge.

STEP 1: Collect the Parts You Need

For parts, go to your favourite parts store and purchase.

  • 1PCS GY-BME280 3.3 precision altimeter atmospheric pressure BME280 sensor module
    • There are numerous breakout board variations of these out there. The circuit I use was based on the GY-BME/P280 breakout board, but would work with others as well.
  • 50cm 5pin female to female DuPont connector cable

I already had the RaspberryPI, so I didn't need to purchase that.

For a case for the BME280, I used an old SD Memory card holder that I had kicking around. You may want to look around and see what you can find that is similar.

STEP 2: Wiring the Sensor

To connect the sensor we are going to use one end of the 5 pin female/female dupont cable to connect to the RaspberryPI and the other to the sensor. This will require soldering ;-)

  1. Cut 5 pin female/female dupont cable roughly in half, and we will use one end for the connection to the sensor. The other end is spare and could be used for a second sensor.
  2. Trim the cut ends of the wire roughly 3mm, and tin the ends.
  3. Following the attached schematic, solder the wire ends to the appropriate connections on the BME280.
  • Dupont connecter ( RPI ) Pin 1 ( 3.3 VCC ) connects to Pin 1 - ( VCC ) on the sensor
  • Dupont connecter ( RPI ) Pin 2 ( SDA1 ) connects to Pin 4 - ( SDA ) on the sensor
  • Dupont connecter ( RPI ) Pin 3 ( SCL1 ) connects to Pin 4 - ( SCL ) on the sensor
  • Dupont connecter ( RPI ) Pin 4 ( GPIO4 ) is not used, and the wire should be trimmed at the dupont connector end.
  • Dupont connecter ( RPI ) Pin 5 ( GND ) connects to Pin 4 - ( GND ) on the sensor
  • Pins 5 ( CSB ) and 6 ( SDO ) are unused on the sensor end.

STEP 3: Connect the Sensor to the RaspberryPI

To connect the sensor to the RaspberryPI, please power down your PI. And connect the dupont connector to the 40 pin GPIO connector, lining up the pins as follows. This will correspond to left side of the 40 pin header, starting at the top.

1. Connecting the sensor

  • Dupont connecter Pin 1 ( 3.3 VCC ) connects to RPI Pin 1
  • Dupont connecter Pin 2 ( SDA1 ) connects to RPI Pin 3
  • Dupont connecter Pin 3 ( SCL1 ) connects to RPI Pin 5
  • Dupont connecter Pin 4 ( GPIO4 ) connects to RPI Pin 7
  • Dupont connecter Pin 5 ( GND ) connects to RPI Pin 9

2. Power on your RaspberryPI

STEP 4: Configure Your RaspberryPI to Connect to the Sensor

For these steps we need your RaspberryPI powered on, and you need to login to it.

1. See if you can see the sensor via the i2c bus

sudo i2cdetect -y 1

And the output should look like this, the important part of this output is the 76 in the row 70:. This your sensor

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 

10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

70: -- -- -- -- -- -- 76 --

In the event that you get command not found or other errors, please follow the steps here.

Adafruit - Configuring I2C

For all my RaspberryPI's I needed to follow these steps.

2. Add permissions to the account you will be running homebridge from to connect to the i2c bus on the RaspberryPI. Do this as the user you will be running homebridge from.

sudo adduser $USER i2c

STEP 5: Install the Homebridge-bme280 Plugin

I'm going to assume that you already have homebridge installed and working on the RaspberryPI, and if you don't there a lot of guides on the internet to get it up and running on the RaspberryPI.

1. Install homebridge-bme280 with the command

sudo npm install -g NorthernMan54/homebridge-bme280 --unsafe-perm

If this fails with this error

npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone -q git://github.com/NorthernMan54/homebridge-bme280.git /var/root/.npm/_cacache/tmp/git-clone-7237d51c npm ERR! fatal: could not create leading directories of '/var/root/.npm/_cacache/tmp/git-clone-7237d51c': Permission denied npm ERR!

Try this

sudo su -

npm install -g NorthernMan54/homebridge-bme280 --unsafe-perm

2. Create your config.json file in ~/.homebridge with the following:

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-154"
    },
    "description": "This is an example configuration file with one fake accessory and one fake platform. You can use this as a template for creating your own configuration file containing devices you actually own.",
    "accessories": [
{
            "accessory": "BME280",
            "name": "Sensor",
            "name_temperature": "Temperature",
            "name_humidity": "Humidity",
            "options": {
              "i2cBusNo": 1,
              "i2cAddress": "0x76"
            }
        }
    ],
    "platforms": [
    ]
}

3. Start homebridge, the output should look something like this.

[12/11/2016, 6:25:29 AM] Loaded plugin: homebridge-bme280<br>[12/11/2016, 6:25:29 AM] Registering accessory 'homebridge-bme280.BME280'
[12/11/2016, 6:25:29 AM] ---
[12/11/2016, 6:25:30 AM] Loaded config.json with 1 accessories and 0 platforms.
[12/11/2016, 6:25:30 AM] ---
[12/11/2016, 6:25:30 AM] Loading 0 platforms...
[12/11/2016, 6:25:30 AM] Loading 1 accessories...
[12/11/2016, 6:25:30 AM] [Sensor] Initializing BME280 accessory...
[12/11/2016, 6:25:30 AM] [Sensor] BME280 sensor options: {"i2cBusNo":1,"i2cAddress":118}
Found BME280 chip id 0x60 on bus i2c-1 address 0x76
[12/11/2016, 6:25:31 AM] [Sensor] BME280 initialization succeeded
[12/11/2016, 6:25:31 AM] [Sensor] data(temp) = {
  "temperature_C": 18.23,
  "humidity": 39.1710189421353,
  "pressure_hPa": 1016.8910377944043
}
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
^[[30;47m                       ^[[0m
^[[30;47m    ┌────────────┐     ^[[0m
^[[30;47m    │ 031-45-154 │     ^[[0m
^[[30;47m    └────────────┘     ^[[0m
^[[30;47m                       ^[[0m
[12/11/2016, 6:25:33 AM] Homebridge is running on port 51826.
[12/11/2016, 6:26:30 AM] [Sensor] Polling BME280
[12/11/2016, 6:26:30 AM] [Sensor] data(temp) = {
  "temperature_C": 18.35,
  "humidity": 39.27056837670529,
  "pressure_hPa": 1016.8940344587268
}
[12/11/2016, 6:27:30 AM] [Sensor] Polling BME280
[12/11/2016, 6:27:30 AM] [Sensor] data(temp) = {
  "temperature_C": 18.29,
  "humidity": 38.79282900165324,
  "pressure_hPa": 1016.9261147858624
}

4. Pair your homebridge instance with your iPhone if required.

5. Enjoy

Please note that the barometric pressure sensor is only visible in 3rd party homekit apps, and not in "Home",

6. Credits

  • Thanks to Robert X. Seger for the homebridge-bme280 plugin.
  • Thanks to Skylar Stein for the node.js bme280-sensor module
  • Adafruit for publishing the I2C setup guide.

50 Comments

Hello,
Excellent howto.
Please, for newbies, add this:

Until : npm install -g git+ssh://git@github.com/NorthernMan54/homebridge-bme280.git --unsafe-perm
create your ssh key and put public key to github.com profile. You need this to clone with ssh from github.com.

Kind regards,
Hi there,
firstly a great guide. I will finally utilize my old Raspberry Pi 1.
However, I have a problem with the installation for the BME.
Executing as root:
npm install -g NorthernMan54/homebridge-bme280 --unsafe-perm
Results to:
npm ERR! code ENOENT
npm ERR! syscall spawn git
npm ERR! path git
npm ERR! errno ENOENT
npm ERR! enoent Error while executing:
npm ERR! enoent undefined ls-remote -h -t ssh://git@github.com/NorthernMan54/homebridge-bme280.git
npm ERR! enoent
npm ERR! enoent
npm ERR! enoent spawn git ENOENT
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-01-16T14_42_29_268Z-debug.log

any possible ideas?
It's the same when executing as normal user + sudo.
What version of node and npm is it running, it needs to be something modern. Also some of the older versions of NPM had issues that are resolved with modern versions
Hi there and sorry for the delayed answer.

root@raspberrypi:/home/user# node -v
v12.13.0

root@raspberrypi:/home/user# npm -v
6.12.0

I had seen similar issues with older versions of node from a few years ago and am wondering if they broke something again.

I did re-validate it last week and everything was working okay. So I’m stumped as to the problem with NPM.

I’m stuck right now in regards to what to try, as this is an issue with NPM and not the package.
Thanks a lot for the quick reply. I'll try to update to a newer version and check if the problem still exists. I'll post an update.
Thanks for this it's brilliant, do you know how to transpose the data from the JSON file to allow me to upload to Weather Underground?
Hey bud! Great guide, but I have a problem - I get all the data correctly from bme280 when I use troubleshooting mode in homberidge in iPhone (see screenshots), but Home app would show that both temperature and humidity are at 0. Any ideas? And how do I add pressure as well?

Thanks.
my bad - didn't add root to i2c
Hi, great guide, however, Im curious could this work if the Raspberry PI? My reasoning is that I have a Pi/HomeBridge running in my office but want to get temps/humidty from garage. So thinking I could put a PiZero in garage with sensor - would this then talk to the Pi in the office?
HomeKit supports multiple homebridge's, so this would be fine. ( I have never used a PiZero, so can't comment on that )

Another approach is utilizing an ESP8266, which is significantly cheaper. I use this for my remote monitoring needs

https://github.com/NorthernMan54/homebridge-mcuiot

I have 0 experience with Raspberry PIs and Homebridge, but would it be possible to use a raspberry pi zero W for this? (my goal: make the smallest/cheapest wirelessly accessible temperature/humidity sensor possible)
And would this be a good first homebridge project? I do know how to program :P
A RPI 0 should work, but it you goal is small, then I believe there is an HomeKit esp8266 based temperature sensor on GitHub. You should be able to find it with google.
Does anybody knows how to get the trends graph working in the Eve app?
I just read this on the Github page of the homebridge-bme280 plugin:

Support the graphing feature of the Eve app for trends

But there ist nothing showing up in my Eve app
I use some other temperature related plugins for weather etc. that have a working trend graph. So i know that in theory it can be done. But homebridge-bme280 is not working this way it seems...
Please open an issue against the plugin on Github, and if you can include a log from the homebridge much appreciated.

I checked my setup, and it is working so it must be something minor going on.

Homebridge is loading and everything is running but every time the data of the sensor is polled (every minute) I get the message

[Date] [Sensor] Error: BME280 Not Initalized

Sensor is working fine using for example this script.

Any idea?

Hey buddy !
Have you added to the group i2c the user you're executing homebridge ?

To do it, use sudo adduser youruser i2c
If you're running homebridge with systemd for bootup. You should find the username on the /etc/default/homebridge file.

Seeya !

hey Boss, I'm having an issue with config.json, json validator says that there's a problem and it won't load :/ I need your help please :)

More Comments