Project tutorial

Home Automation Using Zone Beacon via Android App

An easy solution for making home automation a more affordable solution.

  • 2,367 views
  • 2 comments
  • 15 respects

Components and supplies

1434509556 android phone color
Android device
×1
A000066 iso both
Arduino UNO & Genuino UNO
×1
Wiz750sr ttl evb package 1024x858 1mcfig9l3s
WIZnet WIZ750SR-TTL-EVB Kit
×1
Relay Module (Generic)
×1

Apps and online services

About this project

Outline

In the hub bub of everyday life we forget to do many things. Some of those things like forgetting to switch off lights, fans and several appliances at home can cost us huge amounts which reflect in our electricity bills. This forgetfulness not only costs us huge bills but is also damaging to the environment as it wastes a lot of valuable resources. This problem has been addressed in our project with the creation of an automated home.

There are several technologies that have been used to create automated homes like Bluetooth, Zigbee, GSM technology, etc.The technology that we have used in our project to accomplish the ease and efficiency of fully automated home is MQTT along with the concept of Zone Beacons. The idea is to divide the house into several zones. Each zone with its own beacon and these individual beacons control only the devices connected to them.

In order to achieve the final result we used WIZnet’s S2E, along with Arduino UNO controlled by a mobile app.

Step One: Gathering Components

  • Arduino UNO: It is the decision maker. It decides which device connected to it is switched on/off.
  • Raspberry Pi: It acts as the broker for transfer of messages via MQTT.
  • WIZ750SR/S2E: It is a protocol converter that transmits data sent by serial equipment (e.g. Arduino) as TCP/IP data and converts the TCP/IP data received through the network(e.g. Mobile App) back into serial data to the serial equipment. Hence, it receives data from the Android app installed on a mobile and transmits this data serially to the Arduino.
  • WIZ750SR-EVB: This is the evaluation board for the WIZ750SR. The S2E can be fixed on top of this board.
  • Relay Module: It is used to amplify the electrical signals received from the Arduino so that it can be used to power devices that operate on higher voltages.

Step Two: Set Up the Circuit

The S2E can be fixed on top of the evaluation board. The S2E acts as a protocol converter using its Ethernet MCU i.e. W7500P. It transmits the TCP/IP messages received from the mobile app as serial data to the Arduino board.

Since, our project works using the Zone Beacon technology, the setup for one of the zones of the house will be as illustrated below:

Take a female to female RS232 cable and plug it into the RS232 port of the S2E. The other end of the RS232 cable needs to be connected to the Arduino. In the RS232 female head, pin 2 acts as the transmitter and pin 3 acts as the receiver. These pins are connected to the Arduino digital pins 11 and 10 respectively. The ground of the RS232 (pin 5) is connected to the ground of the Arduino.

The device that we want to power using the app can be connected to the digital pins of the Arduino. In our project we have selected pins D7, D8 and D12 for this purpose. However, since the voltage supplied by the Arduino is not enough to power up devices used at our homes, we'll be using a relay module to aid in the voltage conversion. The connections between the Arduino and the relay module are specified in the diagram above.

Step Three: Set Up Android Studio

The Android Studio IDE is freely available for download on their official website: https://developer.android.com/studio/.

Once the installation is done download the android code from the given GITHUB link. Open the downloaded file in the Android IDE. Then connect an Android device to the laptop via a USB cable. Then connect the device with the IDE. When connecting the device make sure that the developer option is activated in the settings of the device. The device should also be in debug mode.

Then run the code by clicking the green arrow button or using the "Run" option given in the menu highlighted using the red box in the image given below. These options upload the code onto the Android device in the form of an app.

After the setup and upload of the code onto a Android Device. The UI should look similar to what is shown in the images above.

Step Four: Set Up Raspberry Pi as MQTT Broker

MQTT is a lightweight publish-subscribe messaging protocol meant to be used by mobile devices and small sensors. The MQTT broker is responsible for receiving all messages and determining who is subscribed to each message, and sending the message to these subscribed clients. The MQTT client is any device that runs an MQTT library and connects to an MQTT broker over a network. In this project, we use a Raspberry Pi to act as the MQTT broker.

