Child pages
  • Tutorial N°3: Altimeter Sensor on ESP8266 Board
Skip to end of metadata
Go to start of metadata


In this tutorial we will provide you with a step by step guide to enable and connect to Labeeb IoT platform an Altimeter Sensor using NodeMCU V2.0 - ESP8266 Board.

Before starting



  • We assume you have already been granted an account on Labeeb IoT platform.

Step by step Guide

Step 1: Configure your device and related device and date models on Labeeb IoT platform

 Create New Data Type

First you have to define a Data Type for your Altimeter sensor: navigate to "Data Type" sub-menu under "Platform Management" and click on + to add all the needed Data types as shown below.

 Create a New Data Model

First, you have to create a Data Model for your Altimeter sensor: navigate to "Data Models" sub-menu under "Platform Management" and click on + to add all the needed Data Models as shown below.

Second, attach the three data types (level, pressure and temp) to your Data Model, then set the accuracy profile. For example, if you set the accuracy level to 90, the time period to 1 second and the action Mark Data Model as Malfunctioning then the received data belonging to this Data Model should pass all the filters for 90% of the packets over a period of 1 second. Otherwise, the Data Model will be marked as Malfunctioning.

Finally, click on "Save" to make sure your new data model is created.

 Create a New Device Template
Once the Data Model is created, you have to define a Device Template: navigate to "Device Template" sub-menu under "Platform Management" and click on + to add the template as shown below:
Second, set the Device Heartbeat profile by defining the number of expected packets over the defined Time Period.
Third, you will have to define the password you will use to authorize a device to send the data to your account. This password will be used with the generated login
Fourth, add the data model that you created in previous step.

Finally, click on "Save" to make sure your new device template is created.

 Create New Device

Now, once the new device template "levelDT" has been created with "altimeter" data model, you just need to create a new device: navigate to "Device" menu and click on + to add the new device as shown below:


Define the name of your new device  "qlevel" and Under "Device Template" you have toe select the previously created device template "levelDT":

Finally, click on "Save" to make sure your new device is created. Now your new device is now successfully created and listed in my list of devices.

Important Note

You will have to use the generated MQTT topic and the login password you have set when creating the Device Template in order to push data from this device over MQTT.

You can check the device status and the data statistics under "My Account" menu as shown below:


Step 2: Prepare the ESP8266 and Connect it to Labeeb IoT

 Connect your board to the platform

Start by configuring the WIFI and MQTT parameters as shown below:

 Enable the sensors on the ESP8266 board
  1. Start by collecting the real time values for the  Pressure/Altitude/Temperature using the MPL3115A2 sensor.
  2. Download and install the Adafruit MPL3115A2 sensor library (
  3. Connect the wires as shown below:

 Connet to Labeeb IoT and Start Sending

To double check that the date has been transmitted to Labeeb IoT  go back to your account on Labeeb IoT portal and navigate to Data/Data retrieval , Then click on search button:

 Full Code Source
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <NTPClient.h>
#include <WiFiManager.h> 
#include <string.h> 

NTPClient timeClient(ntpUDP, "", 0, 60000);
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

char ssid[40] = "your WIFI SSID";
char password[40] = "!WIFI Password";
char mqtt_server[40] = "Mqtt address";
char mqtt_user[40] = "Mqtt user";
char mqtt_pass[40] = "Mqtt user password";
String mqtt_topic = "Mqtt your thing/topic name";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

 // We start by connecting to a WiFi network
 Serial.print("Connecting to ");

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {

 Serial.println("WiFi connected");
 Serial.println("IP address: ");

void reconnect() {

 String espid = String(ESP.getChipId());
 int id_str_len = espid.length() + 1;
 char ID[id_str_len];
 espid.toCharArray(ID, id_str_len);
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect(ID, mqtt_user, mqtt_pass)) {
 } else {
 Serial.print("failed, rc=");
 Serial.println(" try again in 5 seconds");
 // Wait 5 seconds before retrying

void setup() {
 pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
 Serial.println("Adafruit_MPL3115A2 test!");
// setup_wifi();
 client.setServer(mqtt_server, 1883);


void loop() {
 if (!client.connected()) {
if (! baro.begin()) {
 Serial.println("Couldnt find sensor");

 float pascals = baro.getPressure();
 // Our weather page presents pressure in Inches (Hg)
 // Use for other units
 Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
 float altm = baro.getAltitude();
 Serial.print(altm); Serial.println(" meters");

 float tempC = baro.getTemperature();
 Serial.print(tempC); Serial.println("*C");

 long now = millis();
 if (now - lastMsg > 300000) {
 lastMsg = now;
 String Al = String (altm);
 String Pa = String (pascals/3377);
 String Te = String (tempC);
 String msgg = String(timeClient.getEpochTime())+ ",,altimeter,level:"+ String (Al)+ ",pressuer:"+ String (Pa)+ ",temp:"+ String (Te);
 int str_len = msgg.length() + 1;
 int tpc_len = String(mqtt_topic).length() + 1;
 char msg[str_len];
 char topic[str_len];
 msgg.toCharArray(msg, str_len);
 mqtt_topic.toCharArray(topic, tpc_len);
 snprintf(msg, sizeof(msg), msg);
 snprintf(topic, sizeof(topic), topic);
 client.publish(topic, msg);