Smart Kitchen + Remotely Controlled Using API

by Hamza_ARBI in Circuits > Arduino

865 Views, 2 Favorites, 0 Comments

Smart Kitchen + Remotely Controlled Using API

Supplies.png
Smart Kitchen.png

Introduction :

This Project called Smart Kitchen made by ARBI Hamza Student at University of Tlemcen -Algeria- ,
This project shows us what can we do with an arduino circuit, an impressive Smart Kitchen whitch can be remotely controlled by Mobile or Laptop by connecting to the website whitch Arduino access to it.

Supplies :

Everything we need for this project is shown in the picture above.

How It Works ? :

  • The Buzzer(3) and the LED(9) is used to make sound and light when Gaz is detected.
  • Servo(10) motor is used to control opening/closing the window.
  • DC Motor(11) is used to bring up/down the curtain and the 2 buttons are used to stop DC Motor from rotating when the curtain reach the Top (Top button stop the rotation) the same thing for the Bottom button.
  • When the Gaz sensor(2) detects the gaz the Window(10) and the Curtain(11) will automatically open.
  • When Light Sensor(8) detects night and Movement Sensor(4) triggered the LED Bulb(15) will automatically power ON.
  • There is two ways to control the components (Light bulb, Window, Curtain) with Remote Control or Hosted Website :
    • Remote Control : (Screenshot in Step 1)
      • Button 1 : Light Bulb ON
      • Button 4 : Light Bulb OFF.
      • Button 2 : Open Window
      • Button 5 : Close Window
      • Button 3 : bring up the curtain
      • Button 6 : bring down the curtain
    • Hosted Website :
      • There is ON/OFF toggles to control the components, Screenshots are showen in Step 2.

And now we gonna take a close look, first the project is divided in 2 pieces:

  1. Arduino, who collects data with all the sensors and access to the hosted website (Remote Controller) with parameters all the data of the sensors.
  2. Hosted Website,we will talk about it later but we just have to know that the website store the data incoming from Arduino in a JSON file.

Arduino

Smart Kitchen.png

In this code ,I writed somewhere {URL:example.com} replace it with your real URL of the hosted site please,
You can also download the code or access directly to an online Emuator (Tinkercad) just by clicking Smart kitchen + Custom API

#include <IRremote.h>
#include <Servo.h>
/*
Button 1 : Light Bulb ON
Button 4 : Light Bulb OFF

Button 2 : Open Window 
Button 5 : Close Window

Button 3 : bring up the curtain
Button 6 : bring down the curtain
*/

const long ESP8266_BAUDRATE = 115200L;
const unsigned long DEFAULT_TIMEOUT = 5000L;
String _dataRecived;

int irPin     = 5;
int pinData   = 5;
int TMP = A0;
int LIGHT = A2;
int INFRAROUGE = 7;
int AMPOULE = 6;
int RECIVER = 5;

int GAZ = A1;
int BUZZER = 8;
int UP = 2, DOWN = 3;
Servo Servo;
bool windowOpened = false;

int MONTE = 11, DESCENTE = 10, CONFIG = 9;

///// Etat des composants dans le Remote
int remoteLamp = 0;
int remoteServo = 0;
int remoteMotor= 0;

int nextTimeout =0;

//Librairie capteur infrarouge
IRrecv irrecv(RECIVER);
decode_results results;


