Project in progress
IoT arduino ESP Garage Door opener (UD)

IoT arduino ESP Garage Door opener (UD) © Apache-2.0

Use your android smartphone to open and close your garage door reliably (Under Development)

  • 13,198 views
  • 6 comments
  • 20 respects

Components and supplies

Necessary tools and machines

Computer
09507 01
Soldering iron (generic)

Apps and online services

About this project

Remember this is still under development and It will be another couple of months before I take out all the small bugs, But feel free to modify this code for your situation.


If done correctly You should get this output

The blue LED indicates waiting for a response

If you look to the right you can see I have two AC-DC converters It's because I was low on parts and the arduino takes 5v and ESP 3v (my 3v voltage regulator burned out on a last project)


Before we Begin

You'll need this library for arduino IDE before we can begin

https://github.com/itead/ITEADLIB_Arduino_ESP8266

Also you'll need to know how to modify the code so you can adjust to your garage door

Parts needed:

  • 10uf capacitor
  • 0.1uf capacitor
  • 1k resistor
  • 2.2k resistor
  • 3.3v regulator
  • Wiring
  • A box
  • Arduino nano (or any other)
  • ESP8266
  • RGB LED
  • A relay
  • A breadboard or protoboard


Before attaching to the garage get it on a breadboard first

Wiring

Relay to arduino:

- to gnd

+ to 5v

s to MOSI (pin 11)


ESP to arduino (unless said otherwise)

gnd to (power) gnd

Tx to 10

Rx to 1k and 2.2k resistor (Arduino outputs 5v so you need to go down to 3.3v) then to pin 11

CH_PD to 3.3v

Vcc to 3.3v (voltage regulator)


arduino to power

 gnd to gnd

vcc to 5v


Other

10uf C to 5v and Gnd

0.1uf C 3.3v and (gnd)3.3v regulator

0.1uf C to esp gnd and esp 3.3v

then plug everything in


Transfer

You can transfer all the stuff onto the breadboard make sure you don't short the ESP so carefully scratch out the area for the esp


That's it for the Wiring look below for everything else


I Am not responsible for you burning up your own parts, but always feel free to contact me if you are nervous about attaching something

Code

GarageUnfinishedC/C++
Upload this to your Arduino
void(* resetFunc) (void) = 0;
#include <ESP8266.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>

//Values most likely needed to be edited//
#define SSID        "SSID"       //Your router SSID
#define PASSWORD    "PASSWORD"    //Your router Password
#define serverIP    "IP" //Your modules IP
#define gateWay     "IP"  //Gateway
#define PORT        8080          //The port that your using(Make sure your router is port forwarded for your phone)
#define debug       true          //Put false if youre arduino is not connected to your computer
#define COMBaud     115200        //Put the computer baudrate(make sure in Serial monitor it's also 115200)
#define ESPBaud     9600          //Don't mess with this unless either switching to hardware serial or ESP only supports 115200
#define RGBs        true          //If you have RGB LED connected   //MAKE SURE YOUR ESP AND ARDUINO HAVE THE SAME BAUDRATE!(Check first)
#define RedGB       3             //Pin for red LED
#define RGreenB     5             //Pin for Green LED
#define RGBlue      4             //Pin for Blue LED
#define Solenoid    11            //The solenoid pin
#define GarageOCT   8000          //The time in Ms for the garage open and close time (Usually 8 seconds)
#define defaultState false        //Once powered up what state will the garage be...(false=Open, true=Close)
#define ServerTimeout  13         //The time it takes for 
//End of most likely

SoftwareSerial ESPSerial(10, 11); //Arduino Rx to Tx on ESP, ESP Rx to Resistors to Arduino Tx

ESP8266 wifi(ESPSerial, ESPBaud); //This library was modified so just use the one I am using to make it work

