Monitor Suhu dan Berat Fuzzy Logic Mamdani IOT Firebase ESP32 (thermocouple tipe K dan Hx711)
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mengukur suhu dan berat dengan menggunakan thermocouple tipe K dan loadcell + Hx711. alat ini dimonitor dengan IOT Firebase dan metode Fuzzy logic Mamdani. untuk lebih jelasnya berikut adalah koding dan komponennya.
1. Komponen
2. Interface Firebase
3. Program Arduino IDE
#include <Fuzzy.h>
#include <Wire.h>
#include <max6675.h>
#include "HX711.h"
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <FirebaseClient.h>
int thermoDO = 25; //bisa juga S0
int thermoCS = 32;
int thermoCLK = 33; //bisa juga SCK
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int suhunya;
int beratnya;
int waktunya;
// HX711.DOUT - pin #26
// HX711.PD_SCK - pin #27
HX711 scale(26, 27);
float tera = 0;
int berat;
float fix;
int x;
float output1;
// Network and Firebase credentials
#define WIFI_SSID "hotspothpku"
#define WIFI_PASSWORD "123456789"
#define Web_API_KEY "hjhkjhkgjygjfhtfhgfrert34fdgfgfxdxfd657f"
#define DATABASE_URL "https://alamatdaratabseyangdipakai-pilihdireatimedatabase/"
#define USER_EMAIL "alamatemail pengguna"
#define USER_PASS "paswordemailnya"
// User function
void processData(AsyncResult &aResult);
// Authentication
UserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);
// Firebase components
FirebaseApp app;
WiFiClientSecure ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client);
RealtimeDatabase Database;
// Timer variables for sending data every 10 seconds
unsigned long lastSendTime = 0;
const unsigned long sendInterval = 10000; // 10 seconds in milliseconds
// Variables to send to the database
int intValue = 0;
float floatValue = 0.01;
String stringValue = "";
Fuzzy *fuzzy = new Fuzzy();
// FuzzyInputn suhu
FuzzySet *light = new FuzzySet(0, 0, 195, 212);
FuzzySet *medium = new FuzzySet(203, 200, 200, 221);
FuzzySet *dark = new FuzzySet(212, 223, 223, 230);
// FuzzyInput berat
FuzzySet *sedikit = new FuzzySet(0, 0, 100, 100);
FuzzySet *banyak = new FuzzySet(100, 200, 200, 250);
// FuzzyInput durasi
FuzzySet *singkat = new FuzzySet(0, 0, 8, 11);
FuzzySet *normal = new FuzzySet(9, 11, 11, 13);
FuzzySet *lama = new FuzzySet(11, 13, 15, 15);
// FuzzyOutput
FuzzySet *lambat = new FuzzySet(0, 0, 50, 75);
FuzzySet *sedang = new FuzzySet(60, 75, 75, 90);
FuzzySet *cepat = new FuzzySet(75, 90, 100, 100);
void setup()
{
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
// Configure SSL client
ssl_client.setInsecure();
//ssl_client.setConnectionTimeout(1000);
ssl_client.setHandshakeTimeout(5);
// Initialize Firebase
initializeApp(aClient, app, getAuth(user_auth), processData, "🔐 authTask");
app.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
delay(5000);
// FuzzyInput suhu
FuzzyInput *suhu = new FuzzyInput(1);
suhu->addFuzzySet(light);
suhu->addFuzzySet(medium);
suhu->addFuzzySet(dark);
fuzzy->addFuzzyInput(suhu);
// FuzzyInput berat
FuzzyInput *berat = new FuzzyInput(2);
berat->addFuzzySet(sedikit);
berat->addFuzzySet(banyak);
fuzzy->addFuzzyInput(berat);
// FuzzyInput durasi
FuzzyInput *durasi = new FuzzyInput(3);
durasi->addFuzzySet(singkat);
durasi->addFuzzySet(normal);
durasi->addFuzzySet(lama);
fuzzy->addFuzzyInput(durasi);
// FuzzyOutput
FuzzyOutput *pwmout = new FuzzyOutput(1);
pwmout->addFuzzySet(lambat);
pwmout->addFuzzySet(sedang);
pwmout->addFuzzySet(cepat);
fuzzy->addFuzzyOutput(pwmout);
// Building FuzzyRule 1
FuzzyRuleAntecedent *suhuLightAndDurasiSingkat = new FuzzyRuleAntecedent();
suhuLightAndDurasiSingkat->joinWithAND(light, singkat);
FuzzyRuleAntecedent *ifSuhuLightAndDurasiSingkatAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuLightAndDurasiSingkatAndBeratSedikit->joinWithAND(suhuLightAndDurasiSingkat, sedikit);
FuzzyRuleConsequent *thenPwmoutLambat= new FuzzyRuleConsequent();
thenPwmoutLambat->addOutput(lambat);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifSuhuLightAndDurasiSingkatAndBeratSedikit, thenPwmoutLambat);
fuzzy->addFuzzyRule(fuzzyRule1);
// Building FuzzyRule 2
//FuzzyRuleAntecedent *suhuLightAndDurasiSingkat = new FuzzyRuleAntecedent();
suhuLightAndDurasiSingkat->joinWithAND(light, singkat);
FuzzyRuleAntecedent *ifSuhuLightAndDurasiSingkatAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuLightAndDurasiSingkatAndBeratBanyak->joinWithAND(suhuLightAndDurasiSingkat, banyak);
FuzzyRuleConsequent *thenPwmoutSedang= new FuzzyRuleConsequent();
thenPwmoutSedang->addOutput(sedang);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifSuhuLightAndDurasiSingkatAndBeratBanyak, thenPwmoutSedang);
fuzzy->addFuzzyRule(fuzzyRule2);
// Building FuzzyRule 3
FuzzyRuleAntecedent *suhuLightAndDurasiNormal = new FuzzyRuleAntecedent();
suhuLightAndDurasiNormal->joinWithAND(light, normal);
FuzzyRuleAntecedent *ifSuhuLightAndDurasiNormalAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuLightAndDurasiNormalAndBeratSedikit->joinWithAND(suhuLightAndDurasiNormal, sedikit);
//FuzzyRuleConsequent *thenPwmoutLambat= new FuzzyRuleConsequent();
thenPwmoutLambat->addOutput(lambat);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifSuhuLightAndDurasiNormalAndBeratSedikit, thenPwmoutLambat);
fuzzy->addFuzzyRule(fuzzyRule3);
// Building FuzzyRule 4
//FuzzyRuleAntecedent *suhuLightAndDurasiNormal = new FuzzyRuleAntecedent();
suhuLightAndDurasiNormal->joinWithAND(light, normal);
FuzzyRuleAntecedent *ifSuhuLightAndDurasiNormalAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuLightAndDurasiNormalAndBeratBanyak->joinWithAND(suhuLightAndDurasiNormal, banyak);
//FuzzyRuleConsequent *thenPwmoutSedang= new FuzzyRuleConsequent();
thenPwmoutSedang->addOutput(sedang);
FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifSuhuLightAndDurasiNormalAndBeratBanyak, thenPwmoutSedang);
fuzzy->addFuzzyRule(fuzzyRule4);
// Building FuzzyRule 5
FuzzyRuleAntecedent *suhuLightAndDurasiLama = new FuzzyRuleAntecedent();
suhuLightAndDurasiLama->joinWithAND(light, lama);
FuzzyRuleAntecedent *ifSuhuLightAndDurasiLamaAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuLightAndDurasiLamaAndBeratSedikit->joinWithAND(suhuLightAndDurasiLama, sedikit);
//FuzzyRuleConsequent *thenPwmoutSedang= new FuzzyRuleConsequent();
thenPwmoutSedang->addOutput(sedang);
FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifSuhuLightAndDurasiLamaAndBeratSedikit, thenPwmoutSedang);
fuzzy->addFuzzyRule(fuzzyRule5);
// Building FuzzyRule 6
//FuzzyRuleAntecedent *suhuLightAndDurasiLama = new FuzzyRuleAntecedent();
suhuLightAndDurasiLama->joinWithAND(light, lama);
FuzzyRuleAntecedent *ifSuhuLightAndDurasiLamaAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuLightAndDurasiLamaAndBeratBanyak->joinWithAND(suhuLightAndDurasiLama, banyak);
FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifSuhuLightAndDurasiLamaAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule6);
// Building FuzzyRule 7
FuzzyRuleAntecedent *suhuMediumAndDurasiSingkat = new FuzzyRuleAntecedent();
suhuMediumAndDurasiSingkat->joinWithAND(medium, singkat);
FuzzyRuleAntecedent *ifSuhuMediumAndDurasiSingkatAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuMediumAndDurasiSingkatAndBeratSedikit->joinWithAND(suhuMediumAndDurasiSingkat, sedikit);
//FuzzyRuleConsequent *thenPwmoutSedang= new FuzzyRuleConsequent();
thenPwmoutSedang->addOutput(sedang);
FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifSuhuMediumAndDurasiSingkatAndBeratSedikit, thenPwmoutSedang);
fuzzy->addFuzzyRule(fuzzyRule7);
// Building FuzzyRule 8
//FuzzyRuleAntecedent *suhuMediumAndDurasiSingkat = new FuzzyRuleAntecedent();
suhuMediumAndDurasiSingkat->joinWithAND(medium, singkat);
FuzzyRuleAntecedent *ifSuhuMediumAndDurasiSingkatAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuMediumAndDurasiSingkatAndBeratBanyak->joinWithAND(suhuMediumAndDurasiSingkat, banyak);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifSuhuMediumAndDurasiSingkatAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule8);
// Building FuzzyRule 9
FuzzyRuleAntecedent *suhuMediumAndDurasiNormal = new FuzzyRuleAntecedent();
suhuMediumAndDurasiNormal->joinWithAND(medium, normal);
FuzzyRuleAntecedent *ifSuhuMediumAndDurasiNormalAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuMediumAndDurasiNormalAndBeratSedikit->joinWithAND(suhuMediumAndDurasiNormal, sedikit);
//FuzzyRuleConsequent *thenPwmoutSedang= new FuzzyRuleConsequent();
thenPwmoutSedang->addOutput(sedang);
FuzzyRule *fuzzyRule9 = new FuzzyRule(9, ifSuhuMediumAndDurasiNormalAndBeratSedikit, thenPwmoutSedang);
fuzzy->addFuzzyRule(fuzzyRule9);
// Building FuzzyRule 10
//FuzzyRuleAntecedent *suhuMediumAndDurasiNormal = new FuzzyRuleAntecedent();
suhuMediumAndDurasiNormal->joinWithAND(medium, normal);
FuzzyRuleAntecedent *ifSuhuMediumAndDurasiNormalAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuMediumAndDurasiNormalAndBeratBanyak->joinWithAND(suhuMediumAndDurasiNormal, banyak);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule10 = new FuzzyRule(10, ifSuhuMediumAndDurasiNormalAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule10);
// Building FuzzyRule 11
FuzzyRuleAntecedent *suhuMediumAndDurasiLama = new FuzzyRuleAntecedent();
suhuMediumAndDurasiLama->joinWithAND(medium, lama);
FuzzyRuleAntecedent *ifSuhuMediumAndDurasiLamaAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuMediumAndDurasiLamaAndBeratSedikit->joinWithAND(suhuMediumAndDurasiLama, sedikit);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule11 = new FuzzyRule(11, ifSuhuMediumAndDurasiLamaAndBeratSedikit, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule11);
// Building FuzzyRule 12
//FuzzyRuleAntecedent *suhuMediumAndDurasiLama = new FuzzyRuleAntecedent();
suhuMediumAndDurasiLama->joinWithAND(medium, lama);
FuzzyRuleAntecedent *ifSuhuMediumAndDurasiLamaAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuMediumAndDurasiLamaAndBeratBanyak->joinWithAND(suhuMediumAndDurasiLama, banyak);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule12 = new FuzzyRule(12, ifSuhuMediumAndDurasiLamaAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule12);
// Building FuzzyRule 13
FuzzyRuleAntecedent *suhuDarkAndDurasiSingkat = new FuzzyRuleAntecedent();
suhuDarkAndDurasiSingkat->joinWithAND(dark, singkat);
FuzzyRuleAntecedent *ifSuhuDarkAndDurasiSingkatAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuDarkAndDurasiSingkatAndBeratSedikit->joinWithAND(suhuDarkAndDurasiSingkat, sedikit);
//FuzzyRuleConsequent *thenPwmoutSedang= new FuzzyRuleConsequent();
thenPwmoutSedang->addOutput(sedang);
FuzzyRule *fuzzyRule13 = new FuzzyRule(13, ifSuhuDarkAndDurasiSingkatAndBeratSedikit, thenPwmoutSedang);
fuzzy->addFuzzyRule(fuzzyRule13);
// Building FuzzyRule 14
//FuzzyRuleAntecedent *suhuDarkAndDurasiSingkat = new FuzzyRuleAntecedent();
suhuDarkAndDurasiSingkat->joinWithAND(dark, singkat);
FuzzyRuleAntecedent *ifSuhuDarkAndDurasiSingkatAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuDarkAndDurasiSingkatAndBeratBanyak->joinWithAND(suhuDarkAndDurasiSingkat, banyak);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule14 = new FuzzyRule(14, ifSuhuDarkAndDurasiSingkatAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule14);
// Building FuzzyRule 15
FuzzyRuleAntecedent *suhuDarkAndDurasiNormal = new FuzzyRuleAntecedent();
suhuDarkAndDurasiNormal->joinWithAND(dark, normal);
FuzzyRuleAntecedent *ifSuhuDarkAndDurasiNormalAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuDarkAndDurasiNormalAndBeratSedikit->joinWithAND(suhuDarkAndDurasiNormal, sedikit);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule15 = new FuzzyRule(15, ifSuhuDarkAndDurasiNormalAndBeratSedikit, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule15);
// Building FuzzyRule 16
//FuzzyRuleAntecedent *suhuDarkAndDurasiNormal = new FuzzyRuleAntecedent();
suhuDarkAndDurasiNormal->joinWithAND(dark, normal);
FuzzyRuleAntecedent *ifSuhuDarkAndDurasiNormalAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuDarkAndDurasiNormalAndBeratBanyak->joinWithAND(suhuDarkAndDurasiNormal, banyak);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule16 = new FuzzyRule(16, ifSuhuDarkAndDurasiNormalAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule16);
// Building FuzzyRule 17
FuzzyRuleAntecedent *suhuDarkAndDurasiLama = new FuzzyRuleAntecedent();
suhuDarkAndDurasiLama->joinWithAND(dark, lama);
FuzzyRuleAntecedent *ifSuhuDarkAndDurasiLamaAndBeratSedikit = new FuzzyRuleAntecedent();
ifSuhuDarkAndDurasiLamaAndBeratSedikit->joinWithAND(suhuDarkAndDurasiLama, sedikit);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule17 = new FuzzyRule(17, ifSuhuDarkAndDurasiLamaAndBeratSedikit, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule17);
// Building FuzzyRule 18
//FuzzyRuleAntecedent *suhuDarkAndDurasiLama = new FuzzyRuleAntecedent();
suhuDarkAndDurasiLama->joinWithAND(dark, lama);
FuzzyRuleAntecedent *ifSuhuDarkAndDurasiLamaAndBeratBanyak = new FuzzyRuleAntecedent();
ifSuhuDarkAndDurasiLamaAndBeratBanyak->joinWithAND(suhuDarkAndDurasiLama, banyak);
//FuzzyRuleConsequent *thenPwmoutCepat= new FuzzyRuleConsequent();
thenPwmoutCepat->addOutput(cepat);
FuzzyRule *fuzzyRule18 = new FuzzyRule(18, ifSuhuDarkAndDurasiLamaAndBeratBanyak, thenPwmoutCepat);
fuzzy->addFuzzyRule(fuzzyRule18);
}
void loop()
{
suhunya = thermocouple.readCelsius();
beratnya = scale.get_units(10) * 1;
Serial.print("Suhu: ");
Serial.print(suhunya);
Serial.print("|| berat: ");
Serial.print(beratnya);
Serial.print("|| waktu: ");
Serial.println(waktunya);
fuzzy->setInput(1, suhunya);
fuzzy->setInput(2, beratnya);
fuzzy->setInput(3, waktunya);
fuzzy->fuzzify();
Serial.print("Suhu : Light-> ");
Serial.print(light->getPertinence());
Serial.print(", Medium-> ");
Serial.print(medium->getPertinence());
Serial.print(", Dark-> ");
Serial.println(dark->getPertinence());
Serial.print("waktu : singkat-> ");
Serial.print(singkat->getPertinence());
Serial.print(", Normal-> ");
Serial.print(normal->getPertinence());
Serial.print(", Lama-> ");
Serial.println(lama->getPertinence());
Serial.print("Berat: Sedikit-> ");
Serial.print(sedikit->getPertinence());
Serial.print(", Banyak-> ");
Serial.println(banyak->getPertinence());
output1 = fuzzy->defuzzify(1);
Serial.println("Output: ");
Serial.print("PWMOUT: lambat-> ");
Serial.print(lambat->getPertinence());
Serial.print(", sedang-> ");
Serial.print(sedang->getPertinence());
Serial.print(", cepat-> ");
Serial.println(cepat->getPertinence());
Serial.print("HASIL: ");
Serial.print("PWM: ");
Serial.println(output1);
Serial.println(" ");
Serial.println(" ");
kirimdata();
delay(1000);
}
void kirimdata(){
app.loop();
if (app.ready()){
// Periodic data sending every 10 seconds
unsigned long currentTime = millis();
if (currentTime - lastSendTime >= sendInterval){
// Update the last send time
lastSendTime = currentTime;
// send an int
Database.set<int>(aClient, "/test/suhu", suhunya, processData, "RTDB_Send_Int");
Database.set<int>(aClient, "/test/berat", beratnya, processData, "RTDB_Send_Int");
Database.set<int>(aClient, "/test/output", output1, processData, "RTDB_Send_Int");
waktunya = Database.get<int>(aClient, "/test/waktu");
}
}
}
void processData(AsyncResult &aResult) {
if (!aResult.isResult())
return;
if (aResult.isEvent())
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());
if (aResult.isDebug())
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
if (aResult.isError())
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
if (aResult.available())
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
4. VIDEO HASILNYA