Reader small image

You're reading from  Arduino IoT Cloud for Developers

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781837637171
Edition1st Edition
Right arrow
Author (1)
Muhammad Afzal
Muhammad Afzal
author image
Muhammad Afzal

Muhammad Afzal is a senior software engineer, with more than 14 years of experience working on web-based and IoT systems in multinational organizations. He always enjoys working and solving real-world business problems with technology. He provides freelance services to IoT-based product companies to write technical reviews and projects, and he also provides consultancy to organizations. In his free time, Muhammad creates videos and courses for YouTube and Udemy. He also runs a maker movement in his region for young students to boost their interest in adopting the latest technologies.
Read more about Muhammad Afzal

Right arrow

Working with the Arduino IoT Cloud SDK and JavaScript

Every platform provides APIs and SDKs to make their product compatible with other platforms, and these endpoints are used by developers to create new functionalities to solve real-world issues. Likewise, the Arduino IoT Cloud comes with its own built-in features and functionalities, but there are also three different ways to extend the Arduino IoT Cloud platform’s functionality or make it compatible with other tools and services: namely, REST APIs, SDKs, and webhooks.

In this chapter, we will talk about APIs and SDKs and how they work. Specifically, we will use the Node.js SDK to illustrate different coding exercises. This chapter will cover Create, Read, Update, and Delete (CRUD) operations for devices, Things, properties (aka variables), and dashboards. Beyond that, we will explore some other features such as how to set/get values of properties.

By the end of this chapter, you will understand how to interact with...

Technical requirements

There are no specific hardware requirements for this chapter as we are focusing on the SDK and will work through different coding exercises to perform different operations on the Arduino IoT Cloud platform using the Node.js SDK. To follow along with the coding and testing exercises, the following software is required:

  • Node.js
  • The Node.js Arduino IoT Cloud module installed
  • The VSCode editor
  • Postman
  • An Arduino IoT Cloud account

Postman will be used for cURL testing and authentication, which will help other developers when doing request and response testing. For SDK coding, we will use Node.js and the VSCode editor. The code for this chapter is available from the book’s official GitHub repository at https://github.com/PacktPublishing/Arduino-IoT-Cloud-for-Developers.

Demystifying the Arduino IoT Cloud SDK – functionality and operation

The Arduino IoT Cloud offers a range of features, but there are inevitably situations where developers need to integrate their tools/systems/apps with the Arduino IoT Cloud for improved operations management and control. To cater to these requirements, the Arduino IoT Cloud provides different techniques and tools that can be used by developers and organizations to extend the product’s functionality and provide solutions for real-world issues.

As shown in the following diagram, the Arduino IoT Cloud provides three main interfaces to developers to help meet their requirements. First is webhooks, which we used in the previous chapter to send the data from an Arduino IoT Cloud Thing to a custom endpoint or any well-known platform including Zapier or IFTTT for further integration. Following webhooks, we have the REST API and SDK, both of which are very handy features for developers to mold the system according...

Securing access – unveiling API keys and authentication

Before heading into the meat of the chapter, we first need to talk about the authentication mechanism of the platform. The Arduino IoT Cloud uses a token authentication mechanism to validate API requests. This involves the SDK/tool sending the Client ID and Client Secret to https://api2.arduino.cc/iot/v1/clients/token to get a token that is used later to authenticate for requests/responses.

Before we get started with the SDK installation and testing with Postman, we need to set up the API in the Arduino Cloud that will provide us the Client ID and Secret. In older versions of the interface, the API options are under the Integrations tab, but have now moved to the Arduino Cloud, which is available at https://cloud.arduino.cc/home/.

After visiting the Integration page click on API Keys and you will be taken to the API page. Click on CREATE API KEY. A popup will appear; provide a name for the API and click on the CONTINUE...

Initial exploration – test drive using Postman

Before diving into the SDK, we will try the naked REST APIs endpoints using Postman, a tool that helps developers to debug requests/responses for other programming-language platforms not available as SDKs. Postman is a well-known tool among backend developers and is used to expose API requests/responses without any coding. Postman also helps developers to analyze responses via a graphical user interface (GUI).

