Arduino based IOT sample home automation project

Arduino

This is a sample Arduino based IOT application, IOT based home automation project. We used Arduino uno and Arduino ethernet shield. Ethernet shield connected with internet. Light fan status stored in cloud server, below is our sample URL

http://www.iatlbd.com/iot-remote-switch/

Basic Idea for IOT

ITO-Designs

 

Circuit Diagram

iot

Sample code:
// Code: IATLBD
#include <SPI.h>
#include <Ethernet.h>
#include <LCD.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal.h>
LiquidCrystal_SR lcd(9, 7, 8);
// DATA CLOCK LATCH
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; //<<< ENTER MAC ADDRESS HERE!!!

IPAddress ip(192,168,1,10); //<<< ENTER YOUR IP ADDRESS HERE
IPAddress dnsip(192,168,1,1); //<<< ENTER DNS ADDRESS HERE
IPAddress gateway(192,168,1,1); //<<< ENTER GATEWAY ADDRESS HERE
IPAddress subnet(255,255,255,0); //<<< ENTER SUBNET MASK HERE
EthernetClient client;

const int requestInterval = 6000;
const int lightpin = 4; //Light output pin for relay driver
const int fanpin = 5; //FAN output pin for relay driver
const int lightswitch=2; //Light switch pin
const int fanswitch=3; //FAN switch pin
const int lcdpow=6; // VCC pin for LCD

char serverName[] = “www.iatlbd.com”;// Server address
//Existing URL http://www.iatlbd.com/iot-remote-switch-api.php?light=1&fan=1&write=0

boolean requested;
long lastAttemptTime = 0;

String currentLine = “”;
boolean reading = false;
boolean bellring=true;
boolean lightstate=false;
boolean fanstate=false;
boolean lightpress=false;
boolean fanpress=false;

String cmd=””;
int light=0,fan=0,counter=0;

void setup() {
//Declare PIN mode for Light and FAN switch and relay driver
pinMode(lightpin, OUTPUT);
pinMode(fanpin, OUTPUT);
pinMode(lcdpow, OUTPUT);
pinMode(lightswitch, INPUT);
pinMode(fanswitch, INPUT);
//Provide output HIGH for LCD power supply
digitalWrite(lcdpow,HIGH);
// reserve space for the strings:
currentLine.reserve(256);

// initialize serial:
Serial.begin(9600);
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
lcd.clear(); // clear the screen

Serial.println(“Ethernet begin”);
lcd.setCursor(0, 0);
lcd.print(“Starting”);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a static address:
Ethernet.begin(mac, ip, dnsip, gateway, subnet);
Serial.println(“DHCP fail”);
lcd.setCursor(0, 0);
lcd.print(“DHCP Fail.Stat ip “);
delay(1000);
}else{
lcd.setCursor(0, 0);
lcd.print(“DHCP Ok “);
delay(1000);
}
Serial.println(“Ethernet start”);
lcd.setCursor(0, 1);
lcd.print(“Initialized”);

// Set Output PIN to low
digitalWrite(lightpin,LOW);
digitalWrite(fanpin,LOW);

lastAttemptTime = millis();

}

void loop()
{
lcd.setCursor(0, 0);
lcd.print(“IOT Home App “);

if (client.connected()) {
if (client.available()) {

char inChar = client.read();
currentLine += inChar;

if (inChar == ‘\n’) {
Serial.println(currentLine);
currentLine = “”;
}

if ( currentLine.endsWith(“IOTE”)) {

reading = true;
Serial.println(currentLine);
cmd=currentLine.substring(4,7);
Serial.println(cmd);
}

if (reading) {
//Process server response
Serial.println(“Stop the client”);
String Fan=getValue(cmd,’:’,0);
String Light=getValue(cmd,’:’,1);
fan=Fan.toInt();
light=Light.toInt();

Serial.print(“Fan:”);Serial.println(fan);
Serial.print(“Light:”);Serial.println(light);
lcd.setCursor(0, 1);
lcd.print(“Fan:”);lcd.print(fan);lcd.print(“::Light:”);lcd.print(light);
//Sync data from server
if(fan==1 && fanstate==false){
digitalWrite(fanpin,HIGH);
fanstate=true;
Serial.print(“Fan state change”);
}
if(fan==0 && fanstate==true){
digitalWrite(fanpin,LOW);
fanstate=false;
Serial.print(“Fan state change”);
}
if(light==1 && lightstate==false){
digitalWrite(lightpin,HIGH);
lightstate=true;
Serial.print(“Light state change”);
}
if(light==0 && lightstate==true){
digitalWrite(lightpin,LOW);
lightstate=false;
Serial.print(“Light state change”);
}
client.stop();
reading=false;
currentLine=””;

}
}
}
else if (millis() – lastAttemptTime > requestInterval && bellring==true) {

connectToServer(0,0,0);
bellring=false;

}else{
if(bellring==false){
Serial.println(“bellpress false”);

delay(1000);
counter++;
if(counter>3){
counter=0;
bellring=true;
}

}

//Read local data change and upload to server
lightpress=digitalRead(lightswitch);
fanpress=digitalRead(fanswitch);

if(lightpress==false){
if(lightstate==true){
digitalWrite(lightpin,LOW);
lightstate=false;
Serial.print(“Light state change-o”);

}else{
digitalWrite(lightpin,HIGH);
lightstate=true;
Serial.print(“Light state change-s”);

}
//Update state to server
connectToServer(lightstate,fanstate,1);
bellring=false;
}

if(fanpress==false){
if(fanstate==true){
digitalWrite(fanpin,LOW);
fanstate=false;
Serial.print(“Fan state change-o”);
}else{
digitalWrite(fanpin,HIGH);
fanstate=true;
Serial.print(“Fan state change-s”);
}
//Update state to server
connectToServer(lightstate,fanstate,1);
bellring=false;
}

}
}

void connectToServer(int light,int fan,int writec) {

Serial.println(“connecting to server…”);
if (client.connect(serverName, 80)) {
Serial.println(“making HTTP GET request…”);

client.print(“GET /iot-remote-switch-api.php?light=”);client.print(light);client.print(“&fan=”);client.print(fan);client.print(“&write=”);client.print(writec);client.println(” HTTP/1.1″);

client.println(“HOST: www.iatlbd.com”);
client.println();
}else{
lcd.setCursor(0, 1);
lcd.print(“Server Error!!”);
}

lastAttemptTime = millis();
}
//String parsing function
String getValue(String data, char separator, int index)
{
int maxIndex = data.length() – 1;
int j = 0;
String chunkVal = “”;

for (int i = 0; i <= maxIndex && j <= index; i++)
{
chunkVal.concat(data[i]);

if (data[i] == separator)
{
j++;

if (j > index)
{
chunkVal.trim();
return chunkVal;
}

chunkVal = “”;
}
else if ((i == maxIndex) && (j < index)) {
chunkVal = “”;
return chunkVal;
}
}
}

 

Video tutorial