Room Climate Monitor

Analog sensor polling

In order to read the analog channels A0 – A4 of ADS1015, you must add the Adafruit_ADS1x15.py module to your path, which is part of the Adafruit Raspberry Pi Python library. For measuring light-intensity using an LDR, you should have a look into the datasheet of your LDR and adjust the equation below as described in this tutorial.

#!/usr/bin/python

import time, signal, sys
from Adafruit_ADS1x15 import ADS1x15

ADS1015 = 0x00  # 12-bit ADC
ADS1115 = 0x01  # 16-bit ADC

# Select the gain
gain = 6144    # +/- 6.144V
# gain = 4096  # +/- 4.096V
# gain = 2048  # +/- 2.048V
# gain = 1024  # +/- 1.024V
# gain = 512   # +/- 0.512V
# gain = 256   # +/- 0.256V

# Select the sample rate
sps = 8    # 8 samples per second
# sps = 16   # 16 samples per second
# sps = 32   # 32 samples per second
# sps = 64   # 64 samples per second
# sps = 128  # 128 samples per second
# sps = 250  # 250 samples per second
# sps = 475  # 475 samples per second
# sps = 860  # 860 samples per second

# Initialise the ADC using the default mode (use default I2C address)
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
adc = ADS1x15(ic=ADS1015)

# Read channel 0 in single-ended mode using the settings above
volts = adc.readADCSingleEnded(0, gain, sps) / 1000
R = 48 / volts - 10

# For R to LUX conversion, adjust the equation below according to the datasheet of your LDR
lux = 41.1054 * R ** 0.3226
print round(lux,1)

Controlling the LEDs and OLED display

Printing of sensor data on the OLED display can be achieved with a few lines of Python code. In order to retrieve data from a MySQL database, you must install the python-mysqldb module. The Adafruit 128×32 OLED display is sold in two different flavors, SPI and I2C. At the time of writing, Adafruit Python libraries were only available for the Arduino platform. However, they were ported for the Raspberry Pi and can be found on github, including some example code for controlling the display. The three status LEDs are controlled with the RPi.GPIO Python module, which is already part of the Debian Wheezy distro.

#!/usr/bin/python
# -*- coding: utf8 -*-

import datetime
import gaugette.ssd1306 as ssd1306
import RPi.GPIO as GPIO
import MySQLdb as raspi
from time import sleep

# GPIO settings
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT) # GPIO_18, Green LED
GPIO.setup(16, GPIO.OUT) # GPIO_23, Yellow LED
GPIO.setup(15, GPIO.OUT) # GPIO_22, Red LED

# font settings
import fonts.arial_regular_10
import fonts.ms_reference_sans_serif_regular_10
import fonts.calibri_regular_10
import fonts.arial_monospaced_for_sap_regular_10
import fonts.arial_monospaced_for_sap_regular_12
import fonts.arial_monospaced_for_sap_regular_14
import fonts.arial_monospaced_for_sap_regular_20

# connect to mysql database
database = raspi.connect('localhost', 'user', 'password', 'database_name')
query = database.cursor()
query.execute("SELECT * FROM sensors ORDER BY timestamp DESC LIMIT 1") # the default cursor returns the mysql data in a tuple of tuples. This requires tuple unpacking ...
[[date, time, timestamp, temp1, hum, temp2, bar, alt],] = query.fetchall()
DATE = str(date) + " - " + str(time)
VAL1 = str(temp1) + " C   " + str(hum) + " {c7f7cb1468c0d02af358b3ce02b96b7aadc0ce32ccb53258bc8958c0e25c05c4}"
VAL2 = str(temp2) + " C   " + str(bar) + " hPa" 

# intialize display
ssd1306 = ssd1306.SSD1306_I2C(bus=1, device=0x3c)
ssd1306.begin()
ssd1306.clear_display()
ROW1 = ssd1306.draw_text(3,0,DATE)
ROW2 = ssd1306.draw_text(3,10,VAL1)
ROW3 = ssd1306.draw_text(3,20,VAL2)
ssd1306.display()

# turn off all led
GPIO.output(12, False)
GPIO.output(15, False)
GPIO.output(16, False)

# switch led depending on humidity
if hum > 50:
 GPIO.output(15, True)
elif hum >= 40 and hum <= 50:
 GPIO.output(16, True)
elif hum < 40:
 GPIO.output(12, True)