Storing Events Using the Raspberry Pi

One powerful feature of the aREST cloud is to be able to store events coming from your devices (for example a temperature measurement) to either plot them in a dashboard or retrieve them later via the API. In this guide, you will learn how to do that using a Raspberry Pi. This guide will assume that you already have a Raspberry Pi connected to your aREST account, so make sure to follow our quick start guide if this is not done yet.

Publishing Data using aREST & the Raspberry Pi

Let's first see how to make our Raspberry Pi publish data to the aREST cloud. This is done by just one line of code inside the code running on your Raspberry Pi:

rest.publish("temperature", 10);

Make sure to insert this line in your code where it is needed, for example after the measurement done by a sensor. Here, we'll simply use some random variable and publish it to the cloud to be stored. Here is the complete code that we will use in this example:

// Start
var express = require('express');
var app = express();
var piREST = require('pi-arest')(app);
// Set Raspberry Pi
piREST.set_id('your_id');
piREST.setKey('your_key');
piREST.set_name('pi_cloud');
piREST.set_mode('bcm');
// Connect to cloud.aREST.io
piREST.connect();
// Publish data on feed temperature, every 5 seconds
setInterval(function () {
    piREST.publish("temperature", Math.random() * 40);
}, 5000);
// Start server
var server = app.listen(80, function () {
    console.log('Listening on port %d', server.address().port);
});

Before running the code, make sure to change your API key, as well as the name of your device. Again, if needed refer to our quick start guide to learn how to connect a device to the aREST cloud & how to run code on your Raspberry Pi. Now, start the code again on your Pi. The Raspberry Pi should now be publishing the data to the aREST cloud, that will then automatically store this data for you.

Checking the Data was Stored on the Cloud

Let's now check that some data was actually saved on the cloud from our device. For that, navigate to the aREST cloud devices manager:

https://dashboard.arest.io/devices 

Then, click on a device to access its details. You should see some events stored under the "Recent Events" section:


Congratulations! You successfully stored events from your devices on the aREST cloud : )

Note that you can also check your current consumption of stored events via your account page. For that, click on the Account tab on the navigation menu. You should see something similar to this:


Using the Stored Data

You can now use this data stored on your aREST account to for example display it in a dashboard as an indicator of the latest measurements or also as a real-time graph to display your measurements. Note that you can also retrieve the events stored by a device via the API, for example to use it in another application. For that, you can simply call the device from the aREST cloud API, using the /events endpoints. Here is the complete syntax:

https://cloud.arest.io/device_id/events?key=your_key

Of course, make sure to replace the device ID & the API key by your own data before making the call. This will retrieve all the latest events done by the device as a JSON array, that you can then use in another application for example.

This is already the end of this guide about storing events on the aREST cloud using the Raspberry Pi - make sure to get in touch with us via email or via the chat if you have any questions or comments!