Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using Thinger.io Server on an UP Squared Board

24 Oct 2018 1  
This article demonstrates how to use Thinger.io on an UP Squared board running the Ubuntu operating system.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

Thinger.io* is an online cloud platform for the Internet of Things (IoT), allowing many devices to integrate with the Thinger.io cloud. It provides real-time data processing and displaying. Visit the Thinger.io site for detailed information. This article demonstrates how to use Thinger.io on an UP Squared* board running the Ubuntu* operating system. It will read the real-time light sensor value and display it in a time series chart. If the light sensor indicates it’s getting dark, it will send an email notification. The UP Squared board is a low-power and high performance platform ideal for IoT applications. The UP Squared board is based on either the Intel® Celeron® processor (N3350) or Pentium® processor (N4200). For more information, visit the UP Squared home page.

Hardware Requirements

The hardware components used in this project are listed below:

Figure 1: UP Squared board with light sensor

Software Requirements

The software requirements used in this project are listed below:

Install Ubuntu* Kernel

The default kernel in the Ubuntu Core doesn’t allow access to the GPIO pins. Install the required kernel below to gain access to the GPIO pins.

  1. Before updating the kernel, use the following command to check whether you have the right kernel for UP Squared.
    uname -srv
  2. The kernel for UP Squared should look like this:
    Linux 4.10.0-42-generic #5000~upboard9-Ubuntu SMP Tue Dec 12 11:46:16 UTC 2017
  3. Add the repository:
    sudo add-apt-repository ppa:ubilinux/up
  4. Update the repository list:
    sudo apt update
  5. Remove all the generic installed kernel:
    sudo apt-get autoremove -purge ‘linux-.*generic’
  6. Install the kernel for UP Squared:
    sudo apt-get install linux-image-generic-hwe-16.04-upboard
  7. Reboot:
    sudo reboot
  8. Verify that the kernel was installed:
    uname -srv

Set Up Thinger.io* on UP Squared*

Upgrade the system:

sudo apt-get update
sudo apt-get upgrade

Download latest version of the Thinger.io Linux Client and build and run it:

git clone https://github.com/thinger-io/Linux-Client.git
cd Linux-Client/src
chmod +x run.sh
./run.sh

Download and Build MRAA and UPM libraries

UPM is a library that allows you to interface with sensors and actuators. MRAA is a GPIO library that provides easy connections to IO pins on the UP Squared and other boards on Linux.

MRAA library

git clone https://github.com/intel-iot-devkit/mraa.git
mkdir mraa/build
cd mraa/build
cmake .. -DBUILDSWIGNODE=OFF
make
make install

UPM library

git clone https://github.com/intel-iot-devkit/upm.git
mkdir upm/build
cd upm/build
cmake .. -DCMAKE-INSTALL_PREFIX:PATH=/usr -DBUILDEXAMPLES=ON -DBUILDSQIGPYTHON=OFF -DBUILDSWIGNODE=OFF
make
sudo make install

By default, the MRAA and UPM libraries and includes will be installed in /usr/local. If you prefer them to be installed in a different directory, for example /usr, then use option -DCMAKE-INSTALL_PREFIX:PATH=/usr for the cmake as below. This example will use the default directory /usr/local.

cmake .. -DCMAKE-INSTALL_PREFIX:PATH=/usr -DBUILDEXAMPLES=ON -DBUILDSQIGPYTHON=OFF -DBUILDSWIGNODE=OFF

Create a New Thinger.io* Device

Thinger.io allows multiple devices to connect to the Thinger.io cloud. Visit thinger.io console to create a free account if you don’t have one. Then navigate to Devices on the left panel of the Thinger.io console to add a new device.

Figure 2: Add new device

Fill out Device ID and Device description. For the credentials field, either fill out a value or click on Generate Random Credentials for a random credentials value. Make a note of the credentials value for later when it’s time to connect the device into the Thinger.io server in Code Example 3: Thinger.io instance. Then, click the Add Device button to add the new device.

Once your first device is successfully added into Thinger.io, it should show 1/2 devices as below. The maximum is two devices for free accounts.

Figure 3: Add device window

Once your first device is successfully added into Thinger.io, it should show 1/2 devices as shown below. The maximum is two devices for free accounts.

Figure 4: Cloud Statistics

Set Up Thinger.io Endpoint

Endpoints are pre-defined channels that the device uses to send email, SMS, or others to the services that consume your device data. Navigate to Endpoints on the left panel, then click on Add Endpoints to add the new endpoint.

Figure 5: Add new endpoint

Fill out all of the required fields to define the endpoint. Below is an example of a light sensor endpoint. Then click on Add Endpoint in the bottom of the Add Endpoint window to register the endpoint.

Figure 6: Add new endpoint window

Update main.cpp to Interface with the Grove* Sensor

Edit main.cpp to interface with the light sensor. If the light sensor indicates it’s getting dark, it will send an email to notify.

cd src
vi main.cpp

Add this line into main.cpp to interface with Grove shield.

mraa.addSubplatform(mraa.GROVEPI, "0 ")
Code Example 1: Interface with Grove shield

Connect the light sensor to the analog pin A1.

// Light sensor is connected to the analog A1
#define OFFSET 512
Light *light_sensor = new Light(OFFSET + 1);
Code Example 2: Light sensor object

Initialize the Thinger.io instance using the UP Squared device information.

#define USER_ID          		"your_thinger.io_user_id"
#define DEVICE_ID       		"your_upsquared_device_id"
#define DEVICE_CREDENTIAL	"your_thinger.io_credential" // From Figure 3: Add device window above

