Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Li FangBohan Jiang
Created March 19, 2016

Home Auto-Control System

Our system consists of three parts, cooking stove status detection, door lock detection, and indoor air circulation system.

BeginnerProtipOver 1 day606
Home Auto-Control System

Things used in this project

Story

Read more

Schematics

Stove Monitor

Door Monitor

Air Circulation System

Code

Stove Monitor

Arduino
/*
  WiFi Web Server LED Blink
 
 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.
 
 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off
 
 This example is written for a network using WPA encryption. For 
 WEP or WPA, change the Wifi.begin() call accordingly.
 
 Circuit:
 * WiFi shield attached
 * LED attached to pin 9
 
 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "Chess";      //  your network SSID (name) 
char pass[] = "test2015";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(8080);

void setup() {
  Serial.begin(9600);     // initialize serial communication
  pinMode(3, OUTPUT);      // set the LED pin mode
  pinMode(A0, INPUT);      // real the angular sensor value

  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 
  
  // check the firmware version
  String fv = WiFi.firmwareVersion();
  if( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients
  int angleValue = analogRead(A0);
  int controlValue;

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {  
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:    
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            if(angleValue > 10)
            {
              client.print("The digital relay is on<br>");
            }
            else
            {
              client.print("The digital relay is off<br>");
            }

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the REALY on pin 3 on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the RELAY on pin 3 off<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;         
          } 
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }     
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          controlValue = 1;               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          controlValue = 0;                // GET /L turns the LED off
        }

        if(angleValue > 10)           //if the switch is on
        {
          digitalWrite(3, LOW);       //disconnect the power supply
        }
        else if(controlValue == 1)    //if the switch is off and the user want to connect the power supply
        {
          digitalWrite(3, HIGH);      //connect the power supply
        }
        else
        {
          digitalWrite(3, LOW);     //disconnect the power supply
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

Door System

Arduino
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "Chess";      //  your network SSID (name) 
char pass[] = "test2015";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(8081);

void setup() 
{
  Serial.begin(9600);     // initialize serial communication
  pinMode(4, INPUT);      // read the button value
  pinMode(A0, INPUT);      // read the angular sensor value

  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) 
  {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 
  
  // check the firmware version
  String fv = WiFi.firmwareVersion();
  if( fv != "1.1.0" )
  {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) 
  { 
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
  
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() 
{
  WiFiClient client = server.available();   // listen for incoming clients
  
  if (client)                               // if you get a client,
  {                             
    Serial.println("new client");          // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    
    while (client.connected())              // loop while the client's connected
    {                                       
      if (client.available())               // if there's bytes to read from the client,
      {             
        int angleValue = analogRead(A0);
        int buttonValue = digitalRead(4);
        
        char c = client.read();             // read a byte, then
        Serial.write(c);                   // print it out the serial monitor
        if (c == '\n')                      // if the byte is a newline character
        {                    
                                            // if the current line is blank, you got two newline characters in a row.
                                            // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0)  
          {  
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:    
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            if(buttonValue == 0)
            {
              client.print("You forgot to close the door");
            }
            else if(angleValue > 20)
            {
              client.print("You forgot to lock the door");
            }
            else
            {
              client.print("Your door is locked");
            }

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;         
          } 
          else 
          {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } 

        else if (c != '\r')       // if you got anything else but a carriage return character, add it to the end of the currentLine
        {    
          currentLine += c;
        }

       }
    }
    
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

void printWifiStatus() 
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

Air Circulation System

Arduino
//Attention: all the values in this code is for demo use only

#include <Wire.h>
#include <math.h>
#include <SPI.h>
#include <WiFi.h>

//Wi-Fi setup
char ssid[] = "Chess";        //  your network SSID (name) 
char pass[] = "test2015";     // your network password
int keyIndex = 0;             // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(8081);

//motor
#define MotorSpeedSet             0x82
#define PWMFrequenceSet           0x84
#define DirectionSet              0xaa
#define MotorSetA                 0xa1
#define MotorSetB                 0xa5
#define Nothing                   0x01
#define I2CMotorDriverAdd         0x0f   // Set the address of the I2CMotorDriver

//temperature&02
#define version  11
const int pinO2  = A1;
const int AMP  =121;
const float K_O2  =7.43;
int a;
float temperature;
int B=3975;
float resistance;

//oxygen
float sensorValue;
float sensorVoltage;
float Value_O2;


void MotorSpeedSetAB(unsigned char MotorSpeedA , unsigned char MotorSpeedB)
{
  MotorSpeedA=map(MotorSpeedA,0,100,0,255);
  MotorSpeedB=map(MotorSpeedB,0,100,0,255);
  Wire.beginTransmission(I2CMotorDriverAdd);    // transmit to device I2CMotorDriverAdd
  Wire.write(MotorSpeedSet);                    // set pwm header 
  Wire.write(MotorSpeedA);                      // send pwma 
  Wire.write(MotorSpeedB);                      // send pwmb    
  Wire.endTransmission();                       // stop transmitting
}

void MotorPWMFrequenceSet(unsigned char Frequence)
{    
  Wire.beginTransmission(I2CMotorDriverAdd);      // transmit to device I2CMotorDriverAdd
  Wire.write(PWMFrequenceSet);                    // set frequence header
  Wire.write(Frequence);                          //  send frequence 
  Wire.write(Nothing);                            //  need to send this byte as the third byte(no meaning)  
  Wire.endTransmission();                         // stop transmitting
}

void MotorDirectionSet(unsigned char Direction)       //  Adjust the direction of the motors 0b0000 I4 I3 I2 I1
{
  Wire.beginTransmission(I2CMotorDriverAdd);          // transmit to device I2CMotorDriverAdd
  Wire.write(DirectionSet);                           // Direction control header
  Wire.write(Direction);                              // send direction control information
  Wire.write(Nothing);                                // need to send this byte as the third byte(no meaning)  
  Wire.endTransmission();                             // stop transmitting 
}

void MotorDriectionAndSpeedSet(unsigned char Direction,unsigned char MotorSpeedA,unsigned char MotorSpeedB)    //you can adjust the driection and speed together
{
  MotorDirectionSet(Direction);
  MotorSpeedSetAB(MotorSpeedA,MotorSpeedB);  
}

void printWifiStatus() 
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void setup()  
{
  Serial.begin(9600);
  Wire.begin(); // join i2c bus (address optional for master)

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) 
  {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 
  // check the firmware version
  String fv = WiFi.firmwareVersion();
  if( fv != "1.1.0" )
  {
    Serial.println("Please upgrade the firmware");
  }
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) 
  { 
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
  }
  delayMicroseconds(10000); //wait for motor driver to initialization
}



void loop()
{
  WiFiClient client = server.available();   // listen for incoming clients
  
  if (client)                               // if you get a client,
  {                             
    Serial.println("new client");          // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
  
    while (client.connected())              // loop while the client's connected
    {                                       
      if (client.available())               // if there's bytes to read from the client,
      {
        // read and print temperature sensor value
        a=analogRead(A0);
        resistance=(float)(1023-a)*10000/a;5;
        temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;
        delay(1000);
        sensorValue =analogRead(A1);
        sensorVoltage=(sensorValue/1024.0)*5.0;
        sensorVoltage=sensorVoltage/(float)AMP*10000.0;
        Value_O2 =sensorVoltage/K_O2;

        char c = client.read();             
        Serial.write(c);                   
        if (c == '\n')                      
        {
          if (currentLine.length() == 0)  
          {  
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:    
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // print temperature value online
            client.print("Current temperature is ");
            client.println(temperature);
            
            // print oxygen percentage online
            client.print("Concentration of O2 is ");
            client.print(Value_O2,1);
            client.println("%");
            delay(1000);
 
            if ((temperature>=10) && (Value_O2>=27))
            {
              // the air quality is good to open the window
              client.println("The air outdoor is good, you can open the window for circulation");
              MotorSpeedSetAB(100,20);
              delay(10);
              MotorDirectionSet(0b1010);  // rotate in the positive direction to open the window
              delay(10000);
              MotorSpeedSetAB(0,0);
              // close the window after fixed time
              client.println("It's time to close the window");
              MotorDirectionSet(0b0101);  // rotating in the opposite direction to close the window
              MotorSpeedSetAB(100,20);
              delay(10000);
              MotorSpeedSetAB(0,0);
            }
          }
          else 
          {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r')       // if you got anything else but a carriage return character, add it to the end of the currentLine
        {    
          currentLine += c;
        }
      }
    }

    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

Credits

Li Fang

Li Fang

1 project • 2 followers
A graduate student of University of Michigan Dearborn, major in ECE, Working in a lab focusing on Intelligent System and data mining. Pretty interested in IOT .
Bohan Jiang

Bohan Jiang

3 projects • 2 followers
Electrical Engineering

Comments