29 April 2016 / Last updated: 27 Jan 2017

ARTIK & ARTIK Cloud tutorial

Execution time:
Yesterday, during the opening keynote for SDC we released official support for the Samsung Artik 10 & 5. With the help of our partner Snappin.io we put together a live demo which you can watch below.
In this post, we'll run through a minimalist version of our onstage demo to illustrate how to update your ARTIK with speed and safety.
In particular, we’ll learn how to:

Getting Started

Have your ARTIK(5 or 10) an SD card and your proximity sensor(optional) handy.
Sign up with resin.io. If you already have an account skip ahead
Add your public ssh key, this is so we can push code later, do so by importing it from github, using an existing key on you're machine or by generating a new one.
Next, create an app, name whatever you like and select the device type Artik 5 or 10. Then download your device OS, add your network configurations. Once the resinOS has downloaded burn it to an SD card using etcher or your image flasher of choice.

Setting Up Your Device

In order to get the resinOS up and running on our ARTIK we need to first set it up to boot from our SD card rather than it's internal eMMC memory. To do this we need to set the tiny SW2 dip switch to position 1:on and 2:on, as shown below.
Note: This resinOS will completely write over the existing eMMC.
We can now insert the 5VDC power cable and flip the power switch, labelled PWR SW, to the on position. We should now have some glowing LEDs indicating a sign of life. Next, we need to press and hold the SW3 POWER push button for 1 or 2 seconds, this starts the boot from the SD Card.
After a few seconds, you should see your ARTIK appear on the resin.io dashboard. If it still hasn't appeared after 1 or 2 minutes, double check your ethernet connection or that you entered the right wifi credentials at download time. If you still can't get it online, contact us on [email protected].
Note: In order for the wifi to work correctly, you need to set jumpers for (ARTIK 5 J20 and J33) and ( ARTIK 10 J20 and J36) towards the edge of the board.
Your ARTIK will now flash resinOS onto the internal eMMC so that you can remove the SD. This will take 2 or 3 minutes. Once it has finished it will shut itself down and you will see the device on the dashboard in a Post-Provisioning state. At this point you need to:
  1. Flip the power switch off position.
  2. Remove the SD Card.
  3. Set the SW2 dip switches to 1:off and 2:off.
After all of that we flip the PWR SW back on and once again hold down the SW3 POWER button for a second or so. If all goes according to plan we should now have a freshly provisioned sitting in an IDLE state on our dashboard.

Connecting the sensor

In this step we will be using an Infrared Proximity Sensor (Sharp GP2Y0A21YK). If you don't have a sensor you can skip to the next section.
Connect the sensor as shown below:
Consult the ARTIK documentation for specifics on pin mapping.

Configuring Artik Cloud

Signup with Artik-cloud.
There are two portals to manage your Artik cloud. The developer portal and the user Portal. The developer portal provides an interface for you to create and manage device types and applications where as the user portal allows you to manage devices and their data.
So first we'll need to create a device type. To do this you'll need to create a Manifest. A Manifest is just a set of instructions that describe the device data to Artik Cloud. For instance:
    "name": "reading",
    "type": "CUSTOM",
    "valueClass": "Integer",
    "isCollection": false,
    "tags": []
Manifests also allow you to interpret that data and trigger actions, but that's out of the spec for this tutorial. For more information on this you can consult the documentation.
For your convenience we've included manifest.json.
So name the device type anything you like e.g. resinArtik and upload the manifest.json we have provided.
Next, we'll create and connect a device on the user portal
(My ARTIK Cloud)
artik cloud dash
Select the cog icon and generate a device token.
Grab your newly generated device token and device ID and add them as device environment variables in your resin.io device view.
Ensure they are named accordingly: ARTIKCLOUD_DEVICE_ID ARTIKCLOUD_DEVICE_TOKEN.
resin env-vars

The code

As I mentioned we'll be pushing is a stripped down our demo, in short it's a little node app that take's a reading from the proximity sensor, if you don't have a sensor connected it'll randomly generate one. If said reading exceeds a threshold (default = 250) pushing the event to ARTIK Cloud.
The first thing you'll notice is the presence of a `Dockerfile.template, this is because resin.io builds and runs all applications in the form of a docker container. We use containers for a variety of reasons, it allows us to support every language and update the devices in a safe atomic way while still maintaining a lightweight footprint on the device. It has also become the defacto way to update applications on the web so why not use it on embedded devices.
# base-image for node on any machine using a template variable,
# see more about dockerfile templates here:https://docs.resin.io/pages/deployment/docker-templates

Save source folder

RUN printf "%s\n" "${PWD##}" > SOURCEFOLDER

Move to /usr/src/app

WORKDIR /usr/src/app

Move package to filesystem

COPY "$SOURCEFOLDER/app/package.json" ./

Install NodeJS dependencies via NPM

RUN JOBS=MAX npm i --unsafe-perm --production && npm cache clean

Move app to filesystem

COPY "$SOURCEFOLDER/app/index.js" ./

Start app

CMD ["node", "/usr/src/app/index.js"]
This file is relatively short, but there a few important points to take away:
  1. We use resin's official base image. The %%RESIN_MACHINE_NAME%% allows us to dynamically pull images from resin.io's Dockerhub so it can be replaced automatically with artik5 or artik10 depending on which device you are using. Unsurprisingly it also has node pre-installed.
  2. We run NPM install before adding the rest of our source, this allows us to make changes to our index.js without invalidating resin.io builder's cache of /node_modules. This is a performance enhancement so don't worry if it's not completely clear right now.
  3. Everything before the CMD command runs on our builders, the complete container is then downloaded to the device and the CMD command is executed, starting our application.
The Application logic is, for the most part self-explanatory, As I mentioned it has a fallback in case you don't have a sensor connected.
function getReading(cb){
  // got a sensor ?
	if (reading != 0) {
	  // sensor connected
	} else {
	  // no sensor connected so generate a value
When the sensor_threshold is exceeded it pushes the event data to the cloud.
Function that enables and reads proximity sensor
function enable_proximity_sensor() {
  console.log('Monitoring for movement');

  setInterval(function() {
console.log('Sensor reading: ' + reading)
if (reading > sensor_threshold) {
console.log('pushing event to ARTIK Cloud');
}, poll_interval);

Push the code

Okay, let's deploy. Luckily it's a simple three-step process: Clone, add remote, push. Start off by cloning this repository.
git clone https://github.com/resin-io-projects/resin-artik-cloud-publisher && cd resin-artik-cloud-publisher
Navigate to your app on the resin dashboard and copy your devices endpoint top right.
git add <resin-app-endpoint>

git push resin master
Once you see the resin.io unicorn in the build logs it means you're container has been built, after which it will download to all devices in your application. You can see the progress from you resin.io dashboard.
downloading progress
Once the application has started running. Take a look at ARTIK Cloud to view the live events stream.
note: We have tested this against the following versions of the ARTIK. Artik10: Dev. Board10, VER. 0.5 (2015.12.23), Artik5: TYPE 5, VER. 3.2 (2015.10.22)


So here's what we've accomplished:
  • We’ve taken an app and deployed it to a piece of hardware using a web-like workflow, futuristic high-five!
  • We used Environment variables to remotely provide runtime configuration for the ARTIK devices.
  • We poked our heads into the ARTIK Cloud and sent some basic event data.
If you'd like to learn more about resin, dive into our documentation or ask our team some questions in our chat room.
by Alexandros MarinosFounder/CEO, balena

Share this post