The Linino™ One meets Firebase

This is a continuation to my last post Getting reacquainted with the Linino One.  In this post I will be setting up a little IOT app using the Linino device, a Python script, a python-firebase SDK and Firebase.

Prerequisites

      • dogUSB and a properly formatted SD card
      • Python: You should know basic Python concepts, such as imports, variables and print statements.
      • The terminal: You should know how to open a terminal or command-line prompt on your development machine, how to change directories, and how to execute commands.
      • The Arduino IDE: You should now your way around the interface and how to upload scripts to a device.
      • A Firebase account.
      • A spoonful of creativity.

If you’ve been reading along this series, the last post, I commandeered my Linino and was ready to start coding. So, lets do just that, fire up terminal and ssh into your Linino device.

Type:

 

ssh root@your.ip.address

 

and enter your password

**NOTE – READ ONLY IF YOU RECEIVED THE MIDDLE_MAN ATTACK MESSAGE : If you had previously ssh into your Linino and then reset the device to its default state, you will most likely received the middle-man attack message when trying to ssh back into the new reset device.
Don’t worry, here’s a link to show you how to resolve the issue.

 

Screen Shot 2016-05-07 at 8.21.48 PM

 

If you didn’t get the middle man attack, you’re most likely going to get this annoying message,

 

annoying_message

 

just type:

 

yes 

 

move along, nothing to see here and BOOM,  just like that you’re in!

 

 

BUSY_BOX

 

Retyping your password each time you log in gets old real fast, you can find a wealth of information on the interwebs on how to properly set up a Public key based authentication for SSH. A tutorial for setting up public key based authentication on OpenWRT can be found  here.

Lets start by updating the Linux package manager on the Linino, type:

 

opkg update

 

Next, install the distribute package. This will allow you to use the easy_install command line tool.

 

opkg install distribute

Proceed, by installing the python-openssl package

 

opkg install python-openssl

Finally, use easy_install to install pip.

 

easy_install pip

 

By default, pip will install Python packages to the Linino onboard memory. As mentioned in my previous post, it is discouraged, due to the limited number of writes. Instead we will install all other software packages onto a micro SD card.

Add the dogUSB (USB port & a formatted SD card) onto the Linino module, if you haven’t already done so.

Next, lets do a sanity check by typing:

 

dmesg

 

command to show all the system messages. If the SD card was correctly detected you should be able to locate this line of text in terminal

sda:sda1

 

Screen Shot 2016-05-04 at 9.53.48 PM

 

We are now ready to start creating directories on the SD card. Lets add a directory for python packages, type:

 

mkdir /mnt/sda1/py-packages

 

Just to make sure that all is well, lets check the SD card and make sure the newly created directory exists, type:

 

cd ~ 
cd /mnt/sda1
ls

 

you should see the py-packages folder, like so.

 

Screen Shot 2016-05-06 at 3.45.34 PM

 

Now let’s edit the /etc/profile file and set a path environment variable so that Python knows where to locate python packages, type:

 

cd ~
vim /etc/profile

 

You’ll see several lines that start with export. Move the cursor down below those lines, then press i to enter insert mode.

Type:

 

export PYTHONPATH=${PYTHONPATH}:/mnt/sda1/py-packages

 

Press esc to go back to command mode, then  type :

 

:wq 

 

to save the file and quit vim. Now, lets reload the environment, type:

 

source /etc/profile

 

Cool, so before we move along, lets veer off for a moment and make sure that you have a free Firebase account.

Firebase is a transparent back-end for applications that relieves developers from the hassles related to data transfer, storage and synchronization across devices. Its been adopted by start up communities to quickly get applications to market. We are going to use Firebase, to collect our sensor data from our linino. This enables us to create robust client applications like IOS, Android or better yet an Angular 2 responsive app to access our data from anywhere. So, go ahead and take a few minutes to create an account.

Ok, you should now have your very own Firebase account, now we need a library to access it via our device.

Lets force Pip to install a python firebase SDK into the device’s py-package directory (by default the package will also download and install the request package), fire up terminal and type:

 

pip install --target /mnt/sda1/py-packages python-firebase

 

Sweet, lets do another sanity check, type:

 

cd ~
cd /mnt/sda1/py-packages
ls

 

Screen Shot 2016-05-06 at 4.16.01 PM

 

Everything looks great, now, we are ready to write a simple python script to send a test variable to our Firebase account. The first thing we need to do is create an Arduino folder on the SD card so the linino can access and run the python script. Once again in terminal, type:

 

cd ~
mkdir /mnt/sda1/arduino

 

lets do another sanity check, type:

 

cd /mnt/sda1
ls

 

Screen Shot 2016-05-06 at 4.30.38 PM

 

Perfect, next, lets move into the arduino directory and create the python script, in terminal type:

 

cd /arduino
vim post-to-firebase.py

 

Press i to enter insert command mode, and type:

 

#!/usr/bin/python
import sys
from firebase import firebase

firebase = firebase.FirebaseApplication('https://replace-this-with-your-app-path.firebaseio.com/', None)

