Stories on maker education and innovation 

Home making Remotely Control IoT devices using NodeJs, Firebase, and Johnny5
formats

Remotely Control IoT devices using NodeJs, Firebase, and Johnny5

Hello, makers!  In our blog post today, I wanted to share a simple way to remotely control IoT devices using NodeJs and Google Firebase.  Let’s say you’re trying to remotely control a small lego crane like this.  You’ll notice there are two servo motors connected to an Arduino.  You can learn more about how you can build this in our post on Arduino and Lego motor control.   For the scope of this blog post, let’s say we wanted to remotely control the servo motor at the bottom from any place in the world.  How would we do that?

Lego Crane

Firstly, check out Johnny 5,  a very nice NodeJs library for controlling IoT devices like Arduino’s, Raspberry Pi, and more.  I really appreciate the clarity of their documentation and API.  You can do a lot with a small amount of javascript.

I started to wonder if we could connect Johnny 5 to the real-time database of Firebase.   What’s a real-time database?  In a traditional relational database like MySQL, you need to declare database tables and structures.  You can make a database table to store a list of persons and their addresses (see sample code here).   After doing that, you can insert data into that table.  In this traditional database world, you can’t listen for inserts into a database table and easily write code to reach to that event.

The Firebase real-time database organizes information in a tree structure.  You can store information in that tree any way that you want.  Other users who have access to the database can listen for data changes at various locations in the tree and write code to react to that event.  Check out the following video to learn how the Firebase Real-time database works. Especially listen to how the value change event works.

This link review the details of getting started with FireBase on the web or NodeJS: https://firebase.google.com/docs/database/web/start

So, let’s explore the code for moving a single servo motor using Johnny 5 and Firebase.

At the top of the JavaScript file, we import johnny-five and firebase-admin. We start our firebase
database session by calling “initializeApp.”

On line 9, we create a “Board” object. I have my Arduino connected to my computer by a serial cable. Johnny5 handles this situation by default.

Once the board enters “ready” state, we create an instance of a servo motor connected to pin 10.

On line 14 and 15, we connect to a storage location called ‘servo_angle.’ Using the servo_angle “on value” event, we listen for changes to this location and
set the angle of the servo. And that’s it!

To write values into “servo_angle”, check out the following code.

In this script, we connect to the Firebase database in the same way. On line 10, we accept an angle from command line arguments. On line 16, we write that angle to ‘servo_angle’.

It’s a very simple pattern for making internet connected robots or home automation.

We love to hear from our readers. Leave a comment below if you get other ideas for internet connected robots, toys or devices.

Related Blog Posts

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Inspired To Educate
credit