Easy IoT Device - BMP280+HC-SR04+ThingsBoard

ThingsBoard is an open-source IoT platform for device management, data collection, processing and visualization for your IoT projects.

Before we start with the tutorial I have to give a few important links. The first link is to the Documentation of ThingsBoard. The second one is to the Samples demos and codes.


In this tutorial I will show you how to connect a BMP280 temp/hum/pressure sensor and HC-SR04 ultrasonic sensor to ESP32 and transmit the information from the sensors to the ThingsBoard platform.

In the picture above you can see all the necessary components needed for this tutorial. Now I will give links to Amazon to find more information about the components and if you want you can order them from there .I just personally ordered them from Amazon and from exactly these sellers because price turned out to be the best for what they offer, and the transport was at a lower price


1 x Adafruit HUZZAH32 – ESP32

1 x BMP280

1 x HC-SR04

Once you have ordered everything and everything has come to you, it's time to connect the sensors. In the diagram below you can see an example diagram of how the sensors should be connected to the ESP32 .The block diagram is made with Fritzing, if you are interested.



If you've connected everything, it's time to log in to TingsBoard and create an account. Then you need to follow their tutorial which shows how to register your device and how to make your first dashboard. If you did everything right, then it's time to start programming your device. You can copy the program below directly, but you have to put the name of your WIFI connection, the password to it and lastly you have to put the token that you must already have from the platform when the device is registered.

I must point out that you need to install the necessary libraries in order for the code to be compiled. If you are on Arduino Ide, you can download the libraries from the Library manager by just typing the name of the library (you can check out it below) and clicking install. But if you fail or you are on another IDE here you can find the library for BMP280 and here the Thingsboard library.


#include <WiFi.h>   
#include <ThingsBoard.h>   
  
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>      

#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;

#define ECHOPIN 13
#define TRIGPIN 12

int distance = 0;

// Helper macro to calculate array size
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

#define WIFI_SSID           "SSID OF YOUR WIFI"
#define WIFI_PASSWORD       "PASSWORD OF YOUR WIFI"

#define TOKEN "YOUR TOKEN"
#define THINGSBOARD_SERVER "demo.thingsboard.io"

// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;

void setup() 
{
  Serial.begin(115200);
  bme.begin(0x76);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  InitWiFi();
  
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  digitalWrite(ECHOPIN, HIGH);

}

void loop() 
{
  delay(20);
  
  if (WiFi.status() != WL_CONNECTED) 
  {
    reconnect();
    return;
  }
  
  if (!tb.connected()) 
  {
    Serial.print("Connecting to: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.println(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) 
    {
      Serial.println("Failed to connect");
      return;
    }
  }

  digitalWrite(TRIGPIN, LOW);
  delay(2);
  digitalWrite(TRIGPIN, HIGH);
  delay(10);
  digitalWrite(TRIGPIN, LOW);

  distance = pulseIn(ECHOPIN, HIGH, 26000);
  distance = distance/58;
    
  tb.sendTelemetryFloat("distance", distance);
  tb.sendTelemetryFloat("temperature", bme.readTemperature());
  tb.sendTelemetryFloat("humidity", bme.readHumidity());
  delay(100);
  tb.loop();
}

void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}

void reconnect() 
{
  status = WiFi.status();
  if ( status != WL_CONNECTED) 
  {
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) 
    {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to AP");
  }
}

If you did everything right and read this, then you already have your first IOT device up and working. Congratulations, you succeeded. You can now view all the information from your sensors on the Thingboard platform.


3 views0 comments

Recent Posts

See All

If you want to support me

  • Dimitar Rangelov linkedin blog
  • Dimitar Rangelov github blog
  • Dimitar Rangelov facebook blog
  • Dimitar Rangelov instagram blog

ʕ•ᴥ•ʔ  Dimitar Rangelov Blog © 2020 | Sharing is Caring | About me ʕ•ᴥ•ʔ