###############################################
## Climate Controller #########################
## Version 1.4.12 #############################
## By: Corey Beck #############################
###############################################
#### GAS SERVO CONTROL NOT IMPLEMENTED YET ####
###############################################
### DIP SWITCH CONFIGURATION OVERRIDES ANY ####
### SETTINGS IN CODE ##########################
###############################################
from climate_controller import ClimateController
from climate_controller_types import MQ2ProbeMode, UpdateMode
print ("--- Climate Controller v1.4.12 ----")
print ("--- DIP SWITCH CONFIGURATION OVERRIDES CODE SETTINGS --")
print ("1. Adjust the realtime temperature and humidity with the DHT22")
print (" -> RGB Led will change color depending how hot or cold temperature is")
print (" -> Temperature Servo will change depending how hot or cold the temperature is")
print (" -> Humidity Servo will change depending what percentage humidity")
print ("2. Display time mode by pressing time button")
print (" -> Toggle between 24hr/12hr time mode by pressing time button again")
print ("3. Display humidity mode by pressing humidity button")
print ("4. Display temperature mode by pressing the C/F button")
print (" -> Toggle between Celsius/Fahrenheit mode by pressing C/F button again")
print ("5. Display real time temperature/humidity live mode by pressing live button")
print ("6. Display maximum detected temperature/humidity mode by pressing max button")
print ("7. Display minimum detected temperature/humidity mode by pressing min button")
print ("8. Adjust Gas PPM with the MQ2")
print (" -! 30 seconds for MQ2 to heat up for readings")
print (" -! Wokwi Simulation, please adjust MQ2 Gas(PPM) prior to end of calibration")
print (" otherwise false positive could happen cause simulator issues")
print (" -> Gas Alert LED will blink when gas detected")
print ("9. Use Boot Configuration DIP Switch to switch settings. Must reset after each change")
####################################
## SETTINGS
####################################
######
####################################
# ClimateController
###################
# Set the amount of time to update climate controller
# !- Set to None or 0 to have no sleep in between update calls
CTRL_UPDATE_TIME = 0.001 # ~seconds (can adjust to impact response time and cpu load)
####################################
# MQ2
######
# Set the Carbon Monoxide(CO) ppm threshold, set this to None to use default(19.8271ppm) smoke threshold
# !- Only matters if both SkipWarmUp and SkipCalibration are set to False and MQ2_PROBE_MODE being MQ2ProbeMode.ANALOG,
# otherwise the MQ2 onboard gas threshold usually controlled by a potentiometer will be used
MQ2_CO_THRESHOLD = 25 # ~ppm
# Set the Methane(CH4) ppm theshold, set this to None to use default(1000ppm) methane threshold
# !- Only matters if both SkipWarmUp and SkipCalibration are set to False and MQ2_PROBE_MODE being MQ2ProbeMode.ANALOG,
# otherwise the MQ2 onboard gas threshold usually controlled by a potentiometer will be used
MQ2_CH4_THRESHOLD = 25 # ~ppm
# Set the Liquified Hydrocarbon Gas(LPG) ppm theshold, set this to None to use default(2000ppm) liquified hydrocarbon gas threshold
# !- Only matters if both SkipWarmUp and SkipCalibration are set to False and MQ2_PROBE_MODE being MQ2ProbeMode.ANALOG,
# otherwise the MQ2 onboard gas threshold usually controlled by a potentiometer will be used
MQ2_LPG_THRESHOLD = 30 # ~ppm
# Set the amount of time to poll the MQ2 for information
# !- MQ2 in MQ2ProbeMode.ANALOG probe mode uses large amounts of cpu, balance time and cpu with this setting(4 or 5 seconds for good balance)
# !- MQ2 in MQ2ProbeMode.DIGITAL probe mode can run faster measure times(2 seconds good balance)
MQ2_MEASURE_TIME = 5 # ~seconds
####################################
## GAS ALERT LED
################
# Set the amount of time inbetween Gas Alert led blinking
GASALERT_LED_BLINK_TIME = .25 # ~seconds
####################################
## TEMPERATURE RGB LED
######################
# Set the RGB Indicator Map
# !- Set to None to use default Indicator Map
# !- [TempF, [Red, Green, Blue]
TEMP_RGBLED_IMAP = [
# Below 32F(Min) - White
[32, [255, 255, 255]],
# Below 45F - Light Blue
[45, [178, 247, 255]],
# Below 55F - Blue
[55, [0, 0, 255]],
# Below 75F - Yellow
[75, [255, 251, 0]],
# Below 100F - Orange
[100, [255, 128, 0]],
# Below 1000F(Max) - Red
[1000, [255, 0, 0]]
]
####################################
## TEMPERATURE SERVO
####################
# Set the Temperature Servo Indicator Map
# !- Set to None to use default Indicator Map
# !- [TempF, Angle]
# Angle values 0-180
TEMP_SERVO_IMAP = [
# Below 32F(Min), 0 degree angle
[32,0],
# Below 45F, 35 degree angle
[45,35],
# Below 55F, 70 degree angle
[55,70],
# Below 75F, 105 degree angle
[75,105],
# Below 100F, 140 degree angle
[100,140],
# Below 1000F(Max), 175 degree angle
[1000,175]
]
####################################
## HUMIDITY SERVO
####################
# Set the Humidity Servo Indicator Map
# !- Set to None to use default Indicator Map
# !- [Humidity %, Angle]
# Humidity values 0-101, Angle values 0-180
HUMIDITY_SERVO_IMAP = [
# Below 10%(Min), 0 degree angle
[10,0],
# Below 20%, 19 degree angle
[20,19],
# Below 30%, 38 degree angle
[30,38],
# Below 40%, 57 degree angle
[40,57],
# Below 50%, 76 degree angle
[50,76],
# Below 60%, 95 degree angle
[60,95],
# Below 70%, 114 degree angle
[70,114],
# Below 80%, 133 degree angle
[80,133],
# Below 90%, 152 degree angle
[90,152],
# 100% and below(max), 175 degree angle
[101,175]
]
####################################
## DHT22
########
# Set the amount of time to poll the DHT22 for information
# !- 2 secs typical time for accurate measurements and cpu load balancing
DHT22_MEASURE_TIME = 2 # ~seconds
####################################
## BUTTONS
##########
# Set the debounce time for pushbuttons
BTN_DEBOUNCE_TIME = 200 # ms
####################################
## PIN SETUP
####################################
#####
####################################
# 74HC165 ISR(INPUT SHIFT REGISTER) - DIP SWITCH
###################################
ISR_CE_PIN = 1
ISR_Q7_PIN = 2
ISR_CP_PIN = 3
ISR_PL_PIN = 4
####################################
# I2C
######
I2C_SDA_PIN = 18
I2C_SCL_PIN = 19
####################################
# DS1307 RTC
############
RTC_I2C_ADDR = 0x68
####################################
# LCD1602 LCD
#############
LCD_I2C_ADDR = 0x27
#####################################
# DHT22
#######
DHT22_PIN = 22
#####################################
# MQ2
#####
MQ2_ANALOG_PIN = 28
MQ2_DIGITAL_PIN = 20
#####################################
# Gas Alert LED
###############
AIRQUALITY_LED_PIN = 16
#####################################
# Temperature Indicator RGB LED
###############################
TEMP_RGB_LED_R_PIN = 15
TEMP_RGB_LED_G_PIN = 14
TEMP_RGB_LED_B_PIN = 13
#####################################
# Servos
########
TEMP_SERVO_PIN = 6
HUMIDITY_SERVO_PIN = 5
# coming for v1.2
# GAS_SERVO_PIN = 17
######################################
# 74HC165 ISR(INPUT SHIFT REGISTER) - BUTTONS
#########
ISR_BUTTONS_CE = 7
ISR_BUTTONS_Q7 = 8
ISR_BUTTONS_PL = 9
ISR_BUTTONS_CP = 10
######################################
# Climate Controller
####################
# -> controls all elements of the climate controller
climateCtrl = ClimateController(dht22=DHT22_PIN, configdip=[ISR_Q7_PIN,ISR_PL_PIN,ISR_CP_PIN,ISR_CE_PIN], i2c=[I2C_SDA_PIN, I2C_SCL_PIN], rtc=RTC_I2C_ADDR, lcd=LCD_I2C_ADDR, servos=[TEMP_SERVO_PIN, HUMIDITY_SERVO_PIN], mq2=[MQ2_ANALOG_PIN, MQ2_DIGITAL_PIN], leds=[AIRQUALITY_LED_PIN, TEMP_RGB_LED_R_PIN, TEMP_RGB_LED_G_PIN, TEMP_RGB_LED_B_PIN], btns=[ISR_BUTTONS_PL, ISR_BUTTONS_CP, ISR_BUTTONS_Q7, ISR_BUTTONS_CE], dht22waittime=DHT22_MEASURE_TIME, mq2waittime=MQ2_MEASURE_TIME, mq2smokelimit=MQ2_CO_THRESHOLD, mq2lpglimit=MQ2_LPG_THRESHOLD, mq2methanelimit=MQ2_CH4_THRESHOLD, btndebouncetime=BTN_DEBOUNCE_TIME, airledblinktime=GASALERT_LED_BLINK_TIME, temprgbimap=TEMP_RGBLED_IMAP, tempservoimap=TEMP_SERVO_IMAP, humiservoimap=HUMIDITY_SERVO_IMAP)
######################################
# Inititalize ########################
######################################
climateCtrl.update(sleeptime=None, updatemode=UpdateMode.NONE)
######################################
# System Loop ########################
######################################
__last_update_mode__ = UpdateMode.OUTPUTS
while True:
# Updates the ClimateController and all it controls and operates
# sleeptime = parameter for update to sleep after updating
"""if __last_update_mode__ == UpdateMode.OUTPUTS:
__last_update_mode__ = UpdateMode.INPUTS
elif __last_update_mode__ == UpdateMode.INPUTS:
__last_update_mode__ = UpdateMode.OUTPUTS"""
#climateCtrl.update(sleeptime=CTRL_UPDATE_TIME, updatemode=__last_update_mode__)
climateCtrl.update(sleeptime=CTRL_UPDATE_TIME, updatemode=UpdateMode.INPUTS)
climateCtrl.update(sleeptime=CTRL_UPDATE_TIME, updatemode=UpdateMode.OUTPUTS)C/F
Time
Live
Max
Min
Humidity
Temp.
LED
Readout Display
Temperature
Controlled Servo
DHT22
Temperature
Humidity Sensor
DS1307
Real Time Clock
Humidity
Controlled Servo
Gas
Alert LED
MQ2
Gas Sensor
Gas
Gas
Controlled Servo
Boot Configuration
DIP Switch
1 - LED Gas Alert will blink or stay on when alert detected
2 - Ignore LPG Detected in Analog Mode
3 - Ignore CH4 Detected in Analog Mode
4 - Ignore CO Detected In Analog Mode
5 - Sets Base Voltage to 3.3v(on) or 5v(off) for Analog Mode
6 - Sets Analog(on) or Digital Mode(off) of MQ2
7 - Skip Calibration of MQ2
8 - Skip Warm-Up of MQ2