<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luca Cipriani</title>
    <description>The latest articles on DEV Community by Luca Cipriani (@mastrolinux).</description>
    <link>https://dev.to/mastrolinux</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F321438%2F758a7d9c-a12f-4397-9e0f-536954371447.jpg</url>
      <title>DEV Community: Luca Cipriani</title>
      <link>https://dev.to/mastrolinux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mastrolinux"/>
    <language>en</language>
    <item>
      <title>Using Arduino's New Oplà Kit to Read Remote Sensors and Display the Data in Your Home</title>
      <dc:creator>Luca Cipriani</dc:creator>
      <pubDate>Wed, 19 May 2021 12:26:10 +0000</pubDate>
      <link>https://dev.to/mastrolinux/using-arduino-s-new-opla-kit-to-read-remote-sensors-and-display-the-data-in-your-home-116i</link>
      <guid>https://dev.to/mastrolinux/using-arduino-s-new-opla-kit-to-read-remote-sensors-and-display-the-data-in-your-home-116i</guid>
      <description>&lt;p&gt;(part one is &lt;a href="https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-1-4o2l"&gt;here&lt;/a&gt; and part two is &lt;a href="https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-2-4ckc"&gt;here&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;A tutorial using the Oplà Kit to have 2 different IoT devices communicate with each other. This is the third part of the articles about IoT, Heroku and Arduino. &lt;/p&gt;

&lt;p&gt;Recently, I received the new &lt;a href="https://store.arduino.cc/opla-iot-kit"&gt;Arduino Oplà Kit&lt;/a&gt; for testing. To try it out, I decided to create an IoT project with multiple environmental sensors that remotely collect data, then report that data over the cloud and back to my home IoT device. It sounds complicated, but the setup was easy, and it allows for many advanced customizations in the future.&lt;/p&gt;

&lt;p&gt;In this article, I'll walk through how simple it is to create a similar setup—the architecture, devices, and code. Where you take it from there is up to you! &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Project&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To get started, I created a straightforward project to capture the outside temperature, display it on a device in my home, and compare it to the inside temperature. I also wanted to make sure I created something customizable and powerful, where I could add my own logic to allow for more complicated projects in the future.  &lt;/p&gt;

&lt;p&gt;Note: For a quick background on IoT terms (Thing, Variable, Sketch, and so on), &lt;a href="https://www.arduino.cc/en/IoT/HomePage"&gt;check out this overview&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Our Architecture&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The architecture is simple but very powerful. We will leverage the Arduino IoT Cloud REST and Realtime API to allow two different devices to communicate with each other. In the middle, we'll host a custom Node.js application on&lt;a href="https://www.heroku.com"&gt; Heroku&lt;/a&gt;. We'll also use the code my friend Francesco Stasi wrote to help us move the data. The code is entirely open source, with an MIT license, and is available at my&lt;a href="https://github.com/mastrolinux/js-iot-proxy"&gt; GitHub repo&lt;/a&gt;. Francesco and I will keep working on the code, if there is interest.&lt;/p&gt;

&lt;p&gt;The architecture looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--usd6iXtE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfs796cl9hmf5f3zq2d9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--usd6iXtE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfs796cl9hmf5f3zq2d9.png" alt="Alt Text"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Our first Thing (Thing 1) is connected via Wi-Fi. It reads data (such as temperature) from the environment and sends the data to the Arduino IoT Cloud. By creating our own Node.js application hosted on Heroku, we are able to get the data from Arduino IoT in near real-time by using the MQTT/WebSockets API. &lt;/p&gt;

&lt;p&gt;Once we have the data streaming to our application, we can manipulate the incoming information as needed. For simplicity, we'll simply republish the same values from Thing 1 on Thing 2. (Note: You can only do this if Thing 1 and Thing 2 have identical property names.) &lt;/p&gt;

&lt;p&gt;This is all simpler than it sounds. Let’s see how to build it!&lt;/p&gt;

&lt;p&gt;What we need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  1x Oplà Kit (The kit contains 1x MKR1010 and one year of free service for Arduino IoT.)&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://create.arduino.cc/iot"&gt;Arduino IoT Cloud account&lt;/a&gt; (included in the Oplà Kit)&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://heroku.com"&gt;Heroku account&lt;/a&gt; (the free hobby account should be fine)&lt;/li&gt;
&lt;li&gt;  1x&lt;a href="https://store.arduino.cc/usa/mkr-wifi-1010"&gt; Arduino MKR1010&lt;/a&gt; (a basic Arduino board with Wi-Fi, included in the Oplà Kit)&lt;/li&gt;
&lt;li&gt;  1x&lt;a href="https://store.arduino.cc/usa/mkr-env-shield"&gt; MKR ENV Shield&lt;/a&gt; (environmental sensors)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Creating Thing 1: the External Sensor&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;First, open the Arduino IoT cloud dashboard and create an Outdoor sensor Thing with just one variable named "temperature."&lt;/p&gt;

&lt;p&gt;As you can see in the image below, the type of the variable is “Temperature sensor (°c).” In the sketch, it is represented as a CloudTemperatureSensor. The property does not need to be set; it is just a sensor, not an actuator, so we do not need any kind of user interaction. To avoid sending too much data to the cloud, I set a threshold of 0.2 C.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yoQEg9Kk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7e1ynpzt0m3sda6d50w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yoQEg9Kk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7e1ynpzt0m3sda6d50w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, I created a new device with my MKR Wi-Fi 1010 and attached it to this Thing.&lt;/p&gt;

&lt;p&gt;The whole setup looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3GFPV4MM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ioxpwbjf2zxx5ktjscit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3GFPV4MM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ioxpwbjf2zxx5ktjscit.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember to add the network parameters before uploading your sketch. If you don't, you will get an error message. &lt;/p&gt;

&lt;p&gt;Next, I added the MKR ENV Shield on top of the MKR Wi-Fi 1010 and created a simple sketch to send the temperature online. Here is the code of the whole&lt;a href="https://create.arduino.cc/editor/mastrolinux/e988e61f-6cb2-4449-882a-f684a5512e98"&gt; Sensors Sketch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Besides the print to the serial, there are really only two other important lines: &lt;strong&gt;ArduinoCloud.update()&lt;/strong&gt; to send the data to the Arduino IoT Cloud, and the &lt;strong&gt;ENV.readTemperature()&lt;/strong&gt; method, which allows us to measure the temperature and put it in a variable. Then, that variable will be sent to the cloud.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void loop() {
  ArduinoCloud.update();
  // Your code here 
  temperature = ENV.readTemperature();
  delay(200);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After uploading your sketch to the board, you should see the following in the serial monitor:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e12HI6Rr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nxk44z6f4qxncdaeppj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e12HI6Rr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nxk44z6f4qxncdaeppj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are registering the temperature every 200ms but only sending it to the cloud if it changes at least 0.2 Celsius. Everything else is recorded and visible locally, though not sent to our cloud application. Feel free to improve the sketch and add your own remote properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The In-house Device to Visualize Data&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The whole project came to my mind because I just received the new&lt;a href="https://store.arduino.cc/opla-iot-kit"&gt; Arduino Oplà Kit&lt;/a&gt; for testing. The kit includes many sensors (more or less the same as the MKR ENV Shield), five LEDs, and five capacitive buttons (touch sensors). More importantly, it has a useful battery-powered screen to display data. To power the&lt;a href="https://store.arduino.cc/usa/opla-iot-kit"&gt; Oplà&lt;/a&gt; Kit, I bought some&lt;a href="https://it.rs-online.com/web/p/batterie-a-dimensioni-speciali-ricaricabili/8801551/?cm_mmc=IT-PLA-DS3A-_-google-_-PLA_IT_IT_Batterie_e_caricabatterie_Whoop-_-(IT:Whoop%21)+Batterie+a+dimensioni+speciali+ricaricabili-_-8801551&amp;amp;matchtype=&amp;amp;pla-339269539139&amp;amp;gclid=CjwKCAiA2O39BRBjEiwApB2IkqTedp8fVxDnl1gvK_9C6HQ7H5FPny7Fol_JNyWGgJkEKf9oUR6M6RoC9K8QAvD_BwE&amp;amp;gclsrc=aw.ds"&gt; Samsung 18650 batteries from RS Components&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GMgUkw7---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzgbj05pxhr4hqnnxnsu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GMgUkw7---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzgbj05pxhr4hqnnxnsu.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because we have such a powerful kit, I decided to create three different visualizations on the screen.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Button 1&lt;/strong&gt; displays the indoor temperature (&lt;strong&gt;internalTemperature&lt;/strong&gt; property) of the Oplà Kit.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Button 2&lt;/strong&gt; displays the outdoor temperature from the MKR Wi-Fi 1010 with the ENV Shield (what I called the Outdoor Board device and the Outdoor Sensors Thing).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Button 3&lt;/strong&gt; shows the difference between the internal and external temperatures.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;LED 1&lt;/strong&gt;: the LED turns blue if the difference is negative (outside is colder than inside), and red otherwise.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Setting Up the Kit in Arduino IoT Cloud&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To set up the kit, first be sure to unplug your board; otherwise, it will not work. Then, configure your MKR1010. I named it CarrierBoard as seen in the image here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K88Bvgi6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ck2doz6g6pucmzy7sl9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K88Bvgi6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ck2doz6g6pucmzy7sl9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After setting up the board, I created a new Thing named OplaKit, then I added two different properties: internalTemperature and temperature. The other board fetches the temperature property, while the internalTemperature comes from the Oplà Kit itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4dK_66wA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb7hrd2uk29cg756utuu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4dK_66wA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb7hrd2uk29cg756utuu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The internal Temperature is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Type: Temperature&lt;/li&gt;
&lt;li&gt;  User interaction: Disabled (read only)&lt;/li&gt;
&lt;li&gt;  Send Values: On Change (threshold: 0.1 C)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3HEHsNR0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/su288l3hlegxuejqlzy3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3HEHsNR0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/su288l3hlegxuejqlzy3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that the temperature property is exactly the same as the property in the Outdoor Sensors Thing.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating the Sketch and Adding Interaction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The sketch running in the Oplà Kit seems like magic, but everything is extremely simple. We need to detect when a button is pressed, and change the values accordingly on the screen.&lt;/p&gt;

&lt;p&gt;First, we need to add the proper libraries to the sketch to control the LEDs and touch buttons.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Arduino_MKRIoTCarrier - Version: Latest 
#include &amp;amp;lt;Arduino_MKRIoTCarrier.h&amp;gt;
#include &amp;amp;lt;Arduino_MKRIoTCarrier_Qtouch.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we need to include the cloud library (automatically done) and tell the system if we are using the case or not. (It will calibrate the sensitivity of the touch buttons.) Then, I set a variable changeDisplayCounter to switch between multiple screens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include "thingProperties.h"
MKRIoTCarrier carrier;
bool CARRIER_CASE = true;
int changeDisplayCounter = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop is simple. We check each button to see if it's been pressed before displaying data on the screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void loop() {
 ArduinoCloud.update();
 // Your code here
 carrier.Buttons.update();
 if (carrier.Button1.onTouchDown()) {
   Serial.println("Touching Button 1");
   changeDisplayCounter = 0;
   printMeasure(internalTemperature, "Int. Temp");
 }
 // Checks if new data are available
 if (carrier.Button2.onTouchDown()) {
   Serial.println("Touching Button 2");
   changeDisplayCounter = 1;
   printMeasure(temperature, "Ext. Temp");
 }
 if (carrier.Button3.onTouchDown()) {
   Serial.println("Touching Button 3");
   changeDisplayCounter = 2;
   printMeasure(temperature - internalTemperature, "Temp. diff");
 }
 internalTemperature = carrier.Env.readTemperature();
 calcDiff(temperature, internalTemperature);
 delay(200);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, at the end, we always read the internalTemperature from the kit, and calculate the difference with the external temperature.&lt;/p&gt;

&lt;p&gt;The rest of the sketch contains details on the functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void onTemperatureChange() {
 // Do something
 calcDiff(temperature, internalTemperature);
}
void calcDiff(float temp1, float temp2){
 if(temp1 &amp;gt;= temp2) {
   //( led index , red , green , blue )
   carrier.leds.setPixelColor(0, 10, 180, 10);
   // return true;
 }
 else {
   carrier.leds.setPixelColor(0, 10, 10, 180);
   // return false;
 }
 carrier.leds.show();
}
void printMeasure(float temp, String name){
 int colors[] = {ST77XX_RED, ST77XX_BLUE, ST77XX_YELLOW};
 int index = 0;
 Serial.println(name);
 Serial.print(temp);
 Serial.println(" C");
 // Reset screen
 carrier.display.fillScreen(ST77XX_BLACK);
 index = changeDisplayCounter % 3;
 carrier.display.setTextColor(colors[index]);
 carrier.display.setCursor(30, 100);
 carrier.display.setTextSize(2);
 carrier.display.println(name);
 carrier.display.setTextSize(4);
 carrier.display.setCursor(50, 120);
 carrier.display.print(temp);
 carrier.display.setTextSize(2);
 carrier.display.print(" C");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can find a full copy of my&lt;a href="https://create.arduino.cc/editor/mastrolinux/b5a10e02-67a1-44bf-9967-29482c36127b/preview"&gt; Oplà Kit Sketch&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;As of now, the second sketch will not receive any kind of value for the variable temperature because that data lives in the MKR Wi-Fi 1010. Though it is sent to Arduino IoT Cloud, it is not yet sent back to our Oplà Kit. We can solve this by creating our own Node.js application to relay the data.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating a Custom Node.js Application on Heroku&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To create our custom application, we can use two different kinds of Arduino IoT APIs: one is the&lt;a href="https://www.arduino.cc/reference/en/iot/api/"&gt; REST API&lt;/a&gt;, and the other is a real-time MQTT/WebSocket API. The latter is not documented yet, but luckily a JavaScript client is available. You can find the two JavaScript modules here: one is the&lt;a href="https://www.npmjs.com/package/@arduino/arduino-iot-client"&gt; Arduino IoT Js Client&lt;/a&gt;, and the other is the&lt;a href="https://www.npmjs.com/package/arduino-iot-js"&gt; Arduino IoT Js lib&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To interact with the APIs, we will need an API token. You can find yours by clicking the Integrations button at the top of the screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BVmHubw9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5ocy1u086neuj5d29s4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BVmHubw9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5ocy1u086neuj5d29s4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Be sure to keep your API key handy. We will use it in a moment.&lt;/p&gt;

&lt;p&gt;My friend,&lt;a href="https://github.com/fstasi"&gt; Francesco Stasi&lt;/a&gt;, is an amazing developer. (You can find him on GitHub.) He helped create the application that proxies a property of a first Thing to another property with the same name in a second Thing. &lt;/p&gt;

&lt;p&gt;The whole software is really small, and you can find it in my own&lt;a href="https://github.com/mastrolinux/js-iot-proxy"&gt; Js-IoT-Proxy&lt;/a&gt; GitHub repository. The software is designed to be developed locally and deployed to Heroku. The main part of it is visible in this&lt;a href="https://github.com/mastrolinux/js-iot-proxy/blob/13d8002eb1af0d4b34ca85e738fbca5ca3aa0751/src/thing.mjs#L43-L72"&gt; snippet of code available on the repo&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Register a callback for every property in the object to keep the values
updatedObject.keys(thingProperties).forEach((propertyName) =&amp;gt; {
   console.log(`${thingId} - register callback for ${propertyName}`);
   ArduinoIoTCloud.onPropertyValue(thingId, propertyName, (newValue) =&amp;gt; {
     console.log(`${thingId} - property ${propertyName} changed`);
     thingProperties[propertyName].value = newValue;
     // Invoke the proxy callback
     proxyThingProperty(thingId, propertyName, newValue);
   });
 });
 return {
   // Get the array of known properties names
   getPropertiesList() {
     return Object.keys(thingProperties);
   },
   // Get the value of a property
   getPropertyValue(propertyName) {
     return typeof thingProperties[propertyName] !== "undefined"
       ? thingProperties[propertyName].value
       : null;
   },
   // Set the value for a property
   setPropertyValue(propertyName, newValue) {
     console.log(`${thingId} - writing ${propertyName}, value: ${newValue}`);
     return ArduinoIoTCloud.sendProperty(thingId, propertyName, newValue);
   },
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, if the two properties have the same name, the devices communicate with each other. The logic is visible in the&lt;a href="https://github.com/mastrolinux/js-iot-proxy/blob/13d8002eb1af0d4b34ca85e738fbca5ca3aa0751/src/proxy.mjs"&gt; proxy.mjs file&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Deploying the Application&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I have been a Linux Engineer for quite a long time. However, when I need something robust that works out of the box without any maintenance, I prefer to use Heroku.&lt;/p&gt;

&lt;p&gt;To deploy the application, I used a very handy feature I recently discovered: the&lt;a href="https://devcenter.heroku.com/articles/github-integration"&gt; GitHub automatic deploy&lt;/a&gt; feature. Every time I merge to main, the app immediately shows up in my Heroku panel. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qOeYYCXO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8bnt5ghq3oslmapratr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qOeYYCXO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8bnt5ghq3oslmapratr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are only missing a few things to make all this work. We need to create and store different environment variables containing the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The Arduino API app ID (IOT_CLIENT_ID)&lt;/li&gt;
&lt;li&gt;  The Arduino API Secret Key (IOT_CLIENT_SECRET)&lt;/li&gt;
&lt;li&gt;  The Thing ID of the OutDoor Sensors (THING_ID_FROM)&lt;/li&gt;
&lt;li&gt;  The Thing ID of the OplaKit (THING_ID_TO)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can do all of this by using the Heroku settings panel in our&lt;a href="https://dashboard.heroku.com/apps/"&gt; dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---5Gr_Lgy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cz6gtere62f0tcik9sl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---5Gr_Lgy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cz6gtere62f0tcik9sl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we can deploy the app and check the logs. Now, every time the temperature changes in the ENV Shield, you can see it being sent to the Oplà Kit in real-time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6Ogm42m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9jvcnw8q2d3568rrakk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6Ogm42m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9jvcnw8q2d3568rrakk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is how everything looks like on the device itself. You can see the internal temperature by clicking Button 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f9TZmnKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5buxwk1zcalb27pcp2li.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f9TZmnKa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5buxwk1zcalb27pcp2li.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With Button 2, we get the external temperature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iC2PAl0N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewbik3kjg0knyon61hl1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iC2PAl0N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewbik3kjg0knyon61hl1.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And, finally, with Button 3, we see the temperature difference. The LED will turn blue if the external temperature is less than the internal. Otherwise, it will be red.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AM8PXy0R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c176q0kc3if9pnc6y6x1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AM8PXy0R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c176q0kc3if9pnc6y6x1.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To record the temperature, I created a dashboard in Arduino IoT Cloud by directly adding the Things to our&lt;a href="https://create.arduino.cc/iot/dashboards/dashboards/"&gt; dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hBSS9rpE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5k2pricaemlwzij0lrsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hBSS9rpE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5k2pricaemlwzij0lrsk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want your system to be even more advanced and interactive, you can also install the Arduino IoT Remote app.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Resources&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To simplify the system-creation process for you, I collected all the information you need.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The&lt;a href="https://create.arduino.cc/editor/mastrolinux/e988e61f-6cb2-4449-882a-f684a5512e98/preview"&gt; ENV Shield OutDoor Sensors sketch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  The&lt;a href="https://create.arduino.cc/editor/mastrolinux/b5a10e02-67a1-44bf-9967-29482c36127b/preview"&gt; Oplà Kit sketch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  The&lt;a href="https://github.com/mastrolinux/js-iot-proxy"&gt; iot-js-proxy Node.js application&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Ideas for the Future&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;That's it! Now you have a fully functional IoT sensor that records outside environment variables, relays them over the cloud, and reports them to a second device in your home. In the future, I plan on further enhancing this by creating my own mobile or web application with a custom interface. (It should be simple with the JavaScript client we used already.) And then, on top of that, I'd like to store all the data points in my own Heroku Postgres instance, and share them via Heroku's&lt;a href="https://blog.heroku.com/dialog-with-data-new-dataclips"&gt; Dataclips&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>iot</category>
      <category>arduino</category>
      <category>heroku</category>
      <category>opensource</category>
    </item>
    <item>
      <title>How to Build a Robust IoT Prototype In Less Than a Day - Part 2</title>
      <dc:creator>Luca Cipriani</dc:creator>
      <pubDate>Mon, 29 Mar 2021 09:30:29 +0000</pubDate>
      <link>https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-2-4ckc</link>
      <guid>https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-2-4ckc</guid>
      <description>&lt;p&gt;(part one is&lt;a href="https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-1-4o2l"&gt; here&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How to Build a Robust IoT Prototype In Less Than a Day - Part 2&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Welcome back to our second article about creating a robust, bidirectional IoT prototype in less than a day using&lt;a href="https://www.arduino.cc/"&gt; Arduino&lt;/a&gt;,&lt;a href="https://www.heroku.com/"&gt; Heroku&lt;/a&gt;, and&lt;a href="https://nodered.org/"&gt; Node-RED.&lt;/a&gt; If you missed the first part, we covered setting up Node-RED, adding security, and deploying to Heroku. In this article we'll look at creating our embedded system on the Arduino boards, connecting them to our Node-RED instance, customizing Sketches, and creating a flow that allows our devices to talk to each other. Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Arduino IoT Cloud?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To connect the physical devices (boards) to the internet we will use Arduino IoT Cloud, a device manager and data visualization tool. After setting up an account at&lt;a href="https://create.arduino.cc"&gt; https://create.arduino.cc&lt;/a&gt;, we can create our first &lt;em&gt;digital twin&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A digital twin is a logical online representation of a system. In Arduino, this is referred to as “Things” or “Apps”. We need to create two different Apps—one for the sensors, and another for the actuators. Then, we can associate a physical device (one of our MKR1010 boards) to it.&lt;/p&gt;

&lt;p&gt;Here is the scenario: We want one of the devices to read the environmental luminosity (the sensor device), and the LED Matrix on the other MKR1010 (the actuator device) to display ON if the value of the luminosity is over a certain limit, OFF otherwise.&lt;/p&gt;

&lt;p&gt;First, we need to create a new Thing and give it a name. I will call mine EnvSensors. Then, we need to associate a device. To do this, click “Configure a new device”.&lt;/p&gt;

&lt;p&gt;Before configuring the device, be sure to unplug any shield on the MRK1010 and plug it to your PC with a USB-micro cable. After installing the device, you can assign it a name. I will call it SensorsBoard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QKqDj3z8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ei6qa85yctbqntdvpii.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QKqDj3z8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ei6qa85yctbqntdvpii.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the first phase, the system will recognize the board; it should find an MKR1010. Then, it will upload a sketch to the board to automatically put an SSL certificate in the secure element for future encrypted communications. This process takes a few minutes, so be patient.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2JrztjBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk4l1n2mgtusygj91s08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2JrztjBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk4l1n2mgtusygj91s08.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EoO9T79g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewc4897pp9r6a92ql702.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EoO9T79g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewc4897pp9r6a92ql702.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once finished, we can continue creating our Thing. We need to create at least one property, luminosity, to start gathering data from our device.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QJ_aXcz8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b218iu0v43ef93ax6ue6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QJ_aXcz8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b218iu0v43ef93ax6ue6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The property variable name will be Luminosity. The Type will be Illuminance. We will also set a min value of 0 for the obscurity, and a max of 1000 for high brightness. Then we set the permissions as “Read Only” because we can only read from sensors; we cannot write values to them. Next, we want to send luminosity values periodically, so we set the frequency to one-second intervals.&lt;/p&gt;

&lt;p&gt;Finally, click “Add Property” and check to make sure the SensorsBoard is set as the associated device.&lt;/p&gt;

&lt;p&gt;Now we can click “Edit Sketch”, which will open a web IDE. Arduino IoT Cloud automatically creates a template to send the data as we specified before—we just need to read the data from the sensor.&lt;/p&gt;

&lt;p&gt;To read the sensors from the board, we have to put the MKR ENV shield on top of the MKR WIFI 1010. Then, we can plug the USB cable again in the board and modify the sketch a bit before the final upload.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pyiRo4lS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fktc5imifdj44gg1bu7p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pyiRo4lS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fktc5imifdj44gg1bu7p.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the software side, we need to add a library to read the values from the sensor. The following line is enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;Arduino_MKRENV.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, to avoid trying to read data before the initialization, we continue looping until the library loads and communicates with the sensors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; if (!ENV.begin()){
    Serial.println("Failed to initialize MKR ENV shield!");
    while(1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is where the magic happens! The automatically-generated file named thingProperties.h has already declared the luminosity variable for us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;float luminosity;
void initProperties(){
  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addProperty(luminosity, READ, 1 * SECONDS, NULL);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, never change thingProperties.h, it is automatically generated and can fall out of sync if you change it manually.&lt;/p&gt;

&lt;p&gt;The interesting part is we can use the variable luminosity as a normal variable. It will be sent automatically to the Arduino Cloud every second, as we specified in the interface.&lt;/p&gt;

&lt;p&gt;To read the data we just need a few lines in our loop() function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void loop() {
  ArduinoCloud.update();
  luminosity = ENV.readIlluminance();
  delay(50); //avoid reading the sensor too many times
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are almost done, but how do we connect the board to the WiFi network? We specify the network credentials in the secret tab:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D7siI7k9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9xwtfoymuks2s95u7ro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D7siI7k9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9xwtfoymuks2s95u7ro.png" alt="Secret Tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://create.arduino.cc/editor/mastrolinux/c16770da-a59b-4e2f-9cc6-cf63c0bfa7fc/preview"&gt;Here is the whole sketch.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upload this sketch to your board.&lt;/p&gt;

&lt;p&gt;Now it's time to verify that everything works as expected. So let's create a new simple dashboard in Arduino IoT Cloud: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://create.arduino.cc/iot/dashboards"&gt;https://create.arduino.cc/iot/dashboards&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I named mine Sensors and Actuators. Now we'll add a new widget. In the Things tab, there's an easy way to create a dashboard with just one click. I selected my thing EnvSensors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YwM34sLx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9pd09784cv5mokm7154.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YwM34sLx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9pd09784cv5mokm7154.png" alt="Thing Widget"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After placing the widget you should see the dashboard showing real-time luminosity data from the sensor:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ISr-Opn0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c44vxjh055ihtv0db5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ISr-Opn0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c44vxjh055ihtv0db5t.png" alt="Luminosity Widget"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great! Now let’s create a new Thing and configure a new device. Let's call this new Thing EnvActuators and name the new device ActuatorsBoard. I suggest removing the SensorsBoard from your USB device. Avoid plugging in the LED matrix until after you configure the device.&lt;/p&gt;

&lt;p&gt;The new Thing will have two variables: “display_text” to show online the same value the LED matrix is actually displaying, and a “switch” variable to tell us if the luminosity has reached a certain level. So we need to change the text on the display.&lt;/p&gt;

&lt;p&gt;Display Test variable can be set as:&lt;/p&gt;

&lt;p&gt;Name: Display Text&lt;/p&gt;

&lt;p&gt;Variable: display_text&lt;/p&gt;

&lt;p&gt;Type: Character String&lt;/p&gt;

&lt;p&gt;Permission: Read and Write&lt;/p&gt;

&lt;p&gt;Update: When the value changes&lt;/p&gt;

&lt;p&gt;History: OFF&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;Name: LED Switch&lt;/p&gt;

&lt;p&gt;Variable: led_switch&lt;/p&gt;

&lt;p&gt;Type: ON/OFF (Bool)&lt;/p&gt;

&lt;p&gt;Permission: Read and Write&lt;/p&gt;

&lt;p&gt;Update: When the value changes&lt;/p&gt;

&lt;p&gt;History: ON&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iuaEseXm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mynhr288qjobvnsqbicn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iuaEseXm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mynhr288qjobvnsqbicn.png" alt="EnvActuators Properties"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After we click “Edit Sketch”, we have a new template to start with. There are two functions at the end of the sketch allowing the device to react when a property changes. Those functions are named CallBacks, and are called every time a property changes.&lt;/p&gt;

&lt;p&gt;The following code allows you to change the value displayed in the LED Matrix every time the switch changes value from “true” to “false” and back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void onLedSwitchChange() {
  if(led_switch){
    display_text = "on";
  }else{
    display_text = "off";
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the main loop code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void loop() {
  ArduinoCloud.update();
  // Your code here
  MATRIX.beginDraw();
  MATRIX.clear();
  MATRIX.stroke(80, 80, 100);
  MATRIX.text(display_text, 0, 1);
  MATRIX.endDraw();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So every time the display_text variable changes, the LED Matrix clears and a new string is written on it.&lt;/p&gt;

&lt;p&gt;You can find the entire code in my &lt;a href="https://create.arduino.cc/editor/mastrolinux/44e068bc-3199-4def-969e-a75f6ecf3277/preview"&gt;EnvActuators sketch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now we can upload the sketch to our MRK 1010 with the RGB Shield and add the Thing to the dashboard.&lt;/p&gt;

&lt;p&gt;You should have a dashboard that looks similar to the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G1mwoH0J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d30eh92oksvg7ffiyram.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G1mwoH0J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d30eh92oksvg7ffiyram.png" alt="Sensor &amp;amp; Actuators Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every time you change the led_switch toggle, the display text will change on both the display_text widget and on your physical device.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--deQGpxjq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zuqn4nqcl9ncx0dxk4v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--deQGpxjq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zuqn4nqcl9ncx0dxk4v.jpg" alt="LED Shield in Action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Interact with the Arduino Create IoT APIs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In order to create an application allowing the two different devices to speak to each other while adding our custom logic, we need to use the Arduino Create IoT REST APIs.&lt;/p&gt;

&lt;p&gt;There are two kinds of APIs for Arduino IoT Cloud:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A fully-documented REST&lt;a href="https://www.arduino.cc/reference/en/iot/api/"&gt; Arduino IoT Cloud API&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  A realtime API (only available for JavaScript as of the time of this writing), available on GitHub.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The system is throttled so you can't make more than 10 req/s for the REST API, but that's good enough for our use case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WjNazPbg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7kkid5q3jl4bvuct1gk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WjNazPbg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7kkid5q3jl4bvuct1gk.png" alt="Project Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our first article, we learned how to deploy Node-RED on Heroku. This time we need to connect it to Arduino IoT Cloud by using an API token.&lt;/p&gt;

&lt;p&gt;To obtain an IoT API token, go to the&lt;a href="https://create.arduino.cc/iot/things"&gt; Things page&lt;/a&gt; and create one at the end of the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LvYBqlvH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu0afk4wlb8ozc4lzsvp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LvYBqlvH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu0afk4wlb8ozc4lzsvp.png" alt="API Token"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Be sure to keep your credentials in a safe place. (You won't be able to recover your client secret; you'll only be able to generate a new one.)&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Connecting to Node-RED&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you followed the first article of this series, you should be able to access Node-RED from your&lt;a href="https://dashboard.heroku.com/apps/"&gt; Heroku Apps page&lt;/a&gt;. After accessing the app, you'll be able to see the Arduino blocks on the left panel and place them.&lt;/p&gt;

&lt;p&gt;Double-clicking on a property will open a connection panel as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RJIjnOGB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqfkfup76hyfa60jk9tx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RJIjnOGB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqfkfup76hyfa60jk9tx.png" alt="Node-RED Connection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And clicking on the pencil will allow you to add the Arduino IoT Cloud API credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tst0XdY6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kj8ay6thfoo9yint2sny.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tst0XdY6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kj8ay6thfoo9yint2sny.png" alt="Arduino Connection Configuration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, click “Add”. Now we can connect our luminosity property.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ElnlpUjI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di7941wrkdm9zcj71tyt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ElnlpUjI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di7941wrkdm9zcj71tyt.png" alt="Adding Arduino IoT Thing to Node-RED"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After clicking “Deploy” at the top right of the interface, we can begin to see the data flowing. Important: Nothing happens if you do not deploy your flow!&lt;/p&gt;

&lt;p&gt;We are finally getting the property in Node-RED. The Luminosity I read is about 650 Lumen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5QKhYCYz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dvq0711a1csc0gojjmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5QKhYCYz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dvq0711a1csc0gojjmw.png" alt="Lumen reading after deployment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Design a Flow and Deploy It&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Our flow is running on a Heroku Dyno. A Heroku Dyno is an application container with an&lt;a href="https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem"&gt; ephemeral filesystem&lt;/a&gt;. This means your application will lose any data stored on a disk. Unfortunately, by default, Node-RED stores the flows and credentials on the filesystem. &lt;/p&gt;

&lt;p&gt;There are many different strategies to avoid this, but they all require the development of a Node-RED configuration handler plugin. I created a simpler but effective solution. If we store the flows and the credentials in our source code, then they can be deployed on a new Dyno every time without losing our configuration. So I added the parameter flowFile: 'data/flows.json' in the settings.js file. Please see the previous article for more details.&lt;/p&gt;

&lt;p&gt;Let’s run the application locally (in our node-red-private repo with the iot-proto branch).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now log into Node-RED to start creating the first flow. We should add an input property, a switch, and an output property. The input is connected to the luminosity sensor, while the output is connected to the LED Switch. In the middle, we'll place a switch block to control our LED value.&lt;/p&gt;

&lt;p&gt;Our flow should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nALvazJa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sca8l42rfpwrcgbo9j4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nALvazJa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sca8l42rfpwrcgbo9j4l.png" alt="Node-RED Workflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are five components. The green ones are the Arduino properties, and include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  An input Arduino Property block (connected to the luminosity)&lt;/li&gt;
&lt;li&gt;  A logical switch, with the output 1 if the luminosity is above 300 and the output 2 if the luminosity is lower&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--05uab41H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjbq7mwk8bt5t48xno2i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--05uab41H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjbq7mwk8bt5t48xno2i.png" alt="Logical Switch"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A set block to activate the switch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E1RxFc9P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yv0hzzxeb4saij4nzx1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E1RxFc9P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yv0hzzxeb4saij4nzx1i.png" alt="Set Block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A similar block to deactivate it&lt;/li&gt;
&lt;li&gt;  An output Arduino Property block bounded to the LED Switch property&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't forget to click “Deploy” once you are done.If you're feeling lazy, you can import my simple&lt;a href="https://gist.github.com/mastrolinux/7f8cddc1aa85212a810af39b30bfc0e9"&gt; Arduino IoT Cloud Node-RED Flow&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now we have to commit our code for the flows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git add data
$ git commit -a -m ‘Saving the flow'
$ git push heroku iot-proto:master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, we have the app in production!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vlrvozjt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/880s3rasa0wsbytd4vco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vlrvozjt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/880s3rasa0wsbytd4vco.png" alt="Whole Flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now have the RGB Matrix displaying ON if the luminosity is more than 300, and OFF if it less than 300.&lt;/p&gt;

&lt;p&gt;We now have a fully-functional, bi-directional IoT prototype up and running in just a few hours, and ready for any projects you can dream up&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q0zNAzJ2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://youtu.be/C5r7pcAnIfY" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q0zNAzJ2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://youtu.be/C5r7pcAnIfY" alt=" See a video of it all working here"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here is a quick recap of what we learned: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to set up boards in Arduino IoT Cloud&lt;/li&gt;
&lt;li&gt;How to set up properties for the Thing&lt;/li&gt;
&lt;li&gt;How the Sketch is automatically built from the system, and how to customize it. Sketches are available at:

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://create.arduino.cc/editor/mastrolinux/c16770da-a59b-4e2f-9cc6-cf63c0bfa7fc/preview"&gt;Sensor Sketch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://create.arduino.cc/editor/mastrolinux/44e068bc-3199-4def-969e-a75f6ecf3277/preview"&gt;Actuator Sketch&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;How to create a dashboard&lt;/li&gt;
&lt;li&gt;How to use the Arduino IoT Cloud API&lt;/li&gt;
&lt;li&gt;How to deploy Node-RED to Heroku&lt;/li&gt;
&lt;li&gt;How to make the Heroku website persistent (with encrypted credentials)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All of those steps allowed us to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a custom logic application on Node-RED&lt;/li&gt;
&lt;li&gt;Have two different devices talk to each other&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By leveraging Node-RED, you can also add many integrations with third-party services.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Potential Improvements&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the future, I'd like to add proper persistence by using a Redis or Postgres storage supported by the&lt;a href="https://devcenter.heroku.com/categories/data-management"&gt; Heroku Database &amp;amp; Data Management System.&lt;/a&gt; This will require writing a small js plugin to allow Node-RED to store the flows and configurations in a database. I found an old repo by&lt;a href="https://github.com/joeartsea"&gt; Atsushi Kojo&lt;/a&gt; containing a PoC. The code is old but I will probably update it in the future. The repo is named&lt;a href="https://github.com/joeartsea/node-red-heroku"&gt; node-red-heroku&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After I finish that setup, it will be possible to use a nice Heroku feature, the&lt;a href="https://devcenter.heroku.com/articles/heroku-button"&gt; Deploy to Heroku&lt;/a&gt; button. This will allow a one-click setup of everything we've seen so far.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What’s Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the next article, we'll create a working prototype of an entire custom&lt;a href="https://nodejs.org/en/"&gt; Node.js&lt;/a&gt; application by using the&lt;a href="https://github.com/arduino/arduino-iot-js"&gt; Arduino Real Time Js Client&lt;/a&gt; working inside&lt;a href="https://www.heroku.com/"&gt; Heroku&lt;/a&gt; and interacting with Arduino IoT Cloud.&lt;/p&gt;

</description>
      <category>iot</category>
      <category>heroku</category>
      <category>arduino</category>
    </item>
    <item>
      <title>How to Build a Robust IoT Prototype In Less Than a Day - Part 1</title>
      <dc:creator>Luca Cipriani</dc:creator>
      <pubDate>Thu, 14 Jan 2021 13:53:01 +0000</pubDate>
      <link>https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-1-4o2l</link>
      <guid>https://dev.to/mastrolinux/how-to-build-a-robust-iot-prototype-in-less-than-a-day-part-1-4o2l</guid>
      <description>&lt;p&gt;The IoT world is powerful, but also quite complex. There are many protocols and vendors and the landscape is fragmented. [As&lt;a href="https://mattturck.com/iot2018/" rel="noopener noreferrer"&gt; Matt Turck has already shown in 2018&lt;/a&gt;.] &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn8dhusxd56srw0zncv9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn8dhusxd56srw0zncv9u.png" alt="image5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating stable and effective prototypes is becoming essential for every web or embedded developer. However, to create a prototype, you have to deal with: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  the hardware&lt;/li&gt;
&lt;li&gt;  the connectivity&lt;/li&gt;
&lt;li&gt;  the embedded software&lt;/li&gt;
&lt;li&gt;  the IoT vendor API&lt;/li&gt;
&lt;li&gt;  the security and deployment of your custom application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even for a basic prototype, this process could take weeks. In this article, I’ll show you how to build a robust IoT prototype in only a few hours by leveraging the power of&lt;a href="https://www.arduino.cc/" rel="noopener noreferrer"&gt; Arduino&lt;/a&gt;,&lt;a href="https://www.heroku.com/" rel="noopener noreferrer"&gt; Heroku&lt;/a&gt;, and&lt;a href="https://nodered.org/" rel="noopener noreferrer"&gt; Node-RED&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Many IoT projects focus only on reading data from sensors. However, for real prototypes, you need &lt;em&gt;bi-directionality&lt;/em&gt; of the data, from the IoT sensor node to the cloud and back from the cloud to the actuators. For all of you with more knowledge in web development and a little less in embedded systems, here are the definitions of sensor and actuator.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A sensor is an input device, reading some physical dimension and converting it to either a digital or analog value. This value is then read by a microcontroller. If you want to measure temperature, you would use a temperature sensor.&lt;/li&gt;
&lt;li&gt;  An actuator is an output device, acting on the physical world and changing the status of something physical. A switch or an LED are both actuators—you can turn them ON or OFF.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to know more, read this article about&lt;a href="https://microcontrollerslab.com/difference-between-actuator-and-sensor/" rel="noopener noreferrer"&gt; the difference between sensors and actuators.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The overall architecture of most IoT projects might look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9zoyxubenf2udyfmxv4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9zoyxubenf2udyfmxv4q.png" alt="image2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let’s create a sample app that will allow you to quickly create prototypes. We’ll use the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Arduino boards as the IoT Node.&lt;/li&gt;
&lt;li&gt;  Arduino compatible shields as the sensors and actuators.&lt;/li&gt;
&lt;li&gt;  Arduino IoT Cloud account as the IoT server.&lt;/li&gt;
&lt;li&gt;  Node-RED for business logic (running in Heroku and with source code in GitHub)&lt;/li&gt;
&lt;li&gt;  A JavaScript custom application for the UI and mobile use case.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our sample app, we’ll create a scenario where one Arduino board sends data from a sensor. (We will use luminosity, temperature, and humidity sensors.) The other device will show some information on the LED matrix as soon as it gets the information. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F88vujxrub2hsjivmta1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F88vujxrub2hsjivmta1j.png" alt="image3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  2x&lt;a href="https://store.arduino.cc/arduino-mkr-wifi-1010" rel="noopener noreferrer"&gt; Arduino MKR WiFi 1010&lt;/a&gt; ($32.10)&lt;/li&gt;
&lt;li&gt;  1x&lt;a href="https://store.arduino.cc/arduino-mkr-rgb-shield" rel="noopener noreferrer"&gt; Arduino RGB Shield&lt;/a&gt; ($44.90)&lt;/li&gt;
&lt;li&gt;  1x&lt;a href="https://store.arduino.cc/arduino-mkr-env-shield" rel="noopener noreferrer"&gt; Arduino MKR ENV Shield&lt;/a&gt; ($34.40)&lt;/li&gt;
&lt;li&gt;  An&lt;a href="https://store.arduino.cc/digital" rel="noopener noreferrer"&gt; Arduino IoT Cloud Maker Account&lt;/a&gt; ($6.99/month)&lt;/li&gt;
&lt;li&gt;  A&lt;a href="https://github.com" rel="noopener noreferrer"&gt; GitHub Account&lt;/a&gt; (free)&lt;/li&gt;
&lt;li&gt;  A&lt;a href="https://www.heroku.com/pricing" rel="noopener noreferrer"&gt; Heroku Hobby Account&lt;/a&gt; (free, or $7/month to avoid your app going to sleep)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Let’s Get Started&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As a first step, while we wait for the hardware to arrive, we can set up Node-RED. Node-RED is an incredibly powerful low-code programming tool for event-driven applications based on Node.js. It allows you to easily and graphically wire together flows between hardware devices, then deploy those flows with just one click. Node-RED is built on Node.js. (I will assume you are already familiar with Node.js. If not, then you can learn about and install it at&lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt; https://nodejs.org/&lt;/a&gt;.)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Installing Node-RED&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;TL;DR: If you want to quickly test the application you can skip this part and jump to the section &lt;em&gt;Wrapping It Up&lt;/em&gt; which has a link to a quick-start version of this project.&lt;/p&gt;

&lt;p&gt;However, if you want to understand how to build this yourself and eventually update your work to the latest version of Node-RED, please follow the detailed instructions below.&lt;/p&gt;

&lt;p&gt;It’s a little bit of work to install Node-RED and get it set up, secured, and working, so let’s walk through the steps. To start, be sure you have the latest stable version. For our purpose, we are going to use git.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone git@github.com:node-red/node-red.git node-red-private
cd node-red-private
## Let's move the latest stable version as of 2020-07-01
git checkout 1.1.0
## And create our branch for experimenting
git checkout -b iot-proto
## Install deps and build the app
npm install
npm run build
## Start the app
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Node-RED should be up and running, usually at&lt;a href="http://localhost:1880" rel="noopener noreferrer"&gt; http://localhost:1880&lt;/a&gt;. Open it, and you should see an interface similar to the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2tbrhc5gw8wgpwtl3mza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2tbrhc5gw8wgpwtl3mza.png" alt="image4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a good start, but there's no security, and everything is running locally. This setup is not suitable for serious prototyping, and we do not want to behave like this comic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1y5dww321rcmg67uy178.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1y5dww321rcmg67uy178.png" alt="image9"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://donthitsave.com/comic/2016/07/15/it-works-on-my-computer" rel="noopener noreferrer"&gt;https://donthitsave.com/comic/2016/07/15/it-works-on-my-computer&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Adding Credentials to Node-RED&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To add security to Node-RED it’s best to protect the interface (static assets), the visual editor, and the Admin API. To do this, follow the official guide about&lt;a href="https://nodered.org/docs/user-guide/runtime/securing-node-red" rel="noopener noreferrer"&gt; Securing Node-RED&lt;/a&gt;. To add the credentials, we need a way to create a password and to obtain a hash for it. Node-RED has a simple tool for this called node-red-admin. It can be installed via:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install --save-dev node-red-admin
## Here is the dir where we will put our configurations
$ mkdir data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s also install the&lt;a href="https://flows.nodered.org/node/node-red-node-arduino" rel="noopener noreferrer"&gt; Arduino plugin&lt;/a&gt; in the same directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd data
$ npm install @arduino/node-red-contrib-arduino-iot-cloud
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s create a password. The script will ask for a password interactively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# ./node_modules/node-red-admin/node-red-admin.js hash-pw
Password: 
$2b$08$xGtfBswU4BF53FymNK9oae/Oz56d35W4/xxxxxxxxxxxx9cbC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The string given back is the hash we can use in the settings.js file. A proper settings file can be found on&lt;a href="https://github.com/node-red/node-red/blob/master/packages/node_modules/node-red/settings.js" rel="noopener noreferrer"&gt; the Github repo of Node-RED&lt;/a&gt;. Let's start with that file and adapt it to our needs. First, we have to uncomment the part about adminAuth and change it to something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adminAuth: {
       type: "credentials",
       users: [{
           username: process.env.NODERED_USERNAME || "admin",
           password: process.env.NODERED_PASSWORD_HASH,
           permissions: "*"
    }]
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows us to use environment variables to set the hash of the password. (we just created it with the hash-pw command of node-red-admin), and a username (with fallback to “admin”).&lt;/p&gt;

&lt;p&gt;The same will apply to another section of the file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;httpStaticAuth: {
        user: process.env.NODERED_USERNAME,
        pass: process.env.NODERED_PASSWORD_HASH
    },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;credentialSecret: process.env.NODERED_CREDENTIAL_SECRET,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My complete settings file is available at [[ xxxxx ]]&lt;/p&gt;

&lt;p&gt;The final important step is to set the port.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    uiPort: process.env.PORT || 1880,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be sure to save your work in a proper directory. Usually, Node-RED saves the data into $HOME/.node-red, but this will not work well with our setup in Heroku, nor is useful to have the source code revisions for our work. So I added...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     flowFile: 'data/flows.json',
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     userDir: 'data/',
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The full file settings.js without comments is less than 40 lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    // the tcp port that the Node-RED web server is listening on
    uiPort: process.env.PORT || 1880,
    mqttReconnectTime: 15000,
    serialReconnectTime: 15000,
    debugMaxLength: 1000,
    flowFile: 'data/flows.json',
    credentialSecret: process.env.NODERED_CREDENTIAL_SECRET,
    userDir: 'data/',
    adminAuth: {
       type: "credentials",
       users: [{
           username: process.env.NODERED_USERNAME || "admin",
           password: process.env.NODERED_PASSWORD_HASH,
           permissions: "*"
    }]
    },
    httpStaticAuth: {
        user: process.env.NODERED_USERNAME || "admin",
        pass: process.env.NODERED_PASSWORD_HASH
    },
    functionGlobalContext: {
    },
    exportGlobalContextKeys: false,
    logging: {
        console: {
            level: "info",
            metrics: false,
            audit: false
        }
    },
    editorTheme: {
        projects: {
            enabled: false
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Setting Up Environment Variables&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To declare the needed variables, you can create a local .env file like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODERED_PASSWORD_HASH='yourpassword-hash-from-the-node-red-admin-output'
NODERED_USERNAME='admin'
NODERED_CREDENTIAL_SECRET='averylongsecretofyourchoice'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s load those variables and start Node-RED again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export $(cat .env | xargs)
$ npm run build
$ npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as we see the Node-RED interface again, we will be prompted with a password.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd84h64tsxw4s5n5x9bgr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd84h64tsxw4s5n5x9bgr.png" alt="image8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our server is now secure, but still running locally. And after the login, we should be able to see the new Arduino plugin available in the left palette.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fri4v431gsglt5r57wz6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fri4v431gsglt5r57wz6q.png" alt="image7"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbgoz0sh94sxzk24fp3c0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbgoz0sh94sxzk24fp3c0.png" alt="image6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Arduino IoT Cloud plugin for Node-RED allows Node-RED to read data from the sensors, send a payload back to the board, inject a value in one of our Arduino IoT Cloud properties, and access the historical values of a property. A property is the logical web representation of either a sensor or an actuator.&lt;/p&gt;

&lt;p&gt;Now it’s time to make everything simpler and use Heroku to deploy Node-RED to our staging environment.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Installing Heroku CLI&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.heroku.com" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt; is a PaaS vendor that makes it easy and fast to deploy apps to the cloud. We’ll use the Heroku CLI to set up and deploy to Heroku from our local machine. (The best way to install and understand how this works is by following the official tutorial on&lt;a href="https://devcenter.heroku.com/articles/heroku-cli" rel="noopener noreferrer"&gt; how to install Heroku CLI&lt;/a&gt;.) Once installed, in the same directory of our Node-RED instance, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku login -i
heroku: Enter your login credentials
Email [l.cipriani@hey.com]:             
Password: **************
Two-factor code: ******
Logged in as l.cipriani@hey.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create a new app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku create
Creating app... done, ⬢ arcane-castle-06470
https://arcane-castle-06470.herokuapp.com/ | https://git.heroku.com/arcane-castle-06470.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally give it a better, memorizable name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $ heroku apps:rename node-red-arduino
    Renaming arcane-castle-06470 to node-red-arduino... done
    https://node-red-arduino.herokuapp.com/ | https://git.heroku.com/node-red-arduino.git
    Git remote heroku updated
     ▸    Don't forget to update git remotes for all other local checkouts of the app.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From now on, we’ll let Heroku handle the heavy work with deploying and running the app, as it supports Node.js and grunt out of the box.&lt;/p&gt;

&lt;p&gt;To run Node-RED locally with Heroku, mimicking 100% what will happen in production, you can run...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $ heroku local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open the app at&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt; http://localhost:5000&lt;/a&gt;. However, we want to run and deploy our app on Heroku. This is just a matter of setting our vars and then pushing to the Heroku git endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ## This will set the proper variables in Heroku. You can skip this if you want to use the Heroku Web UI to set your variables
    $ cat .env | xargs heroku config:set
    ## Now deploy our app!
    $ git push heroku iot-proto:master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And see it online!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdp7cf5v4fbraazekb1s6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdp7cf5v4fbraazekb1s6.png" alt="image1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Wrapping It Up&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We now have a secure Node-RED instance running on Heroku and ready to hook up to our Arduino boards. &lt;/p&gt;

&lt;p&gt;I’ve also built a quick way to reproduce the steps I’ve shown in detail. Full instructions are available at my&lt;a href="https://github.com/mastrolinux/node-red-iot" rel="noopener noreferrer"&gt; node-red-iot repo on Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here’s a recap of what we learned today: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The most common IoT Architectures&lt;/li&gt;
&lt;li&gt;  Setting up Node-RED locally&lt;/li&gt;
&lt;li&gt;  Adding security to Node-RED&lt;/li&gt;
&lt;li&gt;  Add environment variables and manage settings&lt;/li&gt;
&lt;li&gt;  Install Heroku CLI&lt;/li&gt;
&lt;li&gt;  Deploy our Node-RED instance to Heroku&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Next Steps&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the next article, I’ll show you how to create our embedded system on the Arduino boards, connect them to our Node-RED instance, then create and save a flow to our Heroku instance that is resilient to restarts. Finally, I’ll show you how to create a custom application on top of this setup using JavaScript.&lt;/p&gt;

</description>
      <category>iot</category>
      <category>arduino</category>
      <category>heroku</category>
      <category>nodered</category>
    </item>
  </channel>
</rss>