bool state; //Set value above to global non static value
int first = 0;
int baudaddr = 1;
int states = 2; //Make sure your EEPROM IS CLEAR FOR THIS TO WORK or phone will get weird answers!!!!
byte firstes;
byte baudaddres;
byte stateses;

void setup()
{
    firstes = EEPROM.read(first);
    baudaddres = EEPROM.read(baudaddr); //Read the EEPROM values for index of 0, 1, 2
    stateses = EEPROM.read(states);
    if(RGBs) //If RGBs are enabled open the pins
    {
      pinMode(RedGB, OUTPUT);
      pinMode(RGreenB, OUTPUT); //Start RGBs
      pinMode(RGBlue, OUTPUT);
      off(); //Turn them all off
    }
    if(debug) //Option above for computer debugging
    {
    Serial.begin(COMBaud); //Start at Buadrate above
    while(!Serial);
    delay(500); //Since Serial is Asyncronous sometimes you might get some jumbled unwanted crap from the arduino
    Serial.println("Started Garage System\nGoing Station mode");
    Serial.println("Current Version: " + (String) wifi.getVersion().c_str()); //Get all the board info such as manufacturer 
    }
    else
    {
      wifi.getVersion().c_str(); //This has to be runned anyway
    }
    if (wifi.setOprToStation()) { //Make sure when switching modes everything goes right
        if(debug)
        {
        Serial.println("Station Mode okay");
        }
    } else {
        if(debug)
        Serial.println("Station Mode Not okay");
        error();
    }
    if(firstes != 5) //If any value but 5
    {
      if(baudaddres != ESPBaud) // and the current ESP baud is not the same
    {
      if(wifi.setUart(ESPBaud, 2)) //Tell the device to change the baud
      {
        if(debug)
        Serial.println("Welcome first I automatically changed the baud for you");
        
      }
      else
      {
        if(debug)
        Serial.println("Sorry first timer, there was an error changing the baud");
        error();
      }
      EEPROM.write(baudaddr, ESPBaud); //EEPROM the new baud(Optional) 
    }
    if(stateses != 1 || stateses != 2) //Set default state if there already isn't one
    {
      if(!defaultState)
      {
        EEPROM.write(states, 1);
        states = false;
      }
      else
      {
        EEPROM.write(states, 2);
        states = true;
      }
    }
    EEPROM.write(first, 5); //Make the first time finished
    }
    else //Call this if it isn't the first time
    {
      if(stateses == 1) //Remember false is open so 1 is open
      {
        states = false;
      }
      else //Remember that true is closed so 2 is closed
      {
        states = true;
      }
    }
    if(debug)
       Serial.println("Attempting to connect to router");
    if (wifi.joinAP(SSID, PASSWORD)) { //Connect to a router with specified SSID and PASSWORD
        if(debug)
        {
        Serial.println("Connection succesful\nIp Address");
        Serial.println(wifi.getLocalIP().c_str()); //Print current address for easier use
        }
        else
        {
          wifi.getLocalIP().c_str(); //Just to be safe call this command even when debug isn't on
        }
    } else {
        if(debug)
        Serial.println("Connection failed");
        error();
    }
    if(debug)
    Serial.println("Setting mode to static");
    if(wifi.setStationIp(serverIP, gateWay, "255.255.255.0")) //Go from DHCP to Static
    {
      if(debug)
      Serial.println("Success to moving into static");
    }
    else
    {
      if(debug)
      Serial.println("Error going into static mode");
      error();
    }
    if(debug)
       Serial.println("Trying to allow multiple clients");
    if (wifi.enableMUX()) { //AT+MUX= 0 or 1, 0 meaning only one client 1 meaning multiple
        if(debug)
        Serial.println("Multiple activated");
    } else {
        if(debug)
        Serial.println("Multiple activation error");
        error();
    }
    
    if (wifi.startTCPServer(PORT)) { //Start a TCP server the AT command for this is AT+CIPSTART="TCP", PORT
        if(debug)
        Serial.println("Started TCP server on port: " + (String) PORT);
    } else {
        if(debug)
        Serial.println("Failed to start on port: " + (String) PORT);
        error();
    }
    
    if (wifi.setTCPServerTimeout(ServerTimeout)) { //Set a timeout for a packet so it doens't just sit there stupidly
        //if(debug)
        //Serial.println("Set timeout for: " + (String) ServerTimeout);
    } else {
        if(debug)
        Serial.println("Couldn't set up timeout");
        error();
    }
    if(debug)
    Serial.println("Setup complete READY for input\n\n\nThe Current garage State is " + (String) state);
    start();
}
 
