Home Made Pi — Part 1

Steve Clifton
4 min readApr 28, 2020

Something I have always wanted to do is build my own weather station and have the readings viewable from any web browser near me at a time. Having full control of my data and being able to send it how and where I like is what lead me to creating this new project, simply titled MyHome.

MyHome has 2 parts, the Client (Installed on the Pi and written in Python), and the API/Website to receive and display my data (Created in PHP using Laravel).

Both of these projects are completely open source (click the links above to inspect them).

At the time of writing this, MyHomePi has the following features

  • Supports only 1 sensor — a BME280 temperature sensor
  • Collects the data and writes it to a local SQLite database
  • Exports unsent data via an HTTP Post request to a REST API, supporting auth using a bearer token

MyHome (API/Website) has the following features (more to come)

  • Simple card to show current, high and low temps for the day
  • Graph of the days temperature movements
  • Table of all the days records
  • Pretty gradient background with hype transitions

Setting up the Pi

Some basic housekeeping is in order to get the Pi up to scratch, basically we want to update, enable ssh and I2C from the GUI desktop, then throw away the HMDI cable and ssh into your Pi.

This whole process below only takes ~10 minutes

ssh pi@raspberrypi.local

Install stuff we need

sudo apt install vim 
sudo pip3 install RPi.bme280
sudo apt install i2c-tools python-smbus telnet -y
sudo apt install sqlite3
sudo pip3 install Adafruit_Python_DHT
sudo apt-get install libgpiod2

From the root directory, create our folders and git clone MyHomePi

mkdir Project
cd Project
git clone https://github.com/steveclifton/MyHomePi.git

Get the location of the BME280. Here I can see its location 76

sudo vim /boot/config.txt
Uncomment dtparam=i2c_arm=on
sudo i2cdetect -y 1

Now we need to create our environment data and populate the required details. There are two types of variables, LIVE and BUILD. Live is used for exporting data to a remote server, BUILD can be used for testing data locally.

cp env.example.py env.py
# If you are exporting data, change APP_LIVE=True
# In the BME280_DEVICES, add the above location, as a string, and a name you want to give it. Multiple sensors are supported - though untested at this stage
Example of BUILD details

Run the setup script, which creates the required Database tables

python setup.py

Now manually trigger the collect script and check that there is some data being saved into the database. You should have 1 record as below

python collectsensordata.py
sqlite3 database.sqlite
sqlite> select * from bme280;
sqlite> .quit #exit
Output from SQLite query

Congrats! We can now collect data from our BME280 temp sensor!

Now, if you are planning on exporting the data to MyHome (as am I), and you have populated the required ENV variables, lets test exporting the data

python exportsensordata.py

If the data was exported successfully, you should see it in the MyHome dashboard, as below. Otherwise, check the SQLite errorlogs table for possible reasons it has failed.

MyHome dashboard

Automation

Setup 2 Cronjobs to automate the collection and exporting of the data. I have these at a frequency of once every 5 minutes

crontab -e
*/5 * * * * python3 ~/Project/MyHomePi/action_runall.py

That’s it! We now have the data being collected and saved onto the Raspbery Pi, and exported off to a remote server.

Hardware

Solder the BME280 onto the pins, then using a GPIO+breadboard combo, use jumper cables to connect to the Pi. MAKE SURE THE POWER IS OFF FIRST — we don’t want to short anything out. Connect the SDA and SCL points, and make sure to use the appropriate 5v or 3.3v power connection.

BME280 Temperature Sensor
GPIO Extension + Breadboard

--

--