You are on page 1of 4

#include <SoftwareSerial.

h>
#define RX 2
#define TX 3

#include <Wire.h>
//#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
dht DHT;

#define DHT11_PIN 9 // This pin 9 use for sensor sense from DHT11
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an
unmodified module
//const int ledPin = 6; // pin that the LED is attached to

const int bulbPin = 4;


const int ldrPin = A0;
const int ExhaustPin = 5;
const int FanPin = 6;
const int HeaterPin = 7;

//String AP = "(((-SIT-)))"; // AP NAME


//String PASS = "incorrect"; // AP PASSWORD

String AP = "PLDThOmeGleiceVince"; // AP NAME


String PASS = "D@nao2PLDT"; // AP PASSWORD
String API = "OOVMTRQGM52G3FXZ"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;

SoftwareSerial esp8266(RX,TX);

void setup()
{
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");

lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it
to LOW instead of HIGH
lcd.begin(16, 2);
lcd.clear();
pinMode(bulbPin, OUTPUT);
pinMode(ExhaustPin, OUTPUT);
pinMode(FanPin, OUTPUT);
pinMode(HeaterPin, OUTPUT);
pinMode(ldrPin, INPUT);
//Turn all the relay OFF on initial State
digitalWrite(bulbPin, HIGH);
digitalWrite(ExhaustPin, HIGH);
digitalWrite(FanPin, HIGH);
digitalWrite(HeaterPin, HIGH);
Serial.begin(9600);

}
void loop()

{
ldr();
TempHum();
lcd.setCursor(0,0);
lcd.print("Temp Hum Light");
}

void ldr()
{
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 200) {
digitalWrite(bulbPin, LOW);
Serial.print("Its DARK, Turn on the LED : ");
Serial.println(ldrStatus);

lcd.setCursor(12,1);
lcd.println("ON ");

String getData = "GET /update?api_key="+ API +"&field3="+ldrStatus;


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

}
else
{
digitalWrite(bulbPin, HIGH);
Serial.print("Its BRIGHT, Turn off the LED : ");
Serial.println(ldrStatus);

lcd.setCursor(12,1);
lcd.print("Off ");

String getData = "GET /update?api_key="+ API +"&field3="+ldrStatus;


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

}
}

void TempHum()
{
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature); //Temperature reading
//float temp = 36;
float Hum=(DHT.humidity);
// Temperature and humidity display on LCD

//lcd.print("Temp C ");
lcd.setCursor(0,1);
lcd.print(temp);
//lcd.setCursor(0,1);
//lcd.println("Humid % ");
lcd.setCursor(6,1);
lcd.print(Hum);

String getData = "GET /update?api_key="+ API +"&field1="+temp+"&field2="+Hum;


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");
if (Hum > 70.00)
{
digitalWrite(ExhaustPin,LOW);//Exhaust Fan is ON
}
if (Hum < 69.00)
{
digitalWrite(ExhaustPin,HIGH); //Exhaust Fan is OFF
}

if (temp > 35.00)


{
digitalWrite(FanPin,LOW); //Fan is ON
digitalWrite(HeaterPin,HIGH);//HEATER is OFF
}
if (temp < 29.00)
{
digitalWrite(FanPin,HIGH);//Fan is OFF
digitalWrite(HeaterPin,LOW);//HEATER is ON
}
if (temp == 29.00 && temp <= 35.00 )
{
digitalWrite(FanPin,HIGH);//Fan is OFF
digitalWrite(HeaterPin,HIGH);//HEATER is OFF
}
delay(1000);

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("OK");
countTrueCommand++;
countTimeCommand = 0;
}

if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}

found = false;
}

You might also like