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

Sambungan dari post di atas.

Siap dah baiki masalah.

Jadi lepas ini boleh reconnect balik bila Wifi putus.

Secara ringkas, dalam library ESP8266WiFiMulti itu sendiri dia ada guna module Wifi biasa. Jadi aku just baca balik dari artikel lain untuk tengok macam mana dia setup reconnect Wifi.

Untuk kes aku, aku nak guna balik SSID yang sama sekiranya Wifi terputus.

#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.azmi.my"
// InfluxDB database name
#define INFLUXDB_DB_NAME "iot"

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

// Use for Wifi looping

unsigned long previousMillis = 0;
unsigned long interval = 5000;

//

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.mode(WIFI_STA); 
  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
  // Wifi reconnection settings
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
  //
  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_rzero", rzero);
  sensor.addField("mq135_resistance", resistance);
  sensor.addField("mq135_correctedPPM", correctedPPM);
  sensor.addField("temperature", temperature);
  sensor.addField("humidity", humidity);
  sensor.addField("dewPoint", dewPoint);
  sensor.addField("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();
    Serial.println(client.getServerUrl());
    lastSend = millis();
  }
  
// If no Wifi signal, try to reconnect it
//  if (wifiMulti.run() != WL_CONNECTED) {
//    Serial.println("Wifi connection lost");
//    Serial.println("Reconnecting to WiFi...");
//    WiFi.disconnect();
//    WiFi.reconnect();
//  }
  
// Write point
  if (!client.writePoint(sensor)) {
    Serial.print("InfluxDB write failed: ");
    Serial.println(client.getLastErrorMessage());
  }

// print the Wi-Fi status every 5 seconds
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >=interval) {
    switch (WiFi.status()){
      case WL_NO_SSID_AVAIL:
        Serial.println("Configured SSID cannot be reached");
        break;
      case WL_CONNECTED:
        Serial.println("Connection successfully established");
        sendDataCollection();
        Serial.println(client.getServerUrl());
        break;
      case WL_CONNECT_FAILED:
        Serial.println("Connection failed");
        break;
    }
    Serial.printf("Connection status: %d\n", WiFi.status());
    Serial.print("RRSI: ");
    Serial.println(WiFi.RSSI());
    previousMillis = currentMillis;
  }
  
//

//  Wait 5s - LED Blinking
//  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
}

Leave a Comment