BIMA-25 / Final_Project_KEL8

final project
1 stars 1 forks source link

TA-PROGRES 4 BIKIN MAIN CODE (GABUNGIN SEMUA CODE DAN SESUAIKAN LOGIKANYA DENGAN ALAT YANG AKAN DI BIKIN) #21

Closed BIMA-25 closed 1 year ago

BIMA-25 commented 1 year ago

CODE INI ADALAH CODE AKHIR YANG AKAN DI UPLOAD KE ALAT

BIMA-25 commented 1 year ago

//main code

float CO2; float Toluen; float NH4 ; float Aceton ;

int red = 25; int green = 33; int blue = 32;

//milis reconnek wifi unsigned long previousMillis = 0; unsigned long interval = 30000;

void setup() { Serial.begin(115200); ledsetup(); digitalWrite(blue, HIGH); Wifisetup(); buzzersetup(); relaysetup(); mQsetup(); teleSetup(); blynkSetup(); }

void loop() { unsigned long currentMillis = millis(); // if WiFi is down, try reconnecting every CHECK_WIFI_TIME seconds if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) { Serial.print(millis()); Serial.println("Reconnecting to WiFi..."); WiFi.disconnect(); WiFi.reconnect(); Serial.println(ssid); Serial.println(pass); previousMillis = currentMillis; } mQloop(); ledloop(); blynkloop(); teleloop(); relayloop(); buzzerloop(); Wifiloop(); }

BIMA-25 commented 1 year ago

// mq135 code

define placa "ESP32"

define Voltage_Resolution 3.3

define pin A0 //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

MQUnifiedsensor MQ135(placa, Voltage_Resolution, ADC_Bit_Resolution, pin, type);

void mQsetup() { //Init the serial port communication - to debug the library MQ135.setRegressionMethod(1); //_PPM = a*ratio^b 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 ****/

}

void mQloop() { MQ135.update(); // Update data, the arduino will read the voltage from the analog pin

MQ135.setA(605.18); MQ135.setB(-3.937); // Configure the equation to calculate CO concentration value CO = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup

MQ135.setA(110.47); MQ135.setB(-2.862); // Configure the equation to calculate CO2 concentration value CO2 = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup

MQ135.setA(44.947); MQ135.setB(-3.445); // Configure the equation to calculate Toluen concentration value Toluen = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup

MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configure the equation to calculate NH4 concentration value NH4 = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup

MQ135.setA(34.668); MQ135.setB(-3.369); // Configure the equation to calculate Aceton concentration value Aceton = MQ135.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup Serial.println(" Values from MQ-135 **"); Serial.println("| CO | CO2 | Toluen | NH4 | Aceton |");
Serial.print("| "); Serial.print(CO); Serial.print(" | "); Serial.print(CO2 + 400); Serial.print(" | "); Serial.print(Toluen); Serial.print(" | "); Serial.print(NH4); Serial.print(" | "); Serial.print(Aceton); Serial.println(" |"); delay(500); }

BIMA-25 commented 1 year ago

// telegram code

define BOTtoken "6160614665:AAGcNpljKROAO0tzeGqb-LT0luGzePMKb6U"

define CHAT_ID "6097007259"

//milis send masage unsigned long previousTelMillis = 0; unsigned long intervalTel = 60000;

int state =0;

WiFiClientSecure client; UniversalTelegramBot bot(BOTtoken, client);

// Checks for new messages every 1 second. int botRequestDelay = 1000; unsigned long lastTimeBotRan;

// Handle what happens when you receive new messages void handleNewMessages(int numNewMessages){ Serial.println("handleNewMessages"); Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    Serial.println(chat_id);

      if (chat_id != CHAT_ID){
        bot.sendMessage(chat_id, "Unauthorized user", "");
        continue;
      }

   // Print the received message
   String text = bot.messages[i].text;
   Serial.println(text);

   String from_name = bot.messages[i].from_name;
    //kirim comand control
    if (text == "/start") {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "Use the following commands to control your outputs.\n\n";
      welcome += "/CO untuk melihat kadar CO di udara saat ini\n";
      welcome += "/CO2 untuk melihat kadar CO2 di udara saat ini\n";
      welcome += "/Tolueno untuk melihat kadar Tolueno di udara saat ini\n";
      welcome += "/NH4 untuk melihat kadar NH4 di udara saat ini\n";
      welcome += "/Aceton untuk melihat kadar Aceton di udara saat ini\n";
      welcome += "/All untuk melihat semua\n";
      welcome += "/cek untuk melihat kondisi udara saat ini\n";
      bot.sendMessage(chat_id, welcome, "");
    }
    //kirim kadar ppm gas CO
    if (text == "/CO"){
     float coo = CO;
     String kadar = "kadar CO di udara : ";
     kadar += float (coo);
     kadar += " ppm\n";
      bot.sendMessage(CHAT_ID, kadar, "");
    }
    //kirim kadar ppm gas CO2
    if (text == "/CO2"){
     float co22 = CO2 + 400;
     String kadar = "kadar CO2 di udara : ";
     kadar += float (co22);
     kadar += " ppm\n";
      bot.sendMessage(CHAT_ID, kadar, "");
    }
    //kirim kadar ppm gas Tolueno
    if (text == "/Tolueno"){
     float toluenoo = Toluen;
     String kadar = "kadar Tolueno di udara : ";
     kadar += float (toluenoo);
     kadar += " ppm\n";
      bot.sendMessage(CHAT_ID, kadar, "");
    }
    //kirim kadar ppm gas NH4
    if (text == "/NH4"){
     float nh44 = NH4;
     String kadar = "kadar NH4 di udara : ";
     kadar += float (nh44);
     kadar += " ppm\n";
      bot.sendMessage(CHAT_ID, kadar, "");
    }
    //kirim kadar ppm gas aceton
    if (text == "/Aceton"){
     float acetonn = Aceton;
     String kadar = "kadar Aceton di udara : ";
     kadar += float (acetonn);
     kadar += " ppm\n";
      bot.sendMessage(CHAT_ID, kadar, "");
    }
    //kirim informasi detail jumlah kadar ppm gas di udara
     if (text == "/All"){
     float acetonn = Aceton;
     float nh44 = NH4;
     float toluenoo = Toluen;
     float co22 = CO2 + 400;
     float coo = CO;
     String kadar = "kadar : \n";
     kadar += "CO ";
     kadar += float (coo);
     kadar += " ppm\n";
     kadar += "CO2 ";
     kadar += float (co22);
     kadar += " ppm\n";
     kadar += "Touleno ";
     kadar += float (toluenoo);
     kadar += " ppm\n";
     kadar += "NH4 ";
     kadar += float (nh44);
     kadar += " ppm\n";
     kadar += "Aceton ";
     kadar += float (acetonn);
     kadar += " ppm\n";
      bot.sendMessage(CHAT_ID, kadar, "");
    }
    // kirim peringatan gas apa saja yang di ambang batas bahaya
     if (text == "/cek"){
        if (CO > 80 )  {
          bot.sendMessage(CHAT_ID, "PERINGATAN! GAS CO SUDAH DI AMBANG BATAS BAHAYA!", "");  
         }
        if (CO2 > 1000 ) {
          bot.sendMessage(CHAT_ID, "PERINGATAN! GAS CO2 SUDAH DI AMBANG BATAS BAHAYA!", ""); 
         }
        if (Toluen > 100 ) {
          bot.sendMessage(CHAT_ID, "PERINGATAN! GAS TOLUENO SUDAH DI AMBANG BATAS BAHAYA!", ""); 
         }
        if (NH4 > 60 ) {
          bot.sendMessage(CHAT_ID, "PERINGATAN! GAS NH4 SUDAH DI AMBANG BATAS BAHAYA!", ""); 
         }
        if (Aceton > 500 ) {
          bot.sendMessage(CHAT_ID, "PERINGATAN! GAS ACETON SUDAH DI AMBANG BATAS BAHAYA!", ""); 
         }  
        if (CO < 80 && CO2 < 1000 && Toluen < 100 && NH4 < 80 && Aceton < 500 ){
          bot.sendMessage(CHAT_ID, "KONDISI UDARA AMAN", ""); 
         }}}}

