Introduction: Timed Temperature Servo Button Presser

The function of this project is to sense the temperature and if it senses the temperature is out of the range set, it will turn the servo to turn off the A/C.

Supplies

For this project:

1 Argon

1 Breadboard

1 DHT 11 Temperature and Humidity Sensor

1 Micro Servo ( Normal, or High Torque) Or High Power Servo

6 Male to Male Jumper Wires

*Optional*

3 Female to Female Jumper Wires

Step 1: Setting Up Your Temperature Sensor

For this step you take the DHT-11 you have and either attach it directly to the breadboard or use Jumper Wires to attach it to the breadboard as well.

An important note here is that you have to keep track of which Digital Pin you attach the non positive or negative wire, or the input wire from the DHT-11 Sensor, as that would be important for the code.

Step 2: Getting the Code

#include "Adafruit_DHT.h"

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

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

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// 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);

void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a
// very slow sensor)
float h = dht.getHumidity();
// Read temperature as Celsius
float t = dht.getTempCelcius();
// Read temperature as Farenheit
float f = dht.getTempFarenheit();

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.getHeatIndex();
floatdp = dht.getDewPoint();
float k = dht.getTempKelvin();

Serial.print("Humid: ");
Serial.print(h);
Serial.print("% - ");
Serial.print("Temp: ");
Serial.print(t);
Serial.print("*C ");
Serial.print(f);
Serial.print("*F ");
Serial.print(k);
Serial.print("*K - ");
Serial.print("DewP: ");
Serial.print(dp);
Serial.print("*C - ");
Serial.print("HeatI: ");
Serial.print(hi);
Serial.println("*C");
Serial.println(Time.timeStr());
}


First you should make a new project on particle's website. You should name it something.


Setting up the Library


Then you would have to search up a specific library for this project, so to search for it click on the bookmark on the bottom left corner of the screen.


From there you could search up DHT and click on the one named "Adafruit_DHT_Particle" then when clicked you should see a button to add to a project and click that. You should add it to your project.


Setting up the Code


The basis for a lot of my code was based on this code given by ElectronicWings

You could also see the code above as well to get a better look at it.


Important Notes

Near the top of the code it says #define DHTPIN, you would have to put the Digital Pin Number you used for the DHT Sensor.


The basics of the code are that the library creates, methods for the dht class and from there you get your temperature



Step 3: Testing

This step is to establish and is more based on preference than anything.

To be able to test and get results you will need to get a serial monitor, plenty of which would work.

Here's one if you just want to get into it: Coolterm

From there you could be able to see all the information which can be seen above, giving you information on the temperature of your location.

I like to test out the temperature sensor, get a basic idea of the temperature in the room you will have it in and then pick your range of temperatures, for me it was 65 F and 70 F, it can differ on your environment.

Step 4: Setting Up Servo

You would need male to male jumper wires to connect your servo to the breadboard, using the diagram above to connect the right wires. The reason for the multiple servos being named in the supply list is that it could depend on the button you want to press on it so it could depend but a safe bet would be getting a High Power Servo.

Also take note of the wire attach to a Pin number, as you have to attach it to a Digital Pin and take note of the number.

Something to take note of is that you have to add something hard enough to press the button you are trying to hit with the servo, so attaching it onto the attachment to the servo would help.

Step 5: Final Code

//#include "Adafruit_DHT.h"


// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain


#define DHTPIN 2     // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 //DHT 11 
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)


// 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);


Servo myServo;
int val = 1;
bool ac = false;


void setup() {
//Serial setup
Serial.begin(9600); 
//Serial.println("DHTxx test!");
//Servo setup
myServo.attach(5);
myServo.write(0);
//DHT (Temperature and Humidity Sensor) setup
dht.begin();
}


void loop() {
   
   //Time.hour, use to make it only work during certain hours
   delay(4000);
   Time.zone(-6);
   int cst = Time.hour();
   int first = 0;
   if(cst >= 17 && cst <= 22)
   {
    // Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
float h = dht.getHumidity();
// Read temperature as Celsius
float t = dht.getTempCelcius();
// Read temperature as Farenheit
float f = dht.getTempFarenheit();

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// A way to check if the A/C is on or off in the beginning, as the variable ac is orginially set to false
if((val % 20)== 0)
{
   if(h >= 60)
   {
       ac = true;
   }
   else if(h <= 55)
   {
       ac = false;
   }
}

//If it ever reaches 75 degrees Fahrenheit
if(f >= 70.00)
{
//A measure to make sure that the servo doesn't try to turn "on" the A/C again and ends up turning it off again, 
//if the the temperature doesn't drastically change in between the time between each loop
    if(ac == false)
    {
        //Makes the servo move automatically to turn on the A/C
        myServo.write(170);
        delay(2000);
        myServo.write(0);
        ac = true;
        val = 0;
    }
}
else if(f <= 65.00)
{
    if(ac == true)
    {
        myServo.write(170);
        delay(2000);
        myServo.write(0);
        ac = false;
        val = 0;
    }
}

Serial.print("Humid: "); 
Serial.print(h);
Serial.print("% - ");
Serial.print("Temp: "); 
Serial.print(t);
Serial.print("*C ");
Serial.print(f);
Serial.print("*F ");
Serial.println(Time.timeStr());
val++;
   }
}


This code is more complicated from the previous one, however this comes to preference for most people like the timing of the automation, temperature it is activated, and setting up the time.

Time

The beginning with the Time being used is to ensure the servo only goes off during a specified range of time, being from the afternoon. This can be removed to help with testing out the servo, or if you don't want it.

Servo

For myServo.attach() you would put in the Digital Pin Number you attached to it.

The rest of the code you can copy, as those previous issues are probably why you're code would be acting weird like not printing to the serial monitor or not moving the servo.

Step 6: Final Setup

Enjoy your Automatic A/C presser