Introduction: Android Things A.I. Candy Dispenser
A Candy Dispenser running Android Things, that exchange photos for candies. It uses computer vision to classify the image.
Step 1: The Project
The Android Things A.I Candy Dispenser it's a demonstration of how to create a “smart” candy machine. The device is a game that ask the user for a specific thing like a Bird, Dog ou Cat and the user should show a photo of that thing in the predefined time to win candies.
This project uses a button to interact with the user, obtains images via a camera peripheral and a modified electric candy dispenser being controlled by a GPIO with a transistor. When the user takes a picture, it processes the image data using Google’s Cloud Vision API, which returns annotations and metadata of the image. This info is used by the device to see if it matches what was requested. When we have a match, the motor of the candy machine is activated and give the user the prize.
All users interface is presented in a Serial i2C 20x4 Display.
The device (Raspberry Pi 3B) is running Android Things dev preview. It has a camera connected to take pictures, a 20x4 Serial i2c Display to show the "little" game state, a arcade button to interact with the game and a simple NPN transistor to activate the candy machine DC motor.
The core of the project was deeply inspired by the Doorbell: https://github.com/androidthings/doorbell. This code sample was made in Android Studio, using Kotlin Language and Google Vision API to classify the image. Please also note that this has only been tested on a Raspberry Pi 3, but should work on other compatible boards too.
Step 2: Getting Started
You will need to setup your environment to run and program the Raspberry Pi using Android Things. All the content to get started with Android Things can be found at the official website. You should follow this to run this project:
After that you should have a Raspberry Pi running Android Things and with the proper network setup to program it.
Step 3: Pre-requisites
After the previous step, you should have the most of the next components:
- Android Things compatible board
- Android Things compatible camera (for example, the Raspberry Pi 3 camera module)
- Android Studio 2.2+"Google Repository" from the Android SDK Manager
- Google Cloud project with Cloud Vision API
The following individual components:
- 1 push button
- 1 resistor
- 1 electric candy machine
- 1 NPN transistor
- 1 diode
- 1 Serial i2c 20x4 Display
- jumper wires
- 1 breadboard
Step 4: Setup and Build
The code can be found on the Github repository.
You will need to setup a API Key for the Google Vision API to be able to use it. To setup, follow these steps below.
- Add a valid Google Cloud Vision API key in the constant ImageClassifierUtil.CLOUD_VISION_API_KEY
- Create a Google Cloud Platform (GCP) project on GCP Console
- Enable Cloud Vision API under Library
- Add an API key under Credentials
- Copy and paste the Cloud Vision API key to the constant in ImageClassifierUtil.kt
Step 5: Running
To run the app module on an Android Things board:
- Connect a push button to your device's GPIO pin according to the previous schematics
- Deploy and run the app module
- Reboot the Android Things board in order to grant the camera permission (this is a known issue with Developer Preview )
- Press the button to start the game and see what it asks.
- Search for a photo and press the button to take a picture of it.
- Wait for the results, if it succeeds it will ask you to press the button to release the candies.
Runner Up in the
Internet of Things Contest 2017