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

7,071

27

15

Posted in TechnologyApple

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

About: Creator of multiple Apple HomeKit devices using homebridge in an attempt to increase the WAF score of my home automation attempts.

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 https://github.com/NorthernMan54/homebridge-bme280

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.

4 People Made This Project!

Recommendations

  • Microcontroller Contest

    Microcontroller Contest
  • Woodworking Contest

    Woodworking Contest
  • Casting Contest

    Casting Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

1 Questions

I'm getting 0 on both humidity and temperature under Home. If i run the script "bme280.py" i can see the correct humidity and temperature values. In installed the latest npm package from NorthernM

When installing, do you follow this step?

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

Also the log you shared would have had more debugging of the sensor during startup, can you share more the log file. Look at step 5.3 for an example.

10 more answers

Hi. I followed also step 2. Now if I run step 2 I get :

"The user `pi' is already a member of `i2c' "

My user is "pi".

The log doesn't show anything more.

Which is step 5.3?

This step, the output from homebridge shows the connection to the sensor, and the data returned. It likely has an error in your case.

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
}

I found the error that is reported in the log:

"error: Eacces permission denied open /dev/i2c-1 bme280"

Any idea?

I’m thinking homebridge isn’t running as user pi

You can see which user account is running homebridge with the ps command ie

ps -ef | grep homebridge

Then run the permission step again

Many thanks NorthernM. It is working now. The user was homebridge, not pi.

Congrats on getting it working ;-)

I have home bridge install as a system. And the log is under daemon log. I don't have log like in your example.

Can you share the log from homebridge and in particular the section showing sensor initialization? I’m thInking the issue is settings for the plugin, or account permissions.

Apr 22 13:20:23 raspberrypi homebridge[380]: send hb to: 0x0000000004cae7c7

Apr 22 13:20:23 raspberrypi homebridge[380]: {"id":-1,"method":"get_prop","params":["power","bright","rgb"]}

Apr 22 13:20:32 raspberrypi homebridge[380]: send hb to: 0x00000000035ead25

Apr 22 13:20:32 raspberrypi homebridge[380]: {"id":-1,"method":"get_prop","params":["power","bright","rgb"]}

Apr 22 13:20:33 raspberrypi homebridge[380]: send hb to: 0x0000000004cae7c7

Apr 22 13:20:33 raspberrypi homebridge[380]: {"id":-1,"method":"get_prop","params":["power","bright","rgb"]}

Apr 22 13:20:42 raspberrypi homebridge[380]: send hb to: 0x00000000035ead25

Apr 22 13:20:42 raspberrypi homebridge[380]: {"id":-1,"method":"get_prop","params":["power","bright","rgb"]}

Apr 22 13:20:43 raspberrypi homebridge[380]: send hb to: 0x0000000004cae7c7

Apr 22 13:20:43 raspberrypi homebridge[380]: {"id":-1,"method":"get_prop","params":["power","bright","rgb"]}

Apr 22 13:20:51 raspberrypi homebridge[380]: [4/22/2018, 1:20:51 PM] [Sensor] BME read error: You must first call bme280.init()

Apr 22 13:20:52 raspberrypi homebridge[380]: send hb to: 0x00000000035ead25

Apr 22 13:20:52 raspberrypi homebridge[380]: {"id":-1,"method":"get_prop","params":["power","bright","rgb"]}

And here is my config.json:

{

"bridge": {

"name": "YeeBridge",

"username": "18:00:27:40:BC:1B",

"port": 51825,

"pin": "031-45-154"

},

"platforms": [{

"platform": "yeelight",

"name": "yeelight"

}, {

"platform": "TplinkSmarthome",

"name": "TplinkSmarthome",

"addCustomCharacteristics": true,

"inUseThreshold": 0

}],

"accessories": [{

"accessory": "BME280",

"name": "Sensor",

"name_temperature": "Temperature",

"name_humidity": "Humidity",

"options": {

"i2cBusNo": 1,

"i2cAddress": "0x76"

}

}]

}

