Avirup Basu
Published © GPL3+

Home Automation Using Windows Phone and Intel Edison

Perform full scale home automation with the use of an Windows Phone app and an Intel Edison.

IntermediateFull instructions provided2 hours1,980
Home Automation Using Windows Phone and Intel Edison

Things used in this project

Hardware components

Relay (generic)
×1
Electric bulb
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Arduino IDE
Arduino IDE
Microsoft Azure
Microsoft Azure

Story

Read more

Schematics

Schematics with a LED attached to PIN 13.

You can replace it with a relay attached to any digital pins of the Intel Edison

Code

Arduino code for connecting your Intel Edison with Windows Azure

C/C++
Please fill in with your own api key and table name
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>

#define RESPONSE_JSON_DATA_LINENNO 10


const char* server= "Azure server name";
const char* table_name= "Your table name";
const char* ams_key="API Key";

char stat;
WiFiClient client;


char ssid[] = "network ssid"; //  your network SSID (name) 
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

char buffer[150];
int charIndex=0;
StaticJsonBuffer<200> jsonbuffer;

//Send http get request to get azure strng
void send_request()  
{  
  Serial.println("\nconnecting...");  
  
  if (client.connect(server, 80)) {  
  
  
    // POST URI  
    sprintf(buffer, "GET /tables/%s HTTP/1.1", table_name);  
    client.println(buffer);  
  
    // Host hea1der  
    sprintf(buffer, "Host: %s", server);  
    client.println(buffer);  
  
    // Azure Mobile Services application key  
    sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);  
    client.println(buffer);  
  
    // JSON content type  
    client.println("Content-Type: application/json");  
  
    client.print("Content-Length: ");  
    client.println(strlen(buffer));  
  
    // End of headers  
    client.println();  
  
    // Request body  
    client.println(buffer);  
      
  } else {  
    Serial.println("connection failed");  
  }  
} 

/*
** Wait for response
*/
//Wait for the ncoming response from azure
void wait_response()
{
  while (!client.available()) 
  {
    if (!client.connected()) 
    {
      return;
    }
  }
}
//Read the incoming response
void read_response()  
{  
  boolean bodyStarted;  
  int jsonStringLength;  
  int jsonBufferCntr=0;  
  int numline=RESPONSE_JSON_DATA_LINENNO;  
  //Ignore the response except for the 10th line  
  //save the response
  while (client.available())   
  {  
    //Serial.println("Reading:");  
    char c = client.read();    
    if (c == '\n')  
    {  
      numline -=1;  
    }  
    else   
    {  
      if (numline == 0 && (c!='[')&& (c!=']') )  
      {  
        buffer[jsonBufferCntr++] = c;   
        buffer[jsonBufferCntr] = '\0';   
      }  
    }  
  }  
  Serial.println("Received:");  
  Serial.println(buffer);  
  Serial.println("");  
  parse();  
}
//Parse the incoming data
void parse()  
{  
  StaticJsonBuffer<150> jsonbuffer;  
  JsonObject& root = jsonbuffer.parseObject(buffer);  
  if(!root.success())  
  {  
    Serial.println("PARSING FAILED!!!");  
    return;  
  }  
  int f= root["status"];  
  Serial.println("Decoded: ");  
  Serial.println(f);  
  if(f==0)  
    digitalWrite(8,LOW);  
  else  
    digitalWrite(8,HIGH);  
}
//End the response
void end_request()
{
  client.stop();
}

/*
** Arduino Setup
*/
//Setup the initial conditions
void setup()
{
  pinMode(8,OUTPUT);
  Serial.begin(115200);
  while (!Serial) {
    ;
  }
  
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);
  } 

  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 SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
  
  }
}

/*
** Arduino Loop
*/
//Execute statements in a sequential manner
void loop()
{
  //int val = qq++;
  send_request();
  wait_response();
  read_response();
  end_request();
  delay(100);
}

Credits

Avirup Basu

Avirup Basu

2 projects • 17 followers

Comments