Raspberry Pi is a Linux based minicomputer. A MQTT broker can be easily installed in any Linux based OS with three simple commands :

  • sudo apt-get update
  • sudo apt-get dist-upgrade
  • sudo apt-get install mosquitto

We set up the Raspberry Pi using the above commands. Once set up, all we need to make sure is that the broker stays switched on when trying to send data across from one device to another.

Also make sure that the Pi is connected to your local Wifi connection. Then note the IP address assigned to Pi after it is connected to Wifi. This is our broker's IP address, it is necessary so that our devices are able to communicate.

This step is essential because when we use the Android app to turn on a device it publishes a request to the MQTT broker i.e. Raspberry Pi with the help of the IP address obtained above.

Step Five: Upload the Code to S2E

1. First and most step is to set the pin on the evaluation board of the S2E to BOOT mode and press reset. i.e. move the center switch (highlighted using a black box in the bottom left hand corner of the above image) to the right before starting the next step.

2. Then download the W7500P ISP tool from the following link : http://wizwiki.net/wiki/doku.php?id=products:wiz750sr:download:start.The ISP tool will look as shown below:

3. Connect the S2E to a laptop via its microUSB port to figure which COM number is assigned to it using "Device Manager". The USB to UART driver can be downloaded from https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers if it’s not installed on your device. In the image below it can be seen that COM6 has been assigned to the S2E.

Then hit "Refresh" button in the ISP tool and select the appropriate port number from the drop down list. The baud rate should be fixed at 115200.

2. Then hit "Open" button in the ISP tool. If successful, one can see the result - "Serial Open Complete" in the left bottom corner.

3. The S2E uses ARM Cortex-M0 architecture. In order to program the S2E we'll have to use the ARM compiler that can be accessed using: http://os.mbed.com/compiler. When you login into the compiler select the board as "WIZwiki- W7500P".

4. Upload the "S2E_W7500P_Code" available in the "Code" section of this project into the Mbed compiler. Then compile the code and the corresponding bin file will be downloaded automatically into your system.

5. The next step is to open the W7500P ISP and select the bin file downloaded from the MBED compiler. Once selected, hit the "ISP Start" button to write into the WIZnet Board.

Step Six: Upload the Code to Arduino

The Arduino is the mastermind of the project. It decides which device gets the power supply based on the input from the mobile app. In our project we have used an Arduino Uno. It’s the one that is most commonly available in the market and can be purchased from any electronics hobby shop or even ordered online.

In order to use the Arduino we must first install its IDE. The Arduino IDE is easy to use as well as open source and can be easily downloaded from the official Arduino website. Once the IDE is installed, the next step would be to select the board. The board can be selected by going through the “Tools” option as depicted in the image below:

Once the board is selected. The port at which the Arduino will be available for serial communication needs to be selected as depicted below:

The next step would be to upload the “Arduino_code”(available in the code section of this project’s description) into the IDE. Once that is done the code can be compiled and uploaded onto the board by clicking the tick mark and arrow button respectively.

Step Eight: Final Result

We can use CoolTerm to test out the serial port, which can be downloaded from https://coolterm.en.lo4d.com/. CoolTerm will enable us to read all the messages sent to the COM port.

When the message is sent via the app the response output from the Arduino can be seen on the CoolTerm terminal.

The MQTT model, publishes the message from the Android app to the broker i.e. the Raspberry Pi, where the broker forwards the data to any devices subscribing to that data. In the case of our project, the S2E is the device subscribing to the data available with the broker. The data read by the S2E is interpreted by the Arduino and decision is taken on which device is to be turned on/off.

Code

Arduino_CodeArduino
This code can be accessed using an Arduino IDE
/* This code is written for the purpose of choosing which device is switched on and off as the Arduino is the decision maker of our project.
The Arduino does this on the basis of the input string read by it sent by the W7500P board via the S2E channel */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // Set pin 10 as RX of Arduino and pin 11 as TX


void setup() {
  
Serial.begin(9600);     //setting baud rate for serial communication
mySerial.begin(9600);  //setting baud rate for communication b/w Arduino and S2E
pinMode(12, OUTPUT);  //Setting up pin 12 as output pin for device1
pinMode(7, OUTPUT);  //Setting up pin 7 as output pin for device2
pinMode(8, OUTPUT); //Setting up pin 8 as output pin for device3
}

