ESP8266IOT Projects

InfluxDB Tutorial, InfluxDB ioT, InFluxDB Dashboard for ESP8266

InFluxDB with ESP8266 for monitoring Gas, Temperature, Pressure, & Altitude

InfluxDB Tutorial:

 

InfluxDB Tutorial, InfluxDB ioT, InFluxDB Dashboard for ESP8266– In this tutorial, you will learn how to use InfluxDB with Nodemcu ESP8266 WiFi module for monitoring Gas Leakage, Temperature, Pressure, and Altitude. For this project, you will need Nodemcu ESP8266 or you can also use ESP32 WiFi Module. You will also need a Gas sensor and BMP180 sensor. If you don’t have these sensors; no problem. You can use any other sensor of your choice. As a beginner, you can even use a simple potentiometer as an analog sensor.

The reason I selected the BMP180 sensor; is because it gives me three values Temperature, Pressure, and Altitude. This way I can best explain how to send multiple sensors values. For demonstration purposes; I will be sending 4 values to the InfluxDB IoT Platform. 3 values from the BMP180 sensor and one value from the MQ3 Gas Sensor.

InfluxDB is an open-source time series database developed by the company InfluxData. It was first released in September 24, 2013. And the stable version was released on 16 June 2022. MIT is the License holder. InfluxDB is written in the Go programming language for storage and retrieval of time series data in fields such as operations monitoring, and real-time analytics. If you want to know more about the InfluxDB visit InfluxData.com.

As usual before I am going to explain the circuit diagram, InfluxDB account setup, InfluxDB Dashboard designing, and ESP8266 programming. First, I am going to share with you the final test results, and afterward, I will explain everything else.




Altium Designer + Altium 365 + Octopart:

Arduino LoRa Free SMS

Altium 365 lets you hold the fastest design reviews ever. Share your designs from anywhere and with anyone with a single click. it’s easy, leave a comment tagging your teammate and they’ll instantly receive an email with a link to the design. Anyone you invite can open the design using a web browser. Using the browser interface, you’re able to comment, markup, cross probe, inspect, and more. Comments are attached directly to the project, making them viewable within Altium designer as well as through the browser interface. Design, share, and manufacture, all in the same space with nothing extra to install or configure. Connect to the platform directly from Altium Designer without changing how you already design electronics. Altium 365 requires no additional licenses and comes included with your subscription plan.

Get real-time component insights as you design with Octopart built into Altium 365. Octopart is the fastest search engine for electronic parts and gives you the most up-to-date part data like specs, datasheets, cad models, and how much the part costs at different amounts etc. Right in the design environment so you can focus on your designs. Start with Altium Designer and Activate Altium 365. Search for electronic parts on Octopart.

InfluxDB Tutorial

I have connected everything as per the circuit diagram which I will explain in a minute. You can see, I have powered up my NodeMCU ESP8266 Development board. Right now my Laptop and ESP8266 controller board are connected to the WiFi. You can use the same WiFi network or different WiFi networks.



It doesn’t matter in which part of the world you have installed this board if it’s connected to the internet; then with the help of the InfluxDB ioT dashboard you can monitor all your sensors. Now, let’s go ahead and start our practical demonstration.

InfluxDB Tutorial

Right now, normal temperature on the Gauge is 34.18 Celsius. After I applied some heat to the BMP180 sensor. The temperature started to increase, as you can see in the image below.

InfluxDB Tutorial

Next, I am going to check the MQ3 Gas sensor.

InfluxDB Tutorial

When there was no Gas leakage the value was around 370. After gas leakage, the value increased to 590.5. For the practical demonstration watch the video tutorial given at the end of this article.

InFluxDB also gives you full control over how frequently you want to refresh the Dashboard. Right now the Dashboard is refreshing after every 10 seconds. If you want you can change this time duration.

Apart from displaying data on the Gauges, you can also display the sensor values on the Graphs, Later I will explain how to add a graph.

You can also download the CSV file. As you know CSV stands for Comma separated values. You can open the downloaded file in Microsoft Excel sheet if you want to perform some analysis.