15 Comments

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?

Hello.

First of all I would like to say thank you for your work. This is a very useful description.

But unfortunatelly I cant reach the pressure data on my iPhone in the Eve app. According to these log everything works fine and I use your config.json code.

data(humi) = {

"temperature_C": 20.59,

"humidity": 62.48495695427452,

"pressure_hPa": 993.3888336949954

}

data(temp) = {

"temperature_C": 20.59,

"humidity": 62.48495695427452,

"pressure_hPa": 993.3888336949954

Have you got any idea what is the problem and how can I solve that?

Thank you :)

Rasket787

7 replies

Rasket787

I had created a custom version of the plugin that added support for Atmospheric Pressure, and had sent a pull request back to the original developer months ago asks them to merge the changes into the original plugin but that never happened. To use the custom version I created, use this command instead of the one listed in step 5.1

sudo npm install -g https://github.com/NorthernMan54/homebridge-bme28...

This will install the version with support for Atmospheric Pressure

PS I also went back and changed the instructable to include the same command as well. Tks

Hello,

Thank you for your fast reply. I install your modul and everything works :)

And is there any way to log temperature and other data into a txt or csv file? It would be great to log my home data.

If you look in the homebridge log, it will log the current temperature etc every minute.

Yes I see that, but i would like to save these data into a separate file and later i would like to make same diagram. Is it possible?

This is feasible, but I have not done this with this sensor yet. I have used loggly to do this with other sensors. You may want to look at that.

Hello! I still try to solve that, but I can't. I only have a basic linux knowledge. :( Pls help me to import those file in a separate csv.

Thank you I will try that.

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 :)

Screen Shot 2017-02-12 at 20.26.14.pngScreen Shot 2017-02-12 at 20.28.03.png
5 replies

Dom,

Just looking at the two different screen shots, and I see 2 different issues. First for your implementation of homebridge, you need config.json into the directory /root/.homebridge

Second for the invalid json, I step 5.2 I had originally included just a fragment of the config.json file outling the bme280 config. I have since gone back and changed the example in 5.2 to the complete config.json file.

With those 2 changes you should be good to go.

Thank you, this solved the issue :)
The pressure value is being shown in terminal but not in 3rd party apps, I get only temperature and humidity. How do I modify config.json file to add second sensor please ?

Dom,

I had created a pull request for the original developer of the BME280 plugin adding Atmospheric pressure to returned data, but it has not been merged back into the original code base yet. Until that happens you can install my version of the plugin, which supports atmospheric pressure, with this command

sudo npm install -g https://github.com/NorthernMan54/homebridge-bme280

And restart homebridge

This will add the Atmospheric pressure sensor. Please note the Atmospheric pressure is not supported by the Apple Home App, but with the third party home kit apps like Elgato Eve.

much obliged :) all working now!!

it's working now. Can a second bme280 be added to the same raspberry pi? I'm aware of your other tutorial with ESP8266 but if it's possible I'd like to have two sensors wired to one pi :)

I was looking at the code and it should support multiple sensors, you would just need to add an additional accessory per device in the config.json. See below for an updated fragment.

To wire the second sensor, it would go in parallel with the first, except you would need to change the i2c bus address of the second one to 0x77. To change the i2c address you would need to look at the data sheet for the sensor you purchased.

Good luck

"accessories": [

{

            "accessory": "BME280",

            "name": "Sensor",

            "name_temperature": "Temperature",

            "name_humidity": "Humidity",

            "options": {

              "i2cBusNo": 1,

              "i2cAddress": "0x76"

            }

        },
{
"accessory": "BME280",
"name": "Sensor1",
"name_temperature": "Temperature1",
"name_humidity": "Humidity1",
"options": {
"i2cBusNo": 1,
"i2cAddress": "0x77"
}
}
    ],