agxies
Published © CC BY-NC-SA

Raspberry Pi Temperature and CO2 Monitor

Measure and display air quality with the Raspberry Pi.

IntermediateShowcase (no instructions)20,920
Raspberry Pi Temperature and CO2 Monitor

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
0.96" OLED 64x128 Display Module
ElectroPeak 0.96" OLED 64x128 Display Module
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
SparkFun Air Quality Breakout - CCS811
SparkFun Air Quality Breakout - CCS811
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
or any other Debian based Linux distribution

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
optional
3D Printer (generic)
3D Printer (generic)
optional

Story

Read more

Schematics

Schematic

Code

Code

Python
#dtparam=i2c_arm_baudrate=10000 in /boot/config.txt

import time
import board
import busio
import adafruit_ccs811

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess

import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

i2c = board.I2C()  # uses board.SCL and board.SDA
ccs811 = adafruit_ccs811.CCS811(i2c)

# Wait for the sensor to be ready
while not ccs811.data_ready:
    pass

# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0

# 128x32 display with hardware I2C:
#disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)

# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

# Note you can change the I2C address by passing an i2c_address parameter like:
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C)

# Alternatively you can specify an explicit I2C bus number, for example
# with the 128x32 display you would use:
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=2)
# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0


# Load default font.
#font = ImageFont.load_default()

# Alternatively load a TTF font.  Make sure the .ttf font file is in the same directory as the python$# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype('Minecraftia-Regular.ttf', 13)

while True:

    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    
    try:
        eco2 = ccs811.eco2
    except:
        print("Failed getting CO2 value")
    
    try:
        tvoc = ccs811.tvoc
    except:
        print("Failed getting TVOC value")
    
    if humidity is not None and temperature is not None:
        # Draw a black filled box to clear the image.
        draw.rectangle((0,0,width,height), outline=0, fill=0)

        # Write two lines of text.

        digit = temperature * 10
        digit = int(digit % 10)

        humidity = int(humidity)
        temperature = int(temperature)
        draw.text((x, top+5),     "Environment",  font=font, fill=255)
        draw.text((x, top+20),    "T: " + str(temperature) + "." + str(digit) + "C", font=font, fill=255)
        draw.text((x+64, top+20), "H: " + str(humidity) + "%",  font=font, fill=255)
        draw.text((x, top+35),    "CO2: " + str(eco2) + " PPM", font=font, fill=255)
        draw.text((x, top+50),    "TVOC: "  + str(tvoc) + " PPB", font=font, fill=255)

        # Display image.
        disp.image(image)
        disp.display()
    time.sleep(5)

Credits

agxies

agxies

1 project • 6 followers

Comments