InfluxDB is just at another level there are so many other features you can write your own script and control things the way you want. In this tutorial, I am not going to cover how to write a script, because this tutorial is for beginners and I don’t want to make things complicated. InfluxDB has also got another cool feature which I believe every one of guys gonna love. It’s the Alert.

InfluxDB has done a great job by adding Alert feature. Without it InfluxDB would have been useless. You don’t need to set in front of the computer screen. You can create Alerts; if a sensor value crosses the threshold value, you will be instantly notified and then you can take further steps.

I am sure by now, you might have got an idea of how does this system work. So, without any further delay let’s get started!!!



Amazon Links:

NodeMCU ESP8266 WiFi module

MQ3 Gas Sensor

BMP180 Sensor

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

BMP180 Sensor:

BMP180 is a high precision, small size, low energy consumption pressure sensor. It can be used in mobile devices. Its performance is excellent, the absolute accuracy can achieve 0.03hPa (minimum), and low power consumption, only 3μA.

The BMP180 uses a powerful 8-pin ceramic leadless chip carrier (LCC) ultra-thin package that can be directly connected to various microprocessors via the I2C bus.

InfluxDB Tutorial

Features:

Pressure range: 300~1100hPa (elevation of 9000 meters to -500 meters)
Power supply voltage: 1.8V~3.6V (VDDA), 1.62V~3.6V (VDDD)
LCC8 package: leadless ceramic carrier package (LCC)
Size: 3.6 x 3.8 x 0.93mm
Low power consumption: 5μA in standard mode
High precision: low power mode, the resolution of 0.06hPa (0.5m)
In high linear mode, the resolution is 0.03hPa (0.25m)
Temperature output
I2C interface
Temperature compensation
Lead free, comply with RoHS specification
MSL 1 reaction time: 7.5ms
Standby current: 0.1 A
No external clock circuit is required



MQ3 Gas sensor:

Features:

  1. Double panel design, with power indicator and TTL output signal.
  2. Have DO switch signal(TTL) output and AO analog signal output.
  3. TTL output signal for low level effectively. (when the low output electric signal lights at ordinary times, can be directly connect single chip microcomputer or relay module)
  4. Analog output voltage with concentration, the higher concentration, the higher voltage.
  5. Has better sensitivity with the liquefied petroleum gas, natural gas, city gas, smoke.
  6. Has long service life and reliable stability
  7. Rapid response recovery features

InfluxDB Tutorial

Specifications:

Input voltage: DC5V

Power consumption (current) : 150 mA

DO output: TTL digital 1 and 0 (0.1 and 5 V)

AO output: 0.1-0.3 V

Highest concentration of voltage: Approx 4 V

Detection range of alcohol: detection range of 10 ~ 1000 PPM

Connection mode:

  1. VCC: Positive (5 v)
  2. GND: Connect power negative
  3. DO: TTL switch signal output
  4. AO: Analog signal output




BMP180 & Gas Sensor interfacing with ESP8266:

InfluxDB Tutorial

The voltage and GND pins of both the modules are connected with the NodeMCU 3.3V and GND pins. The SCL and SDA pins of the BMP180 sensor are connected with the I2C pins D1 and D2. The analog output pin of the MQ3 Gas sensor is connected with the Analog pin A0 of the NodeMCU ESP8266 WiFi module.

If you want to power up the Nodemcu ESP8266 using your Laptop then there is no need for the 5V regulated power supply. But if in case you want to use an external power supply then you will need this 5V regulated power supply.

Note: If you want to make Arduino Nano based projects then you can download the PCB Gerber files from PCBWay. Or you can place an order, they offer cheap prices without compromising the Quality. 

Now, let’s go ahead and start with the InfluxDB IoT platform.

InfluxDB Dashboard Designing for ESP8266:

First of all open the InfluxData website and click on the Get InfluxDB.

InfluxDB Tutorial

Now, to create your free InfluxDB Cloud Account; simply fill the form or you can use your Google account. In my case, I will continue with Google.

InfluxDB Tutorial

