Source Code Arduino

mail@pastecode.io avatar
unknown
c_cpp
a month ago
7.3 kB
1
Indexable
Never
#include <SoftwareSerial.h>
#include <WiFiEsp.h>
#ifndef HAVE_HWSERIAL1
#endif
#include <Wire.h>
#include <DHT22.h>
#include <BH1750.h>
#define pinDATA SDA
#define kipas 9
#define mistMaker1 10
#define mistMaker2 11

//mencari nilai PPM Seunsor MQ-135
#define RL 1      //nilai RL =10 pada sensor
#define m -0.417  //hasil perhitungan gradien
#define b 0.858   //hasil perhitungan perpotongan
#define Ro 16.47   //hasil pengukuran RO
#define MQ_sensor A0

//variable untuk mencari nilai PPM Gas Ammonia
const int numReadings = 5;  //nilai penambilan sample pembacaan sebesar 5 kali
float readings[numReadings];
int readIndex = 0;
float total = 0;
float average = 0;
//end  PPM AMmmonia

DHT22 dht22(8);
BH1750 lightMeter;
WiFiEspClient client;

//variable untuk metode fuzzy tsukamoto
float keanggotaanKelembaban[3];
float alpha[3];
float z[3];
float Eaizi;
float Eai;
//end fuzzy tsukamoto

char* SSID = "Ciaomi";
char* PASS = "12345678";

SoftwareSerial wifiSerial(2, 3);  // RX, TX

void setWifi(char* ssid, char* pass) {
  wifiSerial.begin(9600);
  WiFi.init(&wifiSerial);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("[WIFI]: Connecting to WiFi...");
    if (WiFi.begin(ssid, pass) == WL_CONNECTED) {
      Serial.println("[WIFI]: Connected");
    } else {
      Serial.println("[WIFI]: Connection failed");
      delay(1000);
    }
  }
}


void fc_keanggotaanKelembaban(float x) {
  // kelembaban kering
  if (x <= 50) {
    keanggotaanKelembaban[0] = 1;
  } else if (x >= 50 && x <= 74) {
    keanggotaanKelembaban[0] = (74 - x) / 24;
  } else {
    keanggotaanKelembaban[0] = 0;
  }

  // Normal / Lembab
  if (x <= 50 || x >= 100) {
    keanggotaanKelembaban[1] = 0;
  } else if (x >= 50 && x <= 75) {
    keanggotaanKelembaban[1] = (x - 50) / 25;
  } else {
    keanggotaanKelembaban[1] = (100 - x) / 25.0;
  }

  // Basah
  if (x >= 95) {
    keanggotaanKelembaban[2] = 1;
  } else if (x >= 87 && x <= 95) {
    keanggotaanKelembaban[2] = (x - 87) / 8;
  } else {
    keanggotaanKelembaban[2] = 0;
  }
}

float getAlphaKering(float alpha) {
  return (keanggotaanKelembaban[0]);
}

float getAlphaLembab(float alpha) {
  return (keanggotaanKelembaban[1]);
}

float getAlphaBasah(float alpha) {
  return (keanggotaanKelembaban[2]);
}

