Adithya Shankar
Published

Desk Climate Control

Automatic Climate Control using DIY components and Arduino to keep the desk environment cool and snug.

IntermediateFull instructions provided3 hours291
Desk Climate Control

Things used in this project

Story

Read more

Schematics

The Circuit Diagram

Code

CodeforACC

Arduino
What the code does:
-connect to your Wi-Fi
-connect to your Thingspeak cloud channel
-collect data from your sensor
-upload the data to the cloud
-control the fan based on the temperature.
-repeat the last three steps.
#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "YOUR_WIFI_SSID";       // AP NAME
String PASS = "YOUR_WIFI_PASSWORD"; // AP PASSWORD
String API = "THINGSPEAK_CHANNEL_WRITE_API_KEY";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;
int on=0;
int off=0;
SoftwareSerial esp8266(RX,TX); 
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// The Vout from the sensor goes to pin 7.
#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// Include the Servo library 
#include <Servo.h> 
// Declare the Servo pin 
int servoPin = 4; 
// Create a servo object 
Servo Servo1; 
// Function to turn my fan on. It is powered by an external source.
// Feel free to write your own code to turn your fan on.
int turn_on()
{
  Servo1.write(0);
  delay(200);
  Servo1.write(25); 
  delay(200); 
  Servo1.write(0);
  delay(200);
  Servo1.write(25); 
  delay(200);
  Servo1.write(0);
  delay(200);
  Servo1.write(25);   
  delay(200);
  Servo1.write(0);
  delay(200);
// Redundant on/off settings to keep it easy to understand.
  on=1;
  off=0;
  return on;
}
int turn_off()
{Servo1.write(0);
  delay(200);
  Servo1.write(25); 
  delay(200); 
  Servo1.write(0);
  delay(200);
  on=0;
  off=1;
  return off;
}  
 
  
void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
   Serial.println("Arduino Digital Temperature // Serial Monitor Version");  
  sensors.begin();
  // We need to attach the servo to the used pin number 
   Servo1.attach(servoPin); 
}

void loop() {
//This where we collect the sensor data to send to Thingspeak  
 valSensor = getSensorData();
 String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
 //This is the value I have set (25C) for regulating the temperature. 
 if(getSensorData()>25)
 { 
   if(on==0)
   {turn_on();
   }
 
 }
 if(getSensorData()<=25)
 { if(off==0)
   {turn_off();
   }
 }
}

int getSensorData(){
  sensors.requestTemperatures();  
  Serial.print("Temperature is: ");
  Serial.println(sensors.getTempCByIndex(0));
  return sensors.getTempCByIndex(0); // Replace with your own sensor code
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

Credits

Adithya Shankar
1 project • 1 follower

Comments