Sunday, September 4, 2016

Deploying a RoR App to Heroku

Deploying a Ruby on Rails application to Heroku is simple and straight forward. 

But, what is Heroku?  Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps.  This cloud platform offers tools to allow us to handle deployment, performance, monitoring, everything to do with infrastructure so we can focus on building our applications.

I am using Heroku as a learning laboratory to deploy to "production" my RoR progress Blog - Personal Site app.  So, here are some of the pointers that I need to keep in mind while deploying to Heroku.

Preparing My RoR for deployment

It is recommended to deploy to "production" early in the development process and as often as possible.  In this learning exercise I am using an "agile" development approach,  therefore, as soon as a sprint finishes a new feature, the production product is updated right away.

My RoR application requires a database.  In my development environment, my local Linux Ubuntu, the database is SQLite, which is very convenient, easy to use and it was deployed with Rails.  However, Heroku uses PostgreSQL database which means that if I deploy my app "as is" then Heroku issues an error on compilation.  To avoid this error I need to create a production group in the gem file and add to it a PostgreeSQL gem "pg" to allow Rails to talk to Postgre.


group : production do


   gem 'pg'


   gem 'rails_12factor'


end

The production group also needs 'rails_12factor' to allow static asset serving and loggin on Heroku.  Adding the production group to the gem file does not affect my local development using SQLite  because bundle install can be run with the flag '--without production', like this

$ bundle install --without production

Publish to Heroku

Publishing to the cloud from my local git repo I need to make sure that it is updated with all recent changes, therefore after changing the gem file I need to commit, then push from git to heroku.  Needless to say that the Heroku command line tool needs to be locally installed and that we need to login to Heroku.

To publish the master brach to Heroku run this command:

$ git push heroku master

Of course I could push any branch but at this time, the master branch is the one with all features.  If the push yields no errors then I am in luck!  In the case that there are errors, Heroku stores them in the logs file.

$ heroku logs

Fix the errors and continue.  In my case, my 'novice' error was that I neglected to create a production group with Postgre gem in it.  I still need the sqlite gem in the gem file to be able to use this SQLite during development.  Therefore, a development group was created containing the sqlite gem.  The reviewed development and production groups look like this:


group :development do


  gem 'sqlite3'


end




group :production do


  gem 'pg'


  gem 'rails_12factor'


end


At this time Heroku does not know anything about our Object Model and the database has not been created yet, therefore, if I run the app after pushing then my app does not run, but if I run it any way, Heroku issues an app error page and the reason for the failure is written to the log and I could view it with "heroku logs" .

To create my database I need to run the rake db migration command in the Heroku command line tool.  As a matter of fact, every time my Active Record model changes I need to run this command in the Heroku command line tool:

$ heroku run rake db:migrate

Now the app can be loaded in the browser using the command:

$ heroku open

There is no data however because that db has not been seeded, but this is a different story.  At this time everything works and I can move to other things...  mainly keep learning RoR!

Other Useful Heroku Commands

There are other commands that I needed to use and they are:

$ heroku login 
$ heroku create
$ heroku restart

The above command are self explanatory.

Stopping and Starting the App Running on Heroku

$ heroku ps:scale web=0

Setting "web=0" scales down the app to 0, and the result is the stopped app.

$ heroku ps:scale web=1

To start it up again scale the app to "web=1".

Wednesday, June 29, 2016

Nodebot Blink Modified

Strobing Led and Piezo Song

This is one of the exercises for the Port Coquitlam Node and Nodebots Meetup.  This challenge was posted in order to do a quick piezo demo. 



'use strict';

var five = require('johnny-five'),   
    board = new five.Board();
 
board.on('ready', function(){
    console.log('Board is ready');
 
    led.strobe(10000, function(){                    

      var led = new five.Led(13);
      var piezo = new five.Piezo(3);

        piezo.play({          
            song: "C D F D A - A A A A G G G G - - C D F D G - G G G G F F F F - -",
            beats: 1 / 4,
            tempo: 100
        });      
    });  
});
console.log('Waiting for board...');

Hey, there is a bug in your code!

You got it!  There is a bug.  Can you spot it?  After loading the firmatta to the microcontroller and running the above program we saw in the console something like this:




The board is telling us that "pin 3 is already in use".  Hum, it is only instantiated once...  or is it? Strobe is an interval operation and every time the callback function is executed we try to create a new piezo in the scope of that function using pin 3.  During the first strobe interval there is no error but the second time the strobe interval passes then we get the warning because pin 3 is already taken.

To fix the bug we need to take the line where the new piezo is declared and place it before led.strobe()

Fixed!

'use strict';

var five = require('johnny-five'),
board = new five.Board();

board.on('ready', function(){
console.log('Board is ready');

var led = new five.Led(13);
var piezo = new five.Piezo(3);

led.strobe(10000, function(){
piezo.play({
song: "C D F D A - A A A A G G G G - - C D F D G - G G G G F F F F - -",
beats: 1 / 4,
tempo: 100
});
});
});
console.log('Waiting for board...');



Sunday, June 5, 2016

NodeBots Day-1: Getting the Environment Ready - Ubuntu 16.04 LTS

Getting the robotic NodeBot environment ready on Ubuntu worked without a glitch.  To get the environment ready follow the same sequence described in the previous note:

  1. Install Node and NPM
  2. Install Johnny-Five
  3. Install Arduino software IDE
  4. Optionally install Visual Studio Code as a Code Editor
    1. Install Git previous to the installation of Visual Studio Code


I have an old Fujitsu laptop kicking around (i3, 2.26 GHz, Dual Core, with 4 GB of RAM) running Ubuntu 16.04 LTS.  Follow the instructions described in the following how to guide for installing Nodejs on Ubuntu.