Download Postman from https://www.postman.com/, choosing the correct version for your operating system. Before getting to work with Postman, firstly create a new workspace for the Arduino IoT Cloud to put all the requests in a separate group. To do this, click on Workspaces and then Create Workspace:

Figure 8.3 – Postman workspace

Figure 8.3 – Postman workspace

A new window will be opened: provide a workspace name and description and set the visibility policy according to your requirements. After creating the new...

Diverse platform compatibility – SDKs for various environments

After testing request/response with Postman, it’s time to explore the SDKs available for the Arduino IoT Cloud. Earlier I mentioned the Arduino Team has developed official SDKs for three programming platforms, Node.js, Python, and GoLang.

Node.js is a very well-known language among backend developers and is used by both web and mobile developers. So, if you are planning to create a mobile or web app for Arduino IoT Cloud automation with custom visualizations, then Node.js is the best language, providing a wide variety of libraries for HTTP services.

On the other hand, Python is also a very well-known and rapidly growing language among web, IoT, and AI/ML developers. Python seems to be trending toward AI/ML, and IoT is very close as a field to AI/ML. If you are planning to develop a solution focused on AI/ML, such as predictive maintenance, time series analysis, or predictions then the Python SDK is the...

Step-by-step – installing the Arduino Node.js SDK client

From this point, we are going to use the Node.js SDK for our exercises and experiments. Before moving to the practical exercises, we need to first install the Arduino IoT Cloud package for Node.js. Download and install Node.js from https://nodejs.org/. After the installation of Node.js is complete, open up the Windows/Mac/Linux Terminal and navigate to the directory where you will set up your coding workspaces using VSCode:

npm install @arduino/arduino-iot-client
npm i request-promise

Type the two preceding commands in the Terminal and wait for the installation to complete. After a successful installation, open up VSCode and add the folder (where you have installed the Node.js Arduino IoT Cloud SDK) to the workspace where you installed the Arduino IoT Cloud and request-promise modules.

Navigate to the book’s official GitHub repository and download all the code from this chapter’s folder. Copy the...

Interacting with devices – hands-on with the Node.js SDK

In this section, we are going to get hands-on and practical with devices. We will perform CRUD operations on a device as well as listing all the devices and showing all the parameters of specific devices. Open up VSCode and navigate to this chapter’s example code. Then click on the device folder: here you can find all the code related to device operations.

Creating a device

Firstly, we will start with device creation. The documentation on device creation can be found at https://www.arduino.cc/reference/en/iot/api/#api-DevicesV2-devicesV2Create. Open create-device.js under the device folder in VSCode.

There are two methods in the code: one is getToken(), which is the same method as in all our coding exercises and helps us to fetch the authentication token for requests. The second method is CreateDevice(), which is responsible for device creation. Consulting the documentation, we can see we need to initiate...

Engaging with Things – Node.js SDK implementation guide

The Thing is one of the most important elements in the Arduino IoT Cloud, and acts as defined container holding all of its ingredients such as cloud variables, the device, a sketch for the device, and the device’s network configuration. In this section, we will try a range of different coding exercises to get a feel for Thing CRUD operations.

Creating a Thing

Firstly, we will start with Thing creation. The relevant documentation can be found at https://www.arduino.cc/reference/en/iot/api/#api-ThingsV2-thingsV2Createe. Open the create-thing.js file under the thing folder in VSCode.

There are two methods in this code: one is getToken(), which is the same method as in all our coding exercises and helps us to fetch an authentication token for our requests. The second method is CreateThing(), which is responsible for Thing creation. Referring to the documentation, we can see that we need to initiate the api variable...

Exploring properties – Node.js SDK interaction techniques

Properties, also called cloud variables, are one of the most important ingredients for Things in the Arduino IoT Cloud, responsible for storage of sensor data from the device to the cloud or vice versa. In this section, we will explore how to create, update, delete, and list cloud variables using the Node.js SDK.

Creating a property

Firstly, we will start with property creation. The property creation documentation can be found at https://www.arduino.cc/reference/en/iot/api/#api-PropertiesV2-propertiesV2Create. Open the create-property.js file under the properties folder in VSCode.

