Monday, October 12, 2015

SMART HOME SYSTEM (ANDROID PROGRAM)

This is the program source code for my application which can control arduino's bluetooth module. It was the java code for this SYSTEM that I have build for myself. It still be okay for your source in making the application like that. This classes code just for communication within bluetooth modules.

The Code :

package com.led.led;

import android.app.Application;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.AsyncTask;
import android.widget.ToggleButton;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Random;
import java.util.UUID;
import java.util.Timer;
import java.util.TimerTask;


public class ledControl extends ActionBarActivity {





    Button btnDis, eStatistic;
    ToggleButton lamp1, lamp2, lamp3, lamp4, fan1, fan2, fan3, fan4, leftgate, rightgate, bothgate, aircond1, aircond2, aircond3, door1, door2;
    ToggleButton hbtoggle;
    Switch Alarmswitch;
    CheckBox Autolamp;
    String address = null;
    SeekBar brightness;
    TextView lumn, showBPM, displaywakeup;
    private ProgressDialog progress;
    BluetoothAdapter myBluetooth = null;
    BluetoothSocket btSocket = null;
    private BufferedReader BufferedReader = null;
    private boolean isBtConnected = false;
    //SPP UUID. Look for it    static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public static String EXTRA_USAGE = "electric_usage";
    String aString = null;
    InputStream aStream = null;
    InputStreamReader aReader = null;
    private BufferedReader mBufferedReader = null;
    private static final Random RANDOM = new Random();
    private LineGraphSeries<DataPoint> series;
    private int lastX = 4, rhrcount=0;
    OutputStream mmOutputStream;
    InputStream mmInputStream;
    Thread workerThread;
    byte[] readBuffer;
    int readBufferPosition;
    boolean latch = false, graphing=true;
    int counter, intdata=0, bitpm=0;
    int[] intusage = {252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252};
    int bpm = 0, xaxis=0, currentusage = 0, usage[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
    volatile boolean stopWorker;

    @Override    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Intent newint = getIntent();
        address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS); //receive the address of the bluetooth device
        //view of the ledControl        setContentView(R.layout.activity_led_control);



        //call the widgtes        btnDis = (Button)findViewById(R.id.button2);
        eStatistic = (Button)findViewById(R.id.Statistic);
        brightness = (SeekBar)findViewById(R.id.seekBar);
        lumn = (TextView)findViewById(R.id.lumn);
        lamp1 = (ToggleButton)findViewById(R.id.LamptoggleButton);
        lamp2 = (ToggleButton)findViewById(R.id.FantoggleButton);
        lamp3 = (ToggleButton)findViewById(R.id.toggleButton);
        lamp4 = (ToggleButton)findViewById(R.id.WindowtoggleButton);
        Autolamp = (CheckBox)findViewById(R.id.checkBoxAuto);
        fan1 = (ToggleButton)findViewById(R.id.toggleButton2);
        fan2 = (ToggleButton)findViewById(R.id.toggleButton3);
        fan3 = (ToggleButton)findViewById(R.id.toggleButton4);
        fan4 = (ToggleButton)findViewById(R.id.toggleButton5);
        hbtoggle = (ToggleButton)findViewById(R.id.HBtoggle);
        aircond1 = (ToggleButton)findViewById(R.id.toggleButton10);
        aircond2 = (ToggleButton)findViewById(R.id.toggleButton14);
        aircond3 = (ToggleButton)findViewById(R.id.toggleButton6);
        leftgate = (ToggleButton)findViewById(R.id.leftgate);
        rightgate = (ToggleButton)findViewById(R.id.rightgate);
        bothgate = (ToggleButton)findViewById(R.id.bothgate);
        Alarmswitch = (Switch)findViewById(R.id.Alarmswitch);
        showBPM = (TextView)findViewById(R.id.BPMShow);
        displaywakeup = (TextView)findViewById(R.id.displaywakeup);
        GraphView graph = (GraphView) findViewById(R.id.graph);
        series = new LineGraphSeries<DataPoint>();
        new ConnectBT().execute(); //Call the class to connect
        //commands to be sent to bluetooth        graph.addSeries(series);
        Viewport viewport = graph.getViewport();
        viewport.setYAxisBoundsManual(true);
        viewport.setMinY(0);
        viewport.setMaxY(10);
        viewport.setScrollable(true);



        Alarmswitch.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (Alarmswitch.isChecked())
                    onAlarm();

                else                    offAlarm();

            }
        });




        lamp1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (lamp1.getText().toString().equals("ON"))
                {
                    onlamp1();
                    setauto();
                    displaywakeup.setText("Wakeup");
                    usage[1]=1;

                }
                else{
                    offlamp1();
                usage[1]=0;}

            }
        });








        lamp2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (lamp2.getText().toString().equals("ON"))
                    onlamp2();
                else                    offlamp2();

            }
        });

        lamp3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(lamp3.getText().toString().equals("ON"))
                    onlamp3();
                else                    offlamp3();

            }
        });

        ......
 
 void beginListenForData()
    {
        final Handler handler = new Handler();
        final byte delimiter = 45; //This is the ASCII code for a newline character
        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable()
        {
            public void run()
            {
                while(!Thread.currentThread().isInterrupted() && !stopWorker)
                {
                    try                    {
                        int bytesAvailable = mmInputStream.available();
                        if(bytesAvailable > 0)
                        {
                            byte[] packetBytes = new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);
                            for(int i=0;i<bytesAvailable;i++)
                            {
                                byte b = packetBytes[i];
                                if(b == delimiter)
                                {
                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;
                                    int intdata = Integer.parseInt(data);
                                    if(intdata<=10)
                                    bpm=intdata;
                                    else                                    {
                                        bitpm = intdata;
                                        rhrcount++;



                                    }
                                    handler.post(new Runnable()
                                    {
                                        public void run()
                                        {
                                            int intdata = Integer.parseInt(data);

                                            if(rhrcount>100)
                                                displaywakeup.setText("Wake UP");
                                            if(intdata>10)
                                                showBPM.setText(data);
                                        }
                                    });
                                    latch = false;
                                }
                                else if(latch==true)
                                {
                                    readBuffer[readBufferPosition++] = b;
                                }
                                else if(b == 35)
                                {
                                     latch = true;
                                }
                                else                                {

                                }
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        stopWorker = true;
                    }
                }
            }
        });

        workerThread.start();
    }


}
 
...... 
 
Please View the full Code on my Google Drive (Click Skip Ads After Clicking link below):
View on Google Drive

You can GET the CHEAPEST Arduino here:
1. Amazon link 1 (Arduino Nano)
2. Amazon link 2 (Arduino Nano)
3. Amazon link 3 (Arduino Nano)
 

No comments:

Post a Comment