Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
After acquiring a Raspberry Pi and reviewing Eben Upton’s Raspberry Pi User Guide—especially the GPIO sections—I embarked on my first project: a home temperature monitoring system. This post details the initial iteration using a Raspberry Pi, an Adafruit MCP9808 breakout, an old Mac Mini, InfluxDB, Grafana, Python, and the runit process supervisor.
Sensor Hardware
For the temperature sensor I selected the MCP9808 breakout board from Adafruit. My choice was driven by:
- Native I²C interface – the Pi’s GPIO pins 3 (SDA) and 5 (SCL) support the required bus.
- Wide voltage range (2.7 V–5.5 V) – compatible with the Pi’s 3.3 V and 5 V rails.
- Cost‑effective – under $5 USD, ideal for a beginner’s first project.
Circuit Assembly
The MCP9808 board arrives mostly assembled; the only soldering required is attaching the 8‑pin header. I used a GPIO breakout and a breadboard to connect the Pi to the sensor, simplifying wiring and making future adjustments easier. The essential connections are:
- Power (VDD)
- Ground (GND)
- Data (SDA)
- Clock (SCL)
Datastore
Capturing data over time allows for trend analysis and correlation with external events. I chose InfluxDB for its time‑series focus and intuitive query language. The database runs on an early 2009 Mac Mini (OS X 10.10) under Homebrew:
brew install influxdb
Configure the InfluxDB Database
After installation, I created a database and a dedicated user via the web console (default port 8083). I also set up a database named mcp9808_test to store the sensor readings.
Raspberry Pi Configuration
Enable I²C
The Pi does not load I²C modules by default. Add these lines to /etc/modules and reboot:
i2c-bcm2708
i2c-dev
Verify the sensor is detected with:
sudo i2cdetect 1
Sensor Software
I leveraged Adafruit’s MCP9808 Python wrapper, which also pulls in the I²C abstraction layer.
Install Build Dependencies
sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus
Install the Adafruit MCP9808 Wrapper
cd ~/Downloads
git clone https://github.com/adafruit/Adafruit_Python_MCP9808
cd Adafruit_Python_MCP9808
sudo python setup.py install
Read, Report, Repeat
Below is the poll.py script that reads temperature every minute and writes the value to InfluxDB:
#!/usr/bin/python
import time
import Adafruit_MCP9808.MCP9808 as MCP9808
from influxdb import InfluxDBClient
# Prepare payload for InfluxDB
def temperature_data(degrees_c):
return [
{
'points': [[c_to_f(degrees_c)]],
'name': 'Temperature Readings',
'columns': ['degrees_f']
}
]
# Convert Celsius to Fahrenheit
def c_to_f(c):
return c * 9.0 / 5.0 + 32.0
# Initialize sensor
sensor = MCP9808.MCP9808()
sensor.begin()
# Capture interval in seconds
capture_interval = 60.0
# Connect to InfluxDB (replace placeholders with actual values)
influxClient = InfluxDBClient('<influx-db-host>', 8086, 'mcp9808', '<password>', 'mcp9808_test')
# Continuous loop
while True:
temp_c = sensor.readTempC()
print('Temperature {0:0.3} F'.format(c_to_f(temp_c)))
influxClient.write_points(temperature_data(temp_c))
time.sleep(capture_interval)
Run the script as root to access the GPIO pins:
sudo python /path/to/poll.py
Process Supervision with runit
To ensure poll.py stays alive across reboots and power cycles, I configured runit as the process supervisor. Detailed setup steps are available in the original project documentation.
Summary
With the sensor, database, and monitoring stack in place, you now have a reliable, low‑cost home temperature logger. Use Grafana to visualize the data, set alerts, and gain actionable insights into your environment.
Manufacturing process
- DS18B20 Temperature Sensor – Precise 1‑Wire Digital Thermometer for Industrial & Consumer Use
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- Professional Raspberry Pi Temperature Monitoring with DS18B20
- Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
- Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
- How to Connect a DS1820 One‑Wire Temperature Sensor to a Raspberry Pi Using GPIO
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Build a Low‑Cost Raspberry Pi Soil Moisture Sensor for Smart Irrigation
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Arduino Temperature Sensor Project: Read, Convert, and Display Fahrenheit