void teleSetup(){ client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org bot.sendMessage(CHAT_ID, "hallo selamat datang di gas bot tulis /start untuk melihat comands control ", ""); } void teleloop(){ if (millis() > lastTimeBotRan + botRequestDelay) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

      while(numNewMessages) {
        Serial.println("got response");
        handleNewMessages(numNewMessages);
        numNewMessages = bot.getUpdates(bot.last_message_received + 1);
      }

//kirim peringatan adanya gas berbahaya
lastTimeBotRan = millis(); } unsigned long currentTelMillis = millis(); if (CO > 80 || CO2 > 1000 || Toluen > 100 || NH4 > 80 || Aceton > 500 && state != 1 ){ state = 1; bot.sendMessage(CHAT_ID, "PERINGATAN! ADA GAS DI AMBANG BATAS BAHAYA! SEND /cek UNTUK LIHAT DETAIL", ""); }else if(currentTelMillis - previousTelMillis >= intervalTel) { state = 2; previousTelMillis = currentTelMillis; } }

BIMA-25 commented 1 year ago

// wifi manager code

WiFiManager wm;

void Wifisetup(){ pinMode(15,INPUT); //for resetting WiFi creds //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around

bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("AutoConnectAP","password"); // password protected ap

if(!res) {
    Serial.println("Failed to connect");
     ESP.restart();
} 
else {
    //if you get here you have connected to the WiFi    
    Serial.println("connected...yeey :)");
} 

}