There are two methods in the code: one is getToken(), which is the same method used in all our coding exercises to fetch the authentication token for our requests. The second method is CreateProperty(), which handles cloud variable creation. Consulting the documentation, we can see that we need to initiate the api variable with AiotApi...

Crafting dashboards – unleashing potential via the Node.js SDK

Dashboards are the most important ingredient in the Arduino IoT Cloud pertaining to data visualization. In the SDK, we have dashboard-specific methods to perform CRUD operations on dashboards, as well as other operations such as sharing, requesting access, and so on, but here we will only focus on the CRUD operations.

Creating a dashboard

Let’s start first with dashboard creation. The relevant documentation can be found at https://www.arduino.cc/reference/en/iot/api/#api-DashboardsV2-dashboardsV2Create. Open the create-dashboard.js file under the dashboard folder in VSCode.

There are two methods in this code: one is getToken(), which is the same method as in all our coding exercises to fetch the authentication token for our requests. The second method is CreateDashboard(), which is responsible for dashboard creation. Referring to the documentation, we can see we need to initiate the api variable...

Fine-tuning with the Node.js SDK – property value management

In this section, we will look at how we can set/get cloud variable values using the SDK. This is very useful for performing bulk operations; for example, if you have 100 lights connected to the Arduino IoT Cloud, it will be difficult to control them all manually from a dashboard. Suppose switching one device on/off takes 3 seconds from the dashboard – with 100 devices this will require 300 seconds, a total of 5 minutes, along with the chance of human error (maybe the user misses one device due to bulk processing). But with the help of the SDK, we can perform bulk operations on Things, saving time and improving confidence in our ability to reach zero-error operations.

Get a property value

Let’s first see how to get the last value of the cloud variable. To do this, we need the Thing ID and cloud variable ID. More precisely, we will get the complete properties of the cloud variable in JSON format here...

Assignment

After playing with SDK, it’s now time to do some more experiments so you can apply your learning in new scenarios and solidify your learning. For this assignment, you will create a script for Thing automation with the following properties:

  1. Set up a method for WeMos D1 Mini device creation. This device belongs to the ESP8266 category.
  2. Set up a Thing named Sense Environment and create a sketch for a Thing. Then, associate the previously created device with the Thing.
  3. Create three cloud variables/properties for temperature, humidity, and air quality and attach these variables to the previously created Thing.

Create the required script in one file containing all the methods, where we just provide the Thing name and it will automatically set up the device, Thing, and sketch, handle device association with the Thing, and take care of variable creation. Successfully completing this task will teach you to automate workflows and processes efficiently...

Summary

In this chapter, we covered API endpoints, SDKs, and the types of SDK platforms available to us on the Arduino IoT Cloud. We also learned how to test APIs with Postman. Next, we explored the Node.js SDK by performing different types of operations, particularly CRUD operations, on devices, Things, properties, and dashboards. Finally, we saw how to get and set the value of a cloud variable.

This chapter was specially designed for backend developers seeking to use the Arduino IoT Cloud as a foundation and wanting to develop a custom frontend for their solution, which is possible when you know how to interact with the Arduino IoT Cloud programmatically using the Node.js SDK. Likewise, the ability to create custom services allows us to integrate with third party cloud services and applications.

The next chapter will be more interesting as we are going to dive into smart agriculture, implementation a project where you will learn how to measure the soil moisture, soil temperature...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Arduino IoT Cloud for Developers
Published in: Nov 2023Publisher: PacktISBN-13: 9781837637171
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Muhammad Afzal

Muhammad Afzal is a senior software engineer, with more than 14 years of experience working on web-based and IoT systems in multinational organizations. He always enjoys working and solving real-world business problems with technology. He provides freelance services to IoT-based product companies to write technical reviews and projects, and he also provides consultancy to organizations. In his free time, Muhammad creates videos and courses for YouTube and Udemy. He also runs a maker movement in his region for young students to boost their interest in adopting the latest technologies.
Read more about Muhammad Afzal