void loop()
{
    uint8_t buffer[128] = {0}; //The buffer (Array) to insert from TCP packet
    uint8_t Client_ID;         //Init var for the Client ID
    String packet = "";        //The final outcome of the packet
    uint32_t lengths = wifi.recv(&Client_ID, buffer, sizeof(buffer), 100); //Return the length of the packet
    if (lengths > 0) { //If larger than zero than move along
        if(debug) 
        Serial.println("Status" + (String) wifi.getIPStatus().c_str() +"\nFrom Client #: " + Client_ID); //Send current connection status
        for(uint32_t i = 0; i < lengths; i++) {
            packet += ((char)buffer[i]); //Place the lengths into a buffer
        }
        recP(); //Indicate with RGBs
        if(debug)
         Serial.println("The Packet info was: " + (String) packet);
         int ifNum = atoi(packet.c_str()); //Look at method below
        if(ifNum == 5)
        {
          //Open packet
          if(debug)
          Serial.println("Got open/close packet\nStarted timer for " + (String)GarageOCT + " Ms");
                    digitalWrite(Solenoid, LOW);
          delay(1200);
          digitalWrite(Solenoid, HIGH);
          for(int a = 0; a < GarageOCT - 1200; a++) //Wait for garage to change state
            delay(1);
          if(debug)
          Serial.println("Finished timer the state has now changed");
          state = !state; //Make the state not the state
          int eep = 1; //Write open
          if(state)//If closed
          {
            eep = 2; //Write closed
          }
          EEPROM.update(states, eep); //Write to EEPROM the state just incase the device shuts off
        }
        else if(ifNum == 1)
        {
         //GetState packet
         if(debug)
         Serial.println("Got getstate packet, state is: " + (String) state);
         int sends = 2; //Opened
          if(state) //If closed
          {
            sends = 3; //Closed
          }
          sendPacket(sends, Client_ID); //Send back the current state 3 open 3 closed
          pass();
        }
        else if(ifNum == 2)
        {
          pass();
          if(debug)
          Serial.println("Device received correct packet");//Device got the packet
        }
        else if(ifNum == 7)
        {
          Serial.println("RESETING");
          delay(500);
          resetFunc();
        }
        else
        {
          if(debug)
          Serial.println("Got error packet"); //Don't read an odd number to me!!
          sendPacket(0, Client_ID); //The phone waits for a recall so send 0 which means error
          error();
        }
        if(debug)
        {
        Serial.print("Status:[");
        Serial.print(wifi.getIPStatus().c_str()); //Print current status
        Serial.println("]");
        }
    }
}

