blob: 42b5bf6ac979156939ae579f301537dbad116c1f [file] [log] [blame]
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="robots" content="all,follow">
<meta name="googlebot" content="index,follow,snippet,archive">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Getting started with Eclipse Hono :: Eclipse Hono&trade;</title>
<meta name="author" content="The Eclipse Hono Project" />
<meta name="description" content="Take a quick tour and learn how Hono can help you connect devices via HTTP and/or MQTT and how downstream applications can consume the data published by devices.">
<meta name="generator" content="Hugo 0.81.0" />
<link href='//fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,500,700,800' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.11.2/css/all.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="/hono/css/animate.css" rel="stylesheet">
<link href="/hono/css/style.hono.css" rel="stylesheet" id="theme-stylesheet">
<link href="/hono/css/custom.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<link rel="apple-touch-icon" sizes="180x180" href="/hono/favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="48x48" href="/hono/favicon/favicon-48x48.png">
<link rel="icon" type="image/png" sizes="32x32" href="/hono/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/hono/favicon/favicon-16x16.png">
<link rel="manifest" href="/hono/favicon/site.webmanifest">
<link rel="mask-icon" href="/hono/favicon/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="/hono/favicon/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="/hono/favicon/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<link href="/hono/css/owl.carousel.css" rel="stylesheet">
<link href="/hono/css/owl.theme.css" rel="stylesheet">
<link rel="alternate" href="/hono/index.xml" type="application/rss+xml" title="Eclipse Hono&amp;trade;">
<meta property="og:locale" content="en_us">
<meta property="og:site_name" content="Eclipse Hono&amp;trade;">
<meta property="og:title" content="Getting started with Eclipse Hono">
<meta property="og:type" content="website">
<meta property="og:url" content="https://www.eclipse.org/hono/getting-started/" />
<meta property="og:description" content="Take a quick tour and learn how Hono can help you connect devices via HTTP and/or MQTT and how downstream applications can consume the data published by devices.">
<meta property="og:image" content="https://www.eclipse.org/hono/img/sharing-default.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="1655">
<meta property="og:image:height" content="827">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@EclipseHono">
<meta name="twitter:title" content="Getting started with Eclipse Hono">
<meta name="twitter:image" content="https://www.eclipse.org/hono/img/sharing-default.png">
<meta name="twitter:description" content="Take a quick tour and learn how Hono can help you connect devices via HTTP and/or MQTT and how downstream applications can consume the data published by devices.">
</head>
<body>
<div id="all">
<header>
<div class="navbar-affixed-top" data-spy="affix" data-offset-top="200">
<div class="navbar navbar-default yamm" role="navigation" id="navbar">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand home" href="/hono/">
<img src="https://www.eclipse.org/hono/img/HONO-Logo_Bild-Wort_quer-s-310x120px.svg" alt="Getting started with Eclipse Hono logo" class="logo">
<span class="sr-only">Getting started with Eclipse Hono - go to homepage</span>
</a>
<div class="navbar-buttons">
<button type="button" class="navbar-toggle btn-template-main" data-toggle="collapse" data-target="#navigation">
<span class="sr-only">Toggle Navigation</span>
<i class="fas fa-align-justify"></i>
</button>
</div>
</div>
<div class="navbar-collapse collapse" id="navigation">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown active">
<a href="/hono/getting-started/">Getting started</a>
</li>
<li class="dropdown">
<a href="/hono/docs/">Documentation</a>
</li>
<li class="dropdown">
<a href="/hono/downloads/">Download</a>
</li>
<li class="dropdown">
<a href="/hono/sandbox/">Sandbox</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Community <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/hono/community/contributing/">Contributing</a></li>
<li><a href="/hono/community/presentations/">Resources</a></li>
<li><a href="/hono/community/get-in-touch/">Get in Touch</a></li>
<li><a href="/hono/community/road-map/">Road Map</a></li>
<li><a href="/hono/community/thankyou/">Thank you</a></li>
</ul>
</li>
</ul>
</div>
<div class="collapse clearfix" id="search">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-template-main"><i class="fas fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</header>
<div id="heading-breadcrumbs">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Getting started with Eclipse Hono</h1>
</div>
</div>
</div>
</div>
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-12">
<div>
<p>This guide will walk you through an interactive example usage scenario of Eclipse Hono. You will learn how devices can use Hono&rsquo;s protocol adapters to publish telemetry data and events using both HTTP and/or MQTT. You will also see how a downstream application can consume this data using Hono&rsquo;s north bound API without requiring the application to know anything about the specifics of the communication protocols used by the devices.</p>
<h2 id="prerequisites-for-the-getting-started-guide">Prerequisites for the Getting started Guide</h2>
<p>This guide requires several tools to be installed on your computer.
During the course of this guide, the devices publishing data will be represented by means of running some command line tools for posting HTTP requests and for publishing MQTT packets.</p>
<h4 id="curl-http-command-line-client">Curl HTTP Command Line Client</h4>
<p>The <code>curl</code> command is used in this guide for registering devices with Hono and for simulating a device publishing data using HTTP.
On most *nix like systems <code>curl</code> will probably already be installed. Otherwise, please refer to the
<a href="https://curl.haxx.se/">Curl project page</a> for installation instructions.</p>
<h4 id="mosquitto-mqtt-command-line-client">Mosquitto MQTT Command Line Client</h4>
<p>The <code>mosquitto_pub</code> command is used in this guide for simulating a device publishing data using the MQTT protocol.
Please refer to the <a href="https://mosquitto.org/">Mosquitto project page</a> for installation instructions, if you do not
have it installed already.</p>
<p><strong>NB</strong> The installation of the Mosquitto command line client is optional. If you do not install it then you will not be able to simulate
an MQTT based device but otherwise will be able to get the same results described in this guide.</p>
<h4 id="hono-command-line-client">Hono Command Line Client</h4>
<p>The Hono command line client is used in this guide for simulating an application that consumes telemetry data and events published by devices.
The client is available from <a href="/hono/downloads/">Hono&rsquo;s download page</a>.
Note that running the command line client requires a Java 11 runtime environment being installed locally.</p>
<h4 id="very-quick-quickstart">Very quick Quickstart</h4>
<p>In the following all necessary commands and operations will be explained on how to register a tenant and a device via the HTTP API.
Then, it will be shown how messages can be sent via the HTTP and MQTT protocol adapters.
If you want to really kickstart your Hono experience there also is a convenience Python script which you can use to
automatically replay the Tutorial given here (at least the telemetry part for now).</p>
<div class="alert alert-notice">
<h4 class="alert-heading"><i class="fas fa-info-circle"></i> Python Quickstart</h4>
<div><p>You can find the Python tutorial in the <a href="https://github.com/eclipse/hono/tree/master/examples/quickstart-python">Examples</a>.
The python script does</p>
<ul>
<li>Set up a Tenant</li>
<li>Set up a Device in the tenant</li>
<li>Add credentials to the device</li>
<li>Start an (northbound) AMQP Receiver as &ldquo;Consumer&rdquo; of the data</li>
<li>Send a Telemetry message via HTTP API</li>
<li>Send a Telemetry message via MQTT API
For more details have a look in the <a href="https://github.com/eclipse/hono/tree/master/examples/quickstart-python/README.md">Readme</a>.</li>
</ul>
</div>
</div>
<h4 id="hono-sandbox">Hono Sandbox</h4>
<p>The most important prerequisite is, of course, a Hono instance that you can work with.</p>
<p>The most straightforward option to use for this guide is the <a href="/hono/sandbox/">Hono Sandbox</a> which is running on
infrastructure provided by the Eclipse Foundation and which is publicly accessible from the internet.</p>
<p>Using the Sandbox, there is no need to set up your own Hono instance locally. However, it requires several non-standard ports
being accessible from your computer which may not be the case, e.g. if you are behind a firewall restricting internet access to
a few standard ports only.</p>
<p>You can verify if you can access the relevant ports of the Sandbox by running the following command and comparing the output:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">curl -sIX GET http://hono.eclipseprojects.io:28080/v1/tenants/DEFAULT_TENANT
</code></pre></div><p>If you get output like this</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">HTTP/1.1 <span style="color:#ae81ff">200</span> OK
etag: 89d40d26-5956-4cc6-b978-b15fda5d1823
content-type: application/json; charset<span style="color:#f92672">=</span>utf-8
content-length: <span style="color:#ae81ff">260</span>
</code></pre></div><p>you can use the Sandbox. Run the following commands to set some environment variables which will be used during the guide</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export REGISTRY_IP<span style="color:#f92672">=</span>hono.eclipseprojects.io
export HTTP_ADAPTER_IP<span style="color:#f92672">=</span>hono.eclipseprojects.io
export MQTT_ADAPTER_IP<span style="color:#f92672">=</span>hono.eclipseprojects.io
</code></pre></div><p>and then proceed to the <a href="#overview-of-hono-components">Overview of Hono Components</a>.</p>
<p>However, if the <code>curl</code> command yielded different output, you will need to set up Hono locally as described in the next section.</p>
<p>For interacting with the Device Registry of the Hono Sandbox (e.g. for creating tenants, devices), you can also use the testing functionality integrated in the <a href="https://www.eclipse.org/hono/docs/api/management/">Device Registry Management API documentation</a>.</p>
<h4 id="setting-up-a-local-hono-instance">Setting up a local Hono Instance</h4>
<p>In case you cannot access the Hono Sandbox as described above, you will need to set up an instance of Hono running on your local computer.
For evaluation purposes a single node <em>Minikube</em> cluster is sufficient to deploy Hono to.</p>
<ol>
<li>Please refer to the <a href="https://www.eclipse.org/hono/docs/deployment/create-kubernetes-cluster/#setting-up-a-local-development-environment">installation instructions</a> for setting up a local Minikube cluster, then</li>
<li>follow the <a href="https://www.eclipse.org/hono/docs/deployment/helm-based-deployment/">Deployment Guide</a> in order to install Hono to your local Minikube cluster.</li>
</ol>
<p>Once Hono has been deployed to your local cluster, run the following commands to set some environment variables which will be used during the guide</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export REGISTRY_IP<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>kubectl get service eclipse-hono-service-device-registry-ext --output<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;jsonpath={.status.loadBalancer.ingress[0][&#39;hostname&#39;,&#39;ip&#39;]}&#34;</span> -n hono<span style="color:#66d9ef">)</span>
export HTTP_ADAPTER_IP<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>kubectl get service eclipse-hono-adapter-http-vertx --output<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;jsonpath={.status.loadBalancer.ingress[0][&#39;hostname&#39;,&#39;ip&#39;]}&#34;</span> -n hono<span style="color:#66d9ef">)</span>
export MQTT_ADAPTER_IP<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>kubectl get service eclipse-hono-adapter-mqtt-vertx --output<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;jsonpath={.status.loadBalancer.ingress[0][&#39;hostname&#39;,&#39;ip&#39;]}&#34;</span> -n hono<span style="color:#66d9ef">)</span>
</code></pre></div><p>Verify the last step with</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">echo $REGISTRY_IP
</code></pre></div><p>If this does not print an IP address, check that <code>minikube tunnel</code> is running.</p>
<p><a name="overview"></a></p>
<h2 id="overview-of-hono-components">Overview of Hono Components</h2>
<p>Hono consists of a set of microservices which are deployed as Docker containers. The diagram below provides an overview of the containers that are part of the example deployment of Hono on the Sandbox or a local Minikube cluster.</p>
<figure>
<img src="Hono_instance.svg"
alt="The Docker containers representing the services of the example Hono deployment"/> <figcaption>
<h4>Components of the example Hono deployment</h4>
</figcaption>
</figure>
<ul>
<li>Hono Instance
<ul>
<li>An <em>HTTP Adapter</em> instance that exposes Hono&rsquo;s Telemetry and Event APIs as URI resources.</li>
<li>An <em>MQTT Adapter</em> instance that exposes Hono&rsquo;s Telemetry and Event APIs as a generic MQTT topic hierarchy.</li>
<li>An <em>AMQP Adapter</em> instance that exposes Hono&rsquo;s Telemetry and Event APIs as a set of AMQP 1.0 addresses.</li>
<li>A <em>Device Registry</em> instance that manages registration information and issues device registration assertions to protocol adapters.</li>
<li>An <em>Auth Server</em> instance that authenticates Hono components and issues tokens asserting identity and authorities.</li>
</ul>
</li>
<li>AMQP Network
<ul>
<li>An <em>Apache Qpid Dispatch Router</em> instance that downstream applications connect to in order to consume telemetry data and events from devices.</li>
<li>An <em>Apache ActiveMQ Artemis</em> instance serving as the persistence store for events.</li>
</ul>
</li>
<li>Monitoring Infrastructure
<ul>
<li>A <em>Prometheus</em> instance for storing metrics data from services and protocol adapters.</li>
<li>A <em>Grafana</em> instance providing a dash board visualizing the collected metrics data.</li>
</ul>
</li>
</ul>
<p>In the example scenario used in the remainder of this guide, the devices will connect to the HTTP and MQTT adapters in order to publish telemetry data and events.
The devices will be authenticated using information stored in the Device Registry. The data is then forwarded downstream to the example application via the AMQP Messaging Network.</p>
<h2 id="registering-devices">Registering Devices</h2>
<p>When a device tries to connect to one of Hono&rsquo;s protocol adapters, the protocol adapter first tries to authenticate the device using information kept in the Device Registry.
The information maintained in the registry includes the <em>tenant</em> (a logical scope) that the device belongs to, the device&rsquo;s unique <em>identity</em> within the tenant and the <em>credentials</em> used by the device for authentication.</p>
<p>Before a device can connect to Hono and publish any data, the corresponding information needs to be added to the Device Registry.</p>
<h3 id="creating-a-new-tenant">Creating a new Tenant</h3>
<p>Register a tenant using Hono&rsquo;s Device Registry&rsquo;s management HTTP API (a random tenant identifier will be generated):</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">curl -i -X POST http://$REGISTRY_IP:28080/v1/tenants
HTTP/1.1 <span style="color:#ae81ff">201</span> Created
etag: becc93d7-ab0f-48ec-ad26-debdf339cbf4
location: /v1/tenants/85f63e23-1b78-4156-8500-debcbd1a8d35
content-type: application/json; charset<span style="color:#f92672">=</span>utf-8
content-length: <span style="color:#ae81ff">45</span>
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;id&#34;</span>:<span style="color:#e6db74">&#34;85f63e23-1b78-4156-8500-debcbd1a8d35&#34;</span><span style="color:#f92672">}</span>
</code></pre></div><div class="alert alert-notice">
<h4 class="alert-heading"><i class="fas fa-info-circle"></i> Random tenant ID value</h4>
<div><p>You will receive a randomly generated tenantId value. It will probably be different than the value given in this example.
Make sure to export it to an environment variable to make the following steps easier:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export MY_TENANT<span style="color:#f92672">=</span>85f63e23-1b78-4156-8500-debcbd1a8d35
</code></pre></div></div>
</div>
<h3 id="adding-a-device-to-the-tenant">Adding a Device to the Tenant</h3>
<p>Register a device using Hono&rsquo;s Device Registry&rsquo;s management HTTP API (a random device identifier will be assigned):</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">curl -i -X POST http://$REGISTRY_IP:28080/v1/devices/$MY_TENANT
HTTP/1.1 <span style="color:#ae81ff">201</span> Created
etag: 68eab243-3df9-457d-a0ab-b702e57c0c17
location: /v1/devices/85f63e23-1b78-4156-8500-debcbd1a8d35/4412abe2-f219-4099-ae14-b446604ae9c6
content-type: application/json; charset<span style="color:#f92672">=</span>utf-8
content-length: <span style="color:#ae81ff">45</span>
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;id&#34;</span>:<span style="color:#e6db74">&#34;4412abe2-f219-4099-ae14-b446604ae9c6&#34;</span><span style="color:#f92672">}</span>
</code></pre></div><div class="alert alert-notice">
<h4 class="alert-heading"><i class="fas fa-info-circle"></i> Random device ID value</h4>
<div><p>You will receive a randomly generated deviceId value. It will probably be different than the value given in this example.
Make sure to export it to an environment variable to make the following steps easier:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export MY_DEVICE<span style="color:#f92672">=</span>4412abe2-f219-4099-ae14-b446604ae9c6
</code></pre></div></div>
</div>
<h3 id="setting-a-password-for-the-device">Setting a Password for the Device</h3>
<p>Choose a (random) password and register it using Hono&rsquo;s Device Registry&rsquo;s management HTTP API (replace <code>my-pwd</code> with your password):</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export MY_PWD<span style="color:#f92672">=</span>my-pwd
curl -i -X PUT -H <span style="color:#e6db74">&#34;content-type: application/json&#34;</span> --data-binary <span style="color:#e6db74">&#39;[{
</span><span style="color:#e6db74"> &#34;type&#34;: &#34;hashed-password&#34;,
</span><span style="color:#e6db74"> &#34;auth-id&#34;: &#34;&#39;</span>$MY_DEVICE<span style="color:#e6db74">&#39;&#34;,
</span><span style="color:#e6db74"> &#34;secrets&#34;: [{
</span><span style="color:#e6db74"> &#34;pwd-plain&#34;: &#34;&#39;</span>$MY_PWD<span style="color:#e6db74">&#39;&#34;
</span><span style="color:#e6db74"> }]
</span><span style="color:#e6db74">}]&#39;</span> http://$REGISTRY_IP:28080/v1/credentials/$MY_TENANT/$MY_DEVICE
HTTP/1.1 <span style="color:#ae81ff">204</span> Updated
etag: cf91fd4d-7111-4e8a-af68-c703993a8be1
Content-Length: <span style="color:#ae81ff">0</span>
</code></pre></div><p><a name="starting-a-consumer"></a></p>
<h2 id="starting-the-example-application">Starting the example Application</h2>
<p>The telemetry data produced by devices is usually consumed by downstream applications that use it to implement their corresponding business functionality.
In this guide we will use the Hono command line client to simulate such an application.
The client will connect to Hono&rsquo;s north bound <a href="https://www.eclipse.org/hono/docs/api/telemetry/">Telemetry</a> and <a href="https://www.eclipse.org/hono/docs/api/event/">Event API</a>s using the AMQP 1.0 transport protocol, subscribe to all telemetry and event messages and log the messages to the console.</p>
<p>Open a new terminal window and set the <code>AMQP_NETWORK_IP</code> environment variable.
If you are using the Sandbox server:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export AMQP_NETWORK_IP<span style="color:#f92672">=</span>hono.eclipseprojects.io
</code></pre></div><p>Otherwise, if you are using a local Minikube cluster:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export AMQP_NETWORK_IP<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>kubectl get service eclipse-hono-dispatch-router-ext --output<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;jsonpath={.status.loadBalancer.ingress[0][&#39;hostname&#39;,&#39;ip&#39;]}&#34;</span> -n hono<span style="color:#66d9ef">)</span>
</code></pre></div><p>The client can then be started from the command line as follows (make sure to replace <code>my-tenant</code> with your tenant identifier):</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#75715e"># in directory where the hono-cli-*-exec.jar file has been downloaded to</span>
export MY_TENANT<span style="color:#f92672">=</span>my-tenant
java -jar hono-cli-*-exec.jar --hono.client.host<span style="color:#f92672">=</span>$AMQP_NETWORK_IP --hono.client.port<span style="color:#f92672">=</span><span style="color:#ae81ff">15672</span> --hono.client.username<span style="color:#f92672">=</span>consumer@HONO --hono.client.password<span style="color:#f92672">=</span>verysecret --spring.profiles.active<span style="color:#f92672">=</span>receiver --tenant.id<span style="color:#f92672">=</span>$MY_TENANT
</code></pre></div><h2 id="publishing-telemetry-data-to-the-http-adapter">Publishing Telemetry Data to the HTTP Adapter</h2>
<p>Now that the downstream application is running, devices can start publishing telemetry data and events using Hono&rsquo;s protocol adapters.
First, you will simulate a device publishing data to Hono using the HTTP protocol.
Go back to the original terminal and run:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">curl -i -u $MY_DEVICE@$MY_TENANT:$MY_PWD -H <span style="color:#e6db74">&#39;Content-Type: application/json&#39;</span> --data-binary <span style="color:#e6db74">&#39;{&#34;temp&#34;: 5}&#39;</span> http://$HTTP_ADAPTER_IP:8080/telemetry
HTTP/1.1 <span style="color:#ae81ff">202</span> Accepted
Content-Length: <span style="color:#ae81ff">0</span>
</code></pre></div><p>If you have started the downstream application as described above, you should now see the telemetry message being logged to the application&rsquo;s console
in the other terminal. The output should look something like this:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">13:36:49.169 <span style="color:#f92672">[</span>vert.x-eventloop-thread-0<span style="color:#f92672">]</span> INFO org.eclipse.hono.cli.Receiver - received telemetry message <span style="color:#f92672">[</span>device: my-device, content-type: application/json<span style="color:#f92672">]</span>: <span style="color:#f92672">{</span><span style="color:#e6db74">&#34;temp&#34;</span>: 5<span style="color:#f92672">}</span>
13:36:49.170 <span style="color:#f92672">[</span>vert.x-eventloop-thread-0<span style="color:#f92672">]</span> INFO org.eclipse.hono.cli.Receiver - ... with application properties: <span style="color:#f92672">{</span>orig_adapter<span style="color:#f92672">=</span>hono-http, device_id<span style="color:#f92672">=</span>my-device, orig_address<span style="color:#f92672">=</span>/telemetry, JMS_AMQP_CONTENT_TYPE<span style="color:#f92672">=</span>application/json<span style="color:#f92672">}</span>
</code></pre></div><p>You can publish more data simply by re-running the <code>curl</code> command above with arbitrary payload.</p>
<div class="alert alert-notice">
<h4 class="alert-heading"><i class="fas fa-info-circle"></i> Service Unavailable</h4>
<div><p>When you invoke the command above for the first time, you may get the following response:</p>
<pre><code>HTTP/1.1 503 Service Unavailable
Content-Length: 23
Content-Type: text/plain; charset=utf-8
Retry-After: 2
<p>temporarily unavailable
</code></pre><p>This is because the first request to publish data for a given tenant is used as the trigger to establish a tenant specific sender link with
the AMQP 1.0 Messaging Network to forward the data over. However, the HTTP adapter may not receive credits quickly enough for the
request to be served immediately. You can simply ignore this response and re-submit the command.</p></p>
</div>
</div>
<p>If you haven&rsquo;t started the application you will always get <code>503 Resource Unavailable</code> responses because Hono does not
accept any telemetry data from devices if there aren&rsquo;t any consumers connected that are interested in the data. The reason for this is
that Hono <em>never</em> persists Telemetry data and thus it doesn&rsquo;t make any sense to accept and process telemetry data if there is no
consumer to deliver it to.</p>
<p>The HTTP Adapter also supports publishing telemetry messages using <em>at least once</em> delivery semantics. For information on how that works
and additional examples for interacting with Hono via HTTP, please refer to the
<a href="https://www.eclipse.org/hono/docs/user-guide/http-adapter/">HTTP Adapter&rsquo;s User Guide</a>.</p>
<h2 id="publishing-events-to-the-http-adapter">Publishing Events to the HTTP Adapter</h2>
<p>In a similar way you can upload events:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">curl -i -u $MY_DEVICE@$MY_TENANT:$MY_PWD -H <span style="color:#e6db74">&#39;Content-Type: application/json&#39;</span> --data-binary <span style="color:#e6db74">&#39;{&#34;alarm&#34;: &#34;fire&#34;}&#39;</span> http://$HTTP_ADAPTER_IP:8080/event
HTTP/1.1 <span style="color:#ae81ff">202</span> Accepted
Content-Length: <span style="color:#ae81ff">0</span>
</code></pre></div><p>Again, you should see the event being logged to the console of the downstream application.</p>
<h2 id="publishing-telemetry-data-to-the-mqtt-adapter">Publishing Telemetry Data to the MQTT Adapter</h2>
<p>Devices can also publish data to Hono using the MQTT protocol. If you have installed the <code>mosquitto_pub</code> command line client, you
can run the following command to publish arbitrary telemetry data to Hono&rsquo;s MQTT adapter using QoS 0:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">mosquitto_pub -h $MQTT_ADAPTER_IP -u $MY_DEVICE@$MY_TENANT -P $MY_PWD -t telemetry -m <span style="color:#e6db74">&#39;{&#34;temp&#34;: 5}&#39;</span>
</code></pre></div><p>Again, you should now see the telemetry message being logged to console of the downstream application.</p>
<p>The MQTT Adapter also supports publishing telemetry messages using QoS 1. For information on how that works
and additional examples for interacting with Hono via MQTT, please refer to the
<a href="https://www.eclipse.org/hono/docs/user-guide/mqtt-adapter/">MQTT Adapter&rsquo;s User Guide</a>.</p>
<h2 id="publishing-events-to-the-mqtt-adapter">Publishing Events to the MQTT Adapter</h2>
<p>In a similar way you can upload events:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">mosquitto_pub -h $MQTT_ADAPTER_IP -u $MY_DEVICE@$MY_TENANT -P $MY_PWD -t event -q <span style="color:#ae81ff">1</span> -m <span style="color:#e6db74">&#39;{&#34;alarm&#34;: &#34;fire&#34;}&#39;</span>
</code></pre></div><p>Again, you should now see the telemetry message being logged to console of the downstream application.</p>
<div class="alert alert-notice">
<h4 class="alert-heading"><i class="fas fa-info-circle"></i> Congratulations</h4>
<div><p>You have successfully connected a device to Hono and published sensor data for consumption by an application connected to Hono&rsquo;s north bound API.
The application used the AMQP 1.0 protocol to receive messages regardless of the transport protocol used by the device to publish the data.</p>
<p><strong>What to try next?</strong></p>
<ul>
<li>Continue with the next sections to learn how applications can send commands to devices by means of the <a href="https://www.eclipse.org/hono/docs/api/command-and-control/">Command &amp; Control API</a>.</li>
<li>Take a look at some of the metrics collected by Hono&rsquo;s components by opening the Hono dashboard. On the Sandbox server the dashboard is available at <a href="https://hono.eclipseprojects.io:3000">https://hono.eclipseprojects.io:3000</a>. When running a local Minikube cluster, please refer to <a href="https://github.com/eclipse/packages/tree/master/charts/hono#accessing-the-grafana-dashboard">Opening the Dashboard</a> for instructions.</li>
<li>Check out the <a href="https://www.eclipse.org/hono/docs/user-guide/">User Guides</a> to explore more options for devices to connect to Hono using different transport protocols.</li>
<li>Learn more about the managing tenants, devices and credentials using the <a href="https://www.eclipse.org/hono/docs/user-guide/device-registry/">Device Registry&rsquo;s HTTP API</a>.</li>
</ul>
</div>
</div>
<h2 id="advanced-sending-commands-to-a-device">Advanced: Sending Commands to a Device</h2>
<p>The following example will guide you through an advanced feature of Hono. You will see how an application can send a command
to a device and receive a response with the result of processing the command on the device. The communication direction here is exactly the other way round than with telemetry and events.</p>
<p>The following assumes that the steps in the <a href="#prerequisites-for-the-getting-started-guide">Prerequisites for the Getting started Guide</a>
and <a href="#registering-devices">Registering Devices</a> sections above have been completed.
To simulate the device, you can use the Mosquitto tools again while the Hono Command Line Client simulates the application as before.</p>
<h3 id="receiving-a-command">Receiving a Command</h3>
<p>With the <code>mosquitto_sub</code> command you simulate an MQTT device that receives a command.
Create a subscription to the command topic in the terminal for the simulated device (don&rsquo;t forget to set the environment variables <code>MQTT_ADAPTER_IP</code>, <code>MY_TENANT</code> and <code>MY_DEVICE</code>)</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">mosquitto_sub -v -h $MQTT_ADAPTER_IP -u $MY_DEVICE@$MY_TENANT -P $MY_PWD -t command/+/+/req/#
</code></pre></div><p>Now that the device is waiting to receive commands, the application can start sending them.
Start the Command Line Client in the terminal for the application side (don&rsquo;t forget to set the environment variables <code>AMQP_NETWORK_IP</code>, <code>MY_TENANT</code> and <code>MY_DEVICE</code>)</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#75715e"># in directory where the hono-cli-*-exec.jar file has been downloaded to</span>
java -jar hono-cli-*-exec.jar --hono.client.host<span style="color:#f92672">=</span>$AMQP_NETWORK_IP --hono.client.port<span style="color:#f92672">=</span><span style="color:#ae81ff">15672</span> --hono.client.username<span style="color:#f92672">=</span>consumer@HONO --hono.client.password<span style="color:#f92672">=</span>verysecret --tenant.id<span style="color:#f92672">=</span>$MY_TENANT --device.id<span style="color:#f92672">=</span>$MY_DEVICE --spring.profiles.active<span style="color:#f92672">=</span>command
</code></pre></div><p>Note that this time the profile is <code>command</code> instead of <code>receiver</code>, which enables a different mode of the Command Line Client.</p>
<p>The client will prompt you to enter the command&rsquo;s name, the payload to send and the payload&rsquo;s content type.
The example below illustrates how a one-way command to set the volume with a JSON payload is sent to the device.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Enter name of command <span style="color:#66d9ef">for</span> device <span style="color:#f92672">[</span>&lt;DeviceId&gt;<span style="color:#f92672">]</span> in tenant <span style="color:#f92672">[</span>&lt;TenantId&gt;<span style="color:#f92672">]</span> <span style="color:#f92672">(</span>prefix with <span style="color:#e6db74">&#39;ow:&#39;</span> to send one-way command<span style="color:#f92672">)</span>:
ow:setVolume
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Enter command payload:
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;level&#34;</span>: 50<span style="color:#f92672">}</span>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Enter content type:
application/json
INFO org.eclipse.hono.cli.app.Commander - Command sent to device
</code></pre></div><p>In the terminal for the simulated device you should see the received command as follows</p>
<pre><code>command///req//setVolume {&quot;level&quot;: 50}
</code></pre>
<h3 id="sending-a-response-to-a-command">Sending a Response to a Command</h3>
<p>Now that you have sent a one-way command to the device, you may get to know <em>request/response</em> commands where the device sends a response to the application.
A <em>request/response</em> command received from an application contains an identifier that is unique to each new command.
The device must include this identifier in its response so that the application can correctly correlate the response with the request.</p>
<p>If you send a <em>request/response</em> command like this</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Enter name of command <span style="color:#66d9ef">for</span> device <span style="color:#f92672">[</span>&lt;DeviceId&gt;<span style="color:#f92672">]</span> in tenant <span style="color:#f92672">[</span>&lt;TenantId&gt;<span style="color:#f92672">]</span> <span style="color:#f92672">(</span>prefix with <span style="color:#e6db74">&#39;ow:&#39;</span> to send one-way command<span style="color:#f92672">)</span>:
setBrightness
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Enter command payload:
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;brightness&#34;</span>: 87<span style="color:#f92672">}</span>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Enter content type:
application/json
INFO org.eclipse.hono.cli.app.Commander - Command sent to device... <span style="color:#f92672">[</span>waiting <span style="color:#66d9ef">for</span> response <span style="color:#66d9ef">for</span> max. <span style="color:#ae81ff">60</span> seconds<span style="color:#f92672">]</span>
</code></pre></div><p>the application will wait up to 60 seconds for the device&rsquo;s response.</p>
<p>In the terminal for the simulated device you should see the received command that looks like this</p>
<pre><code>command///req/10117f669c12-09ef-416d-88c1-1787f894856d/setBrightness {&quot;brightness&quot;: 87}
</code></pre>
<p>The element between <code>req</code> and <code>setBrightness</code> is the request identifier that must be included in the response.</p>
<p>You can cancel the command <code>mosquitto_sub</code> in the terminal of the device (press the key combination <code>Ctrl + C</code>) to reuse the
configuration with the environment variables for sending the response.
The following example shows how an answer can be sent with MQTT. Note that the actual identifier from the received command must be used.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">export REQ_ID<span style="color:#f92672">=</span>10117f669c12-09ef-416d-88c1-1787f894856d
mosquitto_pub -h $MQTT_ADAPTER_IP -u $MY_DEVICE@$MY_TENANT -P $MY_PWD -t command///res/$REQ_ID/200 -m <span style="color:#e6db74">&#39;{&#34;success&#34;: true}&#39;</span>
</code></pre></div><p>The <code>200</code> at the end of the topic is an HTTP status code that reports the result of processing the command to the application.</p>
<p>If the Command Line Client has successfully received the response in time, it will print it to the console. This looks like this:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">INFO org.eclipse.hono.cli.app.Commander - Received Command response: <span style="color:#f92672">{</span><span style="color:#e6db74">&#34;success&#34;</span>: true<span style="color:#f92672">}</span>
</code></pre></div><p>If the 60 seconds have already expired, an error message is logged.
In this case you can send a new command or restart the Command Line Client with a higher timeout (append <code>--command.timeoutInSeconds=120</code>).</p>
<p>Congratulations. Now you have successfully sent commands to a device and responded to them.
For more information on Command &amp; Control refer to <a href="https://www.eclipse.org/hono/docs/user-guide/http-adapter/#specifying-the-time-a-device-will-wait-for-a-response">Commands using HTTP</a>
and <a href="https://www.eclipse.org/hono/docs/user-guide/mqtt-adapter/#command--control">Commands using MQTT</a>.
The <a href="https://www.eclipse.org/hono/docs/concepts/command-and-control/">Command and Control Concepts</a> page contains sequence diagrams that
explain this in more detail.</p>
</div>
</div>
</div>
</div>
</div>
<footer id="footer">
<div class="container">
<div class="col-md-4 col-sm-6">
<h4>More</h4>
<ul>
<li><a href="https://github.com/eclipse/hono" title="View Source Code on GitHub"><i class='fab fa-github'></i> GitHub Repository</a></li>
<li><a href="https://twitter.com/EclipseHono" title="Follow us on Twitter"><i class='fab fa-twitter'></i> Twitter</a></li>
<li><a href="https://gitter.im/eclipse/hono" title="Chat with us on Gitter"><i class='fab fa-gitter'></i> Chat with us</a></li>
<li><a href='/hono/community/thankyou/'>Thank you</a></li>
</ul>
<hr class="hidden-md hidden-lg hidden-sm">
</div>
<div class="col-md-4 col-sm-6">
<h4>Eclipse Legal</h4>
<ul>
<li><a href="http://www.eclipse.org/legal/privacy.php" target="_blank">Privacy Policy</a></li>
<li><a href="http://www.eclipse.org/legal/termsofuse.php" target="_blank">Terms of Use</a></li>
<li><a href="http://www.eclipse.org/legal/copyright.php" target="_blank">Copyright Agent</a></li>
<li><a href="http://www.eclipse.org/legal/" target="_blank">Legal</a></li>
<li><a href="https://www.eclipse.org/legal/epl-2.0/" target="_blank">License</a></li>
<li><a href="https://eclipse.org/security/" target="_blank">Report a Vulnerability</a></li>
</ul>
<hr class="hidden-md hidden-lg">
</div>
<div class="col-md-4 col-sm-6">
<div class="eclipse-logos">
<div class="pull-left">
<a href="https://www.eclipse.org" target="_blank">
<img src="https://www.eclipse.org/hono/img/eclipse_foundation_logo_wo.svg" id="logo-eclipse-foundation"/>
</a>
</div>
<div class="pull-right">
<a href="https://iot.eclipse.org" target="_blank">
<img src="https://www.eclipse.org/hono/img/iot-logo.png"/>
</a>
</div>
</div>
</div>
</div>
</footer>
<div id="copyright">
<div class="container">
<div class="col-md-12">
<p class="pull-left">&copy; 2019-2021 The Eclipse Hono Project</p>
<p class="pull-right">
Template by <a href="https://bootstrapious.com/p/universal-business-e-commerce-template">Bootstrapious</a>.
Ported to Hugo by <a href="https://github.com/devcows/hugo-universal-theme">DevCows</a>.
</p>
</div>
</div>
</div>
</div>
<script src="//code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/Counter-Up/1.0/jquery.counterup.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-parallax/1.1.3/jquery-parallax.js"></script>
<script src="/hono/js/front.js"></script>
<script src="/hono/js/owl.carousel.min.js"></script>
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5WLCZXC');
</script>
<script src="//www.eclipse.org/eclipse.org-common/themes/solstice/public/javascript/vendor/cookieconsent/default.min.js"></script>
<script src="//iot.eclipse.org/assets/js/eclipsefdn.adopters.js"></script>
<script>
eclipseFdnAdopters.getList({
project_id: "iot.hono",
selector: ".eclipsefdn-adopters",
logo_white: true
});
</script>
</body>
</html>