3.1. Hello World Part 1: Sending sensor data to the cloud

3.1.1. Description

Learn how to create your own snap to house your application and use Rigado Edge Direct to deploy that app on your development gateway. To simplify this tutorial, the starting point will be the Node.js Hello World snap from Exploring the Hello World Snap. You will learn to build that snap without changing the source.

After you test on your local computer to iterate quickly, you will use Edge Direct tools to deploy the app to the gateway. Later tutorials will demonstrate a few modifications to the source to change the application’s behavior.

When you complete this tutorial, you will have an environment set up to build your own application prototype running on the gateway, be able to monitor for Edge Compute utilization, and be ready to run a proof-of-concept deployment from Edge Direct.

Expected duration: 2 hours.

3.1.2. Prerequisites

To complete this tutorial you will need the following items. The terms in bold are referenced throughout the document.

  • A Cascade-500 Gateway - The system for running the snap in production. It includes an Edge Direct agent for monitoring app and security updates.
  • A Development Host - This computer or virtual machine should have Ubuntu 16.04 LTS and will be used to edit source files, test scripts, and interact with the snapcraft CLI. It may have any architecture (amd64, x86, etc). This device must have an HCI0 interface with BLE enabled.
  • A Build Host - NodeJS snaps are not cross-compilable, so this build computer must have an armhf processor architecture and have sufficient resources to build the snap. The Raspberry Pi 3 is recommended because it has the armhf architecture, and can run the Ubuntu Core OS (which simplifies the build process). See Prerequisites for Developing your own Snap for more information on setting up a Raspberry Pi.
  • A USB Adapter for Bluetooth Low-Energy - If your Development Host does not have an internal Bluetooth radio, this is required.
  • A Thingy - the Nordic Thingy:52 sensor platform, flashed with stock firmware.
  • An Edge Direct account, and access to the Edge Direct web-application (or the Rigado command-line tool).

3.1.3. Register App Name

Since you’ll be creating your own application, the first thing you should do is register the name you want to use. This name must be globally unique among all snap names. We recommend prepending or appending your organization name, for example: acme-hello-world. Your snap will be published to your private Store, so you control which devices can install your snap, but the namespace for snaps covers all Stores.

  1. Open the Apps page of Edge Direct.
  2. Click the create app button in the top right corner.
  3. Enter your preferred name, and the modal will check if that name is available.
  4. Once you’ve entered an available name, click the REGISTER button.

Later on we’ll use this name in the snap source code.

The rest of this tutorial will use the app name example-hello-world.

3.1.4. Setting up the Development Host

In order to accelerate and simplify the development process, we recommend that you develop and test your applications on a local host instead of directly on the gateway.

We strongly recommend you use a Ubuntu 16.04 LTS Workstation. The rest of this tutorial, and the following ones, will assume your Development Host is running Ubuntu 16.04.

Note

If you’re using a virtual machine:

  • a hard drive of at least 15 GB is recommended;
  • ensure that Bluetooth pass-through from the host is enabled. Your VM application may have an option to allow the VM to use the native or USB-connected Bluetooth device. If your VM does not support Bluetooth, you will need a different solution for your Development Host.
  1. Install Node.js (>=6.11.4). We recommend you use the section “How to Install Using a PPA” of this guide.

  2. Install required libraries with the following command:

    $ sudo apt update
    $ sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev g++ python2.7 git build-essential curl libssl-dev npm
    
  3. Download the hello-world source code with the following command:

    $ git clone https://git.rigado.com/cascade-examples/rigado-node-hello-world.git
    $ cd rigado-node-hello-world
    
  4. Install npm dependencies with the command

    $ npm install
    

    Note

    If the above does not work, you may need to run

    $ npm install --save npm-install-global
    $ sudo npm install --global
    
  5. Install snapcraft with the command

    $ sudo snap install snapcraft
    

3.1.5. Running Hello-World on the Development Host

