Hackster is hosting Hackster Holidays, Ep. 2: Livestream & Giveaway Drawing. Start streaming on Friday!Stream Hackster Holidays, Ep. 2 on Friday!
INSANE SCIENCE
Published © GPL3+

Controlling Air Conditioner temperature Through Internet

Learn how to make you air conditioner smart by allowing it to be controlled by your phone from anywhere in the world!

BeginnerFull instructions provided2 hours6,714
Controlling Air Conditioner temperature Through Internet

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
5V 2.5A Switching Power Supply
Digilent 5V 2.5A Switching Power Supply
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
I2c module
×1
Single channel relay
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Diagram

Connect scl pin to D1
Sda to D2
Dht11 data pin to D3
Relay data in pin to D5

Code

Air conditioner

Arduino
Scl to d1
Sda to d2
Dht11 to d3
Relay to d5
#include <LiquidCrystal_I2C.h>//link to liquid crytal library file:///C:/Users/ajith/Downloads/LiquidCrystal_I2C-master%20(2).zip
#include <ESP8266WiFi.h>

#include <DHT.h>//link to dht 11 library https://www.electronicwings.com/download/attachment=Fri-08-17-20-48-58.NodeMCU_DHT11.zip
#define DHT11_PIN 2
DHT DHT(DHT11_PIN,DHT11);


   //pin where the dht11 is connected

#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);

#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define relay D5
#define DHT11_PIN 0
/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "Your wifi ssid"
#define WLAN_PASS       "Your wifi password"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "adafruit username"
#define AIO_KEY         "aio key"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

const char SLIDER_FEED[] PROGMEM = AIO_USERNAME "/feeds/temp";
Adafruit_MQTT_Subscribe temp = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Temperature_control");
/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>


// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Ac");

Adafruit_MQTT_Publish ac = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp");
/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).

void MQTT_connect();

void setup() {
  pinMode(relay, OUTPUT);
  Serial.begin(115200);
  delay(10);


  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

Wire.begin(D2, D1);
lcd.home();
 lcd.setCursor(0, 0);
  lcd.print("Scanning wifi");
  delay(2000);
  lcd.clear();
  lcd.print("Connecting to ");
  delay(2000);
   lcd.setCursor(0, 1);
 lcd.print(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  delay(2000);
  lcd.clear();
  lcd.print("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
  ////////////////////////
 
   lcd.setCursor(0, 1);
  lcd.print("IP:");
   lcd.setCursor(3, 1);
     lcd.print(WiFi.localIP());
delay(3000);

  mqtt.subscribe(&onoffbutton);

  mqtt.subscribe(&temp);

}

uint32_t x = 0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();
  ////////////////////////////////////


  Serial.print(F("\nSending myValue "));
  ///////////////////////////////


 float t = DHT.readTemperature();
  ///////////////////////////////
  Serial.print("...");
  if (! ac.publish(t)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
     Serial.println(t);
  }
  float state = atoi((char *)onoffbutton.lastread);
 lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("C Temp=");
  lcd.setCursor(7, 0);
  lcd.print(t);
  lcd.setCursor(12, 0);
  lcd.print((char)223);
  lcd.setCursor(13, 0);
  lcd.print("c");
  lcd.setCursor(0, 1);
  lcd.println("R Temp=");
  lcd.setCursor(7, 1);
  lcd.print(state);
  lcd.setCursor(12, 1);
  lcd.print((char)223);
  lcd.setCursor(13, 1);
  lcd.print("c");
  Serial.println(state);

  //////////////////////////////////
  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    uint16_t state = atoi((char *)onoffbutton.lastread);

  float t = DHT.readTemperature();
    if (state < t) {
      Serial.print(F("Relay state: "));
      Serial.println((char *)onoffbutton.lastread);


      Serial.println(state);
      Serial.println(t);
      digitalWrite(relay, LOW);
      void data();
        delay(3000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("PELTIER ON");
    }
    else if (state > t) {
      Serial.print(F("Relay state: "));
      digitalWrite(relay, HIGH);
    Serial.println(t);
     lcd.clear();
    void data();
 lcd.clear();
     
      lcd.setCursor(0, 0);
       lcd.print("PELTIER OFF");}
        
      ////////////////////////////////////


      ///////////////////////////

    
  }
}




void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");
  lcd.setCursor(0, 0);
  lcd.clear();
lcd.print("Connecting to MQTT");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    lcd.print("Retrying MQTT");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
  lcd.setCursor(0, 1);
  lcd.print("MQTT Connected!");
  lcd.clear();
}
void data()
{
   float t = DHT.readTemperature();
   float state = atoi((char *)onoffbutton.lastread);
    lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("C Temp=");
  lcd.setCursor(7, 0);
  lcd.print(t);
  lcd.setCursor(12, 0);
  lcd.print((char)223);
  lcd.setCursor(13, 0);
  lcd.print("c");
  lcd.setCursor(0, 1);
  lcd.println("R Temp=");
  lcd.setCursor(7, 1);
  lcd.print(state);
  lcd.setCursor(12, 1);
  lcd.print((char)223);
  lcd.setCursor(13, 1);
  lcd.print("c");
  Serial.println(state);
}

Credits

INSANE SCIENCE

INSANE SCIENCE

10 projects • 9 followers
We are mechanical student, we been since 2014

Comments