Spivey
Published © MIT

How to Build Your Own Security System Using Android Things

In this tutorial, we will send an event to Wia using Android Things.

IntermediateFull instructions provided1 hour2,419
How to Build Your Own Security System Using Android Things

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Wia
Wia
Android Things
Google Android Things

Story

Read more

Code

MainActivity.java

Java
import android.app.Activity;
import android.os.Bundle;

import android.util.Log;

import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManager;


import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.wia.Wia;

import io.wia.WiaEvent;


public class MainActivity extends Activity implements MotionsSensor.Listener {

    private static final String GPIO_PIN = "BCM18";
    private PirMotionSensor motionSensor;
    private static final String TAG = "TEST";
    private Gpio mGpio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PeripheralManager pms = PeripheralManager.getInstance();
        try {
            mGpio = pms.openGpio(GPIO_PIN);
            motionSensor = new PirMotionSensor(mGpio, this);
            motionSensor.startup();
        } catch (Exception ex) {
            Log.d(TAG, "failing", ex);
        }
        Log.d(TAG, "IN ON CREATE FUNCTION.");

        Wia.initialize(new Wia.Configuration.Builder(this.getApplicationContext())
                .server("https://api.wia.io/v1")
                .build()
        );
        //replace this with your own device secret key 
        Wia.accessToken("your-device-secret-key");

    }

    @Override
    public void onMovement() {
        Log.d(TAG, "in onMovement");
        Log.d(TAG, "MOVEMENT DETECTED");
        Observable<WiaEvent> result = Wia.createEvent("motion");
        result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    Log.d(TAG, "In onMovement function");
                }, error -> {
                    Log.d(TAG, "ERROR IN MOVEMENT EVENT");
                });
    }

PirMotionSensor

Java
import android.util.Log;

import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.GpioCallback;

import java.io.IOException;

public class PirMotionSensor implements MotionsSensor {
    private final Gpio bus;
    private final MotionsSensor.Listener listener;

    PirMotionSensor(Gpio bus, Listener listener) {
        this.bus = bus;
        this.listener = listener;
    }

    @Override
    public void startup() {
        try {
            bus.setDirection(Gpio.DIRECTION_IN);
            bus.setActiveType(Gpio.ACTIVE_HIGH);
            bus.setEdgeTriggerType(Gpio.EDGE_FALLING);
        } catch (IOException e) {
            throw new IllegalStateException("Sensor can't start", e);
        }
        try {
            bus.registerGpioCallback(callback);
        } catch (IOException e) {
            throw new IllegalStateException("Sensor can't register callback", e);
        }
    }

    private final GpioCallback callback = new GpioCallback() {
        @Override
        public boolean onGpioEdge(Gpio gpio) {
            listener.onMovement();
            return true; // True to continue listening
        }
    };

    @Override
    public void shutdown() {
        bus.unregisterGpioCallback(callback);
        try {
            bus.close();
        } catch (IOException e) {
            Log.e("TEST", "Failed to shut down. You might get errors next time you try to start.", e);
        }
    }

}

Credits

Spivey

Spivey

82 projects • 45 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to be COO of Wia the best IoT startup

Comments