Sensor give NaN value

hey guys can u help me i used sensor MQ4, MQ135, MQ16. my code without blynk can work properly and when i added blynk fungction my sensor print Nan value.

We can’t help you without any code
Please post your code and don’t forget backticks at beginning and end

i cant upload my code here, i get notice “Sorry, new users can only put 2 links in a post.”

That’s because you aren’t putting the triple backticks at the beginning and end of your code.

Triple backticks look like this:
```

Copy and paste these if you can’t find the correct character on your keyboard.

Pete.

#define BLYNK_TEMPLATE_ID "TMPL5dCcUEoN";
#define BLYNK_DEVICE_NAME "gas analizer";
#define BLYNK_AUTH_TOKEN "Ei6K6zdatMU1xq21W9KhI4OVL6iJDZfX";
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "malmal";
char pass[] = "akmal123";
WidgetLCD lcd_blynk(V4);
BlynkTimer timer;

#include <SPI.h>

#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
#include <MQUnifiedsensor.h>

#define   Board   ("ESP-32")
#define         Pin4                     (25)  //Analog input 1 of your arduino
#define         Pin135                   (26)
#define         Pin136                   (27)
#define         RatioMQ4CleanAir          (4.4) //RS / R0 = 4.4 ppm 
#define         RatioMQ135CleanAir        (3.6) //RS / R0 = 10 ppm 
#define         RatioMQ136CleanAir        (3.6)
#define         ADC_Bit_Resolution        (12) // 10 bit ADC 
#define         Voltage_Resolution        (3.3) // Volt resolution to calc the voltage
#define         Type                      ("ESP-32") //Board used


#define TFT_GREY 0x5AEB // New colour
//uint32_t updateTime = 0;       // time for next update
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin4, Type);
MQUnifiedsensor MQ135(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin135, Type);
MQUnifiedsensor MQ136(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin136, Type);
int kondisi = 0;
float CH4 = 0;
float NH3 = 0;
float H2S = 0;
void blynk_postdata()
{
    MQ4.update();
    MQ135.update();
    MQ136.update();
   Blynk.virtualWrite(V1, CH4);
Blynk.virtualWrite(V2, NH3);
Blynk.virtualWrite(V3, H2S);
}
unsigned long previousMillis = 0;
void setup(void) {
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  Serial.begin(9600); // For debug
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80); // blynk membaca auth, ssid, pass
  delay(3000);
  timer.setInterval(1000L, blynk_postdata);

  MQ4.init();
  MQ4.setRegressionMethod(1); //_PPM =  a*ratio^b
 
  MQ135.init();
  MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b

  MQ136.init();
  MQ136.setRegressionMethod(1); //_PPM =  a*ratio^b
  
//While calibrating Your sensor Uncomment this calibration portion and calibrate for R0.
  Serial.print("Calibrating please wait.");
  float  MQ4calcR0 = 0,
         MQ135calcR0 = 0,
         MQ136calcR0 = 0;
  for(int i = 1; i <= 10; i ++)
  { 
    //Update the voltage lectures
    MQ4.update();
    MQ135.update();
    MQ136.update();
    MQ4calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
    MQ135calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
    MQ136calcR0 += MQ136.calibrate(RatioMQ136CleanAir);
    
    Serial.print(".");
  }
 
  MQ4.setR0(MQ4calcR0 / 10);
  MQ135.setR0(MQ135calcR0 / 10);
  MQ136.setR0(MQ136calcR0 / 10);

  Serial.println("  done!.");

  Serial.print("(MQ4 - MQ136):");
  Serial.print(MQ4calcR0 / 10); Serial.print(" | ");
  Serial.print(MQ135calcR0 / 10); Serial.print(" | ");
  Serial.print(MQ136calcR0 / 10); Serial.print(" | ");
}
int interval_100ms_getdata; //inisialisasi untuk get data -- ini dari blink with out delay
int interval_100ms_display; 
int event = 1, tampil_lcd = 0;
  void loop() {
    Blynk.run();
   timer.run(); 
    //---------------------------KATEGORI-----------------------------------
 if (CH4 < 0) // jika mq135 lebih kecil 0 maka NH3 hasil 0
 CH4 = 0;
 if (NH3 < 0) // jika mq135 lebih kecil 0 maka NH3 hasil 0
 NH3 = 0;
 if (H2S < 0) // jika mq136 lebih kecil 0 maka H2S hasil 0
 H2S = 0;
//--Kondisi Baik-->jika NH3,H2S,da_dust lebih kecil dari 50 maka kondisi baik dan buzzer tidak menyala
 if (CH4 <= 50 || NH3 <= 2 || H2S <= 10)
 {
 kondisi = 0;
 //buzz = 0;
 }
 //--Kondisi Baik-->jika NH3,H2S,da_dust lebih kecil dari 51 maka kondisi baik dan buzzer tidak menyala
//--Kondisi Sedang-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 51 dan lebih kecil atau sama dengan 100 kondisi sedang buzzer tidak menyala
 else if ( (CH4 >= 51 && CH4 <= 100) || ( NH3 >= 2.1 && NH3 <= 5) || ( H2S >= 11 && H2S <= 115))
 {
 kondisi = 1;
 //buzz = 0;
 }
 //--Kondisi Sedang-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 51 dan lebih kecil atau sama dengan 100 kondisi sedang buzzer tidak menyala
//--Kondisi Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 101 dan lebih kecil atau sama dengan 199 kondisi tdk sehat buzzer menyala
 else if ( (CH4 >= 101 && CH4 <= 199) || ( NH3 >= 6 && NH3 <= 10) || ( H2S >= 16 && H2S <= 20))
 {
 kondisi = 2;
 //buzz = 1;
 }
//--Kondisi Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 101 dan lebih kecil atau sama dengan 199 kondisi tdk sehat buzzer menyala
//--Kondisi Sangat Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 200 dan lebih kecil atau sama dengan 299 kondisi Sangat tdk sehat buzzer menyala
 else if ( (CH4 >= 200 && CH4 <= 299)
 || ( NH3 >= 11 && NH3 <= 15)
 || ( H2S >= 21 && H2S <= 30))
 {
 kondisi = 3;
 //buzz = 1;
 }
 //--Kondisi Sangat Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 200 dan lebih kecil atau sama dengan 299 kondisi Sangat tdk sehat buzzer menyala
//--Kondisi Berbahaya-->jika bukan kondisi berbahaya dan buzzer menyala
 else
 {
 kondisi = 4;
 //buzz = 1;
 }
 //----------------------------------------KATEGORI-----------------------------------
unsigned long currentMillis = millis(); //penyimpanan dengan kapasistas besar (long) yang di beri nama currentMillis dengan nilai millis (hitungan saat MK menyala)
 const long interval_100ms = 100; //sebuah nilai yang besar dengan di beri nama interval_100ms dan di beri nilai 100
 if ((currentMillis - previousMillis >= interval_100ms) || event == 1)
 {
  event = 0;
 previousMillis = currentMillis;
 interval_100ms_getdata++; //interval_100ms_getdata akan selalu bertambah
 if (interval_100ms_getdata >= 50 || event == 1) //100x50
{
interval_100ms_getdata = 0;
 //Serial.print("G"); Serial.print(";");
  //Update the voltage lectures
  
   MQ4.update();
  MQ135.update();  
  MQ136.update(); 

  MQ4.setA(1012.7); MQ4.setB(-2.786); //CH4
float CH4 = MQ4.readSensor(); 
   MQ135.setA(0.858); MQ135.setB(-0.417); //NH4 
float NH3 = MQ135.readSensor(); 
   MQ136.setA(36.737); MQ136.setB(-3.536); //H2S 
float H2S = MQ136.readSensor();
blynk_postdata();
Serial.print("Methane:  "); Serial.println(CH4);
Serial.print("NH3:      "); Serial.println(NH3);
Serial.print("H2S:      "); Serial.println(H2S);

Serial.println("--------------------------------------------------------");

blynk_postdata();


  
  // Set "cursor" at top left corner of display (0,0) and select font 2
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(50, 0, 4);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE,TFT_BLACK);  tft.setTextSize(1);
  // We can now plot text on screen using the "print" class
  tft.println("PENGUKURAN KUALITAS UDARA");

  tft.setTextColor(TFT_GREEN,TFT_BLACK);
  tft.setTextFont(4);
  tft.println("KONDISI UDARA");

  tft.setCursor(0, 150);
  tft.setTextColor(TFT_BLUE, TFT_GREEN);    tft.setTextFont(4);

  tft.print("METHANA = "); tft.print(CH4); tft.println("PPM");          // Print floating point number
  tft.print("AMONIA = "); tft.print(NH3); tft.println("PPM");// Print as integer value in binary
  tft.print("HIDROGEN SULFIDA = "); tft.print(H2S); tft.println("PPM"); // Print as integer number in Hexadecimal
 
 if (kondisi == 0)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print(" Baik ");
 }
 else if (kondisi == 1)
 {
 tft.setCursor(0, 50, 4);
 //tft.print("0123456789012345");
 tft.print(" Sedang ");
 }
 else if (kondisi == 2)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print(" Tidak Sehat ");
 }
 else if (kondisi == 3)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print("Sangat Tdk Sehat");
 }
 else if (kondisi == 4)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print(" Berbahaya ");
 }
delay(5000);
}
 }
  } ```

