MQ135 + DHT22 on NodeMCUv3 ESP8266 to InfluxDB 1 – Part 1

Ini code yang aku guna untuk collect data dari MQ135 (sensor kualiti udara) dan juga DHT22 (sensor suhu) dan kemudian disimpan ke dalam time series database, InfluxDB.

Setelah beberapa hari buat cubaan dan pemantauan aku perasan yang bila Nodemcu terputus signal dengan wifi, nodemcu gagal untuk menyambung semula sambungan wifi dengan sendiri.

Code yang di bawah adalah code yang belum dikemaskini dengan isu di atas. Bila dah release code baru, kita akan buat posting yang berlainan.

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#include <InfluxDbClient.h>
#include "MQ135.h"      // Libraries GitHUB URL; https://github.com/Phoenix1747/MQ135
#include "DHTesp.h"
#define ANALOGPIN A0    //  Define Analog PIN on Arduino Board

const char* ssid = "redacted";
const char* password = "redacted";
WiFiClient espClient;

// INFLUXDB AREA
// InfluxDB server url, e.g. http://192.168.1.48:8086 (don't use localhost, always server name or ip address)
#define INFLUXDB_URL "http://collector.example.com"
// InfluxDB database name
#define INFLUXDB_DB_NAME "iot"

// Single InfluxDB instance
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
Point sensor("dev_gassensor"); // SINI MEASUREMENT
//

DHTesp dht;
MQ135 gasSensor(ANALOGPIN); // A0
unsigned long lastSend;

void setup() {
  Serial.begin(9600);         // Start the Serial communication to send messages to the computer
  dht.setup(14, DHTesp::DHT22); // GPIO14
  pinMode(LED_BUILTIN, OUTPUT);
  delay(10);
  Serial.println('\n');
  
  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer

  sensor.addTag("tag", "iot");
}

void sendDataCollection() {
  /// ALL DHT22 

//  TempAndHumidity newValues = dht.getTempAndHumidity();
  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();
  float heatIndex = dht.computeHeatIndex(temperature, humidity);
  float dewPoint = dht.computeDewPoint(temperature, humidity);
  ///


  /// ALL GAS MQ135

  float rzero = gasSensor.getRZero();
  float correctedRZero = gasSensor.getCorrectedRZero(temperature, humidity);
  float resistance = gasSensor.getResistance();
  float ppm = gasSensor.getPPM();
  float correctedPPM = gasSensor.getCorrectedPPM(temperature, humidity);

  ///

  // INFLUXDB

  sensor.clearFields();
  sensor.addField("mq135_dev_rzero", rzero);
  sensor.addField("mq135_dev_resistance", resistance);
  sensor.addField("mq135_dev_correctedPPM", correctedPPM);
  sensor.addField("dev_temperature", temperature);
  sensor.addField("dev_humidity", humidity);
  sensor.addField("dev_dewPoint", dewPoint);
  sensor.addField("dev_heatIndex", heatIndex);
  //
  
  delay(1000);
  Serial.print("CO2 ppm value : ");
  Serial.println(ppm);
  Serial.print("Corrected CO2 ppm value : ");
  Serial.println(correctedPPM);
  Serial.print("Temperature value : ");
  Serial.println(temperature);
  Serial.print("Humidity value : ");
  Serial.println(humidity);
  Serial.print("DewPoint: ");
  Serial.println(dewPoint);
  Serial.print("Heat Index: ");
  Serial.println(heatIndex);
  Serial.print("Writing to influxdb:");
  Serial.println(client.pointToLineProtocol(sensor));
}

void reconnect() {
  // Loop until we're reconnected
  Serial.println("In reconnect...");
  if (client.validateConnection()) {
    Serial.print("Connected to InfluxDB: ");
    Serial.println(client.getServerUrl());
  } else {
    Serial.print("InfluxDB connection failed: ");
    Serial.println(client.getLastErrorMessage());
  }
}

void loop() {
  if ( millis() - lastSend > 1000 ) {
    sendDataCollection();
    lastSend = millis();
  }
  
  // If no Wifi signal, try to reconnect it
  if (wifiMulti.run() != WL_CONNECTED) {
    Serial.println("Wifi connection lost");
  }
  // Write point
  if (!client.writePoint(sensor)) {
    Serial.print("InfluxDB write failed: ");
    Serial.println(client.getLastErrorMessage());
  }

  //Wait 5s
  Serial.println("Wait 5s");
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(2000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(2000);                       // wait for a second
}

Contoh hasil graph boleh rujuk di page projek scroll ke bawah ke bahagian feed instagram.

Sekian.

1 thought on “MQ135 + DHT22 on NodeMCUv3 ESP8266 to InfluxDB 1 – Part 1”

Leave a Comment