<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ezequiel Miranda on Medium]]></title>
        <description><![CDATA[Stories by Ezequiel Miranda on Medium]]></description>
        <link>https://medium.com/@ezex?source=rss-d5f55cd09680------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*InuJDAPpazNwn-Pgu5tfrA.jpeg</url>
            <title>Stories by Ezequiel Miranda on Medium</title>
            <link>https://medium.com/@ezex?source=rss-d5f55cd09680------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:32:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ezex/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implement a Serverless Event Handler for Raspberry PI Telemetry on AWS IoT Core]]></title>
            <link>https://medium.com/dev-jam/implement-a-serverless-event-handler-for-raspberry-pi-telemetry-on-aws-iot-core-ecec2dc65448?source=rss-d5f55cd09680------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecec2dc65448</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Ezequiel Miranda]]></dc:creator>
            <pubDate>Mon, 20 Dec 2021 19:50:31 GMT</pubDate>
            <atom:updated>2021-12-20T19:50:31.069Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>This article is part of a series. Check out the series:</em><a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704"><em> Part 1</em></a><em> and </em><a href="https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9"><em>Part 2</em></a>, <a href="https://medium.com/dev-jam/real-time-web-app-with-node-js-lit-element-graphql-and-mqtt-pub-sub-subscription-792a4a3dc1f0"><em>Part 3</em></a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*Q5mA6Au9bol1G7I4" /></figure><h4>Introduction</h4><p>In a previous article, we used the <strong>AWS Lambda</strong> service to host a <strong>Node JS</strong> lambda function with the goal of using it as an event handler on an <strong>AWS IoT Core</strong> instance.</p><p>This Node JS function receives data from the AWS IoT Core (<strong>MQTT</strong>) broker and stores it in a <strong>DynamoDB</strong> collection.</p><p>This time, we will improve the stack by using <strong>The Serverless Framework</strong>. A complete automation tool that helps you build, test, deploy and scale your serverless stack rapidly without worrying about infrastructure.</p><p>This is how our stack will look after adding the Serverless Framework:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*vO9zg0LTp6uhh20JpmYG9A.png" /></figure><p>We will guide you through the steps to using the Serverless Framework to deploy Node JS Lambda functions as Serverless services.</p><p>But before we go deep into the details of implementation, let’s refresh some concepts related to serverless computing.</p><h3>Function as a service in cloud computing</h3><p>This computing concept emerged not too long too, It was first introduced by Amazon in 2014 with their Lambda product. The most exciting aspect of using cloud functions is that you don’t have to worry about managing servers during the development process. This approach to thought, design, and building applications place developers focus on logic and removes their worries from the servers.</p><p>The <strong>FaaS</strong>(<strong>Function as service Model</strong>) model has some similarities with the <strong>PaaS</strong>(<strong>platform as a service</strong>) mode but with the difference that FaaS depends on an event-driven architecture.</p><p>Another very cool feature of the FaaS model is auto-scaling; this mechanism can automatically detect an increased demand for computation and then the server resources are scaled.</p><p><strong>Some features of this model:</strong><br>- Focus on the logic<br>- Event-driven architecture<br>- Auto-scaling<br>- Pay-as-you-go model (Paying only for the computing time you use)<br>- Removal of the need for developers to manage servers</p><p><strong>Uses</strong><br>- Data processing<br>- Stream Processing<br>- Extract-transform-load(ETL)<br>- Creating API and micro-services<br>- Internet of things (IoT) services for internet-connected devices<br>- Saving data to a database<br>- Processing files<br>- Performing scheduled tasks</p><h3>Serverless Framework</h3><p><a href="http://www.serverless.com/">http://www.serverless.com</a></p><p>The Serverless framework comes with a command-line toolkit that helps you build, test, deploy and scale your serverless tack rapidly without worrying about infrastructure. It works, with different infrastructure service providers, such as AWS, Azure, Google Cloud Platform.</p><p><strong>These are the steps we are going to accomplish in this article:</strong><br>1. Install The Serverless Framework <br>2. Set up the AWS credentials<br>3. Initiate applications and create service<br>4. Migrate AWS lambda function to the Serverless framework<br>5. Invoke service locally <br>6. Deploy function<br>7. Invoke remote function</p><h4>Install the Serverless Framework</h4><p>Installing The Serverless Framework via <strong>NPM</strong> is easy. By running the following command you will get the Serverless NPM package installed globally in your environment.</p><pre>npm install -g serverless</pre><p>If everything goes fine, you will have access to the <strong>Serverless CLI application</strong> on your console with the command <strong>serverless</strong> or <strong>sls</strong></p><p>You can run the following command and check the Serverless version installed on your computer.</p><pre>serverless -version</pre><p><strong>Output</strong></p><pre>Framework Core: 2.64.1<br>Plugin: 5.5.0<br>SDK: 4.3.0<br>Components: 3.17.1</pre><h4>Set up AWS credentials</h4><p>When using the AWS provider, the most important thing to know is that you’ll need to set up your credentials to create and deploy functions on AWS more easily.</p><p>TO achieve this, you must create an AWS account and then create an IAM User and Access Key.</p><p>The Serverless documentation gives us step-by-step instructions o how to generate these credentials.</p><p><strong>Here is the direct link:</strong></p><p><a href="https://www.serverless.com/framework/docs/providers/aws/guide/credentials#create-an-iam-user-and-access-key">https://www.serverless.com/framework/docs/providers/aws/guide/credentials#create-an-iam-user-and-access-key</a></p><p>Now that you’ve generated these credentials, you can set them up in your local environment.</p><p><strong>AWS Credentials you need</strong></p><pre>**IAM User:** serverless-adm</pre><pre>**Access key ID:** #ACCESS_KEY</pre><pre>**Secret accesskey**: #SECRET_ACCESS_KEY</pre><h4>Set up AWS profile with Serverless</h4><p>Serverless provides a convenient way to configure AWS profiles with the help of the Serverless config credentials command.</p><pre>serverless config credentials — provider aws — key ACCESS_KEY — secret SECRET_ACCESS_KEY</pre><h3>Initiate applications and create service</h3><p>After you have completed your Serverless framework setup, it’s time to start coding. In this part, you’ll create and deploy a simple Node JS service</p><h4>Create service (Lambda function)</h4><p>To create the service, we use the interactive console applications provided by The Serverless Framework in the console, tun the command <strong>serverless</strong></p><pre><br>&gt;&gt; <strong>serverless</strong></pre><pre>What do you want to make? <strong>AWS — Node.js — Starter</strong><br>What do you want to call this project? <strong>iot-save-telemetry-data</strong></pre><pre>Downloading “aws-node” template…</pre><pre>Project successfully created in iot-save-telemetry-data folder</pre><pre>What org do you want to add this service to? <strong>the-service-name</strong><br>What application do you want to add this to? <strong>iot-core-services</strong></pre><pre>Your project has been setup with org ezequielmiranda87 and app <strong>iot-core-services</strong></pre><p>Now that you have created a boilerplate project with the basic set-up, let’s take a look at the folder structure:</p><pre><br>iot-save-telemetry-data/<br> — README.md<br> — handler.js<br> — serverless.yml<br></pre><p>First, you can find the commonly used <strong>README.md</strong> file with some information and instructions regarding the service created by The Serverless framework.</p><p>In the <strong>handler.js</strong> file, you can find. the Node JS function. This is how the boilerplate functions look:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02910b42731f0cd727952486a5698946/href">https://medium.com/media/02910b42731f0cd727952486a5698946/href</a></iframe><p><strong>serverless.yml</strong> is where you define The Serverless stack, functions, events, and resources for your service. It looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9586f986e4d9fa7b2f83572c85ea0999/href">https://medium.com/media/9586f986e4d9fa7b2f83572c85ea0999/href</a></iframe><h3>Update the hanlder with the event handler:</h3><p>From our previous lambda function code, we adapt it for use with The Serverless framework.</p><p>The code shown below, will receive data from our IoT device, routed by the AWS IoT rule, and the function will create a new item in the DynamoDB collection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa493dbb3af11465119ae4475b909330/href">https://medium.com/media/fa493dbb3af11465119ae4475b909330/href</a></iframe><h3>Invoke service locally</h3><p>Now, you must be able to test your functions locally, before deploying them in production. Using the command <strong>serverless invoke local {service-name}</strong> runs your code locally by emulating the AWS Lambda environment.</p><p><strong>Run the following command</strong></p><pre>serverless invoke local -f hello</pre><h3>Deploy Lambda function</h3><p>Once you have created a service using the Serverless Framework, each time you need to deploy your service to AWS Lambda, you must run the following command:</p><p><strong>Run:</strong></p><pre>Serverless deploy</pre><p><strong>Output:</strong></p><pre><br>Serverless: Packaging service…<br>Serverless: Excluding development dependencies…<br>Serverless: Uploading CloudFormation file to S3…<br>Serverless: Uploading artifacts…<br>Serverless: Uploading service iot-serverless-function.zip file to S3 (11.97 MB)…<br>Serverless: Validating template…<br>Serverless: Updating Stack…<br>Serverless: Checking Stack update progress…<br>………<br>Serverless: Stack update finished…<br>Service Information<br>service: iot-serverless-function<br>stage: dev<br>region: us-east-1<br>stack: iot-serverless-function-dev<br>resources: 8<br>api keys:<br> None<br>endpoints:<br>functions:<br> hello: iot-serverless-function-dev-hello<br>layers:<br> None<br>Serverless: Removing old service artifacts from S3…<br>Serverless: Publishing service to the Serverless Dashboard…<br>Serverless: Successfully published your service to the Serverless Dashboard: <a href="https://app.serverless.com/ezequielmiranda87/apps/serveless-test/iot-serverless-function/dev/us-east-1">https://app.serverless.com/ezequielmiranda87/apps/serveless-test/iot-serverless-function/dev/us-east-1</a></pre><h3>Invoke remote service</h3><p>You can invoke Lambda functions and return logs. This is useful for determining whether the Lambda function is returning the expected value.</p><p><strong>Run:</strong></p><pre>serverless invoke -f hello</pre><p><strong>Output</strong></p><pre>{<br> “statusCode”: 200,<br> “body”: “{\n \”message\”: Item created in DB”\n}”<br>}</pre><h3>Wrapping</h3><p>In summary, we chose The serverless framework to manage our serverless functions. Then we migrated the Lambda function to the serverless stack, tested it locally, deployed and ran the functions deployed the AWS Lambda service remotely from our local environment.</p><p>We believe that The Serverless Framework is one of the best frameworks out there when it comes to serverless deployment because it simplifies the software life management cycle of our applications.</p><pre>I work at <a href="http://sytac.io/">Sytac.io</a>; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run <a href="https://medium.com/dev-jam">DevJam</a>, check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecec2dc65448" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dev-jam/implement-a-serverless-event-handler-for-raspberry-pi-telemetry-on-aws-iot-core-ecec2dc65448">Implement a Serverless Event Handler for Raspberry PI Telemetry on AWS IoT Core</a> was originally published in <a href="https://medium.com/dev-jam">Devjam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Real-time web app with Node JS, Lit-element, GraphQL, and MQTT pub/sub subscription]]></title>
            <link>https://medium.com/dev-jam/real-time-web-app-with-node-js-lit-element-graphql-and-mqtt-pub-sub-subscription-792a4a3dc1f0?source=rss-d5f55cd09680------2</link>
            <guid isPermaLink="false">https://medium.com/p/792a4a3dc1f0</guid>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[mqtt]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Ezequiel Miranda]]></dc:creator>
            <pubDate>Thu, 11 Nov 2021 07:58:30 GMT</pubDate>
            <atom:updated>2021-11-11T07:58:30.245Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>This article is part of a series. Check out the series:</em><a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704"><em> Part 1</em></a><em> and </em><a href="https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9"><em>Part 2</em></a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*mclzhIKUw_ywFTv0" /></figure><h3>What are we building?</h3><p>We are back to the third part of the <strong>IoT experiments series</strong>. In this part, we are going to create a real-time web app with lit-element, NodeJS, GraphQL, and an MQTT pub/sub subscription.</p><p>The service will be listening for data from our sensors using the MQTT protocol.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*37zpkfabrm9KIDf_tmHbQQ.png" /></figure><h4><strong>Applications on Glitch:</strong></h4><p>I’ve hosted the application codes used in this article on <strong>Glitch</strong> so you can see it all in action.</p><ul><li><strong>iot-client-mqtt: </strong><a href="https://ezex-iot-mqtt-client.glitch.me/">https://ezex-iot-mqtt-client.glitch.me</a></li><li><strong>iot-server-graphql:</strong> <a href="https://ezex-iot-graphql-server.glitch.me/">https://ezex-iot-graphql-server.glitch.me</a></li><li><strong>iot-web-app:</strong> <a href="https://ezex-iot-lit-components.glitch.me/demo">https://ezex-iot-lit-components.glitch.me/demo</a></li></ul><h4>MQTT broker</h4><p>In this example, I’m using an open MQTT broker <strong>mqtt://broker.hivemq.com</strong></p><p>You can choose another MQTT broker or create your own. If you follow my previous articles, you can use the <strong>AWS IoT Core instance.</strong></p><h4>Repository with examples:</h4><p>You can find the different project examples in the following repository:</p><p><a href="https://github.com/ezequielmiranda87/iot-experiments](https://github.com/ezequielmiranda87/iot-experiments)">https://github.com/ezequielmiranda87/iot-experiments</a></p><p>If you want to run the examples locally, each of them has an npm script to start it:</p><pre>npm start</pre><h3>MQTT Client</h3><p>In order to send <strong>MQTT messages</strong> to the <strong>MQTT broker</strong>, I’ve created this small NodeJS script that simulates sending sensor data every 3 seconds.</p><p><strong>iot-client-mqtt/index.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eabc0515c465af7c82934388850b0cd0/href">https://medium.com/media/eabc0515c465af7c82934388850b0cd0/href</a></iframe><h3>What is GraphQL:</h3><p><strong>GraphQL</strong> is an open-source <strong>data query language</strong> developed by Facebook in 2012 and open-sourced in 2015. It was designed to be used in mobile application development but is increasingly being adopted for use in front-end development. It is frequently compared to <strong>RESTful JSON APIs</strong> since it exposes data in a similar way.</p><p>GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.</p><h3>GraphQL Subscriptions:</h3><p>To add <strong>real-time</strong> functionality to our web app, we implement<strong> GraphQL subscriptions.</strong></p><p>Subscriptions in GraphQL are a way for a server to send data to its clients when a specific event happens on a GraphQL server. To achieve this, the server maintains a <strong>WebSocket</strong> connection open with the client. Every time this particular event happens, the server uses the connection to push the event data to the subscribed client.</p><p>In this case, we subscribe our <strong>GraphQL server </strong>to <strong>MQTT messages</strong>, using a <strong>pub/sub subscription</strong>. So this means we can listen to a certain <strong>MQTT topic</strong>, and if we receive a new message <strong>(sensor data)</strong> we communicate the data payload to the client and the client application doesn’t need to ask the server if there are new events or data to show.</p><p><strong>graphql-mqtt-subscriptions:</strong></p><p><a href="https://github.com/davidyaha/graphql-mqtt-subscriptions">https://github.com/davidyaha/graphql-mqtt-subscriptions</a></p><p>This package implements the <strong>AsyncIterator</strong> and <strong>PubSubEngine interfaces</strong> from the <strong>graphql-subscriptions package</strong>.</p><h3>Set up GraphQL Server:</h3><p>It’s time to set up our GraphQL Server using NodeJS. First, we need to create a web-socket server and then add the GraphQL layer using NPM’s <strong>graphql-ws </strong>package.</p><p><strong>This is the stack we are using:</strong></p><p><strong>ws (web-socket server): </strong>is a simple, blazingly fast, thoroughly tested <strong>WebSocket</strong> client/server implementation.</p><p><a href="https://github.com/websockets/ws">https://github.com/websockets/ws](https://github.com/websockets/ws</a></p><p><strong>graphql-ws: </strong>A coherent, feature-full, zero-dependency, plug-n-play, lazy, simple, client/server implementation of the new, security-first GraphQL over WebSocket Protocol with full support for all three GraphQL operations: <strong>queries, mutations, </strong>and<strong> subscriptions.</strong></p><p><a href="https://github.com/enisdenjo/graphql-ws**">https://github.com/enisdenjo/graphql-ws</a></p><p><strong>graphql-mqtt-subscription: </strong>This package implements the AsyncIterator and PubSubEngine interfaces from the GraphQL subscriptions package. It allows you to connect your subscription-manager to an MQTT-enabled pub-sub broker to support.</p><p><a href="https://github.com/davidyaha/graphql-mqtt-subscriptions](https://github.com/davidyaha/graphql-mqtt-subscriptions)">https://github.com/davidyaha/graphql-mqtt-subscriptions</a></p><h4>GraphQL Schema</h4><p>Let’s start with the schema definition. A schema in GraphQL describes the functionality available to the client applications that connect to it.</p><p>For this example, the schema is quite simple. We define a “hello world” query, a type for the sensorData, and a subscription that will receive a “sensorData“ object. We put this last item together in the schema object.</p><p><strong>iot-server-graphql/src/schema/index.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43f1a8e11a1e3f4ae82fc26bd0af987c/href">https://medium.com/media/43f1a8e11a1e3f4ae82fc26bd0af987c/href</a></iframe><h4>Subscriptions</h4><p>Here we are going to implement a GraphQL Subscriptions system. This allows anyone who is subscribed through GraphQL to get updates from your server.</p><p>For this case, we add an MQTT PubSub subscription to a GraphQL server using the plugin <strong>graphql-mqtt-subscriptions</strong></p><p>This setup enables the client to be informed of certain events. In this case, we receive notification of new sensor data arriving at an MQTT Broker and a specific MQTT topic: <strong>ezex-test/sensor1</strong></p><p><strong>iot-server-graphql/src/resolvers/index.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7025d71ce0358ec740f1aa468752492/href">https://medium.com/media/e7025d71ce0358ec740f1aa468752492/href</a></iframe><h4>Server Script</h4><p>In the following script, we set up a simple web socket server. Then we add GraphQL support with the schema and resolvers definitions.</p><p>Once we have set up the server, we can use a GraphQL client to subscribe to it.</p><p><strong>iot-server-graphql/server.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4078ea1ee3bfecd6f3437b43fcc72bf6/href">https://medium.com/media/4078ea1ee3bfecd6f3437b43fcc72bf6/href</a></iframe><h3>Web app with lit-elements and Graphql client</h3><p>Now I will show you how to build a real-time web app with web components and a GraphQL subscription.</p><p>We’ll use <strong>“lit-elements”</strong> on the front end.</p><p>LitElement is a simple base class for creating fast, lightweight web components that work in any web page with any framework.</p><p>We will also set up the GraphQL client and subscribe to the server events and update the IU whenever it receives a new MQTT message from the “sensor.”</p><h4>GraphQL client</h4><p>To connect the front-end app to the GraphQL client, we will use the package graphql-ws, which provides the client.</p><p><strong>iot-web-consumer/src/js/graphql-client.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b44882280533cdaeb7c730d3caaacd9/href">https://medium.com/media/7b44882280533cdaeb7c730d3caaacd9/href</a></iframe><h4>my-thing component</h4><p>At this step, we create a simple web component with lit elements. This component will use the GraphQL client that is subscribed to the GraphQL server, and we will be notified when a new message arrives.</p><p>Each time the client receives a new message from the server, the client-subscriber will trigger a custom event and update the data. In this way, our web app receives real-time data from the sensors.</p><p>This is how our web component looks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*18GnNUhVldV8frBkuJvjdw.png" /><figcaption>my-thing component</figcaption></figure><p><strong>iot-web-consumer/src/components/my-thing.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b2c7e1e056abd8300709e771da6fb54/href">https://medium.com/media/1b2c7e1e056abd8300709e771da6fb54/href</a></iframe><h3>Wrapping up</h3><p>In this part of the IoT experiments series, we have created a Real-time web app with lit-element, NodeJS, GraphQL, and an MQTT pub/sub subscription.</p><p>The Server listens for data from our sensors using the MQTT protocol. In addition, we hosted our app on Glitch so you can see it all in action.</p><p>Thank you for your interest! Any feedback is welcome.</p><pre>I work at <a href="http://sytac.io/">Sytac.io</a>; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run <a href="https://medium.com/dev-jam">DevJam</a>, check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=792a4a3dc1f0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dev-jam/real-time-web-app-with-node-js-lit-element-graphql-and-mqtt-pub-sub-subscription-792a4a3dc1f0">Real-time web app with Node JS, Lit-element, GraphQL, and MQTT pub/sub subscription</a> was originally published in <a href="https://medium.com/dev-jam">Devjam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tutorial Part 2: AWS IoT + Rules + Lambda Function + DynamoDB]]></title>
            <link>https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9?source=rss-d5f55cd09680------2</link>
            <guid isPermaLink="false">https://medium.com/p/40a7d4ea35b9</guid>
            <category><![CDATA[aws-iot]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Ezequiel Miranda]]></dc:creator>
            <pubDate>Thu, 30 Sep 2021 12:06:52 GMT</pubDate>
            <atom:updated>2021-10-26T16:31:37.934Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>This article is part of a series. Check out the series:</em><a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704"><em> Part 1</em></a><em>.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*nOSQTrX3lb04AJkq" /></figure><h3>Introduction</h3><p>In <a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704"><strong>Part 1</strong></a> we got started with the <strong>Internet of things(IoT)</strong> and <strong>Amazon Web Service</strong>, we set up an <strong>AWS IoT Core</strong> instance, and we created a <strong>Node.js</strong> program (running on a <strong>Raspberry Pi</strong>), that sends telemetry data to the cloud using an <strong>MQTT</strong> protocol, finally, we tested out the message communication between the device and the cloud instance.</p><p>In this video, you can see what we’re going to building on this <strong>Part 2.</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F618151640%3Fh%3Db888b8dda9%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F618151640&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1260246386-672365256ed7372590d410e5b1e95e04c43da32c5a622a67e_640&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="720" height="1280" frameborder="0" scrolling="no"><a href="https://medium.com/media/b479992e89327f0a25ba92a0f840e34f/href">https://medium.com/media/b479992e89327f0a25ba92a0f840e34f/href</a></iframe><p>Here, in the<strong> Part 2</strong> series, we want to add pieces to the game to capture and store telemetry data in a database.</p><p>In the next diagram, you can see a full picture of the implementation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wC_ioSgWk_fs_KkKMpADWA.png" /></figure><p>To make it possible we added 3 pieces</p><ul><li><strong>DynamoDB collection (persisting data)</strong></li><li><strong>Lambda function (handler function)</strong></li><li><strong>AWS IoT Core Rules (message routing)</strong></li></ul><p>The <strong>Node JS</strong> code running on the <strong>Raspberry Pi</strong> device will send the data to the AWS IoT Core, the IoT Rules will redirect the telemetry messages (<strong>MQTT</strong>).</p><p>The <strong>AWS IoT Core Rules</strong> will redirect the telemetry messages (<strong>MQTT</strong>) to a Lambda function, and for last the <strong>Lambda function</strong> will receive the event data and store it in a <strong>DynamoDB</strong> collection.</p><h3>Create the DynamoDB Collection</h3><p>We chose <strong>DynamoDB</strong> to store the telemetry data, but what is DynamoDB?</p><p>Is a fast and flexible <strong>NoSQL</strong> database service provided by Amazon Web Services. Its flexible data model and reliable performance make DynamoDB a great fit for mobile, web, gaming, advertising technology, <strong>IoT</strong>, and other applications.</p><p>Let’s start creating a new table. First, access the <strong>DynamoDB</strong> service platform and click on <strong>Tables</strong> and then <strong>Create Table</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwRHidIZZ8o9I_wfP9yKQw.png" /></figure><p>Enter a name for the table IoTCatalog and assign a <strong>Partition Key</strong> serialNumber and a <strong>SortKey</strong> timeStamp.</p><p><strong>Partition Key</strong>: This is for partition data; data with the same Partition Key will be store in a group. We choose the IoT Device serial number, so this means all the data related to some specific device will be stored in the same partition.</p><p><strong>SortKey:</strong> Determine the order of the data in the partition, we choose to use the <strong>timeStamp</strong> this one.</p><p><strong>Primary Key:</strong> Partition Key + SortKey(optional)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0gArvhArJREvd1V85UD-fg.png" /></figure><p>Click on <strong>Create table</strong> and will be created the new table created and ready to use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eOvb0yzKGoFnmuJB.png" /></figure><h3>Create the Lambda function</h3><p>When the incoming IoT message, the AWS IoT Core triggers the rule, the rule invokes your Lambda function asynchronously and passes data from the IoT message to the function. This lambda function will be responsible for receiving the message and storing the information in the previously created DynamoDB collection(IoTCatalog).</p><h3>Steps:</h3><h4>1 — Open the <a href="https://console.aws.amazon.com/lambda/home#/functions">Functions Page</a> on the Lambda console.</h4><h4>2 — Choose Create Function.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GhWXcGxNxpwEOKuI.png" /></figure><h4>3 — Under Basic Information, do the following:</h4><ul><li>For Function Name, enter IoTSaveEventData.</li><li>For Runtime, confirm that Node.js 14.x is selected.</li></ul><h4>4 — Choose to Create function.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FeicAWORU-NlrYOC.png" /></figure><h4>5 — Add code to the lambda function</h4><p>Lambda automatically creates default code for the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9EhT6yVX9oAu849B.png" /></figure><p>The code showed below, will receive data from our IoT device, routed by the AWS IoT rule, and the function will create a new item in the DynamoDB collection:</p><p>We update the code with the script given below. Click on deploy, and we will have the function ready to use.</p><h4>Lambda function code</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c2a91dc43f2ca19a95e1a60951aaec3/href">https://medium.com/media/5c2a91dc43f2ca19a95e1a60951aaec3/href</a></iframe><h3>Configure the AWS IoT Rule to trigger the Lambda function</h3><h3>AWS IoT Rules Introduction</h3><blockquote><em>Rules give your devices the ability to interact with AWS services. Rules are analyzed and actions are performed based on the </em><strong><em>MQTT topic stream</em></strong></blockquote><p>You can create <strong>AWS IoT rules</strong> for devices to interact with AWS services. The AWS IoT Rules Engine provides an <strong>SQL-based language</strong> to select data from message payloads and send the data to other services, such as<strong> Amazon S3</strong>, <strong>Amazon DynamoDB,</strong> and <strong>AWS Lambda</strong>. Define a Rule to invoke a Lambda function when you want to invoke another AWS service or a third-party service.</p><h3>Create the AWS IoT Rule to trigger a Lambda function:</h3><p>In <a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704">Part 1</a>, we started sending AWS Core telemetry data to a specific <strong>topic</strong>: house/1/temperature.</p><p>It is now time to redirect the traffic of that topic to a destination; in this case, we will send the traffic out to an AWS Lambda function.</p><p>Here is where AWS IoT - Rules enter into action; let’s create this Rule.</p><h4>1- Open the Rules hub of the AWS IoT console and choose to Create.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MIUZ1vlGLGfKSKRh.png" /></figure><h4>2- Locate the top part of Create a rule.</h4><p>Enter the rule(my_rule_to_lambda) name and description(AWS IoT to lambda).</p><p>In the Rule query statement of Create a rule:</p><pre>SELECT * FROM &#39;house/1/temperature&#39;</pre><h4>3- Locate Set one or more.</h4><ul><li>Choose Add action.</li><li>Choose to Send a message to a Lambda function.</li><li>Click on configure the action.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Dy-s7NL9yMKTkeVW.png" /></figure><h4>4- Locate Configure action.</h4><ul><li>In Function name, choose Select.</li><li>Choose IoTSaveEventTelemetryData.</li><li>Click on Add action.</li><li>At the last click on Create rule.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*acWbqjDQMaLJidtG.png" /></figure><h3>Testing complete flow</h3><p>In <a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704">Part 1</a>, we set up our <strong>Raspberry Pi</strong>, created our<strong> Node JS application</strong>, and sent telemetry data to the <strong>AWS IoT instance</strong>.</p><p>It is now time to test the complete flow, so let’s open our <strong>Node JS app</strong> and run it.</p><pre>nodemon index.js</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yJ2GFwwfDj-EYukB.png" /></figure><p>When an incoming IoT message triggers the rule, AWS IoT invokes the Lambda function asynchronously and passes data from the IoT message to the lambda function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Giyi3uS_vMrqjnA7.png" /></figure><h3>Wrapping up</h3><p>At this point, the lambda function receives the data from the <strong>IoT device (Raspberry Pi)</strong> and saves it on the<strong> DybamoDB collection</strong></p><p>Finally can see the information below on the screen the application starts to populate the <strong>DybamoDB</strong> collection with telemetry data sent out by the <strong>Node.js application.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3vQFtrReNrI-IDBe.png" /></figure><h3>Demo video</h3><p>I hope you have enjoyed this article. Lastly, I left you this demo video where you can see in action the complete flow. For this case, I used a real temperature and humidity sensor (<strong>DHT-11</strong>), and the sensor data is store in the <strong>DynamoDB</strong> collection we have created.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F618151640%3Fh%3Db888b8dda9%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F618151640&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1260246386-672365256ed7372590d410e5b1e95e04c43da32c5a622a67e_640&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="720" height="1280" frameborder="0" scrolling="no"><a href="https://medium.com/media/b479992e89327f0a25ba92a0f840e34f/href">https://medium.com/media/b479992e89327f0a25ba92a0f840e34f/href</a></iframe><pre>I work at <a href="http://sytac.io/">Sytac.io</a>; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run <a href="https://medium.com/dev-jam">DevJam</a>, check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40a7d4ea35b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9">Tutorial Part 2: AWS IoT + Rules + Lambda Function + DynamoDB</a> was originally published in <a href="https://medium.com/dev-jam">Devjam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with AWS IoT Core and MQTT Protocol with a Node JS Example]]></title>
            <link>https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704?source=rss-d5f55cd09680------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed16bd542704</guid>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[aws-iot-core]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Ezequiel Miranda]]></dc:creator>
            <pubDate>Fri, 13 Aug 2021 07:39:07 GMT</pubDate>
            <atom:updated>2021-10-26T16:34:45.562Z</atom:updated>
            <content:encoded><![CDATA[<h3>Tutorial: Getting Started with Amazon AWS IoT, MQTT Protocol, and Node JS.</h3><blockquote>Update: This article is part of a series. Check out the series: <a href="https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9">Part 2</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*_sj9DXXWi15kVqgQ" /></figure><h3>0 — Introduction</h3><p>In this article, we will walk you through the steps involved in getting started with <strong>AWS IoT</strong> <strong>Core</strong> connecting the popular source —<strong> Raspberry Pi</strong>. It will guide you to set up an AWS IoT Core instance and write a simple IoT application that helps us to understand the basics of peer-to-peer communication using a <strong>Node Js </strong>program and <strong>MQTT protocol</strong>.</p><p>You will be writing a <strong>Node.js</strong> client app to post data from your <strong>Raspberry Pi</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wC_ioSgWk_fs_KkKMpADWA.png" /></figure><h3>1 — Message Queue Telemetry Transport (MQTT)</h3><p>The Internet of Things is about connecting everything. From tiny sensors to devices that control large systems. You can connect anything to the Internet, but the problem is connecting all this using a standardized protocol. Many protocols have been tried and tested. Some of the most common include <strong>HTTP</strong>, <strong>TCP/IP</strong>, and <strong>HTTPS</strong>. However, these protocols are not optimized for efficiency. Also, the house of IoT is built on a foundation of low power and battery efficiency. This means that IoT devices need to have a small footprint.</p><h3>2 — AWS IoT Core</h3><p>AWS IoT Core is a managed cloud platform that allows connected devices to easily and securely interact with cloud applications and other devices. AWS IoT Coore can support billions of transactions per day from thousands of devices, streamlining communication between the growing number of internet-connected things (including sensors, actuators, embedded devices, edge servers, gateways, mobile devices, and wearables) and the cloud.</p><p><strong>Steps:</strong></p><ul><li>2.1 Register new thing (device)</li><li>2.2 Create a certificate</li><li>2.3 Create and associate policies</li><li>2.4 Create Amazon Cognito</li></ul><h3>2.1 — Register New Thing (device)</h3><p>The first step will be to register the IoT device on AWS IoT Core; in this case, it will be a <strong>Raspberry Pi 4</strong>.</p><p>A <strong>thing</strong> resource is a digital representation of a physical device or logical entity in AWS IoT Core.</p><p>In order to register a new IoT <strong>device</strong> go to the AWS IoT Core dashboard, access “<strong>Manage”,</strong> and click on “<strong>Things”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jBes9ZNY198dE96I.png" /></figure><p>Click “<strong>Create thing”</strong> which will ask how many devices you want to add. Chose <strong>“create a single thing”</strong> and click. <strong>“Next”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/0*LhGKByuyQAv5yv2M.png" /></figure><p>Now we will be asked to name your new thing (my-raspberry-01), so we choose a name for it.</p><p>Leave the other parameters with the default configuration and click on <strong>“Next”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MNUVjFMll6PXl1lS.png" /></figure><h3>2.2 — Create a certificate</h3><p>A device requires a certificate to connect to AWS IoT Core. Select Auto-generate a new certificate. This will generate a certificate, a public key, and a private key using AWS IoT’s certificate authority.</p><p>Leave the policy creation for later, click on <strong>“create thing”</strong> and we will get the next screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IlpoGcRNoOQO_fkp.png" /></figure><p>Download your Device certificate and key files. The key files are unique to this certificate and can’t be downloaded after you leave this page. Download them now and save them in a secure place.</p><p>Download the <strong>Root CA certificates</strong> (Download the root CA certificate file that corresponds to the type of data endpoint and cipher suite you’re using. You can also download the root CA certificates later.)</p><p>Once you have downloaded all of your certificates and keys, click <strong>“Done”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/0*pdd7TGGYilTimjtt.png" /></figure><h3>2.3 — Create and associate policies</h3><p><strong>What is a policy?</strong></p><p>AWS IoT policies give things permission to access <strong>AWS IoT resources</strong> (like other things, MQTT topics, or thing shadows).</p><p><strong>Create a policy:</strong></p><p>We access the section <strong>secure-&gt;policies</strong> and click on “<strong>New policy”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/0*roFX9MVu1wb3DzSx.png" /></figure><p>Chose a policy name my-policy and set up the next statements:</p><p><strong>Action:</strong> iot:*<br><strong>Resource ARN</strong>: *</p><p>This policy will grant permission to the IoT things for all actions and for all our <strong>ARN</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CEmvNJj82XcwcKzZ.png" /></figure><p><strong>Link certificate with policy</strong></p><p>Next, attach the policy to the certificate created in step 2.</p><p>Access <strong>secure-&gt;certificates</strong> and then click the certificate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*c-IiJTs-T3Y8lDw6.png" /></figure><p>Access the <strong>“Policies”</strong> section, click <strong>“Actions”</strong>, then click <strong>“Attach Policy”.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dAHXxK6vFHYglxB-.png" /></figure><p>Select your policy and click <strong>“attach”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/0*CwIOSvwKSKY34MCq.png" /></figure><p><strong>Attach the certificate to the thing and activate</strong></p><p>On the same screen, click <strong>“Things”</strong>, click the thing (<strong>my-raspberry-01</strong>), then click <strong>“Actions”</strong> to select <strong>“Activate”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yWIUQS4OBWdWkuY5.png" /></figure><h3>2.4 — Create Cognito</h3><p>In this step, you will create a new identity pool, which will help you to connect your client application to the IoT Core service.</p><p>Access Amazon Cognito through <a href="https://console.aws.amazon.com/cognit">https://console.aws.amazon.com/cognit</a> and click on <strong>“Manage Identity Pools”</strong>. You will be redirected to the wizard to create a new identity pool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Rv8rhFTtrxhpOZ8E.png" /></figure><p>Enter a name for the identity pool, set <strong>“Enable access to unauthenticated identities”</strong>, and click <strong>“Create Pool”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7YoEnITuWmK2fvas.png" /></figure><p>Before you continue, you will need to copy the <strong>ARN name</strong> from the thing you created on AWS IoT. This can be found in the <strong>AWS IoT Core instance.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y_imK7bHdDcGwidj.png" /></figure><p><strong>Add ARN to the pool</strong>*</p><p>Once you have copied the <strong>ARN</strong>, continue with the configuration of the pool on <strong>Cognito</strong>.</p><p>Access the <strong>“Role Summary”</strong> and click<strong> “View Policy Document”</strong>. update the Policy document. Click <strong>“Edit”</strong> and then add the ARN to the<strong> “Resource”</strong> array. Omit the name of the thing and click<strong> “Allow”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-_xmMM0aorVO4Gp6.png" /></figure><h3>3 — Create Node JS program</h3><p>In this application, you will create an AWS IoT Core client using <strong>Node.js</strong> and will then send data to the AWS IoT server using the <strong>MQTT protocol</strong>.</p><p><strong>The code will:</strong></p><ul><li>use<a href="https://www.npmjs.com/package/aws-iot-device-sdk"> <strong>aws-iot-device-sdk</strong></a> to connect an MQTT client to the AWS IoT Core instance,</li><li>mockup some dummy data for testing,</li><li>send an MQTT message to the server every 3 seconds (this message will be tagged with a specific topic).</li><li>and subscribe the MQTT client a handler fon and specific topic.</li></ul><p>Initialize a new npm project running:</p><pre>npm init</pre><p>Install the package: <a href="https://github.com/aws/aws-iot-device-sdk-js">aws-iot-device-sdk-js</a></p><pre>npm install aws-iot-device-sdk</pre><p><strong>AWS Core Topics</strong></p><p><strong>MQTT topics</strong> identify <strong>AWS IoT messages</strong>. AWS IoT clients identify the messages they <strong>publish</strong> by giving them topic names. <strong>Clients</strong> identify the messages to which they want to <strong>subscribe</strong> (receive) by registering a topic filter with AWS IoT Core. The message broker uses topic names and topic filters to route <strong>messages</strong> from publishing clients to subscribing clients.</p><p><strong>Application code:</strong></p><p>Create your code file and start the application: node index.js</p><p><strong>index.js</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/faf504c1acc1fcf999c36d138e7b0c23/href">https://medium.com/media/faf504c1acc1fcf999c36d138e7b0c23/href</a></iframe><p><strong>Client configuration notes:</strong></p><p>Access the host endpoint from the AWS IoT Core dashboard; click <strong>“Settings”</strong> and copy the custom endpoint.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/0*D7qbX6xbPYSLEnvo.png" /></figure><p>Now, place the certificates you created earlier in the code folder on the Raspberry device.</p><h3>4 — Test Communication (D2C and C2D)</h3><p>AWS Core provides a monitor tool; access it by clicking <strong>MQTT test client</strong>.</p><p><strong>Test D2D</strong></p><p>First, test out the communication D2C (device-to-cloud). Then, click on <strong>“Subscribe to a topic”</strong>, input the <strong>“Topic filter” </strong>house/temperature/room1, and click <strong>“Subscribe”</strong>.</p><p>With the script running, You will be sent to the cloud messages with a specific topic.</p><p>If everything is ok, you should be able to see the messaging sent from our Raspberry with the dummy telemetry data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qyW0x-5Js2hTu1Vy.png" /></figure><p><strong>Test C2D</strong></p><p>Now, test the communication <strong>C2D (cloud-to-device)</strong>. Click <strong>‘Publish to a topic’</strong>, input the <strong>‘Topic name”</strong> core/broadcast , and click “<strong>Publish”.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*52DJFpT62Invtxua.png" /></figure><p>Now, access the <strong>console</strong> of your Node.js application.</p><p>If the code is running and the client is subscribed correctly to the topic, you should be able to log the data from the AWS IoT Core.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mwdPHVHusEad_8Zr.png" /></figure><h3>Wrapping up</h3><p>At this point, you have set up an <strong>AWS IoT Core</strong> instance, created a <strong>Node.js</strong> program (running on a <strong>Raspberry Pi</strong>) that sends events to the cloud using an <strong>MQTT client,</strong> and tested out the messaging flow.</p><p><strong>Thank you for your interest! Any feedback is welcome.</strong></p><pre>I work at <a href="http://sytac.io/">Sytac.io</a>; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run <a href="https://medium.com/dev-jam">DevJam</a>, check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed16bd542704" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dev-jam/getting-started-with-aws-iot-core-and-mqtt-protocol-with-a-node-js-example-ed16bd542704">Getting Started with AWS IoT Core and MQTT Protocol with a Node JS Example</a> was originally published in <a href="https://medium.com/dev-jam">Devjam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Motion Sensor- Enabled Tracking with Raspberry PI and Azure IoT]]></title>
            <link>https://medium.com/dev-jam/motion-sensor-enabled-tracking-with-raspberry-pi-and-azure-iot-f1e68aa2f15e?source=rss-d5f55cd09680------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1e68aa2f15e</guid>
            <category><![CDATA[azure-iot-hub]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[rasperry-pi]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Ezequiel Miranda]]></dc:creator>
            <pubDate>Mon, 28 Jun 2021 11:18:22 GMT</pubDate>
            <atom:updated>2021-10-26T16:34:05.329Z</atom:updated>
            <content:encoded><![CDATA[<h3>Motion Sensor-Enabled Tracking with Raspberry PI and Azure IoT</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*lm5XjY8rFSP5ve-r" /></figure><h3>Introduction</h3><p>Hello Folks</p><p>It’s Ezex here on the other side of the screen :)</p><p>Today I bring you an article about some of my recent playground experiments with some technologies focusing on the Internet of things.</p><p>These are the results of my experiments, which I will show you how to build.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p39IcGkuc0bcLdQvVSq-tg.jpeg" /></figure><p><strong>Disclaimer</strong>: I’m pretty new on the IoT world, so I will present this case as an experiment. In which I start simply playing with these technologies.</p><p>In this first experiment, we will implement a Proof of concept for the next use case:</p><p><strong>“As an IoT developer, I want to connect a Passive Infrared sensor to a Raspberry Pi4, detect movements in a room and communicate these events to a dedicated IoT cloud service”</strong></p><p>To achieve this, we will test the integration between <strong>Raspberry Pi 4</strong>, a PIR sensor, <strong>Node.js</strong> development environment, <strong>Azure IoT Hub</strong>, To implement the next cloud application design:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*73bOa1UFYoI42F6xxX5ryg.jpeg" /></figure><h3>Table of contents</h3><ol><li>Experiment scope</li><li>Introduction to Raspberry</li><li>General-purpose input/output (GPIO)</li><li>Electronic components (PIR sensor)</li><li>Azure IoT Hub</li><li>Create a demo App(Node.js)</li></ol><h3>1. Experiment scope</h3><h4>What do we want to achieve?</h4><p>- Setting up the Raspberry OS<br>- Setting up the development environment<br>- Creating an application service (send sensor events to Azure IoT Hub)<br>- Monitoring cloud IoT hub messages</p><h3>2 . Introduction to Raspberry</h3><p>For the hardware part, I chose a <strong>Raspberry Pi 4</strong>. For those who don’t know, a Raspberry Pi is a series of small single-board computers. This small piece of hardware enables the installation of a series of different operating systems.</p><p>The Raspberry Pi comes with a <strong>GPIO</strong> pins module that allows you to connect different electronic components.</p><p><strong>How it looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ba4YEJTL4rr3dqaEHSieow.jpeg" /></figure><h3>Setting up the operating system</h3><p>The officially supported operating system is <strong>Raspberry Pi OS</strong> (Previously called Raspbian), and under the hood is a <strong>Debian</strong>-based operating system for <strong>Raspberry Pi</strong>.</p><p>For proper instruction on installing the OS on your hardware. follow this link</p><p><a href="https://www.raspberrypi.org/software/">https://www.raspberrypi.org/software/</a></p><h3>Setting up the remote development environment</h3><p>To develop and run our code, we can choose between using the raspberry OS environment directly or connecting to it remotely through <strong>SSH</strong>.</p><p><a href="https://code.visualstudio.com/docs/remote/ssh">Developing on Remote Machines using SSH and Visual Studio Code</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*IP_eto1oLo0CDY71aaB8cQ.png" /></figure><p>We are using <strong>node.js</strong> for our application, so if it is not pre-installed on the environment, you will need to download and install it.</p><h4>Tasks to complete on this step</h4><ul><li>Enable <strong>ssh</strong> connection on <strong>Raspberry OS</strong><br>- Install <strong>node.js</strong> — <a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a><br>- Install <strong>Visual Code editor’s extension</strong> for remote development on a client computer <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack">https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack</a></li></ul><p>As a default, your Raspberry Pi comes configured with the following SSH Credentials:</p><pre>User: raspberry</pre><pre>Password: pi</pre><p><strong>But is very recommended you change to new ones.</strong></p><p>At this point, we should able to connect our <strong>Visual Code </strong>editor to a remote host (<strong>Raspberry Pi</strong>) and map the disk in order to code and run the code remotely.</p><h3>3. Raspberry GPIO</h3><p>GPIO is an input/output system for general purpose, which has a series of pins you can connect sensors, actuators, and other electronic components.</p><p>The number and type of pins will vary depending on the Raspberry model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L0WIznwMc5k2OWeWeTDH5A.png" /></figure><p><strong>Pins types:</strong><br>- 5V<br>- 3V<br>- Ground<br>- Multipurpose pin</p><p>To find proper information about the module on your device, you can look at the official documentation.</p><p>Also, you can use software such as <strong>Pinout</strong> (Already installed on Raspberry OS).</p><p>This <strong>GPIO</strong> Pinout is an interactive reference to the Raspberry Pi GPIO pins, and a guide to the Raspberry Pi’s GPIO interfaces.</p><p><a href="https://pinout.xyz/">https://pinout.xyz/</a></p><p>Running the <strong>CLI</strong> application on your console:</p><pre>pinout</pre><p>We will get the next output with the description of the pins:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*zj5Y30AKjSf1yO-P0HA_Cw.png" /></figure><h4>GPIO libraries</h4><p>To interact with the GPIO we can choose between different languages and libraries.</p><p><strong>For example:</strong></p><ul><li>For <strong>Node.js</strong>, we can use the npm package <strong>onoff</strong> <a href="https://www.npmjs.com/package/onoff">https://www.npmjs.com/package/onoff</a></li><li>For <strong>Python</strong> the GPIO package <a href="https://pypi.org/project/RPi.GPIO/">https://pypi.org/project/RPi.GPIO/</a></li></ul><h3>4. Electronic Components</h3><p>In this experiment, we will use a PIR sensor to detect movement.</p><p><strong>PIR</strong> — <a href="https://en.wikipedia.org/wiki/Passive_infrared_sensor">Wikipedia</a></p><blockquote>A passive infrared sensor is an electronic sensor that measures infrared light radiating from objects in its field of view. They are most often used in PIR-based motion detectors. PIR sensors are commonly used in security alarms and automatic lighting applications</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*35xg49KBysrVzqaB.jpg" /></figure><p>In order to connect our PIR sensor on the Raspberry we should connect with the appropriate pins:</p><ul><li>(-) — To any GND pin (Pin 6 on this example)</li><li>(3.3v) — Pin number 1 for providing the power</li><li>(s) — Connect the signal pint to any GPIO pin, in this tutorial we use the GPIO number 27.</li></ul><h3>5. Create an Azure IoT Hub</h3><p>In this part of the article, we will start playing with Azure IoT Hub, but first I will give a little introduction to the features of this Azure Service.</p><p><strong>Azure IoT Hub</strong> is a managed service for bidirectional communication between IoT devices and Azure.</p><p>For this use case, we will use device-to-cloud communication.</p><p><strong>Device-to-cloud:</strong></p><p>Your devices can send data, alerts, events to the IoT hub and can also connect with other Azure services.</p><p><strong>Steps:</strong><br>- Create account<br>- Create IoT Hub instance<br>- Register device</p><p><strong>Create account</strong></p><p>If you are new to the Azure environment you can apply for a free account and get 12 months of free services.</p><p><a href="https://azure.microsoft.com/en-us/free/">Create Your Azure Free Account Today | Microsoft Azure</a></p><h4>3.1 Create IoT Hub</h4><p>On this main page, you can see your IoT Hub instances</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SJy0aZ1sjdC6DIiXcV1d5Q.png" /></figure><p>Let’s create a new instance, so complete the fields required and click the <strong>Review + create</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*APR_ZZ5A8ixw5o9hpwoXyA.png" /></figure><p>Now we have the IoT Hub instance is created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*5VaNJ72hWx2tdPxdF3Cx-w.png" /></figure><h4>5.2 — Register a new IoT device</h4><p>The next step will be to register your hardware device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X7DBCcAS_kV9c2SBOr9c6Q.png" /></figure><p>We must choose a device ID; in this case: <strong>myRasperryPi01</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Na2rQkQRPoAbuWn76xV6Sw.png" /></figure><p>Once the new device is created, we click on it and take note of the Primary Connection String.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*-vfKKQYWmnrRF5jp2KKBlw.png" /></figure><h3>6 . Create a demo App(Node.js)</h3><p>At this point, we will start coding our application the node.js script which is responsible for communication events captured by the sensor and sending those to the IoT Hub.</p><p>Comment</p><p>For communication from the device to the cloud, we will use Message Queuing Telemetry Transport (<strong>MQTT</strong>) client provided by Azure.</p><p><strong>The application will:</strong></p><ul><li>Detect movement in the room with a PIR sensor</li><li>Light up the LED</li><li>Send event messages to the IoT Hub (Using MQTT)</li></ul><p><strong>Steps:</strong></p><p><strong>1 — Create a new node.js app</strong></p><pre>npm init my-iot-demo-project</pre><p><strong>2 — Install dependencies</strong></p><p>With the next command, we will install the dependencies needed for the application:</p><pre>npm install onoff azure-iot-device azure-iot-device-mqtt</pre><p>Dependencies:</p><ul><li><a href="https://hackmd.io/2Hgh2icnQcawDu7NugPJag?both">onoff</a>- Access to GPIO</li><li><a href="https://hackmd.io/2Hgh2icnQcawDu7NugPJag?both">azure-iot-device</a> — A set of utilities for IoT devices on azure</li><li><a href="https://hackmd.io/2Hgh2icnQcawDu7NugPJag?both">azure-iot-device-mqtt</a> — MQTT client</li></ul><p><strong>3 — Create node.js application</strong></p><pre>//index.js</pre><pre>// Importing dependencies<br>const Gpio = require(&#39;onoff&#39;).Gpio;<br>const Client = require(&#39;azure-iot-device&#39;).Client;<br>const Message = require(&#39;azure-iot-device&#39;).Message;<br>const MqttProtocol = require(&#39;azure-iot-device-mqtt&#39;).Mqtt;</pre><pre>// Set up GPIO pin<br>const PIR = new Gpio(27, &quot;in&quot;, &#39;both&#39;);</pre><pre>var client, connectionString<br>const connectionStringParam = &#39;&lt;YOUR_CONNECTION_STRING_PARAM&#39; // from the Azure IOT device registration;</pre><pre>// Start MQTT Client<br>function startClient() {<br>    console.log(`[Application] -  Running`)<br>    lightUpLED()<br>    setTimeout(lightDownLED, 1000)<br>    connectionString = process.env[&#39;connectionString&#39;] || connectionStringParam;<br>    client = Client.fromConnectionString(connectionString, MqttProtocol);<br>    return client<br>}</pre><pre>// Send messages to Cloud (Azure IoT Hub Instance)<br>function sendMessage() {<br>    const now = new Date()</pre><pre>var contentEvent = JSON.stringify({<br>        time: now, room: 4, message: &#39;A person has entered the room&#39;<br>    })</pre><pre>var message = new Message(contentEvent.toString(&#39;utf-8&#39;));<br>    message.contentEncoding = &#39;utf-8&#39;;<br>    message.contentType = &#39;application/json&#39;;</pre><pre>client.sendEvent(message, (err) =&gt; {<br>        if (err) {<br>            console.error(&#39;[Device] - Failed to send message to Azure IoT Hub due to:\n\t&#39; + err.message);</pre><pre>}<br>        else {<br>            console.log(&#39;[Device] - Message sent to Azure IoT Hub&#39;);<br>            console.log(message)<br>        }<br>    })</pre><pre>}</pre><pre>// Init application and setting event listener for PIR sensor.<br>// A valid config.json IoT Device is needed to start the application<br>// The application will send a new event to the IoTHub every time the sensor detects a person</pre><pre>(function () {<br>    try {<br>        client = startClient()<br>        PIR.watch(sendMessage)<br>        <br>    } catch (err) {<br>        console.error(&#39;Failed to start the client:\n\t&#39; + err.message);<br>        return;<br>    }<br>})();</pre><p><strong>Run project</strong></p><p>To start our application, we will run the following command:</p><pre>node index.js</pre><p>Anytime the sensor detects movements the application will send new event data to the cloud, also we will get a message log on the console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*CfqGnSaIoa4VktIzJBCNQg.png" /></figure><p><strong>5 — Monitor the messages received by your Hub</strong></p><p>If you are using Visual Code Editor, you can install the next plugin and monitor the events sent to the IoT Hub on the output console.</p><p><strong>Visual Code plugin:</strong> <a href="https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-tools">azure-iot-tools</a></p><p>Once the plugin is installed, you will see a new section on the editor, Azure IoT Hub in which you can inspect the devices connected to your IoT Hub. In this case, the Raspberry Pi</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lNqr_mp-gpd1JNJS.png" /></figure><p>Each time your application detects a person moving in the room, a message will be received by the IoT default Built-in endpoint, as shown above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*N-cdjFUPHhgNXdH-.png" /></figure><h3>Wrapping up</h3><p>At this point, we have finished our experiment: we have set up our Raspberry Pi, connected the PIR sensor to it, set up our development environment, created our node.js application, and sent events detected by the sensor to the Azure IoT Hub instance.</p><p><strong>Thank you for your interest! Any feedback is welcome :)</strong></p><pre>I work at <a href="http://sytac.io/">Sytac.io</a>; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run <a href="https://medium.com/dev-jam">DevJam</a>, check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1e68aa2f15e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dev-jam/motion-sensor-enabled-tracking-with-raspberry-pi-and-azure-iot-f1e68aa2f15e">Motion Sensor- Enabled Tracking with Raspberry PI and Azure IoT</a> was originally published in <a href="https://medium.com/dev-jam">Devjam</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>