//Include the library
#include <WiFi.h>
#include <MQUnifiedsensor.h>
#include <HTTPClient.h>
// Ganti dengan token akses dari perangkat ThingsBoard Anda
const char* accessToken = "aksestoken device";
// URL ThingsBoard, biasanya dalam format: http://<THINGSBOARD_HOST>:<PORT>
const char* server = "http://demo.thingsboard.io/api/v1/{aksestoken device}/telemetry";
//Definitions
#define placa "ESP-32" // Wemos ESP-32 or other board, whatever have ESP32
#define Voltage_Resolution 3.3 // 3V3
#define pin 35 //Analog input 0 of your arduino
#define type "MQ-135" //MQ135
#define ADC_Bit_Resolution 12 // For arduino UNO/MEGA/NANO
#define RatioMQ135CleanAir 3.6 //RS / R0 = 3.6 ppm
//#define calibration_button 13 //Pin to calibrate your sensor
// Pin sensor Ultrasonic
#define TRIGPIN 14
#define ECHOPIN 12
#define relaypin 27
#define buzzerpin 26
#define blue 13
#define red 25
#define green 33
// Maksimal jarak dalam cm
#define MAX_DISTANCE 300
//Declare Sensor
MQUnifiedsensor MQ135(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);
const char* ssid = "nama wifi anda";
const char* password = "passwordnya";
void setup() {
//Init the serial port communication - to debug the library
Serial.begin(9600); //Init serial port
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(relaypin, OUTPUT);
pinMode(buzzerpin, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Set math model to calculate the PPM concentration and the value of constants
MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
/********** MQ Init ***************/
//Remarks: Configure the pin of arduino as input.
/****************************/
MQ135.init();
Serial.print("Calibrating please wait.");
float calcR0 = 0;
for (int i = 1; i <= 10; i++) {
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
Serial.print(".");
}
MQ135.setR0(calcR0 / 10);
Serial.println(" done!.");
if (isinf(calcR0)) {
Serial.println("Warning: Conection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
while (1)
;
}
if (calcR0 == 0) {
Serial.println("Warning: Conection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
while (1)
;
}
/********** MQ CAlibration ***************/
Serial.println("* Values from MQ-135 ***");
Serial.println("| CO2 | H2S |");
}
void loop() {
HTTPClient http;
MQ135.update(); // Update data, the arduino will read the voltage from the analog pin
MQ135.setA(110.47);
MQ135.setB(-2.862); // Configure the equation to calculate CO2 concentration value
float CO2 = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
MQ135.setA(415.0);
MQ135.setB(-3.183); // Configure the equation to calculate Aceton concentration value
float H2S = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
Serial.print("| ");
Serial.print(" | ");
Serial.print(CO2 + 400);
Serial.print(" | ");
Serial.print(H2S);
Serial.println(" |");
long duration, distance;
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = duration * 0.034 / 2;
// Hitung persentase jarak
float distancePercentage = (float)distance / MAX_DISTANCE * 100.0;
if (distancePercentage > 100) distancePercentage = 100;
if (distancePercentage < 0) distancePercentage = 0;
// Buat URL request
String url = server;
// Data telemetri dalam format JSON
String payload = "{\"co2\": " + String(CO2, 2) + ", \"h2s\": " + String(H2S, 2) + ", \"level\": " + String(distancePercentage, 2) + "}";
// Debugging: tampilkan URL dan payload
Serial.print("Sending data to: ");
Serial.println(url);
Serial.print("Payload: ");
Serial.println(payload);
Serial.print(distancePercentage);
Serial.println(" %)");
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Kirim data telemetri ke ThingsBoard
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
if (H2S > 30 || CO2 > 15) {
digitalWrite(relaypin, LOW);
digitalWrite(buzzerpin, LOW);
digitalWrite(red, HIGH);
} else {
digitalWrite(relaypin, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(buzzerpin, HIGH);
}
delay(500); //Sampling frequency
}