Monitor Suhu dan BPM Fuzzy Logic Mamdani EFLL IOT Blynk 2.0
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitor suhu dan bpm dengan metode logika fuzzy mamdani. alat ini dipakai untuk mendeteksi penyakit hipotrmia ketika berada di gunung. untuk lebih jelasnya berikut adalah koding dan skemanya.
1. Skema
2. Program Arduino IDE
#include <Fuzzy.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
float tempobject;
float temppeltier;
int dataadc;
int n = 0;
int bpm = 0;
int x = 0;
int bpmnya = 0;
Fuzzy *fuzzy = new Fuzzy();
// FuzzyInput
FuzzySet *dingin = new FuzzySet(0, 0, 25, 28);
FuzzySet *sedang = new FuzzySet(28, 33, 33, 35);
FuzzySet *aman = new FuzzySet(35, 40, 50, 50);
// FuzzyInput
FuzzySet *lambat = new FuzzySet(0, 0, 50, 60);
FuzzySet *normal = new FuzzySet(60, 80, 80, 100);
FuzzySet *cepat = new FuzzySet(100, 130, 150, 150);
// FuzzyOutput
FuzzySet *minimum = new FuzzySet(0, 0, 30, 50);
FuzzySet *average = new FuzzySet(50, 75, 75, 100);
FuzzySet *maximum = new FuzzySet(100, 150, 200, 200);
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
Serial.begin(9600);
// FuzzyInput suhuobject
FuzzyInput *suhuobject = new FuzzyInput(1);
suhuobject->addFuzzySet(dingin);
suhuobject->addFuzzySet(sedang);
suhuobject->addFuzzySet(aman);
fuzzy->addFuzzyInput(suhuobject);
// FuzzyInput suhupeltier
FuzzyInput *nilaibpm = new FuzzyInput(2);
nilaibpm->addFuzzySet(lambat);
nilaibpm->addFuzzySet(normal);
nilaibpm->addFuzzySet(cepat);
fuzzy->addFuzzyInput(nilaibpm);
// FuzzyOutput pwm
FuzzyOutput *pwmout = new FuzzyOutput(1);
pwmout->addFuzzySet(minimum);
pwmout->addFuzzySet(average);
pwmout->addFuzzySet(maximum);
fuzzy->addFuzzyOutput(pwmout);
// Building FuzzyRule 1
FuzzyRuleAntecedent *ifsuhuobjectDinginAndnilaibpmLambat = new FuzzyRuleAntecedent();
ifsuhuobjectDinginAndnilaibpmLambat->joinWithAND(dingin, lambat);
FuzzyRuleConsequent *thenPwmoutMaximum= new FuzzyRuleConsequent();
thenPwmoutMaximum->addOutput(maximum);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifsuhuobjectDinginAndnilaibpmLambat, thenPwmoutMaximum);
fuzzy->addFuzzyRule(fuzzyRule1);
// Building FuzzyRule 2
FuzzyRuleAntecedent *ifsuhuobjectDinginAndnilaibpmNormal = new FuzzyRuleAntecedent();
ifsuhuobjectDinginAndnilaibpmNormal->joinWithAND(dingin, normal);
//FuzzyRuleConsequent *thenPwmoutMaximum= new FuzzyRuleConsequent();
thenPwmoutMaximum->addOutput(maximum);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifsuhuobjectDinginAndnilaibpmNormal, thenPwmoutMaximum);
fuzzy->addFuzzyRule(fuzzyRule2);
// Building FuzzyRule 3
FuzzyRuleAntecedent *ifsuhuobjectDinginAndnilaibpmCepat = new FuzzyRuleAntecedent();
ifsuhuobjectDinginAndnilaibpmCepat->joinWithAND(dingin, cepat);
FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
thenPwmoutAverage->addOutput(average);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifsuhuobjectDinginAndnilaibpmNormal, thenPwmoutAverage);
fuzzy->addFuzzyRule(fuzzyRule3);
// Building FuzzyRule 4
FuzzyRuleAntecedent *ifsuhuobjectSedangAndnilaibpmLambat = new FuzzyRuleAntecedent();
ifsuhuobjectSedangAndnilaibpmLambat->joinWithAND(sedang, lambat);
//FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
thenPwmoutAverage->addOutput(average);
FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifsuhuobjectSedangAndnilaibpmLambat, thenPwmoutAverage);
fuzzy->addFuzzyRule(fuzzyRule4);
// Building FuzzyRule 5
FuzzyRuleAntecedent *ifsuhuobjectSedangAndnilaibpmNormal = new FuzzyRuleAntecedent();
ifsuhuobjectSedangAndnilaibpmNormal->joinWithAND(sedang, normal);
//FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
thenPwmoutAverage->addOutput(average);
FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifsuhuobjectSedangAndnilaibpmNormal, thenPwmoutAverage);
fuzzy->addFuzzyRule(fuzzyRule5);
// Building FuzzyRule 6
FuzzyRuleAntecedent *ifsuhuobjectSedangAndnilaibpmCepat = new FuzzyRuleAntecedent();
ifsuhuobjectSedangAndnilaibpmCepat->joinWithAND(sedang, cepat);
//FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
thenPwmoutAverage->addOutput(average);
FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifsuhuobjectSedangAndnilaibpmCepat, thenPwmoutAverage);
fuzzy->addFuzzyRule(fuzzyRule6);
// Building FuzzyRule 7
FuzzyRuleAntecedent *ifsuhuobjectAmanAndnilaibpmLambat = new FuzzyRuleAntecedent();
ifsuhuobjectAmanAndnilaibpmLambat->joinWithAND(aman, lambat);
//FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
thenPwmoutAverage->addOutput(average);
FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifsuhuobjectAmanAndnilaibpmLambat, thenPwmoutAverage);
fuzzy->addFuzzyRule(fuzzyRule7);
// Building FuzzyRule 8
FuzzyRuleAntecedent *ifsuhuobjectAmanAndnilaibpmNormal = new FuzzyRuleAntecedent();
ifsuhuobjectAmanAndnilaibpmNormal->joinWithAND(aman, normal);
FuzzyRuleConsequent *thenPwmoutMinimum= new FuzzyRuleConsequent();
thenPwmoutMinimum->addOutput(minimum);
FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifsuhuobjectAmanAndnilaibpmNormal, thenPwmoutMinimum);
fuzzy->addFuzzyRule(fuzzyRule8);
// Building FuzzyRule 9
FuzzyRuleAntecedent *ifsuhuobjectAmanAndnilaibpmCepat = new FuzzyRuleAntecedent();
ifsuhuobjectAmanAndnilaibpmCepat->joinWithAND(aman, cepat);
//FuzzyRuleConsequent *thenPwmoutMinimum= new FuzzyRuleConsequent();
thenPwmoutMinimum->addOutput(minimum);
FuzzyRule *fuzzyRule9 = new FuzzyRule(9, ifsuhuobjectAmanAndnilaibpmCepat, thenPwmoutMinimum);
fuzzy->addFuzzyRule(fuzzyRule9);
}
void loop()
{
sensors.requestTemperatures();
tempobject = sensors.getTempCByIndex(0);
bpm = 0;
bpmxx();
lcd.setCursor(0, 0);
lcd.print("T = ");
lcd.print(tempobject);
lcd.print(" ");
fuzzy->setInput(1, tempobject);
fuzzy->setInput(2, bpm);
fuzzy->fuzzify();
/*
Serial.print("Suhu Object: Dingin-> ");
Serial.print(dingin->getPertinence());
Serial.print(", Normal-> ");
Serial.print(sedang->getPertinence());
Serial.print(", Panas-> ");
Serial.println(aman->getPertinence());
Serial.print("BPM: Rendah-> ");
Serial.print(lambat->getPertinence());
Serial.print(", Sedang-> ");
Serial.print(normal->getPertinence());
Serial.print(", Tinggi-> ");
Serial.print(cepat->getPertinence());
*/
float output1 = fuzzy->defuzzify(1);
/*
Serial.println("Output: ");
Serial.print("PWMOUT: Minimum-> ");
Serial.print(minimum->getPertinence());
Serial.print(", Average-> ");
Serial.print(average->getPertinence());
Serial.print(", Maximum-> ");
Serial.println(maximum->getPertinence());
*/
lcd.setCursor(10, 0);
lcd.print(output1);
lcd.print(" ");
if(output1 <= 50){
lcd.setCursor(6, 1);
lcd.print("NMRL");
}
if((output1 > 50)&&(output1 <= 100)){
lcd.setCursor(6, 1);
lcd.print("SDNG");
}
if((output1 > 100)&&(output1 < 200)){
lcd.setCursor(6, 1);
lcd.print("HYPO");
}
Serial.print("*");
Serial.print(tempobject);
Serial.print(",");
Serial.print(bpmnya);
Serial.print(",");
Serial.print(output1);
Serial.println("#");
delay(1000);
}
void bpmxx(){
n++;
int dataadc = analogRead(A0);
lcd.setCursor(14, 1);
lcd.print(n);
lcd.setCursor(0, 1);
lcd.print("B=");
lcd.print(bpmnya);
//lcd.print(" / ");
//lcd.print(dataadc1);
lcd.print(" ");
if ((dataadc > 820) && (x > 1)) {
x = 0;
bpm = bpm + 1;
}
else if ((dataadc < 820) && (x < 1)) {
x = 2;
}
delay(200);
if ( n > 50 ) {
bpmnya = bpm * 6;
n = 0;
return;
}
bpmxx();
}
3. Program ESP8266
#define BLYNK_TEMPLATE_ID "TMPL6wOC--xxx"
#define BLYNK_TEMPLATE_NAME "monitor daya"
#define BLYNK_AUTH_TOKEN "VOK0cWiFN5ycHj3SV_snEDXTfTfeixxx"
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int suhu = 0;
int bpm = 0;
int output1 = 0;
int datain1;
int datain2;
int datain3;
String dataIn;
String dt[10];
int i;
boolean parsing=false;
int pinValue;
BlynkTimer timer;
char ssid[] = "hotspothpku";
char pass[] = "123456789";
void sendSensor()
{
Blynk.virtualWrite(V0, suhu);
Blynk.virtualWrite(V1, bpm);
Blynk.virtualWrite(V2, output1);
delay(1000);
}
void setup(){
Serial.begin(9600);
dataIn="";
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, sendSensor);
}
void loop(){
if( Serial.available()>0) {
char inChar = Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
parsing=false;
dataIn="";
Blynk.run();
timer.run();
}
}
void parsingData(){
int j=0;
//kirim data yang telah diterima sebelumnya
Serial.print("data masuk :");
Serial.print(dataIn);
//inisialisasi variabel, (reset isi variabel)
dt[j]="";
//proses parsing data
for(i=1;i<dataIn.length();i++){
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
//increment variabel j, digunakan untuk merubah index array penampung
j++;
dt[j]=""; //inisialisasi variabel array dt[j]
}
else
{
//proses tampung data saat pengecekan karakter selesai.
dt[j] = dt[j] + dataIn[i];
}
}
datain1 = dt[0].toInt();
datain2 = dt[1].toInt();
datain3 = dt[2].toInt();
suhu = datain1 / 1;
bpm = datain2 / 1;
output1 =datain3 / 1;
//kirim data hasil parsing
Serial.print("data 1 : ");
Serial.println(datain1);
Serial.print("data 2 : ");
Serial.println(datain2);
Serial.print("data 3 : ");
Serial.println(datain3);
Serial.print("\n\n");
}
4. VIDEO HASILNYA
No comments:
Post a Comment