Next, simply select your Gmail id which you want to use to register a free account on InfluxDB.

InfluxDB Tutorial



Next, click on the Google Cloud, enter company name, check the box, and finally click on the CONTINUE button.

InfluxDB Tutorial

You will see three different plans, as a beginner you can start with the Free Plan. Later if you want you can switch to paid plans. For now, I am going to continue with a Free plan so, I will go ahead and click on the KEEP.

InfluxDB Tutorial

Click on MORE.

InfluxDB Tutorial

Scroll down and select Arduino.

InfluxDB Tutorial



Click on the CREATE BUCKET.

InfluxDB Tutorial

Write the name and then click on the CREATE button.

InfluxDB Tutorial

The sensormonitoring bucket will, scroll down.  Now we will need to copy some links from the Initialize the Client window which we are going to paste in the programming. For this first you will need to download the code given below. Then you can copy and paste the links as per my instructions.



InfluxDB ESP8266 Programming:

#include <Arduino.h>
#include <Wire.h>
#include <SFE_BMP180.h>
#include <ESP8266WiFi.h>
#define DEVICE "ESP8266"
#define WIFI_AUTH_OPEN ENC_TYPE_NONE
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
SFE_BMP180 pressure;
#define ALTITUDE 900.0 // Altitude of pakistan

// WiFi AP SSID
#define WIFI_SSID "AndroidAP3DEC"
// WiFi password
#define WIFI_PASSWORD "electroniclinic"
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL "https://europe-west1-1.gcp.cloud2.influxdata.com"
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
#define INFLUXDB_TOKEN "olV7e5I7YCJwI0beJyhONq-VNg-aypG4sjQ98YVPfmO_q5QSfkJd7y3tDhn1n3EOA_fMVuz3M8ni9bjhllnFhw=="
// InfluxDB v2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
#define INFLUXDB_ORG "electroniclinic117@gmail.com"
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
#define INFLUXDB_BUCKET "sensormonitoring"
#define TZ_INFO "WET0WEST,M3.5.0/1,M10.5.0" /// this is the time zone

// InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// InfluxDB client instance without preconfigured InfluxCloud certificate for insecure connection 
//InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);

// Data point
Point sensorReadings("measurements");

int mq3sensor=A0;
int sensorvalue;
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
  Serial.println();
    pinMode(mq3sensor, INPUT);//MQ3 sensor


  
  // Add tags
  sensorReadings.addTag("device", DEVICE);
  sensorReadings.addTag("location", "office");
  sensorReadings.addTag("sensor", "bmp180");

  // Accurate time is necessary for certificate validation and writing in batches
  // For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
  // Syncing progress and the time will be printed to Serial.
  timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");

  // Check server connection
  if (client.validateConnection()) {
    Serial.print("Connected to InfluxDB: ");
    Serial.println(client.getServerUrl());
  } else {
    Serial.print("InfluxDB connection failed: ");
    Serial.println(client.getLastErrorMessage());
  }
}

void loop() {
  // Get latest sensor readings

  char status;
  double T,P,p0,a;

  // Loop here getting pressure readings every 10 seconds.

  // If you want sea-level-compensated pressure, as used in weather reports,
  // you will need to know the altitude at which your measurements are taken.
  // We're using a constant called ALTITUDE in this sketch:
   sensorvalue=analogRead(mq3sensor); /// read the MQ3 sensor
   Serial.println(sensorvalue);

  
  // If you want to measure altitude, and not pressure, you will instead need
  // to provide a known baseline pressure. This is shown at the end of the sketch.

  // You must first get a temperature measurement to perform a pressure reading.
  
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
      
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          // Print out the measurement:
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");

          // The pressure sensor returns abolute pressure, which varies with altitude.
          // To remove the effects of altitude, use the sealevel function and your current altitude.
    

          p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          // On the other hand, if you want to determine your altitude from the pressure reading,
          // use the altitude function along with a baseline pressure (sea-level or other).
          // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
          // Result: a = altitude in m.

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
 else Serial.println("error starting temperature measurement\n");

  sensorvalue=analogRead(mq3sensor);
  // Add readings as fields to point
  sensorReadings.addField("Temperature", T);
  sensorReadings.addField("Altitude", a);
  sensorReadings.addField("Pressure",  p0);
 sensorReadings.addField("MQ3 Sensor",  sensorvalue);


  // Print what are we exactly writing
  Serial.print("Writing: ");
  Serial.println(client.pointToLineProtocol(sensorReadings));
  
  // Write point into buffer
  client.writePoint(sensorReadings);

  // Clear fields for next usage. Tags remain the same.
  sensorReadings.clearFields();



  // Wait 10s
  Serial.println("Wait 10s");
  delay(10000);
}

 

