Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Things - Beginners Introduction

Android Things - Beginners Introduction

Slide from the DevFest Sydney and GDG Melbourne talks.

Andrew Kelly

December 16, 2017
Tweet

More Decks by Andrew Kelly

Other Decks in Technology

Transcript

  1. What is Android Things? • Build connected devices for a

    wide variety of consumer, retail, and industrial applications • Leverage existing Android development tools, APIs, resources, and a thriving developer community. • Develop with new Android framework APIs that provide low level I/O and libraries for common components like temperature sensors, display controllers, and more.
  2. Android Things Console https://partner.android.com/things/console • Create a new Android Things

    image ▪ Choose your hardware platform - Raspberry Pi ▪ Choose your Android Things version - 0.5.1 • Install the image on your hardware ▪ Similar to installing a linux distro • Connect to your hardware over ADB to install your Android Things app.
  3. Building a Smart Garden • A temperature and moisture sensor

    that uploads readings to Firebase. • Firebase functions process data and send notifications based on simple rules e.g. Soil too dry, water your plants!
  4. Fritzing • Fritzing is an open-source hardware initiative that makes

    electronics accessible as a creative material for anyone. • Software tool for creating circuit diagrams
  5. Data Sheets • Each micro component will have an associated

    data sheet. • Data sheets tell you things like ▪ Voltages ▪ PIN outs ▪ Input cycles ▪ Algorithms for converting voltages to discrete values.
  6. Raspberry Pi 3 data sheet • 1.2Ghz Quad-Core ARM processor

    • 1GB memory • 802.11 b/g/n wireless • Ethernet, HDMI, USB • Raspberry Pi and NXP Pico boards are all digital on their GPIO (unlike Arduino), so you’ll need an Analog to Digital Converter (ADC) to convert the voltages to inputs.
  7. MCP3008 data sheet • 10bit Analog to Digital converter •

    2^10 = 1024 range of output • 8 Channels of input
  8. XC-4494 data sheet • VCC = 5V DC • GND

    = Ground • OUT = Analog output • The module outputs an analog voltage that varies directly with temperature
  9. XC-4604 data sheet • VCC = 3.3v or 5V DC

    • GND = Ground • OUT = Analog output • The module outputs an analog voltage that varies directly with moisture
  10. Build a Smart Garden - Software • Hardware complete, now

    for the software • Software ▪ Drivers ▪ Firebase ▪ Android App
  11. Drivers • Drivers are needed to communicate with components. •

    Android Things provides common drivers in the Peripheral Driver Library ▪ https://github.com/androidthings/contrib-drivers ▪ https://github.com/androidthings/drivers-samples • Drivers handle writing inputs and reading outputs • Devices like the MCP3008 ADC chip can have complex logic to perform basic functions. Details are usually in the data sheet.
  12. private void initChannelSelect(int channel) throws IOException { int commandOut =

    channel; commandOut |= 0x18; // start bit + single-ended bit commandOut <<= 0x3; // we only need to send 5 bits for (int i = 0; i < 5; i++) { if ((commandOut & 0x80) != 0x0) { mDinPin.setValue(true); } else { mDinPin.setValue(false); } commandOut <<= 0x1; toggleClock(); } }
  13. Firebase • Real time database for storing readings from sensors

    • Firebase Function to be triggered when a new reading is received • Firebase Push Notification, sent to phone when moisture threshold reached
  14. const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.monitorWater

    = functions.database.ref('/sensor_readings/{pushId}/rawWater') .onCreate(event => { const original = event.data.val(); console.log('rawWater', event.params.pushId, original); if (original > 256) { return console.log('Plant has enough water'); } console.log('Plant is thirsty!'); return sendNotification(); });
  15. sendNotification() { // Get the list of device notification tokens.

    const tokensSnapshot = admin.database().ref('/users/0/notificationTokens').once('value'); // Notification details. const payload = { notification: { title: 'Smart Garden', body: 'Your plants need watering' } }; // Listing all tokens. const tokens = Object.keys(tokensSnapshot.val()); // Send notifications to all tokens. return admin.messaging().sendToDevice(tokens, payload); }
  16. Android application • Start Activity on device boot • Connect

    to MCP3008 • Poll every 30 seconds for readings • Send readings to Firebase
  17. <application> <uses-library android:name="com.google.android.things" /> <activity android:name="com.swizel.androidthings.analog_sensor.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.IOT_LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application>
  18. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDatabase = FirebaseDatabase.getInstance();

    mMCP3008 = new MCP3008("BCM12", "BCM21", "BCM16", "BCM20"); // Raspberry Pi // mMCP3008 = new MCP3008("GPIO_33", "GPIO_10", "GPIO_32", "GPIO_35"); // NXP Nico mMCP3008.register(); mHandler = new Handler(); mHandler.post(mReadAdcRunnable); }
  19. private Runnable mReadAdcRunnable = new Runnable() { @Override public void

    run() { DatabaseReference log = mDatabase.getReference("sensor_readings").push(); int rawTemp = mMCP3008.readAdc(0x0); int rawWater = mMCP3008.readAdc(0x7); log.child("timestamp").setValue(ServerValue.TIMESTAMP); log.child("rawTemp").setValue(rawTemp); log.child("rawWater").setValue(rawWater); log.child("water").setValue(water(rawWater)); log.child("temp").setValue(fahrenheit(rawTemp)); mHandler.postDelayed(this, 30000L); } };