Tuesday, May 12, 2015

Smart Home with IOS Application

The concept of the "Internet of Things" has tied in closely with the popularization of  home automation. There are lots of smart home applications such as Home security,  access control,  lighting control, home health care, fire detection, leak detection energy efficiency, solar panel monitoring and control etc.                                                      
 In this project, we aim to create a smart home application. We used ethernet shield. It works as a webserver in this project. Also we made an iphone application. By the help of this application, user can  control tv, garage door, temperature, light and learn the moisture of the room  while he is far away from his home. 8 way 5V relay is used for this aim. We build a 3-tier structure. The first layer is Arduino, second one is web service and the third one is an Iphone application. Our circuit is given below.




#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
#include "TimerOne.h"
#include 

template
inline Print &operator <<(Print &obj, T arg)
{
  obj.print(arg);
  return obj;
}


// CHANGE THIS TO YOUR OWN UNIQUE VALUE
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// CHANGE THIS TO MATCH YOUR HOST NETWORK
static uint8_t ip[] = { 192, 168, 2, 57 };

#define PREFIX ""

WebServer webserver(PREFIX, 80);

void jsonDigitalCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  if (type == WebServer::POST)
  {
    server.httpFail();
    return;
  }

  //server.httpSuccess(false, "application/json");
  server.httpSuccess("application/json");

  if (type == WebServer::HEAD)
    return;

  int i;
  server << "{ ";
  for (i = 0; i <= 13; ++i)
  {
    // ignore the pins we use to talk to the Ethernet chip
    int val = digitalRead(i);
    server << "\"" << i << "\": " << val;
    if (i != 13)
      server << ", ";
  }
  server << " }";
}

void jsonAnalogCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  if (type == WebServer::POST)
  {
    server.httpFail();
    return;
  }

  //server.httpSuccess(false, "application/json");
  server.httpSuccess("application/json");

  if (type == WebServer::HEAD)
    return;

  int i;
  server << "{ ";
  for (i = 0; i <= 5; ++i)
  {
    int val = analogRead(i);
    server << "\"" << i << "\": " << val;
    if (i != 5)
      server << ", ";
  }

  server << " }";
}

void outputPins(WebServer &server, WebServer::ConnectionType type, bool addControls = false)
{
  P(htmlHead) =
    ""
    ""
    "Arduino Web Server"
    ""
    ""
    "";

  int i;
  server.httpSuccess();
  server.printP(htmlHead);

  if (addControls)
    server << "
"; server << "

Digital Pins

"; for (i = 0; i <= 9; ++i) { // ignore the pins we use to talk to the Ethernet chip int val = digitalRead(i); server << "Digital " << i << ": "; if (addControls) { char pinName[4]; pinName[0] = 'd'; itoa(i, pinName + 1, 10); server.radioButton(pinName, "1", "On", val); server << " "; server.radioButton(pinName, "0", "Off", !val); } else server << (val ? "HIGH" : "LOW"); server << "
"; } server << "

Analog Pins

"; for (i = 0; i <= 5; ++i) { int val = analogRead(i); server << "Analog " << i << ": " << val << "
"; } server << "

"; if (addControls) server << "
"; server << ""; } void formCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { if (type == WebServer::POST) { bool repeat; char name[16], value[16]; do { repeat = server.readPOSTparam(name, 16, value, 16); if (name[0] == 'd') { int pin = strtoul(name + 1, NULL, 10); int val = strtoul(value, NULL, 10); digitalWrite(pin, val); } } while (repeat); server.httpSeeOther(PREFIX "/form"); } else outputPins(server, type, true); } void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { outputPins(server, type, false); } const int analogCounter[] = {0, 1, 2, 3, 4 ,5}; const int analogAddr[] = {6, 9, 12, 15, 18, 21}; bool readAnalogValues = false; void setup() { // set pins 0-8 for digital input for(int i = 0; i <= 5; i++){ EEPROM.write(analogCounter[i], 0); EEPROMWritelong(analogAddr[i], 0); } Timer1.initialize(5000000); // initialize timer1, and set a 1/2 second period Timer1.pwm(9, 512); // setup pwm on pin 9, 50% duty cycle Timer1.attachInterrupt(changeSensorFlag); // attaches readSensors() as a timer overflow interrupt for (int i = 0; i <= 13; ++i) pinMode(i, OUTPUT); Ethernet.begin(mac, ip); webserver.begin(); Serial.begin(9600); webserver.setDefaultCommand(&defaultCmd); webserver.addCommand("jsonDigital", &jsonDigitalCmd); webserver.addCommand("jsonAnalog", &jsonAnalogCmd); webserver.addCommand("form", &formCmd); } void loop() { webserver.processConnection(); // if you wanted to do other work based on a connecton, it would go here if (readAnalogValues) { readSensors(); } } void changeSensorFlag(){ readAnalogValues = !readAnalogValues; } void readSensors(){ for(int i = 0; i<=5; i++){ long sensorVal = (long)analogRead(i); long beforeValue = EEPROMReadlong(analogAddr[i]); int counterValue = EEPROM.read(analogCounter[i]); int newCounter = counterValue + 1; long newSensorValue = sensorVal + beforeValue; EEPROMWritelong(analogAddr[i], newSensorValue); EEPROM.write(analogCounter[i], newCounter); } readAnalogValues = false; } double readSensorsValueAtMeanValue(int sensor){ long sensorAllCollectedData = EEPROMReadlong(analogAddr[sensor]); int dataCount = EEPROM.read(analogCounter[sensor]); return ((double)sensorAllCollectedData / dataCount==0 ? 1 : dataCount); } void EEPROMWritelong(int address, long value) { //Decomposition from a long to 4 bytes by using bitshift. //One = Most significant -> Four = Least significant byte byte four = (value & 0xFF); byte three = ((value >> 8) & 0xFF); byte two = ((value >> 16) & 0xFF); byte one = ((value >> 24) & 0xFF); //Write the 4 bytes into the eeprom memory. EEPROM.write(address, four); EEPROM.write(address + 1, three); EEPROM.write(address + 2, two); EEPROM.write(address + 3, one); } long EEPROMReadlong(long address) { //Read the 4 bytes from the eeprom memory. long four = EEPROM.read(address); long three = EEPROM.read(address + 1); long two = EEPROM.read(address + 2); long one = EEPROM.read(address + 3); //Return the recomposed long by using bitshift. return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF); }

No comments:

Post a Comment