void inferensi(float y) {
  // Reset nilai z di awal inferensi
  for (int i = 0; i < 3; i++) {
    z[i] = 0;
  }

  // Rules 1 = IF kelembaban Kering, THEN mist Maker Hidup
  alpha[0] = getAlphaKering(y);
  if (alpha[0] <= 0.4) {
    z[0] = 0;
  } else if (alpha[0] > 0.4 && alpha[0] <= 0.5) {
    z[0] = (alpha[0] - 0.4) / 0.1;
  } else {
    z[0] = 1;
  }

  // Rules 2 = IF kelembaban Lembab, THEN mist Maker Hidup
  alpha[1] = getAlphaLembab(y);
  if (alpha[1] <= 0.4) {
    z[1] = 0;
  } else if (alpha[1] > 0.4 && alpha[1] <= 0.5) {
    z[1] = (alpha[1] - 0.4) / 0.1;
  } else {
    z[1] = 1;
  }

  // Rules 3 = IF kelembaban Basah, THEN mist Maker Mati
  alpha[2] = getAlphaBasah(y);
  if (alpha[2] >= 0.6) {
    z[2] = 0;
  } else if (alpha[2] >= 0.5 && alpha[2] <= 0.6) {
    z[2] = (0.6 - alpha[2]) / 0.1;
  } else {
    z[2] = 1;
  }

  Serial.println("proses inferensi");
  // Debugging print statements to check the values of z
  for (int i = 0; i < 3; i++) {
    Serial.print("z[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(z[i]);
  }
  Serial.println("End proses inferensi");
}

float defuzzifikasi() {
  // Reset Eaizi dan Eai di awal fungsi
  Eaizi = 0;
  Eai = 0;

  Serial.println("proses Defuzzifikasi");
  for (int i = 0; i < 3; i++) {
    Serial.print("Alpha[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(alpha[i]);
    Serial.print("z[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(z[i]);
    Eaizi += (alpha[i] * z[i]);
    Eai += alpha[i];
  }
  Serial.println("End proses defuzzifikasi");
  Serial.println(Eaizi/Eai);
  return Eaizi / Eai;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(kipas, OUTPUT);
  pinMode(mistMaker1, OUTPUT);
  pinMode(mistMaker2, OUTPUT);
  setWifi(SSID, PASS);
  Wire.begin();
  lightMeter.begin();

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  delay(2000);
  float t = dht22.getTemperature();
  int h = dht22.getHumidity();
  float intensitas = lightMeter.readLightLevel();
  float VRL;
  float RS;
  float ratio;

  VRL = analogRead(MQ_sensor) * (5 / 1023.0);     //konversi analog ke tegangan
  RS = (5 / VRL - 1) * 10;                        //rumus untuk RS
  ratio = RS / Ro;                                // rumus mencari ratio
  float ppm = pow(10, ((log10(ratio) - b) / m));  //rumus mencari ppm
  total = total - readings[readIndex];
  readings[readIndex] = ppm;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;
  if (readIndex >= numReadings) {
    readIndex = 0;
  }
  average = total / numReadings;

  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" °C\tHumidity: ");
  Serial.print(h);
  Serial.println(" %");
  Serial.print("Light: ");
  Serial.print(intensitas);
  Serial.println(" lx");
  Serial.print("Ammonia (PPM): ");
  Serial.println(average);  // Tampilkan nilai ppm ammonia di serial monitor
  fc_keanggotaanKelembaban(h);
  inferensi(h);
  float hasil = defuzzifikasi();
  
  if (hasil >= 0.5) {
    if (h >= 0 && h < 75 ) {
    digitalWrite(kipas, LOW);
    digitalWrite(mistMaker1, LOW);
    digitalWrite(mistMaker2, LOW);
    Serial.println("hidup semua");
    }
    else if (h >= 75 && h <= 90) {
    digitalWrite(kipas, LOW);
    digitalWrite(mistMaker1, LOW);
    digitalWrite(mistMaker2, HIGH);
    Serial.println("hidup 1");
      
    }
  } else {
    digitalWrite(kipas, HIGH);
    digitalWrite(mistMaker1, HIGH);
    digitalWrite(mistMaker2, HIGH);
    Serial.println("mati semua");
  }
  delay(1000);
  //pengiriman data ke server
  sendData(t, h, intensitas, average);
  delay(5000);
}

void sendData(float temperature, int humidity, float intensitas, float average) {
  if (client.connect("iot.vwxyzar.my.id", 5051)) {
    // if (client.connect("192.168.39.234", 80)) {
    Serial.println("Connected to server. Sending data...");
    String requestData = "temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&intensitas=" + String(intensitas) + "&ammonia=" + String(average);
    client.print("POST /2018067/write-data.php HTTP/1.1\r\n");
    client.print("Host: iot.vwxyzar.my.id\r\n");
    client.print("Content-Type: application/x-www-form-urlencoded\r\n");
    client.print("Content-Length: ");
    client.print(requestData.length());
    client.print("\r\n\r\n");  // Additional line break before the body
    client.print(requestData);
    Serial.println("Data sent.");
  } else {
    resetArduino();
    Serial.println("Connection to server failed.");
  }
  client.stop();  // Menutup koneksi setelah pengiriman data
}
void resetArduino() {
  asm volatile("  jmp 0");
}
Leave a Comment