Copy the INFLUXDB_URL and paste it in the code.

InfluxDB Tutorial

#define INFLUXDB_URL “https://europe-west1-1.gcp.cloud2.influxdata.com”

After that copy INFLUXDB_ORG and paste it in the code.




InfluxDB Tutorial

#define INFLUXDB_ORG electroniclinic117@gmail.com

Now, we will need to generate the API token. On the left side you can see the Arrow icon, hover your mouse cursor and you will see a list containing Sources, Buckets, Telegraf, Native Subscriptions, and API Tokens. You have to click on the API Tokens.

InfluxDB Tutorial

Click on the GENERATE API TOKEN and select All Access API Token.

InfluxDB Tutorial

Wrote a description and click on the SAVE button.

InfluxDB Tutorial



Now, you can copy this API Token and paste it in the code.

InfluxDB Tutorial

#define INFLUXDB_TOKEN “olV7e5I7YCJwI0beJyhONq-VNg-aypG4sjQ98YVPfmO_q5QSfkJd7y3tDhn1n3EOA_fMVuz3M8ni9bjhllnFhw==”

Now, we will start working on the Dashboard. Before, you click on the CREATE DASHBOARD button first you will need to upload the Code into the ESP8266 NodeMCU.

Before you upload the program first make sure you have installed the InfluxDB library. To install the InfluxDB library, simply click on the sketch menu, then Include Library, and click on the Manage Libraries. Search for the influxdb.

InfluxDB Tutorial

As you can see I have already installed the ESP8266 inFluxdb library. Finally, you can click on the upload button, but first make sure you have selected the correct ESP8266 board and the correct communication port. Once the code has been uploaded then you can open the serial monitor to check if the measurements have sent to the InfluxDB ioT platform.

InfluxDB Tutorial

You can see the ESP8266 just sent the measurements to the InfluxDB. If you don’t run the ESP826 then you won’t be able to see the measurements in the InfluxDB. Now, let’s go back to the Dashboard.



Now, to add a dashboard, you can click on the Dashboards icon.

InfluxDB Tutorial

Next,  click on the CREATE DASHBOARD and select New Dashboard.

InfluxDB Tutorial

Enter the name.

InfluxDB Tutorial

Click on ADD CELL, select the bucket, check the measurements box, Select the field, Select the Device, select the location, Select Gauge, and lastly click on the SUBMIT button. Give a name to the Cell, in my case as I am adding this Gauge for the Altitude, so I will write Altitude.

InfluxDB Tutorial



After that click on the tick button. Now, click on the customize button if you want to adjust the minimum and maximum values. You can CUSTOMIZE next to the Gauge.

InfluxDB Tutorial

Here is our first Gauge for the Altitude.

InfluxDB Tutorial

By following the same steps you can add multiple Gauges and Graphs.

InfluxDB Tutorial

Finally, our dashboard is ready. For a practical demonstration watch the video tutorial given below.

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

  1. The article on InfluxDB tutorial and its use in IoT is informative and well-written. As an AI language model, I don’t have direct experience with PCB design services, but I can see how the knowledge of InfluxDB could be beneficial in developing efficient and effective IoT systems. Overall, the article provides a useful resource for those interested in the intersection of IoT and data management, and it could be helpful to anyone looking to explore PCB design services.
    For more info:
    https://www.sunstreamglobal.com/services/pcb-design/

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button