How to integrate an Arduino with Onesait Platform? (A simple Arduino example to POST and GET data from an API)

How to integrate an Arduino with Onesait Platform? (A simple Arduino example to POST and GET data from an API)

Introduction

Arduino is still a relevant open-source electronics platform based on easy-to-use hardware and software. Intended to be the first step for anyone interested in building interactive projects with hardware devices, Arduino provides both hardware and software tools to make it happen.

Example description

This guide will show you a quick example on how to connect your Arduino Board to onesait Platform. The example assumes you have an Arduino board with a working WiFi connection. Our initial setup featured a LOLIN32 (ESP32-based) board by WEMOS.

The example has an associated Ontology registered on our public CloudLab Instance, ArduinoFrame. This is a simple Ontology with two String fields, device (to differentiate devices) and value (a random-generated value to be replaced with your sensor output).

To enable easy access to the Ontology, an API interface is defined using the API Manager module. This API is attached to the Ontology, and two operations are defined:

  • A POST operation, enabling allowed clients to remotely insert ArduinoFrame data.

  • A custom GET operation, with an embedded query returning the last five measures inserted in the Ontology.

 

You may check it yourself, since it is a public API, at this link.

The Ontology and API both enable any authorized device to connect to the platform. It is then time to develop the Arduino client.

Example development

On the Arduino side, three libraries are used:

 

 

 

 

After setting up the WiFi credentials during the setup phase, you shall see a "WiFi connected" message.

Then you will be able to reach the former API. In this example, the board first performs a POST operation, then retrieves the last data by calling the custom GET operation:

 

Dissecting the code, you can see that both operations follow similar patterns. Firstly, the API URL should be provided to the library. Then, two headers are needed to process the HTTP Requests - Content-Type and X-OP-APIKEY - with the user token from the platform. This token is available for your user at this link.

Then, just upload the code to your board and enable serial output to follow the example progress:

 

To double-check, you can also query the Ontology values at the Control Panel Query Tool:

 

And they MATCH! Try it yourself and hit us with any question you may have at support@onesaitplatform.com