Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Alex Merchen
Published

Air Conditioner Controller

Take a dumb air conditioner and make it smart so that you can set it and forget it. Uses Particle Photon.

IntermediateFull instructions provided3 hours2,498
Air Conditioner Controller

Things used in this project

Hardware components

Photon
Particle Photon
×1
Breadboard (generic)
Breadboard (generic)
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
SparkFun power switch
This product is currently retired but you can use the functionally equivalent product: https://www.sparkfun.com/products/14236
×1

Software apps and online services

Particle Pi
Particle Pi

Story

Read more

Schematics

Layout of design

Layout of the circuit - I rerouted the wires to make it easier to view what goes where. I don't have a outline of the relay but it takes a single output and only requires VDD and VSS.

Code

Code for Implementing Design

Arduino
Paste this in your particle program. It requires LiquidCrystal and Adafruit_DHT_Particle libraries
/*

 Wiring/instructions for the LCD was done according to:
 https://blog.jongallant.com/2015/10/particle-photon-lcd-setup/
 
 Wiring/instructions for the DHT-22 was done according to:
 https://learn.adafruit.com/dht?view=all
 
*/

#include "application.h"

#include "LiquidCrystal/LiquidCrystal.h"

#include <Adafruit_DHT_Particle.h>

#define DHTPIN D6     // what pin we're connected to

#define DHTTYPE DHT22		// DHT 22 (AM2302)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

// loopCount is what we are going to use as our countdown for the overshooting
int loopCount;
// overshootTime is the amount of time you want the air conditioner 
// on after it reaches the right temp (in sec)
int overshootTime = 300;
// thresholdTemp is the temperature that you want the device to turn on at
float thresholdTemp = 75.0;
// averageIndex takes the average of the last five seconds. 
// This is so I avoid accidental spikes in reading from DHT22
int averageIndex = 0;
// averageTemp is the three second average
int averageTemp = 0;

// average1 and average2 are temporary bits for the average.
int average1 = 0;
int average2 = 0;
    


// Set up the liquid crystal with the order of how you wired 
// (in ascending order so easier)
LiquidCrystal lcd(D0, D1, D2, D3, D4, D5);

// Set D7 as our relay switch
int relaySwitch = D7;

// ########################### SETUP ###########################
void setup() {
    
    // set our air conditioner switch to be an output
    pinMode(relaySwitch, OUTPUT);
    
    // start the DHT-22 device
	dht.begin();
	
	// set loopCount to 0
	loopCount = 0;
	
	// wait a couple seconds for the DHT-22 to initialize
	delay(2000);
    
    // set up the LCD's number of columns and rows: 
    lcd.begin(16,2);
    // Print Temp: to the LCD.
    lcd.print("Temp: ");
}

// ########################### LOOP ###########################
void loop() {
  // set the cursor to column 6 ("Test: " takes up 6 characters), line 0
  lcd.setCursor(6, 0);

// Reading temperature or humidity takes about 250 milliseconds
	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = round(dht.getTempFarenheit()*100)/100;
  
    // onOff will indicate whether the air conditioner is on or off. This is for logging.
    String onOff = "OFF";
    
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		//lcd.print("Failed to read DHT!");
		return;
	}
    
    // increase the average index
    averageIndex++;
    
    // if the index is 0 that means that two samples have just 
    // been taken so we can average all three together
    if ((averageIndex%3) == 0){
        averageTemp = (average1 + average2 + f)/3;
    } else if ((averageIndex%3) == 1){
        average1 = f;
    } else if ((averageIndex%3) == 2){
        average2 = f;
    }
    
    
    // display on the screen the current temperature in farenheight
    lcd.print(String(int(f)) + " *F       ");
	
	// we have a one second delay
	delay(1000);

    // if the averageTemp is above the thresholdTemp
	if (averageTemp > thresholdTemp){
	    // set the cursor on the second row
	    lcd.setCursor(0,1);
	    // display to user that the device is on
	    lcd.print("ON, above " + String(thresholdTemp) + " *F      ");
	    // set the loopCount to be the overshootTime
	    loopCount = overshootTime;
	    // set the relaySwitch to LOW (which will turn on the air conditioner)
	    digitalWrite(relaySwitch, LOW);
	    // set the string that gets sent to IFTTT to ON
	    onOff = "ON";
	    
	} else if (loopCount > 0){
	    // set the cursor on the second row
	    lcd.setCursor(0,1);
	    // display to the user that the device is on and how much time is remaining
	    lcd.print("ON, wait: " + String(loopCount) + " s     ");
	    // decrease the loopCount
	    loopCount--;
	    // set the relaySwitch to LOW (which will turn on the air conditioner)
	    digitalWrite(relaySwitch, LOW);
	    // set the string that gets sent to IFTTT to ON
	    onOff = "ON";
	}
	else{
	    // set the cursor on the second row
	    lcd.setCursor(0,1);
	    // display to the user that the device is off
	    // and it's because the temp is below threshold
	    lcd.print("OFF, below " + String(thresholdTemp) + " *F     ");
	    // set the loopCount to 0 which will prevent it 
	    // from turning on unless averageTemp is above threshold
	    loopCount = 0;
	    // set the relaySwitch to HIGH (which will turn off the air conditioner)
	    digitalWrite(relaySwitch, HIGH);
	    // set the string that gets sent to IFTTT to OFF
	    onOff = "OFF";
	}    
	
	// Every ten seconds we're going to publish the current temperature 
	// and the status of whether or not the air conditioner is on or off
	if ((averageIndex%10) == 0 && onOff == "ON") {
        Particle.publish("AirConditionerController", String(f) + "|||" + onOff);
    }
	
}

Credits

Alex Merchen

Alex Merchen

24 projects • 39 followers
I'm an EE with a Masters in ECE. I like building things.

Comments