Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Artik to Create IoT Projects

DZone's Guide to

Using Artik to Create IoT Projects

Artik is an open, end-to-end IoT platform that connects devices to the cloud. Device agnostic, this tutorial looks at using a Raspberry Pi and its data for further use.

· IoT Zone
Free Resource

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

The Artik IoT platform is an end-to-end IoT platform that helps build awesome IoT projects. It is an open platform that enables different devices to connect to the cloud. Using Artik IoT, connected devices or sensors send data to the cloud. Applications, services, or other connected devices use this information according to their business requirements. 

This tutorial wants to provide a step-by-step description about how to implement an IoT project using Artik IoT. It will explore:

  • How to configure a device.
  • How to create the IoT data model in Artik IoT.
  • How to write an application that sends data to this IoT platform.

Artik IoT Overview

This IoT platform supports several data exchange paradigms and provides different SDKs, depending on the language we prefer.

The main features provided by Artik IoT are:

  • Data-driven development.
  • Real-time data using WebSockets.
  • Different data and device support.

Artik is a well-structured IoT platform, but before using it, I recommend you read this article about  how to use IoT platforms to have an overview. Moreover, Artik is a well-documented platform with useful and interesting examples. You can have more information at this link. An interesting aspect of this platform is the cloud API that enables external devices to connect to this platform and exchange data.

Configure the Device in Artik

First of all, it is necessary to have an account to configure the device. The account is free. This tutorial is based on a Raspberry Pi, but the configurationprocess is device-agnostic. Once you are logged into the Samsung Artik IoT dashboard, you have to click on the device and start configuring it:

samsung artik create device

It is important you add a unique name so that your device can be identified. That’s all! You have created your first device ready to send data. The next step is configuring the data model using the Manifest.samsung artik sending data

Implement the Artik IoT Data Model

Then, we have to configure the variables we want to handle in our IoT project. These variables are sent by the device to the cloud, representing the parameters we measure using the sensors.

The Raspberry Pi, in this project, uses two sensors (to make the project simple).

The first sensor, DHT11, measures the temperature and humidity, while the second one, BMP180, measures the temperature and the pressure. Later in this post, we cover how to connect them to the Pi. For now, it's important to know that the Pi sends three different parameters:

  • Temperature
  • Humidity
  • Pressure

These are the variables we have to configure in Artik IoT. The steps to configure them are described below the image:

Image title


In this case, we add the temperature field that will hold the temperature values coming from the DHT11 sensor. To add the other parameters, you can repeat the same steps. In the end, you will have:

Image title

Now you can activate your manifest!

How to Connect to the Sensors

The configuration is done, so the next step is connecting the Raspberry Pi to the sensors. Be aware of the wiring, otherwise, you could damage your device or sensors. The connection between Raspberry and the two sensors is shown in the pictures below:

raspberry sensors

To use these sensors, install the wiringPi library. The code used to send data to Artik IoT is written in Node.js, but you could use other languages, too. Before reading sensor values, install two Node.js libraries:

These two libraries help you easily read sensor data. First of all, let's import them:

var sensor = require('node-dht-sensor');
var RaspiSensors = require('raspi-sensors');


Now, we can read the sensors:

var BMP180 = new RaspiSensors.Sensor({
  type: 'BMP180',
  address: 0x77
 }, 'BMP180');

 BMP180.fetchInterval(function(err, data) {

  if (data.type == 'Temperature') {
    console.log("Temperature ["+data.value+"]");
  }
  else if (data.type == 'Pressure') {
     console.log("Pressure ["+data.value+"]");
     var data = {"Press": data.value };
   }
}, 5);


While to read DHT11, you have to write:

setInterval(function() {
  var readout = sensor.read();
  console.log('Temp:' + readout.temperature.toFixed(2)
    + "Hum:" + readout.humidity.toFixed(2));
}, 2000);


That’s all. The next step is sending the data to Artik.

Send Data to the Cloud

Finally, we are ready to send the sensor data to the cloud. It is important to note that the device must be authenticated before it can send data. To do that, we need to use a secret token and the device ID that we get from the cloud IoT platform by clicking on the device we have created previously.

Image title


First, let’s connect to them using WebSocket:

var artikUrl=</code><code class="js string">"wss:api.artik.cloud/v1.1/websocket?ack=true";
function connect() {
// connecting
  ws = new WebSocket(artikUrl);
  ws.on('open', function() {
    console.log("Connected...");
    registerDevice();
  });
  ws.on('message', function(data, flags) {
    console.log("Message [" + data + "]");
  });
  ws.on('close', function() {
    console.log("Websocket is closed");
  });
}

As you can notice as soon as the connection is established we register the device.

// Register the Raspberry device first
function registerDevice() {
 try {
  console.log("Registering device..");
  var message = '{"sdid": "d5f71b368bf148xxx","type": "register",
    "Authorization": "bearer fcf3bf9cbf7d49xxx","cid":"1983274" }';
  console.log("Message: " + message);
  ws.send(message, {mask:true});
  console.log("Device registered");
 }
 catch(e) {
  console.error("Exception while sending data...");
 }
}


Finally, here's the method to send the data:

function sendMessage(message) {
  console.log("Sending data to artik...");
  try {
   var payload = '{"sdid": "d5f71b368bf148c9xxxxx", "ts":"' + Date.now().toString() + '","data":'
                  + JSON.stringify(message) + '}';
   console.log("Message: " + payload);
   ws.send(payload, {mask:true});
   console.log("Message sent");
  }
  catch(e) {
    console.error("Exception while sending data...");
    console.log(e.toString());
  }
}


Looking at the code, sdid is the device ID used before and the data is the JSON message holding the values coming from the sensors.

The last step is creating the message. We do it as soon as we get the data from the sensor. For example, for the temperature, we have:

var data = {"Temp": readout.temperature.toFixed(2), 
               "Hum": readout.humidity.toFixed(2)}; 


We do the same thing for the pressure.

var data = {"Press": data.value };


As soon as you start the Node.js app, it begins sending data to Artik. Accessing to the Artik IoT dashboard, you can verify the data and create charts:

artik_io_raspberry

And you're all set! You now know how to use Artik IoT to develop a complete IoT project.

Build an open IoT platform with Red Hat—keep it flexible with open source software.

Topics:
artik ,iot platform ,raspberry pi ,iot

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}