node_path = sys.argv[1]
sensor_data = sys.argv[2]
firebase_node = '/linino/sensor/' + node_path

response = firebase.post(firebase_node, sensor_data)

print  response

Then hit esc and type:

:wq

to save the file and quit.

Here’s a quick explanation of the script above, just in case you are not a python coder. The first line of code is a shebang line, which basically tells the program loader that the file is an executable script.

Next I import sys , which is a module which provides the arguments passed in from the Linino to the script.

Then I import firebase , which wraps up the firebase API.

Next I configure the firebase sdk by adding the firebase url, and then I create some variables:

node_path: a string passed in from the Linino. Example – “light_sensor”

sensor_data: a string passed in from the Linino. Example – “7”

firebase_node: is the name of the node that I want my data saved to appended to a value that I pass in from the Linino. For example the Linino passes in “light_sensor” the resulting path is /linino/sensor/light_sensor, this gives the script a bit of flexibility.

Finally,

response is the actual call that posts the data to firebase, which returns a result back to the Linino.

So now that you have an understanding of whats going on, lets test the python script, in terminal type:

 

python post-to-firebase.py light_sensor 7

 

Open up a browser window and head over to your Firebase account and make sure the script posted to the database. You should have a saved value under linino > sensors > light_sensor node like so:

 

firebase_test

 

As you can see I tested mine quite a bit.

Onward, connect the Linino to your computer via a Micro-B USB cable and open up the Arduino IDE. Just for clarification, I found that uploading scripts to the Linino via my network was not as stable as a wired connection, so I would advise connecting the device to your computer for this portion of the blog post.

In the Arduino IDE Select:

Tools > Board > Arduino Yun and then
Port > /dev/cu.cu.usbmodemXXXX

 

Screen Shot 2016-05-03 at 6.58.46 PM;

 

Lets go ahead and make sure that the Linino is wired up correctly and ready for programming by uploading and running the blink test.

In the Arduino IDE go to and select:

File > Examples > Basic > Blink

and upload. You should see a tiny blinking led on your Linino. Good, so lets start coding in the Arduino IDE, select:

File > New

The script below, is the beginnings of a connected app. It currently mimics a fake sensor reading by selecting a random number, every 5 minutes and posts the data (reading) to Firebase. I added a visual identifier via the LED. Whenever the device triggers the python process the led on pin 13 lights up on the device and turns off when the script is complete. That’s pretty much the gist of it, you can take this script and adapt it to your needs or extend it, let me know what you do with it!

 

#include <Bridge.h>
#include <Process.h>

int LED_PIN = 13;
int MINUTES = 5;

void setup() {
  delay (3500);
  Bridge.begin(115200);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
 const unsigned long minutesDelay = MINUTES * 60 * 1000UL;
 static unsigned long lastSampleTime = 0 - minutesDelay;
 unsigned long now = millis();
 
 if (now - lastSampleTime = minutesDelay)
 {
    lastSampleTime += minutesDelay;
    int sensor = random(0,10);
    digitalWrite(LED_PIN, HIGH);
    postData(sensor);
 }
   
}
void postData(int sensor_value) {
  String path = String("light_sensor");
  String data = String(sensor_value);
  Process process;
  process.begin("python");
  process.addParameter(path);
  process.addParameter(data);
  process.addParameter("/mnt/sda1/arduino/post-to-firebase.py");
  process.run(); 
  while (process.available() > 0) {
    char c = process.read();
    digitalWrite(LED_PIN, LOW);
  }
  
}

If you got this far, you are successfully sending data to the internet via your Linino, the next steps are to actually add a real sensor and access the data via a client. I’m currently awaiting my brand spanking new GA1A12S202 Log-scale Analog Light Sensor from Adafruit.
1384-02

 

If you already have a sensor, know how to add it to the existing code, have a bit of Javascript, Node, framework chops, you can easily check out my Angular 2 sensor app on Github. A preview link and the credentials are provided. I’ll cover how I created the app in a new blog post.

Till then, happy hacking!

5 thoughts on “The Linino™ One meets Firebase

  • Pingback: Getting reacquainted with the Linino™ One – Coding Color

  • March 10, 2017 at 5:41 am
    Permalink

    Sorry please help me!

    I try this arduino code.but i meeting Error:

    firebasepost:18: error: lvalue required as left operand of assignment

    exit status 1
    lvalue required as left operand of assignment

    Reply
  • May 24, 2017 at 12:39 pm
    Permalink

    Sorry, a question:
    I successfully install all, but I get one question “” from firebase import firebase””.
    I try to two arduino yun board is to meet the same question.”” from firebase import firebase””.
    please help me!

    Reply
  • May 25, 2017 at 2:36 am
    Permalink

    Hello, I found the answer.

    export PYTHONPATH=${PYTHONPATH}:/mtn/sda1/py-packages

    must change

    export PYTHONPATH=${PYTHONPATH}:/mnt/sda1/py-packages

    then reset arduino yun can be successful.

    Reply
    • May 25, 2017 at 3:04 am
      Permalink

      Sweet, sorry for the typo.
      Happy hacking!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: