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

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
Tweet

More Decks by Hugo Visser

Other Decks in Technology

Transcript

  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.