void Wifiloop(){
// put your main code here, to run repeatedly:
if(digitalRead(15) == HIGH){ Serial.println("Wiping WiFi credentials from memory..."); wm.resetSettings(); ESP.restart(); } }

BIMA-25 commented 1 year ago

// blynk code

define BLYNK_PRINT Serial

define BLYNK_TEMPLATE_ID "TMPL6-4nIyEFq"

define BLYNK_TEMPLATE_NAME "GasDashboard"

define BLYNK_AUTH_TOKEN "Eh2F7jJGocwFyQlTJHgixZeCPmht7w1g"

BlynkTimer timer;

void myTimerEvent(){ Blynk.virtualWrite(V0, CO ); Blynk.virtualWrite(V1, CO2 + 400); Blynk.virtualWrite(V2, Toluen); Blynk.virtualWrite(V3, NH4); Blynk.virtualWrite(V4, Aceton); }

void blynkSetup(){ Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); timer.setInterval(1000L, myTimerEvent); }

void blynkloop(){ Blynk.run(); timer.run(); // Initiates BlynkTimer }

BIMA-25 commented 1 year ago

// led code

void ledsetup(){ pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); } void ledloop(){ if (CO > 80 || CO2 > 1000 || Toluen > 100 || NH4 > 80 || Aceton > 500 ){ digitalWrite(red,HIGH); digitalWrite(green,LOW); }else{ digitalWrite(green,HIGH); digitalWrite(red,LOW); } if ((WiFi.status() != WL_CONNECTED)){ digitalWrite(blue, HIGH);
}else{ digitalWrite(blue, LOW); } }

BIMA-25 commented 1 year ago

// buzzer code

void buzzersetup(){ pinMode(26, OUTPUT); digitalWrite(26, HIGH); } void buzzerloop(){ if (CO > 80 || CO2 > 1000 || Toluen > 100 || NH4 > 80 || Aceton > 500 ){ digitalWrite(26,LOW); }else{ digitalWrite(26,HIGH); } }

BIMA-25 commented 1 year ago

// relay code

void relaysetup(){ pinMode(27, OUTPUT); } void relayloop(){ if (CO > 80 || CO2 > 1000 || Toluen > 100 || NH4 > 80 || Aceton > 500 ){ digitalWrite(27,LOW); }else{ digitalWrite(27,HIGH); } }