Professional Documents
Culture Documents
Codigo Arduino Controle Ar Condicionado
Codigo Arduino Controle Ar Condicionado
cc/projecthub/BuddyC/ir-controller-for-air-conditioner-
5bd0a2
#include <IRremote.h>
#include <SoftwareSerial.h>
#include "DHT.h" // Written by ladyada, public domain
IRsend irsend;
SoftwareSerial ser(10, 11); // (RX, TX) // Software serial for controling
ESP8266 Module
// Hardware serial for debugging
/*
* next up is adding a timer instead of delays
unsigned long tempMillis = 0; // will store last time temp was updated
unsigned long serverMillis = 0; // will store last time server was
updated
unsigned long postMillis = 0; // will store last time temp was posted
unsigned long ACMillis = 0; // will store last time AC was checked
const long tempival = 30000; // interval to check temp (every 30
seconds)
const long servercheckival = 60000; // interval to check server config (every
minute)
const long temppostival = 900000; // interval to post new temp stats (every
15 minutes)
const long ACcooldown = 300000; // time to wait if AC is already on (5
minutes)
*/
void setup() {
Serial.begin(9600);
ser.begin(9600);
/*
// Uncomment to reset WiFi settings
delay(1000);
ser.println("AT+CWMODE=1");
ser.println("AT+CWJAP=\"SSID\",\"Password\"");
*/
delay(2000);
dht.begin();
dht.begin();
void checkserver() {
// Update thermostat setting (ACTEMP=##) from HAI
// Thermostat override with TurnACon=true or TurnACoff=true
}
void turnACon() {
// Run IR code to turn on AC
Serial.print("Temperature is over ");
Serial.print(ACTEMP);
Serial.print(", AC is on (for 5 minutes)");
// Send IR signal to turn AC Unit ON
// Turn thermostat all the way down to make AC kick on
for (int x = 0; x <250; x++){
irsend.sendRaw(irSignalDown, sizeof(irSignalDown) /
sizeof(irSignalDown[0]), khz);
delay(22);
}
ACON = 1;
delay(285000); // turn on minimum of 5 min
(subtracted 15 seconds for gettemp)
}
void turnACoff() {
// Run IR code to turn off AC
Serial.print("Temperature is under ");
Serial.print(ACTEMP);
Serial.println(", AC is off");
// Send IR signal to turn AC Unit OFF
// Turn thermostat all the way up so that AC never kicks on
for (int x = 0; x <250; x++){
irsend.sendRaw(irSignalUp, sizeof(irSignalUp) / sizeof(irSignalUp[0]),
khz);
delay(22);
}
ACON = 0;
delay(15000); // When AC is off, Temp is read every
30 seconds (15+15)
}
void gettemp() {
// pull temp from sensor and send to server
// whole process takes about
Serial.print("Realtime Temp: \t");
// Check if any reads failed and exit early (to try again).
if (isnan(hi)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += host;
cmd += "\",80";
ser.println(cmd);
delay(1000);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
else{
}
if(ser.find(">")){
ser.print(getStr);
Serial.println("Success!");
}
else{
ser.println("AT+CIPCLOSE");
Serial.println("Connection Failed"); // alert user
}
}
void loop() {
// Get updated settings from server
checkserver();
Serial.println();
Serial.println();