Introduction: Room Automation Control Using Arduino and Android

I’ve started using Arduino a year ago. Since then I’ve seen a lot of good projects but the most useful in my opinion were those for home automation. Thera are a lots of cheap sensors that you can find nowadays so I was wondering why don’t we use them and an arduino in order to make a room automation?

Below you can see some steps as a tutorial to construct your own room automation system. It’s of really importance the cheap cost of the hardware to make your construction.

Focus on a cheap hardware to build the project:

  1. Low cost. Each sensor node costs less than $2

  2. Easy constructable. Arduino’s nodes are so easy to program so that everyone could make a desirable system according to their needs.

  3. Environmentally friendly. For all the times that you have to leave home urgently and you don’t have the appropriate time or you forget to turn off the lights here comes room automation system. It provides you to turn off your forgotten lights just by pussing a button on your smartphone while you are connected with a source of internet . It also takes action when the day comes and turn off the lights. In these ways you don’t have to worry about your electricity bills and it is of course a good way to save energy.

Parts of interest

  • Light

Use PIR and Light Resistor sensors together.When the room is dark and the sensor detects movement then the light turns on and the sensor sends a notification on your smartphone through the application to inform you for the movement inside your house and that the light is on. In addition to that when the sun rises the sensors close the lights automatically.

The light is a led strip that is connected in the relay and 12volt power supply and is placed on the floor to light the room. (We can use the light of the room but I didn’t make it because I didn’t have 220w electric power connection and of course it is dangerous)

  • Temperature and Humidity

The dht sensor sends a notification through the application in your smartphone about the temperature and the humidity when you ask for it.

I have a small fan which when the temperature gets higher than 30 Celsius degree it turns automatically on and when the temperature becomes cooler than 30 degrees it stops working. The fan is connected to relay in same 12volt power supply so as the led strip..

  • IR

I’ve seen the code of the 24 key from the led strip remote control. The app controls the led strip remote using the IR Led transmit from an old TV remote control.

  • Ethernet shield

The Ethernet shield is connected with the router and communicates via UDP packet with the app of your smartphone. That give us the ability to turn on the lights from the app, turn on and change the color of the led strip , get informed about the room’s temperature, humidity, possible gas emissions in the room, movements and stuff

  • Push Button

The push Button is used in order to turn on the lights from the button through the application on our mobile phone like if we were at home.

See the Video (sorry for the quality)

Step 1: What We Need for the Projecct

Microcontroller is an Arduino mega
2560 (clone ebay 6$) you can use the Uno if you want i use mega because i put and an LCD 2X16 and wait for same new sensors to come to put it in the project

1 Arduino Ethernet shield for mega (ebay 5$)

1 PIR sensor (ebay 1$)

1 Photo resistor sensor (ebay 0.99$)

1 DHT21 temperature/humidity sensor (ebay 2.60$)

1 push button (ebay 0.9$)

1 Relay 4 channels (ebay 2.5$)

1 IR sensor I used from an old TV remote controller or you can bay from ebay 1$

1 AC Converter Adapter DC 12V 0.5A 500mA Power Supply (ebay 2.88$)

lots off Arduino cables

1 resistor 100Ohm, 1 resistor 10KOhm, 1 resistor 230 Ohm

optional (is not necessary)

an electronic box to put the project

1 lcd display 2X16

dc connectors male/female to connect the lads and the fan

a pc fan connects with three pins to the sensor which I’ve put in the room. I used pc fan connectors because i had to use a lot of them for every one of the three pin connectors.

I have put RGB led strip and white strip on my one.

Step 2: With Simple Words What Is Make All Project

In the Arduino pins we can see were we connect every sensor.

  • Light

PIR in PIN analog A5

Photo resistor connect in PIN analog A4

Also have GND and get 5volt power from Arduino

Use PIR and Ligth resistor sensor together. When the room is dark and the sensor detect movement opens the light and notification the smart phone application for the movement and the light on.

The light is a led strip which connect in the relay and 12 volt charger and is put on the floor to light for a second the room. Also if the sun goes up and the light is useful and west electric power then Arduino is turn of the light.

  • Temperature and Humidity

Temperature/humidity in PIN analog A3 NEED 3.3VOLT not 5volt Attetion

The DHT sensor send in phone the temperature and the humidity when user ask it from the app. I have a small fan which when the temperature get higher than the 30 Celsius degrees is tern of the fan when cool down to 30 Celsius degree is turn off the fan. The fan connects to relay in same 12volt power supply which connect and the led strip.

  • IR

IR connect in PIN 9 and GND

I have save all the code of the 24 key from the led-strip. The app is control the led strip remote using the IR.

  • Ethernet shield

The Ethernet shield is connects with the router and communicate via UDP with the app of smart phone. This give as the flexibility to open light from the app, open and change the color of led strip , get the temperature, humidity , gas if exist in room, if exist motion and other.

  • Push Button

Push Button connect in Pin 2 GND and 5volt

The push Button is used to open the light from the button

Step 3: Ethernet Conection and Code

The UDP is work like the second image when the sender have the message then put the UDP header after the IP header make the frame and send it to the receiver the receiver remove the headers IP and UDP and get the message and replay with same way. For this reason we need the IP and the port of the Arduino to send the firt message from the app and from this message the Arduino get ip and port of app to replay

First connect the Arduino Mega with the Ethernet Shield.
You need to do the Include libraries SPI.h Ethermet.h EhernetUdp.h and SD.h

After set MAC of Shield the mac is in an sticker in the Ethernet shield or you can use the mac i write bellow and a Port.

// Enter a MAC address and IP address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// The IP address will be dependent on your local network:

IPAddress ip(192, 168, 1, 15); use this IP to work the app