bool sendPacket(int num, uint8_t Client_ID){  //A Function where you can integers back
      sendP();
      bool pass = false; 
      uint8_t buffer[1] = {0}; //Stick in a buffer that is only the size of 1;
      if(num == 0)
      {
      buffer[0] = (char)'0';
      }
      else if(num == 1)
      {
        buffer[0] = (char)'1';   //Putting directly got sometimes buggy so i did some if statements
      }
      else if(num == 2)
      {
        buffer[0] = (char)'2';
      }
      else if(num == 3)
      {
        buffer[0] = (char)'3';
      }
      
        if(wifi.send(Client_ID, buffer, 1)) { //wifi.send(<Id>,<buffer>,<size>)
         if(debug)
         Serial.println("Sent Back okay");
         pass = true;
        } else {
            if(debug)
            Serial.println("There was an error sending the packet");
            pass = false;
        }
        
        if (wifi.releaseTCP(Client_ID)) { //If using Java this is needed because stream reader has to get closed to actually finish reading if you modified this for the computer (Python, etc...) remove it because it will show an error
            if(debug)
            Serial.println("Realeased Client " + (String) Client_ID + "Success");
            pass = true;
        } else {
            if(debug)
            Serial.print("There was an Error trying to release Client " + Client_ID);
            pass = false;
        }
        stopP();
        return pass;
}

//Below are all the RGBs for "light" debugging
void error() {
  if(RGBs)
  {
  digitalWrite(RGBlue, HIGH);
  digitalWrite(RedGB, LOW);
  delay(900);
  digitalWrite(RedGB, HIGH);
  digitalWrite(RGBlue, LOW);
  }
}

void pass() {
  if(RGBs)
  {
  digitalWrite(RGBlue, HIGH);
  digitalWrite(RGreenB, LOW);
  delay(900);
  digitalWrite(RGreenB, HIGH);
  digitalWrite(RGBlue, LOW);
  }
}

void sendP() {
  if(RGBs)
  {
  digitalWrite(RedGB, LOW);
  }
}

void stopP() {
  if(RGBs)
  {
  digitalWrite(RedGB, HIGH);
  }
}

void recP() {
  if(RGBs)
  {
  digitalWrite(RGreenB, LOW);
  delay(500);
  digitalWrite(RGreenB, HIGH);
  }
}

void start() {
  if(RGBs)
  {
  digitalWrite(RGBlue, LOW);
  }
}

void off() {
  digitalWrite(RedGB, HIGH);
  digitalWrite(RGreenB, HIGH);
  digitalWrite(RGBlue, HIGH);
}
Java MainActivityXML
The activity for the app
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/get"
        android:id="@+id/sendbutton"
        android:layout_marginBottom="74dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textStyle="bold|italic" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/opened"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/sendbutton"
        android:layout_below="@+id/space"
        android:contentDescription="@string/desc" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/getstate"
        android:id="@+id/update"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <Space
        android:layout_width="20dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/space"
        android:focusableInTouchMode="true"
        android:layout_toLeftOf="@+id/settings"
        android:layout_toStartOf="@+id/settings" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/getting"
        android:id="@+id/infoss"
        android:layout_above="@+id/sendbutton"
        android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/set"
        android:id="@+id/settings"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>
Java Settings ActivityXML
Java XML file for settings activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.studyarduino.garagedoor.SettingsActivity"
    android:id="@+id/d">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/privateIp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="42dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="@string/priv" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/publicIp"
        android:layout_below="@+id/privateIp"
        android:layout_marginTop="42dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/privateIp"
        android:layout_alignEnd="@+id/privateIp"
        android:hint="@string/pub" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/garageoct"
        android:layout_marginTop="42dp"
        android:layout_below="@+id/publicIp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="@string/gar" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save"
        android:id="@+id/save"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/refresh"
        android:hint="@string/ref"
        android:layout_marginTop="42dp"
        android:layout_below="@+id/garageoct"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/need"
        android:id="@+id/needhelp"
        android:layout_below="@+id/Port"
        android:layout_alignRight="@+id/save"
        android:layout_alignEnd="@+id/save" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/contact"
        android:id="@+id/contactme"
        android:layout_below="@+id/needhelp"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Port"
        android:layout_below="@+id/refresh"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="42dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="@string/port" />