// Initialize Thinger.io instance for UP Squared
Thinger_device thing(USER_ID, DEVICE_ID, DEVICE_CREDENTIAL);
Code Example 3: Thinger.io instance

Once the endpoint is registered, the device can invoke call_endpoint() and pass in the endpoint identifier to send an email to the email address which was defined in Figure 5: Add new endpoint window above.

thing.call_endpoint("light_is_dim");
Code Example 4: Endpoint

The pson inline function will assign the light sensor value to the light private variable and its value will be displayed on the api windows as shown in Figure 8: UP Squared API window.

thing["light"] >> [](pson& out) {
    out = light_value;
    };
Code Example 5: pson inline

Read the light sensor value from the analog pin A1 and stream it to the Thinger.io server. If the light indicates it is getting dark, call the Endpoint to send the email.

int light_value = 0;  // global variable for the pson inline below
while(1) {
    thing.handle();
    light_value = light_sensor->value();
    thing["light"] >> [](pson& out) {
    out = light_value;
    };

    // It is getting dark, send email
    if (light_value < 10) {
        thing.call_endpoint("light_is_dim");
    }
    std::cout << "light_sensor = " << light_sensor->value() << endl;
    thing.stream(thing["light"]);
}
Code Example 6: Read and analyze sensor value

You should receive an email from no-reply@thinger.io with a predefined subject and body when the light sensor indicates that it’s getting dark.

Compile and Execute

Edit the make file to include the dependency libraries

cd Linux-Client
cd build/CMakefiles/thinger.dir
vi flags.make

Add I/usr/local/include/upm -I/usr/local/include -I/usr/local/include/upm -L/usr/local/lib -lupm-light -lupmc-light -lmraa -lupm-utilities -lupmc-utilities into the CXX_FLAGS. The updated flags.make should look like:

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.5

# compile CXX with /usr/bin/c++
CXX_FLAGS =  -std=c++11 -I/usr/local/include/upm -I/usr/local/include -I/usr/local/include/upm -L/usr/local/lib -lupm-light -lupmc-light -lmraa -lupm-utilities -lupmc-utilities -O3 -DNDEBUG  

CXX_DEFINES = -DDAEMON=0 -DOPEN_SSL=1

CXX_INCLUDES = 

Recompile the updated main.cpp

cd ~/Linux-Client/build
make thinger

Execute the updated Thinger.io. Note that you need to be root user to access the GPIOs, so be sure to include "sudo". To run the updated main.cpp, type the following command in a terminal:

sudo ./thinger

View Sensor Real Time Value

From the Thinger.io console, navigate to Devices on the left panel, select UpSquared device, then click on View API.

Figure 7: UP Squared dashboard window

Then, click on View API. The real time light sensor will be displayed on the UpSquared API window. To refresh the real time light sensor value, click on Run.

Figure 8: UP Squared API window

Time Series Chart

To view the real-time light sensor values in the time series chart, navigate to panel on the left, then click on Dashboards. Then click on Add Dashboard, fill out all the fields and then click to create a new dashboard.

Figure 9: Add dashboards window

Once the new dashboard is successfully created, click on Dashboards on the left panel again to view the newly added dashboard then click on it.

Figure 10: Dashboard view

Slide the button on the top right corner to the right and then click on the Add Widget button.

Figure 11: Add widget

Select Time Series Chart.

Figure 12: Add widget option

Fill out all the required fields then click on the Save button.

Figure 13: Add widget window

The real-time light sensor time series chart will look like the one below.

Figure 14: Time series chart

Example Sketch

#include "mraa.hpp"
#include "upm/light.hpp"
#include "thinger/thinger.h"
#include "upm_utilities.h"
#include 

#define USER_ID             		"nnle"
#define DEVICE_ID          	 	"UpSquared"
#define DEVICE_CREDENTIAL   	"tJU5a1VBBYBG"
#define OFFSET              		512

using namespace mraa;
using namespace upm;
using namespace std;

// Global variable for pson inline below
int light_value = 0;

int main(int argc, char *argv[])
{
    unsigned int aPinLight;
    Platform platform = getPlatformType();
    if(platform == INTEL_UP2) {
aPinLight = 1 + OFFSET;  // A1
    }
    else {
cout << "Error: Unrecognize board.\n";
	return 1;
    }

    // Interface with Grove shield
    addSubplatform(GROVEPI, "0");

    // Light sensor is connected to the analog pin A1
    Light *light_sensor = new Light(aPinLight);

    // Initialize Thinger.io instance for UP Squared
    thinger_device thing(USER_ID, DEVICE_ID, DEVICE_CREDENTIAL);

    while(1) {
    	thing.handle();
 	light_value = light_sensor->value();
    	thing["light"] >> [](pson& out) {
 	out = light_value;
         	};

	if (light_value < 10) {
    	    thing.call_endpoint("light_is_dim");
    	}
        	cout << "light_sensor = " << light_sensor->value() << endl;
 	thing.stream(thing["light"]);
    }

    thing.start();
    return 0;
}
Code Example 7: Example sketch

Summary

Thinger.io provides services for building IoT applications. It allows you to connect and manage devices and visualize sensor data in real-time. You can also expand your project with multiples sensors, multiple IoT devices and communicate between devices.

Key References

UP Squared board

About the Author

Nancy Le is a software engineer at Intel Corporation in the Core & Visual Computing Group working on Intel Atom® processor enabling for Intel® IoT projects.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here