Skip to content

Instantly share code, notes, and snippets.

@pingud98
Last active June 25, 2021 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingud98/da4c3080ae85582f964a67b388a2e437 to your computer and use it in GitHub Desktop.
Save pingud98/da4c3080ae85582f964a67b388a2e437 to your computer and use it in GitHub Desktop.
Raspberry Pi Air Monitor setup script
#!/bin/bash
echo "Raspberry Pi Zero W Air Monitoring Setup Script"
echo "by J. Devine"
echo "*** For use on a fresh installation! 10/6/21 ***"
echo "First we set a secure password!"
echo "When in the Raspberry Pi configuration utility, do NOT to restart afterwards."
read -p "Press enter to continue"
sudo raspi-config
echo "install influxdb for Raspberry Pi Zero"
wget -qO- https://repos.influxdata.com/influxdb.key | gpg --dearmor > /etc/apt/trusted.gpg.d/influxdb.gpg
#export DISTRIB_ID=$(lsb_release -si); export DISTRIB_CODENAME=$(lsb_release -sc)
echo "deb [signed-by=/etc/apt/trusted.gpg.d/influxdb.gpg] https://repos.influxdata.com/debian buster stable" | sudo tee -a /etc/apt/sources.list.d/influxdb.list
sudo apt-get update && sudo apt-get install -y influxdb rsync git screen i2c-tools iptables-persistent
sudo systemctl unmask influxdb.service
sudo systemctl start influxdb
sudo netstat -naptu | grep LISTEN | grep influxd
echo "install Grafana for Raspberry Pi Zero"
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana-rpi_8.0.0_armhf.deb
sudo dpkg -i grafana-rpi_8.0.0_armhf.deb
echo "installing avahi zeroconf daemon"
sudo apt-get install -y avahi-utils avahi-daemon
#echo "domain-name=local" | sudo tee -a /etc/avahi/avahi-daemon.conf
#echo "publish-hinfo=yes" | sudo tee -a /etc/avahi/avahi-daemon.conf
#echo "publish-workstation=yes" | sudo tee -a /etc/avahi/avahi-daemon.conf
sudo systemctl enable avahi-daemon.service
sudo systemctl start avahi-daemon.service
sudo systemctl enable grafana-server.service
sudo systemctl start grafana-server.service
echo "switching to Python3"
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
echo "installing python3 influxdb module"
sudo apt-get install -y python3-influxdb
echo "installing pip for python 3 and influxdb library"
sudo apt-get install -y python3-pip
pip3 install --upgrade influxdb
echo "installing log2ram"
git clone https://github.com/azlux/log2ram.git
cd log2ram
chmod +x install.sh
sudo ./install.sh
echo "redirecting port 3000 to port 80 for Grafana"
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 3000
sudo iptables-save > /etc/iptables/rules.v4
echo "installing BME680 library and MQTT client"
pip3 install bme680 paho-mqtt
echo "setting up I2C_LCD, CSS811 and air monitoring script"
cd /home/pi
mkdir airmon
cd airmon
wget -O I2C_LCD_driver.py https://gist.githubusercontent.com/vay3t/8b0577acfdb27a78101ed16dd78ecba1/raw/6cc9bc9989e549c97b32b9df44fe17b1db5a8950/I2C_LCD_driver.py
wget -O airmon_if.py https://gist.githubusercontent.com/pingud98/48062767e93534b0bb2e049bba131df0/raw/e3c021bc8e30b58c5c1a9cd1bb3188a6707f184e/airmon_if.py
wget -O CCS811_RPi.py https://gist.githubusercontent.com/pingud98/f21b44db4a646da267f964396b41c017/raw/99550ee263091a371de7d423cfd6a0860678aef3/CCS811_RPi.py
wget -O airmon.service https://gist.githubusercontent.com/pingud98/a65a5cbab9b008a32b2f7a1e84a04994/raw/f263c0939c6676b64c7614d82a14d4fe43566f2f/airmon.service
sudo cp airmon.service /etc/systemd/system/airmon.service
sudo systemctl daemon-reload
sudo systemctl enable airmon.service
sudo systemctl start airmon.service
echo "Enabling and slowing down I2C on Raspberry Pi to 10kbps, required for CCS811 sensor"
echo "dtparam=i2c_arm=on" | sudo tee -a /boot/config.txt
echo "i2c-dev" | sudo tee -a /etc/modules
echo "dtparam=i2c_arm_baudrate=10000" | sudo tee -a /boot/config.txt
echo "All done!"
read -p "Press enter to reboot"
sudo reboot now
@pingud98
Copy link
Author

pingud98 commented Jun 11, 2021

Step 1: log in (user pi, password raspberry)
Step 2: sudo raspi-config (set up Wi-Fi, enable i2c bus)
Step 3: sudo reboot now (reboots)
Step 4: log in (pi, raspberry)
Step 5: wget -O install.sh https://git.io/JZ2Js (download the installation script)
Step 6: chmod +x install.sh (make executable)
Step 7: sudo ./install.sh (run installation script)
Then follow the instructions as directed!
Software setup:
When it has completed the installation, reboot and connect to http://ip address (it'll be shown on the LCD) and log in to Grafana.
The default credentials for Grafana are user admin, password admin. You will be prompted for a new password.
Add a new datasource via the first Grafana menu, type influxdb. You should specify the address as http://localhost:8086 and the database as airmonlocal.
Save this and you can now create dashboards. For models to copy check out http://data.cosmicpi.org/d/4yQ9taggz/home-air-quality?orgId=1 which uses exactly the same schema.
NB your data is uploaded to an MQTT server and displayed on this site too!

@pingud98
Copy link
Author

Additional info you'll need to actually build it:
The parts to use with this setup:
1602 LCD display + I2C module (NB, remove the two I2C pull-up resistors with a soldering iron, otherwise the 5V might break the RPi! Photo of the board with the resistors removed is below)
CCS811 breakout board
BME680 breakout board
I also added a display stand for the 1602 screen to make the whole thing look nicer.

Wiring (paper sketch also below):
RPi pin 1 = 3.3V
RPi pin 2 = 5V
RPi Pin 3 = SDA
RPi Pin 4 = N/C (Not Connected)
RPi Pin 5 = SCL
RPi Pin 6 = GND

BME680
1 = 3.3V
2 = GND
3 = SCL
4 = SDA
5 = GND (SDO pin)
6 = N/C (CS pin)

CCS811
1 = 3.3V
2 = GND
3 = SCL
4 = SDA
5 = GND (WAK)
6 = N/C (INT)
7 = N/C (RST)
8 = N/C (ADD)

1602 I2C Module
1 = GND
2 = 5V
3 = SDA
4 = SCL

Resistors to remove from 1602 I2C Module

Wiring Sketch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment