Marcos Placona
Published © Apache-2.0

High Five Machine with Android Things and Firebase

Let’s build a high five machine powered by Android Things and Firebase to give us instant gratification when we get a new Twitter follower.

IntermediateFull instructions provided1 hour1,903
High Five Machine with Android Things and Firebase

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
Running Android Things
×1
Servos (Tower Pro MG996R)
Micro Servo Motors 9g look nicer
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Android Things
Google Android Things

Story

Read more

Schematics

Servo connected to Pi

Code

Code snippet #1

Plain text
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version'
    classpath 'com.google.gms:google-services:3.0.0'
}

Code snippet #2

Plain text
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version'
    classpath 'com.google.gms:google-services:3.0.0'
}

Code snippet #13

Plain text
fun highFive(){
    mServo?.angle = mServo?.maximumAngle!! //up
    Thread.sleep(3000)
    mServo?.angle = mServo?.minimumAngle!! //down
}

Code snippet #14

Plain text
fun highFive(){
    mServo?.angle = mServo?.maximumAngle!! //up
    Thread.sleep(3000)
    mServo?.angle = mServo?.minimumAngle!! //down
}

Code snippet #15

Plain text
mDatabaseRef?.addChildEventListener(object : ChildEventListener{
    override fun onCancelled(p0: DatabaseError?) {
        Log.d(TAG, "Cancelled")
    }

    override fun onChildMoved(p0: DataSnapshot?, p1: String?) {
        Log.d(TAG, "Child Moved")
    }

    override fun onChildChanged(p0: DataSnapshot?, p1: String?) {
        Log.d(TAG, "Child Changed")
    }

    override fun onChildAdded(dataSnapshot: DataSnapshot?, prevChildKey: String?) {
        Log.d(TAG, "Background Service" + dataSnapshot?.key)
        highFive()

        // Remove the item once user has been high-fived
        mDatabaseRef?.child(dataSnapshot?.key)?.removeValue()
    }

    override fun onChildRemoved(p0: DataSnapshot?) {
        Log.d(TAG, "Child Removed")
    }

})
}

Code snippet #16

Plain text
mDatabaseRef?.addChildEventListener(object : ChildEventListener{
    override fun onCancelled(p0: DatabaseError?) {
        Log.d(TAG, "Cancelled")
    }

    override fun onChildMoved(p0: DataSnapshot?, p1: String?) {
        Log.d(TAG, "Child Moved")
    }

    override fun onChildChanged(p0: DataSnapshot?, p1: String?) {
        Log.d(TAG, "Child Changed")
    }

    override fun onChildAdded(dataSnapshot: DataSnapshot?, prevChildKey: String?) {
        Log.d(TAG, "Background Service" + dataSnapshot?.key)
        highFive()

        // Remove the item once user has been high-fived
        mDatabaseRef?.child(dataSnapshot?.key)?.removeValue()
    }

    override fun onChildRemoved(p0: DataSnapshot?) {
        Log.d(TAG, "Child Removed")
    }

})
}

Code snippet #17

Plain text
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Code snippet #18

Plain text
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Code snippet #21

Plain text
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
exports.addMessage = functions.https.onRequest((req, res) => {
    const username = req.query.username;
    admin.database().ref('/new-follower').push({ username: username }).then(snapshot => {
        var message = `${username} created`
        res.status(201).send( message );
    });
});

Code snippet #22

Plain text
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
exports.addMessage = functions.https.onRequest((req, res) => {
    const username = req.query.username;
    admin.database().ref('/new-follower').push({ username: username }).then(snapshot => {
        var message = `${username} created`
        res.status(201).send( message );
    });
});

Github file

https://github.com/androidthings/contrib-drivers/tree/master/pwmservo

Github

https://github.com/mplacona/HighFiveMachine

Github

https://github.com/mplacona/HighFiveMachine-Function

Credits

Marcos Placona

Marcos Placona

0 projects • 3 followers

Comments