You should start by reading this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Pete.

This is not allowed in blynk sketch, use timer instead of delay and clean your void loop as Pete said.

Once you’ve restructured your sketch to make it Blynk friendly, you should read this:

Especially the part which says…

and the table which shows which analog pins belong to ASC1 and which to ADC2.

Pete.

1 Like

yeah its work. but my blynk still cant print my sensor values

and its my new code

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL5dCcUEoN";
#define BLYNK_DEVICE_NAME "gas analizer";
#define BLYNK_AUTH_TOKEN "Ei6K6zdatMU1xq21W9KhI4OVL6iJDZfX";
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "malmal";
char pass[] = "akmal123";
WidgetLCD lcd_blynk(V4);
BlynkTimer timer;

#include <SPI.h>

#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
#include <MQUnifiedsensor.h>

#define   Board   ("ESP-32")
#define         Pin4                     (35)  //Analog input 1 of your arduino
#define         Pin135                   (34)
#define         Pin136                   (33)
#define         RatioMQ4CleanAir          (4.4) //RS / R0 = 4.4 ppm 
#define         RatioMQ135CleanAir        (3.6) //RS / R0 = 10 ppm 
#define         RatioMQ136CleanAir        (3.6)
#define         ADC_Bit_Resolution        (12) // 10 bit ADC 
#define         Voltage_Resolution        (3.3) // Volt resolution to calc the voltage
#define         Type                      ("ESP-32") //Board used


