InfluxDB Tutorial, InfluxDB ioT, InFluxDB Dashboard for ESP8266
InFluxDB with ESP8266 for monitoring Gas, Temperature, Pressure, & Altitude
Table of Contents
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.
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.
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.
Next, I am going to check the MQ3 Gas sensor.
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:
Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
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.
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:
- Double panel design, with power indicator and TTL output signal.
- Have DO switch signal(TTL) output and AO analog signal output.
- 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)
- Analog output voltage with concentration, the higher concentration, the higher voltage.
- Has better sensitivity with the liquefied petroleum gas, natural gas, city gas, smoke.
- Has long service life and reliable stability
- Rapid response recovery features
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:
- VCC: Positive (5 v)
- GND: Connect power negative
- DO: TTL switch signal output
- AO: Analog signal output
BMP180 & Gas Sensor interfacing with ESP8266:
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.
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.
Next, simply select your Gmail id which you want to use to register a free account on InfluxDB.
Next, click on the Google Cloud, enter company name, check the box, and finally click on the CONTINUE button.
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.
Click on MORE.
Scroll down and select Arduino.
Click on the CREATE BUCKET.
Write the name and then click on the CREATE button.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
#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.
#define INFLUXDB_URL “https://europe-west1-1.gcp.cloud2.influxdata.com”
After that copy INFLUXDB_ORG and paste it in the code.
#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.
Click on the GENERATE API TOKEN and select All Access API Token.
Wrote a description and click on the SAVE button.
Now, you can copy this API Token and paste it in the code.
#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.
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.
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.
Next, click on the CREATE DASHBOARD and select New Dashboard.
Enter the name.
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.
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.
Here is our first Gauge for the Altitude.
By following the same steps you can add multiple Gauges and Graphs.
Finally, our dashboard is ready. For a practical demonstration watch the video tutorial given below.
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/