Storing Events using the ESP8266

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 an ESP8266 WiFi chip. This guide will assume that you already have a board with the ESP8266 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 ESP8266

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

rest.publish(client, "temperature", value, interval);

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:

// Import required libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>
// Clients
WiFiClient espClient;
PubSubClient client(espClient);
// Remote
aREST rest = aREST(client);
char * key = "your_api_key";
// WiFi parameters
const char* ssid = "wifi_ssid";
const char* password = "wifi_password";
// Functions
void callback(char* topic, byte* payload, unsigned int length);
void setup(void)
{
  // Start Serial
  Serial.begin(115200);
  // Set aREST API key
  rest.setKey(key);
  // Set callback
  client.setCallback(callback);
  // Give ID to device (optional, if not set, a device ID will be auto-assigned to the device)
  rest.set_id("your_device_id");
   
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}
void loop() {
  // Connect to the cloud
  rest.handle(client);
  long randNumber;
  randNumber = random(40);
  // Publish
  rest.publish(client, "temperature", randNumber, 5000);
}
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
  rest.handle_callback(client, topic, payload, length);
}

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 ESP8266. Now, upload the code again on your ESP8266. The ESP8266 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 ESP8266 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 ESP8266 - make sure to get in touch with us via email or via the chat if you have any questions or comments!