Since this application is written in Node.js, running on the Development Host enables faster iteration for testing changes without needing to build and deploy on the gateway.

  1. Plug in USB Adapter for Bluetooth, if needed.

  2. Switch on the Thingy.

  3. In your command-line terminal, start the app with the following command:

    $ npm run start
    

3.1.6. Testing the Application

The application is a daemon which discovers nearby Thingy devices and retransmits sensor data to an MQTT Broker. By default, the daemon connects to the first available Thingy and sends all data to a public MQTT Broker according to an internal configuration file.

Verification steps:

  1. After the application successfully discovers the first Thingy and connects, the Thingy will start blinking green.

    ../_images/connected1.jpg

    Figure 3.1 A connected Thingy

  2. Open the Rigado Playground MQTT client.

  3. Change the Subscription Topic to /gateway/GATEWAY_SERIAL/sensors where GATEWAY_SERIAL is the placeholder for the hostname of your Development Host.

    Note

    The gateway hostname matches its serial number.

    ../_images/subscribe.png

    Figure 3.2 The MQTT Subscription dialog

  1. The data from the Thingy is sent to the cloud.

    ../_images/subscription.png

    Figure 3.3 MQTT client showing traffic

  2. See the Rigado Playground https://playground.rigado.com/dashboards/node-hello-world/ to graph the accelerometer data. Later in this tutorial series, we will be modifying the snap and creating our own version of this dashboard.

3.1.7. Setting up the Build Host

For now, the Node.js plugin for snapcraft doesn’t support a cross-platform build.

  1. Follow the installation guide in Raspberry Pi.

  2. While still logged in to the Raspberry Pi, and in classic mode, install the text editor of your choice.

    $ sudo apt install nano
    
  3. Copy the source code from the Development Host to the Build Host. This can be done via scp on the command-line. See Raspberry Pi’s guide for more information.

  4. Edit the snapcraft.yaml file. Change the value of the name variable to the name you entered in Edge Direct earlier.

3.1.8. Build the Snap

  1. Log in to the Build Host.

  2. Switch to Classic mode:

    $ sudo classic
    
  3. Update software packages

    $ sudo apt update && sudo apt upgrade
    
  4. Build the snap

    $ snapcraft
    
  5. Copy the snap over to the Development Host. This can be done via scp on the command-line. See Raspberry Pi’s guide for more information.

3.1.9. Upload and Release the App

  1. Switch to the Development Host.
  2. Open the Apps page of Edge Direct.
  3. Select your app from the list, and click the Upload your first revision button.
  4. Either drag the snap file onto the modal, or click to browse for the snap file.
  5. When it’s done uploading, a modal will ask you if you want to release the app revision to a channel. Select the edge channel and click the RELEASE button.

3.1.10. Deploy App to Gateway

  1. Open the default bundle, and click the Edit button in the top right corner.

  2. Click the add an app button.

  3. Select your app from the dropdown, and select the edge channel. Click ADD

  4. Click the Save button at the top right, and Save again when the confirmation modal pops up.

  5. You can monitor the deployment progress on the Gateways page. Select your gateway and open the Apps list.

    ../_images/snapinstalling.png

    Figure 3.4 The gateway’s App list, showing the new app installing

    The gateway is processing the change while installation command sent is displayed in the Status column of the app list.

    Refresh the page to update the view.

  6. Repeat the steps in Testing the Application above to see the data from the gateway. Note that only one system may connect to the Thingy at a time, so make sure the Node.js script is not running on the Development Host when you test on the gateway.

3.1.11. Check Gateway Load Average

The gateway sends status information to Rigado Edge Direct at regular intervals (1-10 minutes) that include information about the installed snaps, network IP addresses and basic Load Average. You can check the status by viewing the gateway detail view in Edge Direct

3.1.12. Conclusion

This tutorial included setting up Development and Build Hosts, using the Edge Direct web-application to set up a gateway, and testing your own version of rigado-node-hello-world. In the next tutorial, learn how to modify the source of this snap and deploy the new version to your gateway.