Using Firebase as a real time hub for IoT devices

Using Firebase as a real time hub for IoT devices

Presentation for the IoT Tech Day 14th of April

Hugo Visser

April 14, 2016

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

36 slides
  1. Inbox / outbox Add extra capabilities to control surface, keep

    status simple. Prevent concurrent updates Separate security & clean up
  2. Inbox Firebase ref = new Firebase(CONTROL_ROOT).
 child("lifx/lights").
 child(String.valueOf(device.getTarget()));
 
 ref.child("power").setValue(new

    PowerControl());
 ref.child("color").setValue(new HSBKControl());
 
 String CONTROL_ROOT = "https://lightspeed.firebaseio.com/control/devices";
  3. Listen for changes ref.child("power").addValueEventListener(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot

    dataSnapshot) {
 if (dataSnapshot.exists()) {
 PowerControl power = dataSnapshot.getValue(PowerControl.class);
 updateLifxPowerState(device, power);
 }
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 }); Will be called for current value!
  4. Listen for changes (2) ref.child("color").addValueEventListener(new ValueEventListener() {
 @Override
 public void

    onDataChange(DataSnapshot dataSnapshot) {
 if (dataSnapshot.exists()) {
 HSBKControl color = dataSnapshot.getValue(HSBKControl.class);
 updateLifxColor(device, color);
 }
 }
 
 @Override
 public void onCancelled(FirebaseError firebaseError) {
 
 }
 });
  5. Security rules {
 "rules": {
 ".read": false,
 ".write": false,
 "users":

    {
 "$uid": {
 ".read": "$uid == auth.uid",
 ".write": "$uid == auth.uid"
 }
 }
 }
 } Wildcards
  6. Write types type PercentageFloat extends Number {
 validate() { this

    >= 0 && this <= 1}
 }
 
 type Hue extends Number {
 validate() { this >= 0 && this <= 360 }
 }

  7. Write types type LxColor {
 h : Hue,
 s :

    PercentageFloat,
 b : PercentageFloat,
 k : Number,
 duration: Number
 } type LifxControl {
 color : LxColor,
 power : LxPower
 }
  8. Write rules path /control/devices/lifx/lights/{light} is LifxControl {
 create() { false

    }
 update() { isAuthenticated() }
 delete() { false }
 }
  9. Recap Storing and retrieving IoT data in Firebase is easy,

    and in real time! Inbox / outbox style “serverless” Security is hard, easier with Bolt.

More Decks by Hugo Visser

Other Decks in Technology