IoTaaP - IoT Platform
  • Features
  • Solutions
  • Modules
  • OS
  • Pricing
  • CONSOLE
  • DOCS
Storing data using ESP32 and IoTaaP

In this tutorial, we will write a simple code that will read the temperature from the BME280 sensor, and store this temperature in the IoTaaP Storage service.

platformio.ini

As usual, we will use PlatformIO for this embedded project, and our platformio.ini should look like this:

[env:release]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
    iotaap/IoTaaP OS@^5.2.2
    adafruit/Adafruit BME280 Library@^2.2.2
board_build.partitions = partitions/default_fat.csv

Headers and declaration

#include <IoTaaP_OS.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include "Adafruit_BME280.h"

#define TOKEN "<iotaap-link-secret>" // IoTaaP Link Secret

Adafruit_BME280 bme;         // I2C
IoTaaP_OS iotaapOs("3.2.8"); // Defining Firmware version

We have to include our headers as usual, since Adafruit_BME280.h requires some functions from Arduino.h, we have to include it also. Additionally, we have to declare our bme sensor and create iotaapOs object.

Also, we have to define TOKEN, so please do not forget to replace <iotaap-link-secret> with your IoTaaP Link Secret.

setup() function

void setup()
{
  // BME280 related stuff
  unsigned status;
  status = bme.begin(0x76);
  if (!status)
  {
    Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    while (1)
      delay(10);
  }

  iotaapOs.start(); // Start IoTaaP OS

  iotaapOs.startWifi();         // Connect to WiFi
  iotaapOs.startMqtt(callback); // Connect to MQTT broker
}

In the first part of the function, we are checking if BME280 is available and working, this is a good practice in this example, to eliminate further issues if the sensor is not working properly.

Next, we have to call iotaapOs.start() function to start our IoTaaP OS services, this is followed by iotaapOs.startWifi() to connect to start WiFi service and iotaapOs.startMqtt() to connect to the IoTaaP network and define MQTT callback function.

callback() function

void callback(char *topic, byte *message, unsigned int length)
{
  Serial.println("-------#-----#-----#----------");
  Serial.println("Received data on the topic:");
  Serial.println(topic); // Print topic

  Serial.println("Data:");

  for (int i = 0; i < length; i++) // Print message
  {
    Serial.print((char)message[i]);
  }
  Serial.println();
  Serial.println("------#------#------#---------");
}

This is a basic MQTT callback function that will print received data and topic to our Serial Terminal, it is used to check the status of the service.

loop() function

void loop()
{

  // Fetch measurements from BME280 sensor
  float temperatureC = bme.readTemperature();

  // Store temperature reading to IoTaaP Storage service, and subscribe to callback topic
  iotaapOs.storageServiceStore(TOKEN, "temperature", temperatureC, "/pgiIzx7n/storage/response");

  delay(5000);
}

This function is repeated constantly, and it will read the temperature from our BME280 sensor and store it in temperatureC variable. Next, we will use iotaapOs.storageServiceStore() function to store our reading in the IoTaaP Storage.

Do not forget to update your token and update your callbackTopic. Also your must have permission to listen on your callback topic.

Reading from IoTaaP Storage

We will use Postman to read from IoTaaP Storage. This step is described in detail here.

After populating some data from our sensor, we will get the following response:

{
    "data": [
        {
            "time": "2023-06-27T18:14:20.364Z",
            "value": 26.32
        },
        {
            "time": "2023-06-27T18:14:25.364Z",
            "value": 27.32
        },
        {
            "time": "2023-06-27T18:14:30.364Z",
            "value": 28.14
        }
    ]
}

Complete code

Below you can find the complete code for this example.

#include <IoTaaP_OS.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include "Adafruit_BME280.h"

#define TOKEN "<iotaap-link-secret>" // IoTaaP Link Secret

Adafruit_BME280 bme;         // I2C
IoTaaP_OS iotaapOs("3.2.8"); // Defining Firmware version

// IoTaaP Network (MQTT) callback function
void callback(char *topic, byte *message, unsigned int length)
{
  Serial.println("-------#-----#-----#----------");
  Serial.println("Received data on the topic:");
  Serial.println(topic); // Print topic

  Serial.println("Data:");

  for (int i = 0; i < length; i++) // Print message
  {
    Serial.print((char)message[i]);
  }
  Serial.println();
  Serial.println("------#------#------#---------");
}

void setup()
{
  // BME280 related stuff
  unsigned status;
  status = bme.begin(0x76);
  if (!status)
  {
    Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    while (1)
      delay(10);
  }

  iotaapOs.start(); // Start IoTaaP OS

  iotaapOs.startWifi();         // Connect to WiFi
  iotaapOs.startMqtt(callback); // Connect to MQTT broker
}

void loop()
{

  // Fetch measurements from BME280 sensor
  float temperatureC = bme.readTemperature();

  // Store temperature reading to IoTaaP Storage service, and subscribe to callback topic
  iotaapOs.storageServiceStore(TOKEN, "temperature", temperatureC, "/pgiIzx7n/storage/response");

  delay(5000);
}

If you have any issues with your code, write to us on our Community or check our Documentation.

Related Posts

Sending SMS with ESP32
How To

Sending SMS with ESP32

In this tutorial, we will write a simple code that will read the temperature from the BME280 sensor, and if the temperature is higher than 30°C it will send an SMS to the receiver’s number….

IoTaaP
  • Jun 27
  • 3 mins read
IoTaaP OS Web configurator for ESP32
How To

IoTaaP OS Web configurator for ESP32

Introducing Web Configurator Alongside other features and improvements, we are introducing IoTaaP OS – Web Configurator. Simple web interface hosted on your device that gives you the possibility to configure all device parameters using your…

IoTaaP
  • Mar 17
  • 2 mins read

Latest Post

Hosted Node-RED

Hosted Node-RED

  • July 8, 2023
  • < 1 min read
Storing data using ESP32 and IoTaaP

Storing data using ESP32 and IoTaaP

  • June 27, 2023
  • 3 mins read
Sending SMS with ESP32

Sending SMS with ESP32

  • June 27, 2023
  • 3 mins read

Follow Us

Company

  • About us
  • Careers We’re Hiring!
  • Brand
  • Blog
  • Contact us

Use Cases

  • Smart Metering
  • Process Control
  • Logistics Management
  • Education
  • IoTaaP for Farming

Products

  • Features
  • Modules
  • Console
  • SDK
  • Pricing

Resources

  • Privacy Policy
  • Terms of Usage
  • Documentation
  • Community
  • Status

Contact

  • Icon contact@iotaap.io
  • Icon+385 (0) 91 7851 411
  • IconZagreb, Croatia
© 2023 - IoTaaP
Manage Cookie Consent
We use technologies like cookies to store and/or access device information. We do this to improve browsing experience and to show (non-) personalized ads. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage vendors Read more about these purposes
View preferences
{title} {title} {title}