void setup()
{
  Serial.begin(ESP8266_BAUDRATE);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  pinMode(INFRAROUGE, INPUT);
  pinMode(AMPOULE, OUTPUT);
  
  pinMode(GAZ, INPUT);
  pinMode(BUZZER, OUTPUT);
  Servo.attach(4);
  Servo.write(0);
  
  pinMode(MONTE, OUTPUT);
  pinMode(DESCENTE, OUTPUT);
  pinMode(CONFIG, OUTPUT);
  
  pinMode(UP, INPUT);
  pinMode(DOWN, INPUT);
  
  analogWrite(CONFIG , map(3,0,5,0,255) );
  /*********** INITIALIZE COMMUNICATION *********/
  sendCommandAT("AT+RST", DEFAULT_TIMEOUT, false, false, true);
  sendCommandAT("AT", DEFAULT_TIMEOUT, true, false, true);
  sendCommandAT("AT+GMR", DEFAULT_TIMEOUT, true, false, true);
  //sendCommandAT("AT+CWMODE=1", DEFAULT_TIMEOUT, true, false, true);
  sendCommandAT("AT+CWLAP", DEFAULT_TIMEOUT, true, false, true);
  // Connectez-vous au Wifi Simulator a l'aide de la commande AT + CWJAP
  sendCommandAT("AT+CWJAP=\"Simulator Wifi\",\"\"", DEFAULT_TIMEOUT, true, false, true);
  // Affiche l'ESP8266 IP
  sendCommandAT("AT+CIFSR", DEFAULT_TIMEOUT, true, false, true);
  //sendCommandAT("AT+CWMODE=3", DEFAULT_TIMEOUT, true, false, true);
  
  // Ouvre le canal de communication TCP avec le site Web a l'aide de la commande AT + CIPSTART
  sendCommandAT("AT+CIPSTART=\"TCP\",\"{URL:example.com}\",80", DEFAULT_TIMEOUT, true, false, true);
  
  nextTimeout = millis();
  
  irrecv.enableIRIn(); 
}

void loop()
{
  if (irrecv.decode(&results)) {
    //Returns the value retrieved from the infrared sensor
    unsigned int value = results.value; 
    irrecv.resume(); // Reprendre la réception
  }
  detecterMouvement();
  RemoteOnOff();
}
void RemoteOnOff(){
  if (irrecv.decode(&results)) {
    
    //Returns the value retrieved from the infrared sensor
    unsigned int value = results.value;
    Serial.println(value);
    switch(value){
      case 34935: // Num 2 // Open Window
      	Servo.write(90);
      	break;
      case 43095: // Num 5 // Close Window
      	Servo.write(0);
      	break;
      case 18615: // Num 3 // Open Curtain
      	digitalWrite(MONTE,HIGH);
  		digitalWrite(DESCENTE,LOW);
      	break;
      case 26775: // Num 6 // lose Curtain
      	digitalWrite(DESCENTE,HIGH);
  		digitalWrite(MONTE,LOW);
      	break;
      case 2295: //num 1 Light Bulb ON
      	digitalWrite(AMPOULE, HIGH);
      	break;
      case 10455: //num 4 Light Bulb OFF
      	digitalWrite(AMPOULE, LOW);
      	break;
    }
    irrecv.resume(); // Resume reception
  }
  if(digitalRead(UP)) {
    digitalWrite(MONTE,LOW);windowOpened = true;};
  if(digitalRead(DOWN)) {
    digitalWrite(DESCENTE,LOW); windowOpened = false;};
  
  if(analogRead(GAZ)>120) Alert(true);
  else Alert(false);
  
  if(millis()> nextTimeout){
  	// Request site data using HTTP GET
    
    String Req = "GET /index.php?getData=true&gaz=";
        Req.concat((analogRead(GAZ)>120)? 1 : 0);
        Req.concat("&motion=");
        Req.concat(digitalRead(INFRAROUGE));
        Req.concat("&temp=");
        Req.concat(getTemperature());
        Req.concat(" HTTP/1.1\r\nHost: {URL:example.com}\r\n\r\n");
    String preReq = "AT+CIPSEND=";
    	preReq.concat(Req.length());
    //Pre Req ou il ya la taille de la requette
    sendCommandAT(preReq , DEFAULT_TIMEOUT, false, true, false);
    // Envoie de la requette
    sendCommandAT(Req , 60000, false, false, true);
  	nextTimeout = millis() + 500;
  }
  
}
void detecterMouvement(){
  if(digitalRead(INFRAROUGE))
    digitalWrite(AMPOULE , HIGH);
  if(digitalRead(INFRAROUGE))
    digitalWrite(AMPOULE, LOW);
}

