<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>microbuilder.io</title>
    <description>Open hardware made easier
</description>
    <link>http://microbuilder.io/</link>
    <atom:link href="http://microbuilder.io/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Sun, 10 Jan 2016 12:25:16 +0000</pubDate>
    <lastBuildDate>Sun, 10 Jan 2016 12:25:16 +0000</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>Plotting JSON Data with Chart.js</title>
        <description>&lt;p&gt;&lt;a href=&quot;http://www.chartjs.org/&quot;&gt;Chart.js&lt;/a&gt; is a open-source, light-weight (~11KB)
library based on the HTML5 canvas control. It supports a limited number of
charts, but unlike Google Charts it can be embedded directly in your website
and made available offline.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#basic-usage&quot; id=&quot;markdown-toc-basic-usage&quot;&gt;Basic Usage&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#static-json-data&quot; id=&quot;markdown-toc-static-json-data&quot;&gt;Static JSON Data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#loading-json-data-with-jquery&quot; id=&quot;markdown-toc-loading-json-data-with-jquery&quot;&gt;Loading JSON Data with JQuery&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#line-charts&quot; id=&quot;markdown-toc-line-charts&quot;&gt;Line Charts&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#example&quot; id=&quot;markdown-toc-example&quot;&gt;Example&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#source-code&quot; id=&quot;markdown-toc-source-code&quot;&gt;Source Code&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#global-settings&quot; id=&quot;markdown-toc-global-settings&quot;&gt;Global Settings&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;script src=&quot;/assets/js/jquery-1.12.0.min.js&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/assets/js/Chart.min.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;basic-usage&quot;&gt;Basic Usage&lt;/h2&gt;

&lt;p&gt;To use Chart.js, you will need to include the Chart.js source, add a Canvas
control to your page, and instantiate a new chart passing in a reference to
your dataset:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/assets/js/Chart.min.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/assets/js/jquery-1.12.0.min.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;&amp;lt;!--Optional--&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;myChart&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;740&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;200&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Get the context of the canvas element we want to select&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;myChart&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;2d&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Instantiate a new chart using &amp;#39;data&amp;#39; (defined below)&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myChart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note: For details on defining the default configuration settings for all of your
charts see the &lt;a href=&quot;#global-settings&quot;&gt;Global Settings&lt;/a&gt; section later in this guide.&lt;/p&gt;

&lt;h3 id=&quot;static-json-data&quot;&gt;Static JSON Data&lt;/h3&gt;

&lt;p&gt;You can include a static dataset directly on your page by defining your &lt;code&gt;data&lt;/code&gt;
variable in the script section of your page:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;var data = {
    labels: [&amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;, &amp;quot;March&amp;quot;, &amp;quot;April&amp;quot;, &amp;quot;May&amp;quot;, &amp;quot;June&amp;quot;, &amp;quot;July&amp;quot;],
    datasets: [
      {
        label: &amp;quot;My First dataset&amp;quot;,
        fillColor: &amp;quot;rgba(220,220,220,0.2)&amp;quot;,
        strokeColor: &amp;quot;rgba(220,220,220,1)&amp;quot;,
        pointColor: &amp;quot;rgba(220,220,220,1)&amp;quot;,
        pointStrokeColor: &amp;quot;#fff&amp;quot;,
        pointHighlightFill: &amp;quot;#fff&amp;quot;,
        pointHighlightStroke: &amp;quot;rgba(220,220,220,1)&amp;quot;,
        data: [65, 59, 80, 81, 56, 55, 40]
      },
      {
        label: &amp;quot;My Second dataset&amp;quot;,
        fillColor: &amp;quot;rgba(151,187,205,0.2)&amp;quot;,
        strokeColor: &amp;quot;rgba(151,187,205,1)&amp;quot;,
        pointColor: &amp;quot;rgba(151,187,205,1)&amp;quot;,
        pointStrokeColor: &amp;quot;#fff&amp;quot;,
        pointHighlightFill: &amp;quot;#fff&amp;quot;,
        pointHighlightStroke: &amp;quot;rgba(151,187,205,1)&amp;quot;,
        data: [28, 48, 40, 19, 86, 27, 90]
      }
    ]
  };&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will give you the following chart:&lt;/p&gt;

&lt;canvas id=&quot;myChart&quot; width=&quot;740&quot; height=&quot;200&quot;&gt;&lt;/canvas&gt;

&lt;script&gt;
  var data = {
    labels: [&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;],
    datasets: [
      {
        label: &quot;My First dataset&quot;,
        fillColor: &quot;rgba(220,220,220,0.2)&quot;,
        strokeColor: &quot;rgba(220,220,220,1)&quot;,
        pointColor: &quot;rgba(220,220,220,1)&quot;,
        pointStrokeColor: &quot;#fff&quot;,
        pointHighlightFill: &quot;#fff&quot;,
        pointHighlightStroke: &quot;rgba(220,220,220,1)&quot;,
        data: [65, 59, 80, 81, 56, 55, 40]
      },
      {
        label: &quot;My Second dataset&quot;,
        fillColor: &quot;rgba(151,187,205,0.2)&quot;,
        strokeColor: &quot;rgba(151,187,205,1)&quot;,
        pointColor: &quot;rgba(151,187,205,1)&quot;,
        pointStrokeColor: &quot;#fff&quot;,
        pointHighlightFill: &quot;#fff&quot;,
        pointHighlightStroke: &quot;rgba(151,187,205,1)&quot;,
        data: [28, 48, 40, 19, 86, 27, 90]
      }
    ]
  };

  // Get the context of the canvas element we want to select
  var ctx = document.getElementById(&quot;myChart&quot;).getContext(&quot;2d&quot;);

  // Instantiate a new chart using &#39;data&#39;
  var myChart = new Chart(ctx).Line(data);
