|
1 | 1 | --- |
2 | 2 | title: Arduino IoT Cloud APIs |
3 | 3 | description: A summary of the three APIs available in the Arduino IoT Cloud |
4 | | -author: Karl Söderby |
| 4 | +author: Karl Söderby, Fabrizio Mirabito |
5 | 5 | tags: [IoT Cloud, API, JavaScript] |
6 | 6 | --- |
7 | 7 |
|
8 | | -The [Arduino IoT Cloud](https://create.arduino.cc/iot/) has several APIs that each provide a different set of functionalities. This article serves an entry point to these different APIs, how they function, and how you can utilize them in your projects. |
| 8 | +The [Arduino IoT Cloud](https://create.arduino.cc/iot/) has different sets of APIs that provide different functionalities. This article serves as an introduction to how to work and what you can achieve with them. |
9 | 9 |
|
10 | | -## Arduino IoT Cloud APIs |
| 10 | +## What you can achieve |
| 11 | +Arduino IoT Cloud API allows you to build a large range of IoT solutions: from simple device data communication to building complex custom IoT solutions. Some examples are: |
11 | 12 |
|
12 | | -There are three APIs to consider when working with the Arduino IoT Cloud. |
| 13 | +- Connect your Arduino or 3rd party Devices to the Cloud to monitor and control them, leveraging easy-to-use dashboards in minutes |
| 14 | +- Build your Gateways that poll data from local devices (ex.BLE, Zigbee) and send it to the Cloud |
| 15 | +- Develop your custom web or mobile application that leverages Arduino IoT Cloud infrastructure to handle device connectivity without worrying about protocols, security, data formats, scaling servers... |
| 16 | +- Automate and integrate different IoT Solutions with a single, powerful Cloud |
13 | 17 |
|
14 | | -- **ArduinoIoTCloud library** - an Arduino library for handling authentication, connection & data exchange on the device side. |
15 | | -- **Device Communication API** - a JavaScript package for communicating with the Arduino IoT Cloud MQTT broker. |
16 | | -- **Integrations API (REST)** - for managing Arduino IoT Cloud endpoints, such as **properties, devices, things**. Can be called with any HTTP Client or through our [JavaScript](https://www.npmjs.com/package/@arduino/arduino-iot-client), [Python](https://pypi.org/project/arduino-iot-client/), or [Golang](https://github.com/arduino/iot-client-go) modules. |
| 18 | +## Two Different Sets of API |
17 | 19 |
|
18 | | -## Arduino Libraries |
| 20 | +There are two different API types to consider when working with the Arduino IoT Cloud: |
19 | 21 |
|
20 | | -There are two main Arduino libraries that are included in each Arduino IoT Cloud project. |
| 22 | +**1. Applications API** - a set of powerful and flexible APIs that allows you to create and manage IoT resources like dashboards, devices, things, and variables, along with the retrieval and handling of historical data coming from your IoT Devices. The core of those APIs is organized around [REST](http://en.wikipedia.org/wiki/Representational_State_Transfer). Our API has predictable resource-oriented URLs, accepts [form-encoded](https://en.wikipedia.org/wiki/POST_(HTTP)#Use_for_submitting_web_forms) request bodies, returns [JSON-encoded](http://www.json.org/) responses, and uses standard HTTP response codes, authentication, and verbs. You can use those APIs, both directly calling our HTTP endpoints or using our clients that wrap those calls into easy-to-use abstractions like objects and functions. We have Applications API clients available in `javascript`, `golang`, and `python`. |
21 | 23 |
|
22 | | -- [ArduinoIoTCloud](https://github.com/arduino-libraries/ArduinoIoTCloud) |
23 | | -- [Arduino_ConnectionHandler](https://github.com/arduino-libraries/Arduino_ConnectionHandler) |
| 24 | +**2. Monitor and Control API** - a set of libraries that allow sending and receiving any kind of data (sensors' values, commands for actuators, configuration changes...) from and to IoT Devices and the Cloud. Under the hood, they |
| 25 | + - take care of the data exchange with our MQTT broker |
| 26 | + - handle best in class authentication & security |
| 27 | + - manage compression, data format, and transport protocols |
24 | 28 |
|
25 | | -Both libraries are automatically installed in the online environment, and any dependencies of these libraries are also automatically handled. |
| 29 | +Inside this set of API, we have: |
| 30 | + |
| 31 | +- An official Arduino IoT Cloud Library for your Arduino sketches: `ArduinoIoTCloud.h` |
| 32 | +- An NPM Javascript package: `arduino-iot-js` |
26 | 33 |
|
27 | 34 |
|
| 35 | +## Applications API |
| 36 | +To use the Applications API, you need to create an **API Key** in the [API Keys](https://cloud.arduino.cc/home/api-keys) section. |
28 | 37 |
|
29 | | -### ArduinoIoTCloud |
30 | | - |
31 | | -The [ArduinoIoTCloud](https://github.com/arduino-libraries/ArduinoIoTCloud) library handles the data exchange between your board and cloud. This library is a requirement when creating projects with the Arduino IoT Cloud, as it is used for several purposes: |
32 | | - |
33 | | -- Configuring the ECCX08 crypto chip onboard official Arduino hardware. |
34 | | -- Publishing and subscribing to channels using MQTTS (secure exchange of data over the Internet). |
35 | | -- Maintaining the connection between the board and cloud. |
36 | | -- Handling timers for update frequencies. |
37 | | - |
38 | | -The ArduinoIoTCloud library requires no installation, as it is automatically included in the online environment. When creating a Thing inside the IoT Cloud, a sketch is also automagically generated where the library is also included at the top of the sketch. |
39 | | - |
40 | | -### Arduino_ConnectionHandler |
41 | | - |
42 | | -The [Arduino_ConnectionHandler](https://github.com/arduino-libraries/Arduino_ConnectionHandler) library handles the various ways of connecting to the Arduino IoT Cloud: |
43 | | - |
44 | | -- Wi-Fi |
45 | | -- LoRa |
46 | | -- 5G |
47 | | -- GSM |
48 | | -- Ethernet |
49 | | - |
50 | | -It also provides a keep-alive functionality and automatic reconnection if the board loses its connection. |
51 | | - |
52 | | -### Libraries API |
53 | | - |
54 | | -In this section, you will find a list of most commonly used APIs in both libraries. |
55 | | - |
56 | | -- `initProperties()` - initializes all properties/variables based on your configurations. |
57 | | -- `ArduinoCloud.begin(ArduinoIoTPreferredConnection)` begins the connection to network based on your chosen preference (this is determined by the device you are using). |
58 | | -- `setDebugMessageLevel(2)` - level of granularity on error debug message. Default is `2`. |
59 | | -- `ArduinoCloud.printDebugInfo()` - prints the debug info in the Serial Monitor. |
60 | | -- `ArduinoCloud.update()` - synchronizes the variables on the board with the cloud. |
61 | | -- `ArduinoCloud.getLocalTime()` - gets the local time of your device. Your timezone is configured inside your Thing. |
62 | | - |
63 | | - |
64 | | -### Offline Usage |
65 | | - |
66 | | -The [ArduinoIoTCloud](https://github.com/arduino-libraries/ArduinoIoTCloud) & [Arduino_ConnectionHandler](https://github.com/arduino-libraries/Arduino_ConnectionHandler) libraries can be installed using an offline version of the IDE, including the Arduino CLI. |
67 | | - |
68 | | -Please note that updates made in your Thing configuration are not automatically reflected in your sketch when used locally. This is a great advantage of using the online environment. |
| 38 | +For more examples and detailed instructions, visit the links below: |
| 39 | +- [Applications API Technical Reference](https://www.arduino.cc/reference/en/iot/api/) - the complete REST API documentation with hundreds of examples. |
69 | 40 |
|
70 | | -## Device Communication API |
| 41 | +- [Arduino IoT Cloud Applications API Guide](/arduino-cloud/getting-started/arduino-iot-api) - a guide to using the API with JavaScript, Python & Golang. |
71 | 42 |
|
72 | | -Source: https://www.npmjs.com/package/arduino-iot-js |
| 43 | +## Arduino IoT Cloud Sketch library |
| 44 | +As described above, is the Arduino library that allows your Arduino devices to connect and exchange data with the IoT Cloud. If you edit your sketch directly inside IoT Cloud or Cloud Editor, you don't need to install it: it comes out of the box. |
73 | 45 |
|
74 | | -The **Device Communication API** is designed for communicating with the Arduino IoT Cloud MQTT broker. It is primarily used to publish and subscribe to MQTT channels, as it connects to the cloud via websockets. |
| 46 | +You can find more details at: |
75 | 47 |
|
76 | | -This can for example be used in a browser or node.js project to interact with your properties (variables). This allows you to for example build custom webpages that can stream data directly from an Arduino, or to interact with the Arduino from the browser. |
| 48 | +- The [official repository](https://github.com/arduino-libraries/ArduinoIoTCloud) |
| 49 | +- The [cheat sheet](https://docs.arduino.cc/arduino-cloud/getting-started/technical-reference) |
77 | 50 |
|
78 | | -For interaction with the data, 4 parameters are required: |
79 | | -- **Thing ID:** found inside your Thing. |
80 | | -- **Property Name:** the name of your variable, also found inside your Thing. |
81 | | -- **Client ID:** found in the [API keys](https://cloud.arduino.cc/home/api-keys) section. |
82 | | -- **Client Secret:** found in the [API keys](https://cloud.arduino.cc/home/api-keys) section. Only visible when generating a new API key. |
| 51 | +## Arduino IoT JS |
| 52 | +The `arduino-iot-js` NPM mobile is designed for communicating with the Arduino IoT Cloud broker using the MQTT over Websocket protocol. It is primarily used to send and receive variable values. |
83 | 53 |
|
84 | | -To connect to the Arduino Cloud, use: |
| 54 | +Example: |
85 | 55 |
|
86 | 56 | ```js |
87 | | -ArduinoIoTCloud.connect(options) |
88 | | -``` |
89 | | - |
90 | | -Listen to a property value: |
| 57 | +const { ArduinoIoTCloud } = require('@arduino/arduino-iot-js'); |
91 | 58 |
|
92 | | -```js |
93 | | -ArduinoIoTCloud.onPropertyValue(thingId, propertyName) |
94 | | -``` |
95 | | - |
96 | | -Send a property value: |
97 | | - |
98 | | -```js |
99 | | -ArduinoCloud.sendProperty(thingId, propertyName, value) |
100 | | -``` |
101 | | - |
102 | | -***Please see the full examples at [arduino-iot-js](https://www.npmjs.com/package/arduino-iot-js) for more information.*** |
103 | | - |
104 | | -## Integrations API (REST) |
105 | | - |
106 | | -The **Integrations API** is a REST API that allows you to connect from any HTTP client. With this API, you can interact with a lot of different endpoints in the Arduino IoT cloud, including: |
107 | | - |
108 | | -- Check value / update value of a property. |
109 | | -- List all variables attached to a Thing. |
110 | | -- Perform OTA uploads to devices. |
111 | | -- Create / delete dashboards, Things, variables etc. |
112 | | - |
113 | | -To use the Integrations API, we need to get an **access token**. This can be obtained by providing the **Client ID** and **Client Secret**. |
114 | | - |
115 | | -With the access token, we can start making HTTP requests. For example, to list out the properties of a Thing using **curl**: |
116 | | - |
117 | | -``` |
118 | | -curl -X GET "https://api2.arduino.cc/iot/v2/things/{id}/properties" |
| 59 | +ArduinoIoTCloud.connect(options) |
| 60 | + .then(() => { |
| 61 | + console.log("Connected to Arduino IoT Cloud"); |
| 62 | + return ArduinoIoTCloud.onPropertyValue(thingId, variableName, showUpdates = value => console.log(value)); |
| 63 | + }) |
| 64 | + .then(() => console.log("Callback registered")) |
| 65 | + .catch(error => console.log(error)); |
119 | 66 | ``` |
120 | 67 |
|
121 | | -Using JavaScript, you can create an access token and make a request in the same script. As access tokens expires after 300 seconds, this is needed for automations. |
| 68 | +Full examples and documentation can be found at: |
122 | 69 |
|
123 | | -A full JavaScript example of authentication + and making a POST request can be seen below: |
| 70 | +- The [official repository](https://github.com/arduino/arduino-iot-js) |
| 71 | +- The [NPM module](https://www.npmjs.com/package/arduino-iot-js) page |
124 | 72 |
|
125 | | -```js |
126 | | -var IotApi = require('@arduino/arduino-iot-client'); |
127 | | -var rp = require('request-promise'); |
128 | | - |
129 | | -async function getToken() { |
130 | | - var options = { |
131 | | - method: 'POST', |
132 | | - url: 'https://api2.arduino.cc/iot/v1/clients/token', |
133 | | - headers: { 'content-type': 'application/x-www-form-urlencoded' }, |
134 | | - json: true, |
135 | | - form: { |
136 | | - grant_type: 'client_credentials', |
137 | | - client_id: 'YOUR_CLIENT_ID', |
138 | | - client_secret: 'YOUR_CLIENT_SECRET', |
139 | | - audience: 'https://api2.arduino.cc/iot' |
140 | | - } |
141 | | - }; |
142 | | - |
143 | | - try { |
144 | | - const response = await rp(options); |
145 | | - return response['access_token']; |
146 | | - } |
147 | | - catch (error) { |
148 | | - console.error("Failed getting an access token: " + error) |
149 | | - } |
150 | | -} |
151 | | -``` |
152 | | - |
153 | | -For more examples and detailed instructions, visit the links below: |
154 | | - |
155 | | -- [Full Integrations API (REST)](https://www.arduino.cc/reference/en/iot/api/) - the complete API with hundreds of curl + JavaScript examples. |
156 | | -- [Arduino IoT Cloud Integrations API (REST) Guide](/arduino-cloud/getting-started/arduino-iot-api) - a guide to using the API with JavaScript, Python & Golang. |
157 | 73 |
|
158 | 74 | ## Backward Compatibility Policy |
159 | 75 |
|
160 | | -Public Arduino IoT Cloud APIs are exposing versioned endpoints, and are commited to preserve compatibility with the following policies in place: |
161 | | - |
| 76 | +Public Arduino IoT Cloud APIs are exposing versioned endpoints and are committed to preserving compatibility with the following policies in place: |
162 | 77 | - When making a breaking change to the API signature or behavior, we will expose a new version of the endpoint. |
163 | | -- If we are making a change which is backward compatible with existing version, we won't change the endpoint version. |
| 78 | +- If we are making a change that is backward compatible with the existing version, we won't change the endpoint version. |
164 | 79 | - When introducing a new version of the API, we will preserve the previous version as deprecated for a reasonable amount of time to allow client migrations. |
165 | 80 | - We will not preserve multiple previous versions, just N-1 with respect to the current last released, hence upgrades are recommended. |
166 | | -- In any case, the frequency of version changes is expected be less than once a year. |
167 | | -- It's also recommended that the client keeps the base URL of the API configurable because base URL (currently api2.arduino.cc) might also change. |
| 81 | +- In any case, the frequency of version changes is expected to be less than once a year. |
| 82 | +- It's also recommended that the client keeps the base URL of the API configurable because the base URL (currently api2.arduino.cc) might also change. |
0 commit comments