#define TFT_GREY 0x5AEB // New colour
//uint32_t updateTime = 0;       // time for next update
MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin4, Type);
MQUnifiedsensor MQ135(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin135, Type);
MQUnifiedsensor MQ136(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin136, Type);
int kondisi = 0;
float CH4 = 0;
float NH3 = 0;
float H2S = 0;
void sensorDataSend()
{
    MQ4.update();
    MQ135.update();
    MQ136.update();
   Blynk.virtualWrite(V1, CH4);
Blynk.virtualWrite(V2, NH3);
Blynk.virtualWrite(V3, H2S);
}
unsigned long previousMillis = 0;
void setup(void) {
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  Serial.begin(9600); // For debug
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80); // blynk membaca auth, ssid, pass
  delay(3000);
timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
  MQ4.init();
  MQ4.setRegressionMethod(1); //_PPM =  a*ratio^b
 
  MQ135.init();
  MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b

  MQ136.init();
  MQ136.setRegressionMethod(1); //_PPM =  a*ratio^b
  
//While calibrating Your sensor Uncomment this calibration portion and calibrate for R0.
  Serial.print("Calibrating please wait.");
  float  MQ4calcR0 = 0,
         MQ135calcR0 = 0,
         MQ136calcR0 = 0;
  for(int i = 1; i <= 10; i ++)
  { 
    //Update the voltage lectures
    MQ4.update();
    MQ135.update();
    MQ136.update();
    MQ4calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
    MQ135calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
    MQ136calcR0 += MQ136.calibrate(RatioMQ136CleanAir);
    
    Serial.print(".");
  }
 
  MQ4.setR0(MQ4calcR0 / 10);
  MQ135.setR0(MQ135calcR0 / 10);
  MQ136.setR0(MQ136calcR0 / 10);

  Serial.println("  done!.");

  Serial.print("(MQ4 - MQ136):");
  Serial.print(MQ4calcR0 / 10); Serial.print(" | ");
  Serial.print(MQ135calcR0 / 10); Serial.print(" | ");
  Serial.print(MQ136calcR0 / 10); Serial.print(" | ");
}
int interval_100ms_getdata; //inisialisasi untuk get data -- ini dari blink with out delay
int interval_100ms_display; 
int event = 1, tampil_lcd = 0;
  void loop() {
    Blynk.run();
   timer.run(); 
    //---------------------------KATEGORI-----------------------------------
 if (CH4 < 0) // jika mq135 lebih kecil 0 maka NH3 hasil 0
 CH4 = 0;
 if (NH3 < 0) // jika mq135 lebih kecil 0 maka NH3 hasil 0
 NH3 = 0;
 if (H2S < 0) // jika mq136 lebih kecil 0 maka H2S hasil 0
 H2S = 0;
//--Kondisi Baik-->jika NH3,H2S,da_dust lebih kecil dari 50 maka kondisi baik dan buzzer tidak menyala
 if (CH4 <= 50 || NH3 <= 2 || H2S <= 10)
 {
 kondisi = 0;
 //buzz = 0;
 }
 //--Kondisi Baik-->jika NH3,H2S,da_dust lebih kecil dari 51 maka kondisi baik dan buzzer tidak menyala
//--Kondisi Sedang-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 51 dan lebih kecil atau sama dengan 100 kondisi sedang buzzer tidak menyala
 else if ( (CH4 >= 51 && CH4 <= 100) || ( NH3 >= 2.1 && NH3 <= 5) || ( H2S >= 11 && H2S <= 115))
 {
 kondisi = 1;
 //buzz = 0;
 }
 //--Kondisi Sedang-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 51 dan lebih kecil atau sama dengan 100 kondisi sedang buzzer tidak menyala
//--Kondisi Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 101 dan lebih kecil atau sama dengan 199 kondisi tdk sehat buzzer menyala
 else if ( (CH4 >= 101 && CH4 <= 199) || ( NH3 >= 6 && NH3 <= 10) || ( H2S >= 16 && H2S <= 20))
 {
 kondisi = 2;
 //buzz = 1;
 }
//--Kondisi Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 101 dan lebih kecil atau sama dengan 199 kondisi tdk sehat buzzer menyala
//--Kondisi Sangat Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 200 dan lebih kecil atau sama dengan 299 kondisi Sangat tdk sehat buzzer menyala
 else if ( (CH4 >= 200 && CH4 <= 299)
 || ( NH3 >= 11 && NH3 <= 15)
 || ( H2S >= 21 && H2S <= 30))
 {
 kondisi = 3;
 //buzz = 1;
 }
 //--Kondisi Sangat Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 200 dan lebih kecil atau sama dengan 299 kondisi Sangat tdk sehat buzzer menyala
//--Kondisi Berbahaya-->jika bukan kondisi berbahaya dan buzzer menyala
 else
 {
 kondisi = 4;
 //buzz = 1;
 }
 //----------------------------------------KATEGORI-----------------------------------
unsigned long currentMillis = millis(); //penyimpanan dengan kapasistas besar (long) yang di beri nama currentMillis dengan nilai millis (hitungan saat MK menyala)
 const long interval_100ms = 100; //sebuah nilai yang besar dengan di beri nama interval_100ms dan di beri nilai 100
 if ((currentMillis - previousMillis >= interval_100ms) || event == 1)
 {
  event = 0;
 previousMillis = currentMillis;
 interval_100ms_getdata++; //interval_100ms_getdata akan selalu bertambah
 if (interval_100ms_getdata >= 50 || event == 1) //100x50
{
interval_100ms_getdata = 0;
 //Serial.print("G"); Serial.print(";");
  //Update the voltage lectures
  
   MQ4.update();
  MQ135.update();  
  MQ136.update(); 

  MQ4.setA(1012.7); MQ4.setB(-2.786); //CH4
float CH4 = MQ4.readSensor(); 
   MQ135.setA(0.858); MQ135.setB(-0.417); //NH4 
float NH3 = MQ135.readSensor(); 
   MQ136.setA(36.737); MQ136.setB(-3.536); //H2S 
float H2S = MQ136.readSensor();
Serial.print("Methane:  "); Serial.println(CH4);
Serial.print("NH3:      "); Serial.println(NH3);
Serial.print("H2S:      "); Serial.println(H2S);

Serial.println("--------------------------------------------------------");

sensorDataSend();


  
  // Set "cursor" at top left corner of display (0,0) and select font 2
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(50, 0, 4);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE,TFT_BLACK);  tft.setTextSize(1);
  // We can now plot text on screen using the "print" class
  tft.println("PENGUKURAN KUALITAS UDARA");

  tft.setTextColor(TFT_GREEN,TFT_BLACK);
  tft.setTextFont(4);
  tft.println("KONDISI UDARA");

  tft.setCursor(0, 150);
  tft.setTextColor(TFT_BLUE, TFT_GREEN);    tft.setTextFont(4);

  tft.print("METHANA = "); tft.print(CH4); tft.println("PPM");          // Print floating point number
  tft.print("AMONIA = "); tft.print(NH3); tft.println("PPM");// Print as integer value in binary
  tft.print("HIDROGEN SULFIDA = "); tft.print(H2S); tft.println("PPM"); // Print as integer number in Hexadecimal
 
 if (kondisi == 0)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print(" Baik ");
 }
 else if (kondisi == 1)
 {
 tft.setCursor(0, 50, 4);
 //tft.print("0123456789012345");
 tft.print(" Sedang ");
 }
 else if (kondisi == 2)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print(" Tidak Sehat ");
 }
 else if (kondisi == 3)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print("Sangat Tdk Sehat");
 }
 else if (kondisi == 4)
 {
 tft.setCursor(0,50, 4);
 //tft.print("0123456789012345");
 tft.print(" Berbahaya ");
 }

}
 }
  } ```

That’s not surprising.
Maybe you should resolve the void loop issue, and have one function which takes a reading and displays the results as well as sending those results to Blynk.

I’d also suggest that you use the Auto Format function in the Arduino IDE, it would make your code more readable.

Pete.

1 Like

thanks pete, my project 100% works right now :pray:

By posting your final code, you might help other blynk users :relieved:

this is my final code

#define BLYNK_TEMPLATE_ID "xxx";
#define BLYNK_DEVICE_NAME "xxx";
#define BLYNK_AUTH_TOKEN "xxx";
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxx";
char pass[] = "xxx";
WidgetLCD lcd_blynk(V4);
BlynkTimer timer;

#include <SPI.h>

#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
#include <MQUnifiedsensor.h>

#define   Board   ("ESP-32")
#define         Pin4                     (35)  //Analog input 1 of your arduino
#define         Pin135                   (34)
#define         Pin136                   (33)
#define         RatioMQ4CleanAir          (4.4) //RS / R0 = 4.4 ppm 
#define         RatioMQ135CleanAir        (3.6) //RS / R0 = 10 ppm 
#define         RatioMQ136CleanAir        (3.6)
#define         ADC_Bit_Resolution        (12) // 10 bit ADC 
#define         Voltage_Resolution        (3.3) // Volt resolution to calc the voltage
#define         Type                      ("ESP-32") //Board used


#define TFT_GREY 0x5AEB // New colour

MQUnifiedsensor MQ4(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin4, Type);
MQUnifiedsensor MQ135(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin135, Type);
MQUnifiedsensor MQ136(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin136, Type);
int kondisi = 0;
float CH4 = 0;
float NH3 = 0;
float H2S = 0;


void setup() {
  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  Serial.begin(9600); // For debug
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80); // blynk membaca auth, ssid, pass
  delay(3000);
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec

  MQ4.init();
  MQ4.setRegressionMethod(1); //_PPM =  a*ratio^b

  MQ135.init();
  MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b

  MQ136.init();
  MQ136.setRegressionMethod(1); //_PPM =  a*ratio^b

  //While calibrating Your sensor Uncomment this calibration portion and calibrate for R0.
  Serial.print("Calibrating please wait.");
  float  MQ4calcR0 = 0,
         MQ135calcR0 = 0,
         MQ136calcR0 = 0;
  for (int i = 1; i <= 10; i ++)
  {
    //Update the voltage lectures
    MQ4.update();
    MQ135.update();
    MQ136.update();
    MQ4calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
    MQ135calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
    MQ136calcR0 += MQ136.calibrate(RatioMQ136CleanAir);

    Serial.print(".");
  }

  MQ4.setR0(MQ4calcR0 / 10);
  MQ135.setR0(MQ135calcR0 / 10);
  MQ136.setR0(MQ136calcR0 / 10);

  Serial.println("  done!.");

  Serial.print("(MQ4 - MQ136):");
  Serial.print(MQ4calcR0 / 10); Serial.print(" | ");
  Serial.print(MQ135calcR0 / 10); Serial.print(" | ");
  Serial.print(MQ136calcR0 / 10); Serial.print(" | ");
}
void sensorDataSend()
{
  //---------SENSOR
  MQ4.update();
  MQ135.update();
  MQ136.update();

  MQ4.setA(1012.7); MQ4.setB(-2.786); //CH4
  float CH4 = MQ4.readSensor();
  MQ135.setA(0.858); MQ135.setB(-0.417); //NH4
  float NH3 = MQ135.readSensor();
  MQ136.setA(36.737); MQ136.setB(-3.536); //H2S
  float H2S = MQ136.readSensor();
  Serial.print("Methane:  "); Serial.println(CH4);
  Serial.print("NH3:      "); Serial.println(NH3);
  Serial.print("H2S:      "); Serial.println(H2S);

  Serial.println("--------------------------------------------------------");
  //-------SENSOR
  //---------------------------KATEGORI-----------------------------------
  if (CH4 < 0) // jika mq135 lebih kecil 0 maka NH3 hasil 0
    CH4 = 0;
  if (NH3 < 0) // jika mq135 lebih kecil 0 maka NH3 hasil 0
    NH3 = 0;
  if (H2S < 0) // jika mq136 lebih kecil 0 maka H2S hasil 0
    H2S = 0;
  //--Kondisi Baik-->jika NH3,H2S,da_dust lebih kecil dari 50 maka kondisi baik dan buzzer tidak menyala
  if (CH4 <= 50 || NH3 <= 2 || H2S <= 10)
  {
    kondisi = 0;
  }
  //--Kondisi Baik-->jika NH3,H2S,da_dust lebih kecil dari 51 maka kondisi baik dan buzzer tidak menyala
  //--Kondisi Sedang-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 51 dan lebih kecil atau sama dengan 100 kondisi sedang buzzer tidak menyala
  else if ( (CH4 >= 51 && CH4 <= 100) || ( NH3 >= 2.1 && NH3 <= 5) || ( H2S >= 11 && H2S <= 115))
  {
    kondisi = 1;
  }
  //--Kondisi Sedang-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 51 dan lebih kecil atau sama dengan 100 kondisi sedang buzzer tidak menyala
  //--Kondisi Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 101 dan lebih kecil atau sama dengan 199 kondisi tdk sehat buzzer menyala
  else if ( (CH4 >= 101 && CH4 <= 199) || ( NH3 >= 6 && NH3 <= 10) || ( H2S >= 16 && H2S <= 20))
  {
    kondisi = 2;
  }
  //--Kondisi Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 101 dan lebih kecil atau sama dengan 199 kondisi tdk sehat buzzer menyala
  //--Kondisi Sangat Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 200 dan lebih kecil atau sama dengan 299 kondisi Sangat tdk sehat buzzer menyala
  else if ( (CH4 >= 200 && CH4 <= 299)
            || ( NH3 >= 11 && NH3 <= 15)
            || ( H2S >= 21 && H2S <= 30))
  {
    kondisi = 3;
  }
  //---------------------------------------KATEGORI-----------------------------------
  //--Kondisi Sangat Tdk Sehat-->jika NH3,H2S,da_dust lebih besar atau sama dengan dari 200 dan lebih kecil atau sama dengan 299 kondisi Sangat tdk sehat buzzer menyala
  //--Kondisi Berbahaya-->jika bukan kondisi berbahaya dan buzzer menyala
  else
  {
    kondisi = 4;
    //buzz = 1;
  }




  //------LCD PRINT
  tft.setCursor(50, 0, 4);
  tft.setTextColor(TFT_WHITE, TFT_BLACK);  tft.setTextSize(1);
  tft.println("PENGUKURAN KUALITAS UDARA");
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.setTextFont(4);
  tft.println("KONDISI UDARA");

  tft.setCursor(0, 150);
  tft.setTextColor(TFT_BLUE, TFT_GREEN);    tft.setTextFont(4);

  tft.print("METHANA = "); tft.print(CH4); tft.println("PPM");          // Print floating point number
  tft.print("AMONIA = "); tft.print(NH3); tft.println("PPM");// Print as integer value in binary
  tft.print("HIDROGEN SULFIDA = "); tft.print(H2S); tft.println("PPM"); // Print as integer number in Hexadecimal

  if (kondisi == 0)
  {
    tft.setCursor(0, 50, 4);
    //tft.print("0123456789012345");
    tft.print(" Baik ");
  }
  else if (kondisi == 1)
  {
    tft.setCursor(0, 50, 4);
    //tft.print("0123456789012345");
    tft.print(" Sedang ");
  }
  else if (kondisi == 2)
  {
    tft.setCursor(0, 50, 4);
    //tft.print("0123456789012345");
    tft.print(" Tidak Sehat ");
  }
  else if (kondisi == 3)
  {
    tft.setCursor(0, 50, 4);
    //tft.print("0123456789012345");
    tft.print("Sangat Tdk Sehat");
  }
  else if (kondisi == 4)
  {
    tft.setCursor(0, 50, 4);
    //tft.print("0123456789012345");
    tft.print(" Berbahaya ");
  }
  //-----LCD PRINT
  Blynk.virtualWrite(V1, CH4);
  Blynk.virtualWrite(V2, NH3);
  Blynk.virtualWrite(V3, H2S);
}
void loop() {
  Blynk.run();
  timer.run();

}```

Don’t you think this is a rather clunky way of doing things?

Why aren’t you simply doing the TFT print if the first if statement is met?

Also, why are you specifying the server and port in the Blynk.begin command?

Pete.

1 Like

yes this code not fully simplified, thanks for telling me know this code clunky, and i will make this code more simple