Supporting Sensors

You are here:
< All Topics

Install I2C Helper Tools

Install the i2c-tools package, so you can confirm connections to I2C devices.

$ sudo apt-get install -y i2c-tools

Run i2cdetect to ensure the Adafruit BME680 breakout is detected.

$ sudo i2cdetect -y 1
a device responded at 0x77, which is the default I2C address of the Adafruit BME680.

Install the BME680 Library

The easiest way to work with the Adafruit BME680 Breakout is with the Adafruit Library, which requires a few dependencies. Let’s get those installed first.

  1. Start by installing Python 3 Pip:
$ sudo apt install python3-pip
  1. Use pip3 to install the following dependencies:
$ sudo pip3 install RPI.GPIO pyserial adafruit-blinka adafruit-circuitpython-bme680

Adafruit has a clean test process;

cd ~
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py

Links;

POST to database

In this case we post the data to Dolos data via JSON. Dolos is another PI then runs LAMP stack supporting an API to receive the data and store in MySQL database then display said data via a web page.

from busio import I2C
import adafruit_bme680
import time
import board
import requests
import json

# Create library object using our Bus I2C port
i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c)

# change this to match the location's pressure (hPa) at sea level
bme680.sea_level_pressure = 1013.25

print("\nTemperature: %0.1f C" % bme680.temperature)
print("Gas: %d ohm" % bme680.gas)
print("Humidity: %0.1f %%" % bme680.relative_humidity)
print("Pressure: %0.3f hPa" % bme680.pressure)
print("Altitude = %0.2f meters" % bme680.altitude)

while True:

    post_data = {'id':10,'8':0, '9':0, '18':0, '60':0}
    post_data['8'] = round(bme680.temperature,2)
    post_data['18'] = round(bme680.pressure,2)
    post_data['9'] = round(bme680.relative_humidity,2)
    post_data['60'] = (bme680.gas / 1000)
	  
    now = time.localtime()
    current_time = time.strftime("%H:%M:%S",now)
    print (current_time , post_data)  
    # Post to http://dolos/api/ADDNEW
    requests.post("http://dolos/api/ADDNEW",json.dumps(post_data))

    time.sleep(300)

Use SYSTEMD to start as service

systemd provides a standard process for controlling what programs run when a Linux system boots up.

  1. Create A Unit File

Open a sample unit file using the command as shown below:

sudo nano /etc/systemd/system/sensors.service

Add in the following text :

[Unit]
Description=Sensors
After=network.target

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Restart=on-failure
RestartSec=5s
Type=forking
User=root
ExecStart=/home/allen/sensors.sh

[Install]
WantedBy=multi-user.target

This defines a new service called “Sensors” and we are requesting that it is launched once the multi-user environment is available. The paths are absolute. The “ExecStart” parameter is used to specify the command we want to run in this case a bash script.

#!/bin/bash
nohup /usr/bin/python3 /home/pi/sensors/post_BME680.py & >> /tmp/BME680.log 2>&1

The permission on the unit file needs to be set to 644 :

sudo chmod 644 /home/pi/sensors.sh

Step 2 – Configure systemd

Now the unit file has been defined we can tell systemd to start it during the boot sequence :

sudo systemctl daemon-reload
sudo systemctl enable sensors.service

Reboot the Pi and your custom service should run:

sudo reboot

Previous Serial Access
Table of Contents