NITIN GShivalika Singh
Published

Home Automation Using Zone Beacon via Android App

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

IntermediateFull instructions providedOver 1 day4,890

Things used in this project

Story

Read more

Schematics

CIRCUIT_diagram

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

Code

Arduino_Code

Arduino
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_Code

C 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

Credits

NITIN G

NITIN G

1 project • 0 followers
Shivalika Singh

Shivalika Singh

1 project • 0 followers

Comments