&lt;/script&gt;

&lt;h3 id=&quot;loading-json-data-with-jquery&quot;&gt;Loading JSON Data with JQuery&lt;/h3&gt;

&lt;p&gt;jQuery can also be used to load JSON data from a URL, but in most cases the
raw JSON data will also need to be converted to a format that chart.js
understands.&lt;/p&gt;

&lt;p&gt;The code below will use jQuery to load JSON data from a URL and separate it
into two arrays (&lt;code&gt;labels&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt;), and then insert that data into an object
chart.js understands (&lt;code&gt;tempData&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;drawLineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Add a helper to format timestamp data&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;formatMMDDYYYY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getMonth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getFullYear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jsonData&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ajax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;http://d.microbuilder.io:8080/test/temp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;dataType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;json&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Split timestamp and data into separate arrays&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;packets&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;formatMMDDYYYY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;parseFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;payloadString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Create the chart.js data structure using &amp;#39;labels&amp;#39; and &amp;#39;data&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tempData&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;datasets&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;fillColor&lt;/span&gt;             &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,0.2)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;strokeColor&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;pointColor&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;pointStrokeColor&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;#fff&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;pointHighlightFill&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;#fff&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;pointHighlightStroke&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;                  &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Get the context of the canvas element we want to select&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;myLineChart&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;2d&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Instantiate a new chart&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myLineChart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tempData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;drawLineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;line-charts&quot;&gt;Line Charts&lt;/h2&gt;

&lt;h3 id=&quot;example&quot;&gt;Example&lt;/h3&gt;

&lt;p&gt;A sample chart can be seen below based on &lt;a href=&quot;http://d.microbuilder.io:8080/test/temp&quot;&gt;temperature data&lt;/a&gt;
logged from MQTT publications to the &lt;code&gt;/test/temp&lt;/code&gt; topic. Data was published to
this topic via the following command (using &lt;a href=&quot;https://nodejs.org/en/&quot;&gt;node&lt;/a&gt; and
the mqtt package installed via &lt;code&gt;sudo npm install -g mqtt&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;$ mqtt pub -h d.microbuilder.io -t test/temp -m &amp;#39;17.8&amp;#39;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The MQTT broker at d.microbuilder.io is shown as an example and is
not publicly accessible, although the JSON records are publicly available.&lt;/p&gt;

&lt;canvas id=&quot;myLineChart&quot; width=&quot;740&quot; height=&quot;200&quot;&gt;&lt;/canvas&gt;

&lt;script&gt;
  function drawLineChart() {

    // Add a helper to format timestamp data
    Date.prototype.formatMMDDYYYY = function() {
        return (this.getMonth() + 1) +
        &quot;/&quot; +  this.getDate() +
        &quot;/&quot; +  this.getFullYear();
    }

    var jsonData = $.ajax({
      url: &#39;http://d.microbuilder.io:8080/test/temp&#39;,
      dataType: &#39;json&#39;,
    }).done(function (results) {

      // Split timestamp and data into separate arrays
      var labels = [], data=[];
      results[&quot;packets&quot;].forEach(function(packet) {
        labels.push(new Date(packet.timestamp).formatMMDDYYYY());
        data.push(parseFloat(packet.payloadString));
      });

      // Create the chart.js data structure using &#39;labels&#39; and &#39;data&#39;
      var tempData = {
        labels : labels,
        datasets : [{
            fillColor             : &quot;rgba(151,187,205,0.2)&quot;,
            strokeColor           : &quot;rgba(151,187,205,1)&quot;,
            pointColor            : &quot;rgba(151,187,205,1)&quot;,
            pointStrokeColor      : &quot;#fff&quot;,
            pointHighlightFill    : &quot;#fff&quot;,
            pointHighlightStroke  : &quot;rgba(151,187,205,1)&quot;,
            data                  : data
        }]
      };

      // Get the context of the canvas element we want to select
      var ctx = document.getElementById(&quot;myLineChart&quot;).getContext(&quot;2d&quot;);

      // Instantiate a new chart
      var myLineChart = new Chart(ctx).Line(tempData, {
        //bezierCurve: false
      });
    });
  }

  drawLineChart();
&lt;/script&gt;

&lt;h3 id=&quot;source-code&quot;&gt;Source Code&lt;/h3&gt;

&lt;p&gt;The Javascript code required to read JSON data from a specific URL is shown
below.  Please note that this code will attempt to convert the &lt;code&gt;.payloadString&lt;/code&gt;
field in the JSON record to a float value, so any string based payloads will
fail to parse.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/assets/js/jquery-1.12.0.min.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/assets/js/Chart.min.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;myLineChart&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;740&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;200&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;drawLineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Add a helper to format timestamp data&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;formatMMDDYYYY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getMonth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getFullYear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jsonData&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ajax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;http://d.microbuilder.io:8080/test/temp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;dataType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;json&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Split timestamp and data into separate arrays&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;packets&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;formatMMDDYYYY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;parseFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;payloadString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Create the chart.js data structure using &amp;#39;labels&amp;#39; and &amp;#39;data&amp;#39;&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tempData&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;datasets&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;fillColor&lt;/span&gt;             &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,0.2)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;strokeColor&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;pointColor&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;pointStrokeColor&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;#fff&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;pointHighlightFill&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;#fff&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;pointHighlightStroke&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;rgba(151,187,205,1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;                  &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Get the context of the canvas element we want to select&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;myLineChart&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;2d&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Instantiate a new chart&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;myLineChart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tempData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//bezierCurve: false&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;drawLineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;global-settings&quot;&gt;Global Settings&lt;/h2&gt;

&lt;p&gt;Chart.js uses a set of &lt;strong&gt;global settings&lt;/strong&gt; that determine how every chart will be
rendered. If you wish to change any of settings from the library defaults
you should include the global settings section below in your script:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;// Set the global Chart values (these will apply to all charts)
Chart.defaults.global = {
  // Boolean - Whether to animate the chart
  animation: true,

  // Number - Number of animation steps
  animationSteps: 60,

  // String - Animation easing effect
  // Possible effects are:
  // [easeInOutQuart, linear, easeOutBounce, easeInBack, easeInOutQuad,
  //  easeOutQuart, easeOutQuad, easeInOutBounce, easeOutSine, easeInOutCubic,
  //  easeInExpo, easeInOutBack, easeInCirc, easeInOutElastic, easeOutBack,
  //  easeInQuad, easeInOutExpo, easeInQuart, easeOutQuint, easeInOutCirc,
  //  easeInSine, easeOutExpo, easeOutCirc, easeOutCubic, easeInQuint,
  //  easeInElastic, easeInOutSine, easeInOutQuint, easeInBounce,
  //  easeOutElastic, easeInCubic]
  animationEasing: &amp;quot;easeOutQuart&amp;quot;,

  // Boolean - If we should show the scale at all
  showScale: true,

  // Boolean - If we want to override with a hard coded scale
  scaleOverride: false,

  // ** Required if scaleOverride is true **
  // Number - The number of steps in a hard coded scale
  scaleSteps: null,
  // Number - The value jump in the hard coded scale
  scaleStepWidth: null,
  // Number - The scale starting value
  scaleStartValue: null,

  // String - Colour of the scale line
  scaleLineColor: &amp;quot;rgba(0,0,0,.1)&amp;quot;,

  // Number - Pixel width of the scale line
  scaleLineWidth: 1,

  // Boolean - Whether to show labels on the scale
  scaleShowLabels: true,

  // Interpolated JS string - can access value
  scaleLabel: &amp;quot;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%=value%&amp;gt;&amp;quot;,

  // Boolean - Whether the scale should stick to integers, not floats even if drawing space is there
  scaleIntegersOnly: true,

  // Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
  scaleBeginAtZero: false,

  // String - Scale label font declaration for the scale label
  scaleFontFamily: &amp;quot;&amp;#39;Helvetica Neue&amp;#39;, &amp;#39;Helvetica&amp;#39;, &amp;#39;Arial&amp;#39;, sans-serif&amp;quot;,

  // Number - Scale label font size in pixels
  scaleFontSize: 12,

  // String - Scale label font weight style
  scaleFontStyle: &amp;quot;normal&amp;quot;,

  // String - Scale label font colour
  scaleFontColor: &amp;quot;#666&amp;quot;,

  // Boolean - whether or not the chart should be responsive and resize when the browser does.
  responsive: false,

  // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
  maintainAspectRatio: true,

  // Boolean - Determines whether to draw tooltips on the canvas or not
  showTooltips: true,

  // Function - Determines whether to execute the customTooltips function instead of drawing the built in tooltips (See [Advanced - External Tooltips](#advanced-usage-external-tooltips))
  customTooltips: false,

  // Array - Array of string names to attach tooltip events
  tooltipEvents: [&amp;quot;mousemove&amp;quot;, &amp;quot;touchstart&amp;quot;, &amp;quot;touchmove&amp;quot;],

  // String - Tooltip background colour
  tooltipFillColor: &amp;quot;rgba(0,0,0,0.8)&amp;quot;,

  // String - Tooltip label font declaration for the scale label
  tooltipFontFamily: &amp;quot;&amp;#39;Helvetica Neue&amp;#39;, &amp;#39;Helvetica&amp;#39;, &amp;#39;Arial&amp;#39;, sans-serif&amp;quot;,

  // Number - Tooltip label font size in pixels
  tooltipFontSize: 14,

  // String - Tooltip font weight style
  tooltipFontStyle: &amp;quot;normal&amp;quot;,

  // String - Tooltip label font colour
  tooltipFontColor: &amp;quot;#fff&amp;quot;,

  // String - Tooltip title font declaration for the scale label
  tooltipTitleFontFamily: &amp;quot;&amp;#39;Helvetica Neue&amp;#39;, &amp;#39;Helvetica&amp;#39;, &amp;#39;Arial&amp;#39;, sans-serif&amp;quot;,

  // Number - Tooltip title font size in pixels
  tooltipTitleFontSize: 14,

  // String - Tooltip title font weight style
  tooltipTitleFontStyle: &amp;quot;bold&amp;quot;,

  // String - Tooltip title font colour
  tooltipTitleFontColor: &amp;quot;#fff&amp;quot;,

  // String - Tooltip title template
  tooltipTitleTemplate: &amp;quot;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%= label%&amp;gt;&amp;quot;,

  // Number - pixel width of padding around tooltip text
  tooltipYPadding: 6,

  // Number - pixel width of padding around tooltip text
  tooltipXPadding: 6,

  // Number - Size of the caret on the tooltip
  tooltipCaretSize: 8,

  // Number - Pixel radius of the tooltip border
  tooltipCornerRadius: 6,

  // Number - Pixel offset from point x to tooltip edge
  tooltipXOffset: 10,
  
  // String - Template string for single tooltips
  tooltipTemplate: &amp;quot;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%if (label){%&amp;gt;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%=label%&amp;gt;: &lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%}%&amp;gt;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%= value %&amp;gt;&amp;quot;,
  
  // String - Template string for multiple tooltips
  multiTooltipTemplate: &amp;quot;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%= value %&amp;gt;&amp;quot;,

  // Function - Will fire on animation progression.
  onAnimationProgress: function(){},

  // Function - Will fire on animation completion.
  onAnimationComplete: function(){}
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can override these default values in individual charts during instantiation:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;bezierCurve&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Sun, 10 Jan 2016 21:23:00 +0000</pubDate>
        <link>http://microbuilder.io/blog/2016/01/10/plotting-json-data-with-chart-js.html</link>
        <guid isPermaLink="true">http://microbuilder.io/blog/2016/01/10/plotting-json-data-with-chart-js.html</guid>
        
        
        <category>blog</category>
        
      </item>
    
      <item>
        <title>Plotting JSON Data with Google Charts</title>
        <description>&lt;p&gt;This example shows how to take raw JSON data and plot it using the
&lt;a href=&quot;https://developers.google.com/chart/&quot;&gt;Google Charts&lt;/a&gt; API. See Google’s
&lt;a href=&quot;https://developers.google.com/chart/interactive/docs/gallery&quot;&gt;chart gallery&lt;/a&gt;
for examples of what you can plot based on your JSON data.&lt;/p&gt;

&lt;h2 id=&quot;line-charts&quot;&gt;Line Charts&lt;/h2&gt;

&lt;h3 id=&quot;example&quot;&gt;Example&lt;/h3&gt;

&lt;p&gt;A sample chart can be seen below based on &lt;a href=&quot;http://d.microbuilder.io:8080/test/temp&quot;&gt;temperature data&lt;/a&gt;
logged from MQTT publications to the &lt;code&gt;/test/temp&lt;/code&gt; topic. Data was published to
this topic via the following command (using &lt;a href=&quot;https://nodejs.org/en/&quot;&gt;node&lt;/a&gt; and
the mqtt package installed via &lt;code&gt;sudo npm install -g mqtt&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;$ mqtt pub -h d.microbuilder.io -t test/temp -m &amp;#39;17.8&amp;#39;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The MQTT broker at d.microbuilder.io is shown as an example and is
not publicly accessible, although the JSON records are publicly available.&lt;/p&gt;

&lt;div id=&quot;chart&quot; style=&quot;width: 100%&quot;&gt;&lt;/div&gt;

&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;https://www.google.com/jsapi&quot;&gt;&lt;/script&gt;

&lt;script&gt;

function drawChart() {

  var jsonData = $.ajax({
    url: &#39;http://d.microbuilder.io:8080/test/temp&#39;,
    dataType: &#39;json&#39;,
  }).done(function (results) {

    var data = new google.visualization.DataTable();

    data.addColumn(&#39;datetime&#39;, &#39;Time&#39;);
    data.addColumn(&#39;number&#39;,   &#39;Temp&#39;);

    results[&quot;packets&quot;].forEach(function(packet) {
      data.addRow([
          (new Date(packet.timestamp)),
          parseFloat(packet.payloadString),
        ]);
    });

    var chart = new google.visualization.LineChart($(&#39;#chart&#39;).get(0));

    chart.draw(data, {
      title:  &#39;Ambient Temperature (/test/temp)&#39;,
      height: 150
    });

  });

}

google.load(&#39;visualization&#39;, &#39;1&#39;, {
  packages: [&#39;corechart&#39;]
});

google.setOnLoadCallback(drawChart);

&lt;/script&gt;

&lt;h3 id=&quot;source-code&quot;&gt;Source Code&lt;/h3&gt;

&lt;p&gt;The Javascript code required to read JSON data from a specific URL is shown
below.  Please note that this code will attempt to convert the &lt;code&gt;.payloadString&lt;/code&gt;
field in the JSON record to a float value, so any string based payloads will
fail to parse.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;chart&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;width: 100%&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;https://www.google.com/jsapi&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 3. This function fires when Google Charts has been fully loaded&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;drawChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// 4. Retrieve the raw JSON data&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jsonData&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ajax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;http://d.microbuilder.io:8080/test/temp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;dataType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;json&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 5. Create a new DataTable (Charts expects data in this format)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;visualization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DataTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 6. Add two columns to the DataTable&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;datetime&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Time&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;number&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;s1&quot;&gt;&amp;#39;Temp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 7. Cycle through the records, adding one row per record&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;packets&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;timestamp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
          &lt;span class=&quot;nb&quot;&gt;parseFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;packet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;payloadString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 8. Create a new line chart&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;visualization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#chart&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// 9. Render the chart, passing in our DataTable and any config data&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;s1&quot;&gt;&amp;#39;Ambient Temperature (/test/temp)&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;150&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 1. Start loading Google Charts&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;visualization&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;packages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;corechart&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 2. Set a callback function to fire when loading is complete&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setOnLoadCallback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;drawChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Wed, 30 Dec 2015 05:40:00 +0000</pubDate>
        <link>http://microbuilder.io/blog/2015/12/30/plotting-json-data-google-charts.html</link>
        <guid isPermaLink="true">http://microbuilder.io/blog/2015/12/30/plotting-json-data-google-charts.html</guid>
        
        
        <category>blog</category>
        
      </item>
    
      <item>
        <title>MQTT to JSON HTTP Logger</title>
        <description>&lt;p&gt;This guide will help you setup an MQTT broker and HTTP server that logs all
published MQTT data to a local database, and exposes the data in JSON
format over HTTP. An AWS EC2 instance running Ubuntu Server 14.04 will be used
here, but a Raspberry Pi would also work fine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#what-is-mqtt&quot; id=&quot;markdown-toc-what-is-mqtt&quot;&gt;What is MQTT?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#objectives&quot; id=&quot;markdown-toc-objectives&quot;&gt;Objectives&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#system-requirements&quot; id=&quot;markdown-toc-system-requirements&quot;&gt;System Requirements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mqtt-log-source&quot; id=&quot;markdown-toc-mqtt-log-source&quot;&gt;MQTT-Log Source&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#server-setup&quot; id=&quot;markdown-toc-server-setup&quot;&gt;Server Setup&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#build-tools-gcc-make-etc&quot; id=&quot;markdown-toc-build-tools-gcc-make-etc&quot;&gt;Build Tools (GCC, Make, etc.)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#node&quot; id=&quot;markdown-toc-node&quot;&gt;Node&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pm2-node-process-manager&quot; id=&quot;markdown-toc-pm2-node-process-manager&quot;&gt;PM2 (Node Process Manager)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#git&quot; id=&quot;markdown-toc-git&quot;&gt;Git&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#mqtt-tools-optional&quot; id=&quot;markdown-toc-mqtt-tools-optional&quot;&gt;MQTT Tools (Optional)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#installing-mqtt-log&quot; id=&quot;markdown-toc-installing-mqtt-log&quot;&gt;Installing MQTT-Log&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#starting-mqtt-log-as-a-pm2-process&quot; id=&quot;markdown-toc-starting-mqtt-log-as-a-pm2-process&quot;&gt;Starting MQTT-Log as a PM2 Process&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#setup-pm2&quot; id=&quot;markdown-toc-setup-pm2&quot;&gt;Setup pm2&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#start-mqtt-log-via-pm2&quot; id=&quot;markdown-toc-start-mqtt-log-via-pm2&quot;&gt;Start MQTT-Log via pm2&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#persist-the-mqtt-log-process&quot; id=&quot;markdown-toc-persist-the-mqtt-log-process&quot;&gt;Persist the MQTT-Log process&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sidenote-resolving-eacces-errors&quot; id=&quot;markdown-toc-sidenote-resolving-eacces-errors&quot;&gt;Sidenote: Resolving EACCES Errors&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#managing-the-mqtt-logpm2-process&quot; id=&quot;markdown-toc-managing-the-mqtt-logpm2-process&quot;&gt;Managing the MQTT-Log/PM2 Process&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#updating-the-script&quot; id=&quot;markdown-toc-updating-the-script&quot;&gt;Updating the script&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#stopping-the-process&quot; id=&quot;markdown-toc-stopping-the-process&quot;&gt;Stopping the process&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#check-the-process-status&quot; id=&quot;markdown-toc-check-the-process-status&quot;&gt;Check the process status&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deleting-the-mqtt-database&quot; id=&quot;markdown-toc-deleting-the-mqtt-database&quot;&gt;Deleting the MQTT database&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#checking-the-logs&quot; id=&quot;markdown-toc-checking-the-logs&quot;&gt;Checking the logs&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#panic-reinstalling-pm2&quot; id=&quot;markdown-toc-panic-reinstalling-pm2&quot;&gt;Panic: Reinstalling pm2&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#testing-the-system&quot; id=&quot;markdown-toc-testing-the-system&quot;&gt;Testing the System&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#credits&quot; id=&quot;markdown-toc-credits&quot;&gt;Credits&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;what-is-mqtt&quot;&gt;What is MQTT?&lt;/h2&gt;

&lt;p&gt;If you’re not familiar with &lt;a href=&quot;http://mqtt.org/&quot;&gt;MQTT&lt;/a&gt; it’s a light-weight
&lt;strong&gt;publish-subscribe&lt;/strong&gt; protocol that sits between sensor nodes on one side, and
various data subscribers on the other.&lt;/p&gt;

&lt;p&gt;It allows you to decouple the data provider(s) from the data consumer(s) in a
scalable, flexible manner, and with a minimum of complexity.&lt;/p&gt;

&lt;p&gt;MQTT uses a key-based system, where records are stored and accessed via a key
called a &lt;code&gt;topic&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Topics are hierarchical strings in a similar format as HTTP requests or file
and folder names:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;home/kitchen/fridge/temperature&lt;/li&gt;
  &lt;li&gt;home/kitchen/fridge/luminosity&lt;/li&gt;
  &lt;li&gt;office/secondfloor/kitchen/temperature&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By &lt;strong&gt;publishing&lt;/strong&gt; to a topic, you overwrite the previous value, and any
device that has &lt;strong&gt;subscribed&lt;/strong&gt; to that topic will receive an alert letting it
know that the topic has changed, along with the updated value.&lt;/p&gt;

&lt;p&gt;Each topic can have multiple publishers or subscribers, and new subscribers
can join or leave at any moment.&lt;/p&gt;

&lt;p&gt;Publishers can also be subscribers at the same time, meaning that communication
can happen in both directions using any number of topics and involving any
number of endpoints or sensor nodes.&lt;/p&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;There are a variety of excellent platforms out there that are based on or use
MQTT (&lt;a href=&quot;https://learn.adafruit.com/category/adafruit-io&quot;&gt;Adafruit IO&lt;/a&gt;, etc.).&lt;/p&gt;

&lt;p&gt;If you simply want to setup a quick test platform to capture MQTT publications,
though, and make sure the data is arriving properly, the existing open source
or proprietary services might be more complicated than you want or need.&lt;/p&gt;

&lt;p&gt;This guide tries to provide the easiest possible solution to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Setup an MQTT Broker that can be accessed from anywhere&lt;/li&gt;
  &lt;li&gt;Record all published data in a local database, including historical data&lt;/li&gt;
  &lt;li&gt;Expose all published data for a specific topic in JSON format via HTTP&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;system-requirements&quot;&gt;System Requirements&lt;/h2&gt;

&lt;p&gt;This guide was tested using a clean &lt;strong&gt;t2.medium&lt;/strong&gt; &lt;a href=&quot;https://aws.amazon.com/ec2/&quot;&gt;AWS EC2&lt;/a&gt;
instance running &lt;strong&gt;Ubuntu 14.04 Server&lt;/strong&gt;, although a &lt;strong&gt;Raspberry Pi&lt;/strong&gt; or any
other similar Debian-based computer should also work fine.&lt;/p&gt;

&lt;p&gt;The tutorial assumes that you have shell access, that the server has internet
access, and that you have already opened up the following ports (if you plan to
access the system remotely):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1883 - MQTT access&lt;/li&gt;
  &lt;li&gt;8080 - HTTP requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Details aren’t provided here on configuring the Security Group for incoming
and outgoing traffic, or the DNS setup if you want to redirect a domain or
sub-domain to your server. Google should be able to help you there.&lt;/p&gt;

&lt;h2 id=&quot;mqtt-log-source&quot;&gt;MQTT-Log Source&lt;/h2&gt;

&lt;p&gt;The node.js utility that runs the MQTT broker (&lt;a href=&quot;https://github.com/mcollina/aedes&quot;&gt;aedes&lt;/a&gt;),
writes the code to the database (&lt;a href=&quot;https://github.com/louischatriot/nedb&quot;&gt;nedb&lt;/a&gt;),
opens the HTTP Server and supplies the JSON data is shown below is available
on Github.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Latest Source&lt;/strong&gt;: &lt;a href=&quot;https://github.com/adafruit/mqtt-log&quot;&gt;https://github.com/adafruit/mqtt-log&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code below is purely for illustration purposes and may be out of date!
Always pull from the git repo for the latest updates.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/5c23f578df567d78112c.js&quot;&gt; &lt;/script&gt;

&lt;h2 id=&quot;server-setup&quot;&gt;Server Setup&lt;/h2&gt;

&lt;p&gt;To setup mqtt-log on a clean system, the following components are required or
are highly recommended:&lt;/p&gt;

&lt;p&gt;You may not be missing these on your own system, but they aren’t installed on
a clean AWS instance running Ubuntu Server 14.04:&lt;/p&gt;

&lt;h3 id=&quot;build-tools-gcc-make-etc&quot;&gt;Build Tools (GCC, Make, etc.)&lt;/h3&gt;

&lt;p&gt;Install GCC and various build tools that you will need:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;sudo apt-get install -y build-essentials&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;node&quot;&gt;Node&lt;/h3&gt;

&lt;p&gt;Install node via the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl -sL https://deb.nodesource.com/setup_4.x &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; sudo -E bash -
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;sudo apt-get install -y nodejs&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;pm2-node-process-manager&quot;&gt;PM2 (Node Process Manager)&lt;/h3&gt;

&lt;p&gt;Install PM2, which is a helpful utility that can take node scripts and keep
the service running as a daemon, restarting the service if it goes down, etc.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;sudo npm install -g pm2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;git&quot;&gt;Git&lt;/h3&gt;

&lt;p&gt;Install git to be able to easily pull the latest source code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;sudo apt-get install git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;mqtt-tools-optional&quot;&gt;MQTT Tools (Optional)&lt;/h3&gt;

&lt;p&gt;To publish data to an MQTT broker from the command line the following Packages
is very useful:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;sudo npm install -g mqtt&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;installing-mqtt-log&quot;&gt;Installing MQTT-Log&lt;/h3&gt;

&lt;p&gt;Clone the latest copy of the mqtt-log repo via git:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/adafruit/mqtt-log.git&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then setup mqtt-log and any of it’s dependencies (aedes, nedb, etc.):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;mqtt-log
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;npm install&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;starting-mqtt-log-as-a-pm2-process&quot;&gt;Starting MQTT-Log as a PM2 Process&lt;/h2&gt;

&lt;p&gt;Now that mqtt-log is installed, we need to run it as a service via
&lt;a href=&quot;https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md&quot;&gt;pm2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;pm2 allows us to automatically start the service at boot, and restart the
service if it goes down. It hides the setup details, and exposes an easy to
use &lt;a href=&quot;https://github.com/Unitech/pm2/blob/master/README.md#commands-overview&quot;&gt;management system&lt;/a&gt;
for the processes it controls.&lt;/p&gt;

&lt;p&gt;You normally &lt;strong&gt;only ever need to run through this process once!&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;setup-pm2&quot;&gt;Setup pm2&lt;/h3&gt;

&lt;p&gt;Before you can use pm2 you need to configure your system to load pm2 at startup and install some config files.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;sudo su -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;env PATH=$PATH:/usr/bin pm2 startup ubuntu -u ubuntu --hp /home/ubuntu&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This should give you:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Spawning PM2 daemon
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; PM2 Successfully daemonized
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Generating system init script in /etc/init.d/pm2-init.sh
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Making script booting at startup...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; -ubuntu- Using the &lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;:
      su -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;chmod +x /etc/init.d/pm2-init.sh &amp;amp;&amp;amp; update-rc.d pm2-init.sh defaults&amp;quot;&lt;/span&gt;
 System start/stop links &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; /etc/init.d/pm2-init.sh already exist.
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Done.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;start-mqtt-log-via-pm2&quot;&gt;Start MQTT-Log via pm2&lt;/h3&gt;

&lt;p&gt;With pm2 up and setup on your system, you can start mqtt-log (&lt;code&gt;index.js&lt;/code&gt;) as a pm2 managed process with:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 start index.js&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This should give you the following results:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Starting index.js in fork_mode &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; instance&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;PM2&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ index    │ &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;  │ fork │ &lt;span class=&quot;m&quot;&gt;1353&lt;/span&gt; │ online │ &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;       │ 0s     │ 13.246 MB   │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
 Use &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;pm2 show &amp;lt;id&lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;name&amp;gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; to get more details about an app&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;persist-the-mqtt-log-process&quot;&gt;Persist the MQTT-Log process&lt;/h3&gt;

&lt;p&gt;Once the process is running (and not before!) you need to &lt;code&gt;save&lt;/code&gt; it, which means
that pm2 will restart anything running on the host when the save command was
sent:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 save&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;sidenote-resolving-eacces-errors&quot;&gt;Sidenote: Resolving EACCES Errors&lt;/h3&gt;

&lt;p&gt;You may get &lt;strong&gt;EACCES&lt;/strong&gt; errors in one of the above steps or after reboot, such
as:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;events.js:141
      throw er&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; // Unhandled &lt;span class=&quot;s1&quot;&gt;&amp;#39;error&amp;#39;&lt;/span&gt; event
      ^

Error: connect EACCES /home/ubuntu/.pm2/rpc.sock
    at Object.exports._errnoException &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;util.js:870:11&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    at exports._exceptionWithHostPort &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;util.js:893:20&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    at PipeConnectWrap.afterConnect &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;as oncomplete&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;net.js:1063:14&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is typically because you &lt;strong&gt;started pm2 via sudo&lt;/strong&gt; (&lt;code&gt;sudo pm2 start ubuntu&lt;/code&gt;),
though there may be other issues with user account configuration with pm2 and
Ubuntu 14.04 on EWS as well. See for example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Unitech/PM2/issues/1100&quot;&gt;PM2 Issue #1100&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Unitech/PM2/issues/1156&quot;&gt;PM2 Issue #1156&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Unitech/PM2/issues/1222&quot;&gt;PM2 Issue #1222&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you see these errors or if pm2 isn’t persisting across system resets check
the owner of the &lt;code&gt;~/.pm2&lt;/code&gt; folder via:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ls -la ~/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will show you who owns the ~/.pm2 folder (the first user is the owner, the
second is the group):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;drwxr-xr-x   &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt; ubuntu root    &lt;span class=&quot;m&quot;&gt;4096&lt;/span&gt; Dec &lt;span class=&quot;m&quot;&gt;29&lt;/span&gt; 16:10 .pm2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If the folder is owned by &lt;code&gt;root&lt;/code&gt;, you can change the access rights to the .pm2
folder to give the &lt;code&gt;ubuntu&lt;/code&gt; user control of it again via:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sudo chown -R ubuntu ~/.pm2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You may need to remove the pm2 config files and set things up again:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sudo rm -rf ~/.pm2
sudo rm -rf /etc/init.d/pm2-init.sh
sudo su -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;env PATH=$PATH:/usr/bin pm2 startup ubuntu -u ubuntu --hp /home/ubuntu&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;managing-the-mqtt-logpm2-process&quot;&gt;Managing the MQTT-Log/PM2 Process&lt;/h2&gt;

&lt;h3 id=&quot;updating-the-script&quot;&gt;Updating the script&lt;/h3&gt;

&lt;p&gt;If you make any changes to the mqtt-log script, you should restart pm2 with the
changes as follows.&lt;/p&gt;

&lt;p&gt;For example, to (optionally) pull the latest version of the source:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git pull&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then restart the process so that any changes take effect:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 restart all&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;stopping-the-process&quot;&gt;Stopping the process&lt;/h3&gt;

&lt;p&gt;To stop the service you can run the following command:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 stop all&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you no longer want to run the script at startup run:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 delete all
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 update&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;check-the-process-status&quot;&gt;Check the process status&lt;/h3&gt;

&lt;p&gt;You can check the current state of pm2 managed processes via:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 list&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also look at utilities like &lt;a href=&quot;http://hisham.hm/htop/&quot;&gt;htop&lt;/a&gt; to see the
entire system status, including pm2 managed processes.&lt;/p&gt;

&lt;h3 id=&quot;deleting-the-mqtt-database&quot;&gt;Deleting the MQTT database&lt;/h3&gt;

&lt;p&gt;If you want to delete all of the historical MQTT data, you can simply kill the
nedb database and restart with the following commands:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 stop all
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;rm mqtt.db
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pm2 start all&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;checking-the-logs&quot;&gt;Checking the logs&lt;/h3&gt;

&lt;p&gt;If you have any issues with the pm2 process, you can check the pm2 log via:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;pm2 logs&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Press &lt;strong&gt;CTRL+C&lt;/strong&gt; to exit the log browser.&lt;/p&gt;

&lt;h3 id=&quot;panic-reinstalling-pm2&quot;&gt;Panic: Reinstalling pm2&lt;/h3&gt;

&lt;p&gt;If you &lt;strong&gt;really&lt;/strong&gt; get stuck, you can kill and reinstall pm2 via:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sudo npm uninstall -g pm2
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/.npm/ &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; sudo rm -rf pm2*
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~ &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; sudo rm -rf .pm2
sudo npm install pm2 -g&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then run through the setup process again.&lt;/p&gt;

&lt;h2 id=&quot;testing-the-system&quot;&gt;Testing the System&lt;/h2&gt;

&lt;p&gt;At this point, you should have an MQTT broker running on port 1883 and a simple
HTTP server listening on port 8080, which will display raw JSON records for all
data published to the requested topic.&lt;/p&gt;

&lt;p&gt;To test the system, you can publish a record to the MQTT broker from the
command-line with the following command (changing &lt;code&gt;example.com&lt;/code&gt; to your own
domain name or IP address):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;mqtt pub -h example.com -t &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;/topic -m &lt;span class=&quot;s1&quot;&gt;&amp;#39;Glorious Aedes!&amp;#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you point your browser to &lt;code&gt;http://example.com:8080/test/topic&lt;/code&gt; you should get:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;count&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&amp;quot;packets&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;cmd&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;publish&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;brokerId&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;NJ5yvRoIx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;brokerCounter&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;topic&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test/topic&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;payload&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;{\&amp;quot;type\&amp;quot;:\&amp;quot;Buffer\&amp;quot;,\&amp;quot;data\&amp;quot;:[71,108,111,114,105,111,117,115,32,65,101,100,101,115,33]}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;qos&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;retain&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;messageId&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;payloadString&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Glorious Aedes!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;payloadLength&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;timestamp&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;2015-12-29T20:07:15.769Z&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&amp;quot;_id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;LxqeAK00mXiB5peU&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also see a log of all MQTT events by browsing to the root folder, which
would be something like http://example.com:8080/&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Tue Dec &lt;span class=&quot;m&quot;&gt;29&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 18:37:51 GMT+0000 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;UTC&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  disconnect   mqttjs_b5655c0c
Tue Dec &lt;span class=&quot;m&quot;&gt;29&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 18:37:51 GMT+0000 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;UTC&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  publish   mqttjs_b5655c0c &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;/topic
Tue Dec &lt;span class=&quot;m&quot;&gt;29&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2015&lt;/span&gt; 18:37:51 GMT+0000 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;UTC&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  connect   mqttjs_b5655c0c&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits&lt;/h2&gt;

&lt;p&gt;A huge thanks to &lt;a href=&quot;https://twitter.com/uniontownlabs&quot;&gt;Uniontown Labs&lt;/a&gt;
for the node source code, and help getting a simple test server up and
running. Icons courtesy &lt;a href=&quot;https://www.iconfinder.com/iconsets/technology-and-hardware-2&quot;&gt;Maxim Basinski&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Tue, 29 Dec 2015 05:40:00 +0000</pubDate>
        <link>http://microbuilder.io/blog/2015/12/29/mqtt-to-json-http-logger.html</link>
        <guid isPermaLink="true">http://microbuilder.io/blog/2015/12/29/mqtt-to-json-http-logger.html</guid>
        
        
        <category>blog</category>
        
      </item>
    
  </channel>
</rss>