int getTemperature(){
  int reading = analogRead(TMP);
  float voltage = reading * 5.0;
  voltage /= 1024.0; 
  float temperatureC = (voltage - 0.5) * 100 ;
     return (int)temperatureC;

}
void Alert(bool a){
  if(a){
  	digitalWrite(BUZZER,HIGH);
    Servo.write(90);
    if(!windowOpened){
    	digitalWrite(DESCENTE,LOW);
  		digitalWrite(MONTE,HIGH);
      windowOpened=true;
    }
  }else{
  	digitalWrite(BUZZER,LOW);}}
/******** Custom API (Controller) *****/

// Send the command received from the serial monitor to ESP8266
void sendCommandAT(String commandement, unsigned long timeout, bool attendsOK, bool attendPlus, bool imprimer) {
  Serial.println(commandement);
  _dataRecived += showResponseCommandAT(timeout, attendsOK, attendPlus);
  if (imprimer) {
    if (0 == _dataRecived.length()) {
      Serial.println("ESP8266: No Response");
    } else {
      Serial.println(_dataRecived);
      _dataRecived = "";
      Serial.println("-----------------------------------------------------------");
      Serial.println();
      //delay(2000);
    }
  }
}

// Displays the response of the ESP8266 on the serial monitor, with accelerators
String showResponseCommandAT(unsigned long timeout, bool attendsOK, bool attendPlus) {
  if (!attendsOK && !attendPlus) {
    // Something in the header stops ESP8266 data
    while(!Serial.available()) 
    {
      delay(1);
    }
  }
  
  //Displays each character received
  unsigned long fim = millis() + timeout;
  bool bientot = false;
  bool recivedSomething = false;
  String recived = "";
  char beforeLast = ' ', last = ' ';
  do {
    while (!bientot && Serial.available()) {
      recivedSomething = true;
      beforeLast = last;
    last = (char) Serial.read();
      if(last != '"')
      	recived += last;
      if (attendsOK) {
        bientot = ('O' == beforeLast) && ('K' == last);
      } else if (attendPlus) {
        bientot = ('>' == last);
      } else if (recived.length() >= 32) {
        // Partial impressions so as not to overflow the chain
        Serial.print(recived);
        ///////////////////////////////////////////////////////////
    int lampIdx = recived.indexOf("l:")+2;char l=recived.charAt(lampIdx);
    int servoIdx = recived.indexOf("s:")+2;char s=recived.charAt(servoIdx);
    int motorIdx = recived.indexOf("m:")+2;char m=recived.charAt(motorIdx);
        
        digitalWrite(AMPOULE, (l=='1')? HIGH : LOW );
        
        Servo.write((s=='1')? 90 : 0);
        
        if(windowOpened && m=='0'){//Close Window
        	digitalWrite(DESCENTE,HIGH);
  			digitalWrite(MONTE,LOW);
          	windowOpened=false;
        }if(!windowOpened && m=='1'){//Open Window
        	digitalWrite(MONTE,HIGH);
  			digitalWrite(DESCENTE,LOW);
          	windowOpened=true;
        } 
        ///////////////////////////////////////////////////////////
        recived = "";
      } else {
        bientot = ('}' == beforeLast) && ('\r' == last);
      }
    }
  } while (!bientot && (fim >= millis()));
  
  return recived;
}

Hosted Website

Screen1.png
Screen2.png

As I said before I used JSON File to store my data, so there is an example of data in JSON,

and all the website Example is downloadable below.

NOTE : For Some reason I can't upload my files with their original extentions so you have to change it after downloading it,

index.txt -> index.php.

data.txt -> data.json.

{
    "lamp": 0,// 0-> Light OFF
    "servo": 0,// 0->Window Closed
    "motor": 0,// 0->Curtain Closed
    "motion": "0",//0->No motion Detected
    "gaz": "0",//->No Gaz Detected
    "temp": "24"
}

Downloads