</RelativeLayout>
Java MainActivity (Java not XML)Java
The java code for the activity
package com.studyarduino.garagedoor;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Properties;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    public static Properties props;
    public static boolean pass = false;
    public static volatile Button button;
    public static volatile ImageView states;
    public static volatile TextView info;
    public static InetAddress Addr;
    public static int PORT = 8080, Refresh = 20, garageopenclosetime = 8000;
    public static String IP = "PUT PRIV IP", path, state = "", PubIP = "PUT PUB IP";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); //Return to previous state
        setContentView(R.layout.activity_main); //Set the view

        //Map the Gui to the backend
        states = (ImageView) findViewById(R.id.imageView);
        info = (TextView) findViewById(R.id.infoss);
        button = (Button) findViewById(R.id.sendbutton);
        Button SETTINGS = (Button) findViewById(R.id.settings);
        //End of Mapping

        //Almost the Same as the Settings Activity
        props = new Properties();
        directory();
        if(!(new File(path).exists()))
            try{
                String[] starting = {"", "", "", "", ""};
                write(starting);
            } catch (Exception ignored) {}

        String[] data = read();
        if(data[0].length() > 2 && data[0] != null)
            IP = data[0];

        if(data[1].length() > 2 && data[1] != null)
            PubIP = data[1];

        if(data[2].length() > 2 && data[2] != null)
            garageopenclosetime = Integer.parseInt(data[2]);

        if(data[3].length() > 1 && data[3] != null)
            Refresh = Integer.parseInt(data[3]);

        if(data[4].length() > 1 && data[4] != null)
            PORT = Integer.parseInt(data[4]);

        //End of Properties


        Log.d("Properties", "The path to the Properties is " + path); //Log the path
        Log.d("Properties", "The values are IP: " + IP + " PORT: " + PORT + " GarageOCT: " + garageopenclosetime + " Refresh: " + Refresh); //Log the Values


        (new connect()).start(); //Get the address, if Local or public
        try {
            new getState().execute(""); //Get the current state
            Thread.sleep(500);
        } catch (NullPointerException ignored) {
            new getState().execute(""); //If there was a problem retry
        } catch (InterruptedException ignored) {}

        //Set on click lisenter for the MAIN button
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new send().execute("");
            }
        });

        SETTINGS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(i);
            }
        });

        Button updatestate = (Button) findViewById(R.id.update); //Update state manually
        //Set on click listener for the update button
        updatestate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new getState().execute("");
            }
        });
    }

    public void ButtonText(Button but, String text) {
        but.setText(text);
    }

    public void toast(CharSequence text) {
        Context context = getApplicationContext();
        int dur = Toast.LENGTH_SHORT;

        Toast.makeText(context, text, dur).show();
    }

    public boolean WifiState() {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return mWifi.isConnected();
    }

    public boolean DataState() {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return (mobileInfo.getState() == NetworkInfo.State.CONNECTED);
    }

    private class getState extends AsyncTask<String, CharSequence, String> {

        private String rec;

        protected String doInBackground(String... param) {
            try {
                publishProgress();
                Socket sock = new Socket(Addr, PORT);
                DataOutputStream out = new DataOutputStream(sock.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                out.writeBytes("1");
                rec = in.readLine();
                if (rec.equals("2")) {
                    rec = "Open";
                } else if (rec.equals("3")) {
                    rec = "Close";
                }
                pass = true;
                state = rec;
                sock.close();
            } catch (IOException e) {
                pass = false;
            } catch (NullPointerException ignored) {
            }
            return rec;
        }

        protected void onProgressUpdate(CharSequence... progress) {
            info.setText("Getting state");
        }

        protected void onPostExecute(String result) {
            try {
                if (result.equals("Open")) {
                    states.setImageResource(R.drawable.closed);
                    info.setText("Got Closed");
                } else if (result.equals("Close")) {
                    states.setImageResource(R.drawable.opened);
                    info.setText("Got Opened");
                }
                ButtonText(button, result);
                toast("Succesful connection");
            } catch (NullPointerException ee)
            {
                toast("There was an Error");
                info.setText("There was an Error");
            }
        }
    }

    private class connect extends Thread
    {
        public void run()
        {
            try {
                if(WifiState())
                {
                    Log.d("Connection", "Connecting with Wifi");
                    Addr = InetAddress.getByName(IP);
                }
                else if(DataState())
                {
                    Log.d("Connection", "Connecting with Data");
                    java.net.InetAddress[] x=  java.net.InetAddress.getAllByName(PubIP) ;
                    Addr = InetAddress.getByName(x[0].getHostAddress());
                    Log.d("Connection", "Host address " + Addr.getHostAddress());
                }
                else
                {
                    toast("No Internet Connection");
                    Log.d("Connection", "There is no Connection");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class send extends AsyncTask<String, Integer, String> {

        protected String doInBackground(String... param) {
            try {
                Socket sock = new Socket(Addr, PORT);
                DataOutputStream out = new DataOutputStream(sock.getOutputStream());
                out.writeBytes("5");
                publishProgress();
                sock.close();
                Thread.sleep(garageopenclosetime + 1000);
                (new getState()).execute("");
            } catch (Exception e) {
                toast("There was an error sending new state");
            }
            return "None";
        }

        protected void onProgressUpdate(Integer... progress) {
            if (state.equals("Close")) {
                ButtonText(button, "Closing....");
                states.setImageResource(R.drawable.closing);
                info.setText("Sending Close");
            } else {
                ButtonText(button, "Opening....");
                states.setImageResource(R.drawable.opening);
                info.setText("Sending Open");
            }
        }

        protected void onPostExecute(String result) {
        }
    }

    private class reset extends AsyncTask<String, Integer, String> {

        protected String doInBackground(String... param) {
            try {
                Socket sock = new Socket(MainActivity.Addr, MainActivity.PORT);
                DataOutputStream out = new DataOutputStream(sock.getOutputStream());
                out.writeBytes("7");
                publishProgress();
                sock.close();
            } catch (Exception ignored) {
            }
            return "None";
        }

        protected void onProgressUpdate(Integer... progress) {
            MainActivity.info.setText("RESETTING DEVICE please restart app");
        }

        protected void onPostExecute(String result) {
        }
    }

    public static void directory()
    {
        if(Build.VERSION.SDK_INT >= 19)
        {
            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() + "/GarageDoor.properties";
        }
        else
        {
            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/GarageDoor.properties";
        }
    }

    public static String[] read()
    {
        String[] data = {"0", "0", "0", "0", "0"};
        try {
            //load a properties file
            FileInputStream file = new FileInputStream(path);
            props.load(file);

            //get the property value and print it out
            data[0] = props.getProperty("priv", IP);
            data[1] = props.getProperty("pub", PubIP);
            data[2] = props.getProperty("gar", String.valueOf(garageopenclosetime));
            data[3] = props.getProperty("ref", String.valueOf(Refresh));
            data[4] = props.getProperty("port", String.valueOf(PORT));
            file.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return data;
    }

    public static void write(String[] data)
    {
        try {
            //set the properties value
            props.setProperty("priv", data[0]);
            props.setProperty("pub", data[1]);
            props.setProperty("gar", data[2]);
            props.setProperty("ref", data[3]);
            props.setProperty("port", data[4]);

            //save properties to project root folder
            FileOutputStream file = new FileOutputStream(path);
            props.store(file, null);
            file.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Java SettingsActivity (Java not XML)Java
Use this to control the Settings activity
package com.studyarduino.garagedoor;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;


public class SettingsActivity extends Activity {

    private static EditText IP, PUBIP, GARAGEOCT, REFRESH, PORT; //These have to be declared here
    private static Properties props; //Access the Properties file (Init)
    private static String path; //The path to the Properties

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState); //Once created get the last state
        setContentView(R.layout.activity_settings); //Set the layout file to use

        //Mapping of the Buttons
        IP = (EditText) findViewById(R.id.privateIp);
        PUBIP = (EditText) findViewById(R.id.publicIp);
        GARAGEOCT = (EditText) findViewById(R.id.garageoct);
        REFRESH = (EditText) findViewById(R.id.refresh);
        PORT = (EditText) findViewById(R.id.Port);

        Button SAVE = (Button) findViewById(R.id.save); //This can be local
        //End of mapping

        //Reading/Writing to the Properties file
        props = new Properties(); //Init the Properties class

        directory(); //Get directory according to the API

        if(!(new File(path).exists())) //If no file exist create a blank one
            try{
                String[] starting = {"", "", "", ""};
                write(starting);
            } catch (Exception ignored) {}

        Log.d("Properties", "The path to the Properties is " + path); //Log the path

        //Show current values in the file currently and display them
        try {
            String[] makevis = read();
            Log.d("Values", Arrays.toString(makevis));
            IP.setText(makevis[0]);
            PUBIP.setText(makevis[1]);
            GARAGEOCT.setText(makevis[2]);
            REFRESH.setText(makevis[3]);
            PORT.setText(makevis[4]);
        } catch (Exception ignored) {}
        //End File to Gui

        //Save button pressed write to the Properties file
        SAVE.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            String[] data = {IP.getText().toString(), PUBIP.getText().toString(), GARAGEOCT.getText().toString(), REFRESH.getText().toString(), PORT.getText().toString()};

                try {
                    write(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void directory()
    {
        if(Build.VERSION.SDK_INT >= 19) //The documents folder can only be accessed by api 19 and above
        {
            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() + "/GarageDoor.properties";
        }
        else
        {
            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/GarageDoor.properties";
        }
    }

    //Return a String array from each line from the properties file
    public static String[] read()
    {
        String[] data = {"0", "0", "0", "0", "0"};
        try {
            //load a properties file
            FileInputStream file = new FileInputStream(path);
            props.load(file);

            //get the property value and print it out
            data[0] = props.getProperty("priv");
            data[1] = props.getProperty("pub");
            data[2] = props.getProperty("gar");
            data[3] = props.getProperty("ref");
            data[4] = props.getProperty("port");

            file.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return data;
    }
    //End of loading

    //Write lines to the properties file with a String array
    public static void write(String[] data)
    {
        try {
            //set the properties value
            props.setProperty("priv", data[0]);
            props.setProperty("pub", data[1]);
            props.setProperty("gar", data[2]);
            props.setProperty("ref", data[3]);
            props.setProperty("port", data[4]);

            //save properties to project root folder
            FileOutputStream file = new FileOutputStream(path);
            props.store(file, null);
            file.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    //End write lines

}
Java Manifest XMLXML
The required permissions for the App
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.studyarduino.garagedoor" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" >
        </activity>
    </application>

</manifest>
Drawable imagesPlain text
The media fire link to the images to put in the drawable folder
Here is the Link: https://www.mediafire.com/folder/c783gk5vjyjtb//Pictures

Schematics

Schematic
Here is the Wiring for the Garage Door
Garage%20door%20schematic bb

Comments

Similar projects you might like

IoT Garage Door Monitor/Opener with Finger Print Scanner

Project tutorial by PlastiBots.com

  • 14,784 views
  • 8 comments
  • 41 respects

OH HAI! on Windows 10 IoT Core

Project in progress by BuddyC

  • 13,156 views
  • 3 comments
  • 48 respects

$20 Zigbee Door Chime

Project tutorial by BuddyC

  • 25,152 views
  • 10 comments
  • 51 respects

Android App-Based Home Automation System Using IOT

Project tutorial by Team Autoshack

  • 24,826 views
  • 17 comments
  • 75 respects

Easy IOT Water Leakage Indicator

Project tutorial by Patel Darshil

  • 9,186 views
  • 2 comments
  • 13 respects
Add projectSign up / Login