We already know how to install Johnny-Five using NPM:

$ npm install johnny-five

To install the Arduino software IDE (Sketch) on ubuntu , the easiest way, is by issuing a command from terminal:

$ sudo apt-get update
$ sudo apt-get install arduino

The first command updates the list of packages and dependencies and the second command installs the arduino software.  You will need to provide the password of the super user.  Accepts the defaults and required dependencies are installed.  Once the installation process ends, search for Arduino and lock it on the launcher for convenience.

Start the sketch IDE and a permission dialog is loaded:



Click Add and provide the super user password again.  Connect the Arduino board to your computer through the provided USB cable and verify that the correct port is selected by going to Tools > Serial Port...  however, initially "Serial Port" was not enabled.  This was strange but after restarting the computer the Tools > Serial Port item was enabled and I selected the only port available to me '/dev/ttyACM0'.

Verify that the board under Tools > Boards is Arduino Uno

Now you can load the firmata from File > Examples > Firmata > StandardFirmata.  In the Windows environment we selected StandardFirmataPlus, but this one is not available in the version which was just installed.  Uploading the firmata to the board works perfect and there is no need to edit SerialFirmata.h header file.

Now you can test Blink.js.  Optionally install Visual Studio Code and GIT which I highly recommend, it provides intellisense, you can debug your scripts, you'll love it and it is free.

Write Blink or copy the Windows version but omit the configuration object passed into the Board constructor function.  There is no need to specify the port number for this exercise in this environment.

$ node Blink.js

Enjoy.

In our meetings we will try to cover as much as possible using the Johnny-Five gude.

References

Saturday, June 4, 2016

NodeBots Day-1: Getting the Environment Ready - Windows

For our "NodeBots Day" activity we are going to follow the "Johnny-Five" guide.  I feel that there is enough material to get the meetup started.  Johnny-Five is a JavaScript Robotics and Internet of Things (IoT) platform.

Pre Requisites

To be able to run the exercises we need:
  1. Install Node.js and NPM.
  2. Install Johnny-Five
  3. Arduino Software IDE
  4. Optionally install a Code Editor
    1. Visual Studio Code is excellent:  recommended over Notepad++, Notepad and even Sublime
      1. Visual Studio Code has a GIT dependency therefore you will need to install Git on your station.  Install Git Bash...  you'll love it!
  5. You might need to install Python and make sure it is included in the path in the case of Windows OS

Quickly

What is Node?  It is a server side platform built on Google's Chrome JavaScript engine (V8 Engine).  As we advance in our journey you will be exposed to other fantastic things that we can do with Node including:
  • Asynchronous
  • Event Driven
  • Very fast!
  • Single thread but it is very scalable
  • It is open source!
To install Nodejs, follow the link provided in this note and select the appropriate version for your system.  If you are using the Window installer make sure that you select to include NPM.  'npm' is the Nodejs package manager and we are going to install required modules using it.

What is Johnny-Five?
As previously mentioned; it is a JavaScript Robotic and IoT development platform.  Its is community based, open source.

Once you have installed Node with npm you can install Johnny-Five.  On my git bash window it looks like this:


Great!  We are almost there!  Next we need to install the Arduino IDE.

Do I really need the Arduino Software?
Well, no.  If you do not have an Arduino board you do not really need the software and instead you could run the Nodebot-Workshop, which provides a "stubbed" Arduino board for testing each workshop challenge...  but we have boards, and we are going to have these boards around so, you can do either!

Taking the path of the real thing requires a bit more work.  We need a protocol to communicate with the board.  Arduino provide this software and it is called "StandardFirmataPlus".


O load the firmata you need to go "File > Examples > Firmata > StandardFirmataPlus".  Once the firmata loads you need to upload it to the board by clicking the "upload" arrow.  This might not work.  I am not sure why this error is "out there".



To get rid of it you need to open the header file "SerialFirmata.h" and go to line 31 and comment the include "#include "



Save the file and upload again.  It should work now.  If it does not then write a comment here and we'll work it out together.

You are almost there!

Why do I need Python installed? 
We need Python because there are a few modules that need to be built for the specific platform.  We might not need it right away, but we will for sure.

Which programming language are we going to be using?  Can I use C for Arduino?
We are using Node, which is JavaScript.  Yes you are right, you could use a C like language for Arduino but in our meetup we are doing Node.

How do I know that I am ready to "go"?  How do I know that everything is ready?
That is a good question and it will depend on a variety of factors including:

  • Operating System
  • Version of Node
  • Version or Arduino and firmware
The classic "Hello World" for the board is the "Blink-Blink" program:


Testing "Blink-Blink"

Assume your board is ready to go.


and you try to run your code after loading the firmata and...  what!?


This was difficult to troubleshoot.  The solution to this "bug" is to explicitly indicate that the Arduino board, in my case, was connected to port COM4 and this was indicated passing a configuration object into the Board construction function.


Run again and smile!  You are on your way.  This is actually documented by Johnny-Five.  What johnny-Five says is that the platform will try to determine the port, however, the port could also be explicitly set, like we did above. 

We are going to make sure that every one can run this exercise on the available boards.

I will document this same setup for Ubuntu in a part 2.  I will add references to this note a little later, but I will publish it now so some of your can get going.

In our meetings we will try to cover as much as possible the examples posted by the Johnny-Five guide.

References



A1 Repo: Simple Pagination for WCF Service Operation

It is never a good idea to paginate on the client.  This post is about a simple pagination for a WCF service operation.  For this work I nee...