void loop() {
  String c ;
 while(mySerial.available()) 
 {
    c += mySerial.readString(); // Storing the input from the S2E Board
    
    if(c == "ON1")
      {
        Serial.println("Switching on device 1");
        digitalWrite(12, HIGH); //Switches on device 1
      }
     else if(c == "ON2")
      {
        Serial.println("Switching on device 2");
        digitalWrite(7, HIGH); //Switches on device 2
      }
     else if(c == "ON3")
     {
        Serial.println("Switching on device 3");
        digitalWrite(8, HIGH); //Switches on device 3
     }
     else if(c == "OFF1")
      {
        Serial.println("Switching off device 1");        
        digitalWrite(12, LOW); //Switches off device 1
      }
     else if(c == "OFF2")
      {
        Serial.println("Switching off device 2");
        digitalWrite(7, LOW); //Switches off device 2
      }
     else if(c == "OFF3")
     {
        Serial.println("Switching off device 3");
        digitalWrite(8, LOW); //Switches off device 3
     }
 }
    Serial.println(c);
}
S2E_W7500P_CodeC Header File
This is the code to receive data from the Android APP. It is created using MBED online Complier
#include "mbed.h"
#include "DHT.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"

#include <string> 
#define ECHO_SERVER_PORT   7
 
int arrivedcount = 0;
Serial a(D1,D0);
Serial s(USBTX, USBRX);
char* topic = "1";  // enter topic name here

//In-Built function for recieving messages
void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;
    //printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
    printf("%.*s%s\n", message.payloadlen, (char*)message.payload);
    ++arrivedcount;
    a.printf("%.*s%s", message.payloadlen, (char*)message.payload);
    //a.printf("%s", topic);
}

//Setting Baud Rate
void baud(int baudrate) {
    s.baud(baudrate);
}

int main (void)
{
    baud(115200);
    printf("Wait a second...\r\n");
    
    MQTTEthernet ipstack = MQTTEthernet();
    
    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
    
    char* hostname = "XXX.XXX.X.X"; // INSERT BROKER IP ADDRESS 
    int port = 1883; // INSERT ASSIGNED PORT NUMBER OR USE DEFAULT PORT 1883
    
    int rc = ipstack.connect(hostname, port);
    if (rc != 0)
        printf("rc from TCP connect is %d\n", rc);
        
    //Setting up the required DATA for MQTT
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
    data.MQTTVersion = 3.1;
    data.clientID.cstring = "parents";
    data.username.cstring = "xxxx"; //SET USERNAME IF REQUIRED FOR BROKER
    data.password.cstring = "xxxxxx"; //SET PASSWORD FOR THE BROKER

    if ((rc = client.connect(data)) != 0)
        printf("rc from MQTT connect is %d\n", rc);
        
    if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0)
        printf("rc from MQTT subscribe is %d\n", rc); 
  // LOOP to listen for MQTT subscription data
    while(1)
    {
        client.yield(60000);
    }             
}
Android Application Code
This is the mobile app used to control the device using the internet. The code can be accessed using ANDROID STUDIO

Schematics

CIRCUIT_diagram
In R232, the port 2 is the TX and port 3 is the RX and port 5 is GND.
Schematic sfcollocxz

Comments

Similar projects you might like

Android App-Based Home Automation System Using IOT

Project tutorial by Team Autoshack

  • 46,124 views
  • 22 comments
  • 132 respects

Control Arduino Robot Arm with Android App

Project tutorial by Slant Concepts

  • 18,368 views
  • 8 comments
  • 35 respects

Open Data Home Automation - Arduino 101 & App Inventor

Project tutorial by 3 developers

  • 3,437 views
  • 1 comment
  • 11 respects

Infrared Remote

Project tutorial by 4 developers

  • 4,614 views
  • 0 comments
  • 37 respects

Aquarium Assistant

Project tutorial by Fady Tarek

  • 2,319 views
  • 0 comments
  • 8 respects

Arduino Uno + Electrical Device (Bulb) + Android Phone

Project tutorial by Stephen Simon

  • 9,981 views
  • 9 comments
  • 40 respects
Add projectSign up / Login