int localPort = 8888; // local port to listen on

and initialize two char arrays which help to store the receive packet and other to build the send packet.

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet

char ReplyBuffer[38];//[] = "acknowledged"; // a string to send back

And start the UDP to use to send and receive in the loop of arduino code

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

in setup code start the Ethernet and UDP:
Ethernet.begin(mac, ip);

Udp.begin(localPort);

In the loop

int packetSize = Udp.parsePacket(); // hear receiving the packet

if (packetSize) { if exist a recieve packet then the IF is true

Serial.print("Received packet of size "); /// for

Serial.println(packetSize); /// debugging

Serial.print("From "); /// show in serial monitor the packet

IPAddress remote = Udp.remoteIP(); /// Get the IP from the sender

for (int i = 0; i < 4; i++) {

Serial.print(remote[i], DEC);

if (i < 3) { Serial.print("."); } }

Serial.print(", port ");

Serial.println(Udp.remotePort()); /// The port

When we want to send UDP packet then

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); // first give the IP and port of the App we have it from the incoming pucket

Udp.write("message"); /// set the message in udp packet

Udp.endPacket(); //. end send it

Step 4: PIR and Light Conection and Code

Use PIR and Light resistor sensor together.

We connect the PIR is in A5 and the Photo resistor sensor is connect in A4.

They power from 5Volt Arduino

When the room is dark and the
sensor detect movement opens the light and notification the smart phone application for the movement and the light on (notification is to turn off or on the light switch of android app).

The light is a led strip which connect in the relay and 12volt charger and is put on the floor to light for a second the room.

if (analogRead(photosensor) >= 850) { // dark if the value of photosensor is biger than 850
Serial.print("Dark");

PIRState = digitalRead(PIR); // then get the value of the PIR sensore // if detect move the PIRState is HIGH if not move then is LOW if (PIRState == HIGH) {

digitalWrite(relayfloor, LOW); // open the relayfloor to give power on leds delay(5000); // delay to stay on the leds

Serial.print("Motion PIR"); // debugiing show in serial monitor

PIRState == LOW; // set the PIRState low no motion-movement

} else { // if have not movement-motion close the

Serial.print("No motion");

digitalWrite(relayfloor, HIGH); }

} else { // the photosensor take value for day and the relay close // we not check the PIR sensore digitalWrite(relayfloor, HIGH);

Serial.print("Day No light need"); }

And from the application we can open the light buy send a udp message on Ledon and turn off the light send the udp Ledoff

The arduino receive and check the message is in packetBuffer and check if is Ledon or Ledoff

if (strcmp(packetBuffer, "Ledon") == 0) {
digitalWrite(relaydesk, LOW); replay = "leds desk on"; }

else if (strcmp(packetBuffer, "Ledoff") == 0) {

digitalWrite(relaydesk, HIGH); replay = "leds desk off"; }

Step 5: Temperature and Humidity Conection and Code

The DHT need to download a library is here

We connect the DHT in 3.3Volt for power and data in pin A3.

The DHT21 sensor send in phone the temperature and the humidity when user ask it from the app.

We include the DHT.h

define the type and the pin of the sensor

DHT dht(DHTPIN, DHTTYPE);

start the in setup in code

dht.begin();

first the sensor get the temperature and humidity and make a string

h = dht.readHumidity();
t = dht.readTemperature();

String te = "Temperature: " + String(t) + "*C Humidity: " + String(h) + " %";

And after send a packet to the app with the follow code

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

Udp.write(temp);

Udp.endPacket();

I have a small fan which when the temperature get higher than the 30 dirge is tern of the fan when cool down to 30 degree is stop. The fan connect to relay in same 12 volt charger which connect and the wight led strip.

if (t > 30) {
digitalWrite(relayfan, LOW);

} else {

digitalWrite(relayfan, HIGH);

}

Step 6: IR Senor Connection and Code

The IR Is from an old TV remote control, first connect the positive of IR led with a 230 Ohm and connect in 9 PIN and the GND of Arduino.

We need to download the IRremote library here

I have save all the code of the 24 key from the led strip.

For example, the code to close the led strip is 0x00F700FF.

The app is control the led strip remote using the IR.

First the app send a packet with the command on/off , red/blue etc , then the Arduino check witch code of IR is for this command and the IR is send signal to led strip.

if (strcmp(packetBuffer, "ON") == 0) {
irsend.sendNEC(0x00F7C03F, 32);

delay(2000); }

else if (strcmp(packetBuffer, "OFF") == 0) {

irsend.sendNEC(0x00F740BF, 32);

delay(2000); }

if (strcmp(packetBuffer, "BrightnessUp") == 0) {

irsend.sendNEC(0x00F700FF, 32);

delay(2000); }

Step 7: Push Button Connection and Code

The push button connect in Pin 2. GND and 5Volt of arduino.

The push Button is used to open the light from the button with the old traditional way using switch

if (buttonState == HIGH) {
counter++; }

if (counter % 2 == 0) {

// turn LED on:

digitalWrite(relaydesk, LOW); }

else { // turn LED off: digitalWrite(relaydesk, HIGH); }

Step 8: A Show for the End

Firstly, I am sorry if my English is bad :(.

If you like the Project, please vote me.

You can download the arduino code here

You can download the apk from here(the app use the xxx.xxx.xxx.xxx)ip of the aruino set in your arduino project this IP)

In future work i have an Idea to add gas sensor and find motors 12volt to make the Shutters to open from the app, and an alarm with leaser detect movement and some other ideas.

And the app to put the ip and port of arduino and use in anyone arduino which connect in TCP/IP and have IP and port.
When have got the part ;) and some time and money off course :P because I am in the end of my MAster

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017