diff --git a/get_data.php b/get_data.php new file mode 100644 index 0000000..a1caf64 --- /dev/null +++ b/get_data.php @@ -0,0 +1,11 @@ + +#include "HX711.h" +#include +#include +#include +#include +#include +#include +#include + +const char* ssid = "Asus_Rog_E"; // Ganti dengan SSID WiFi Anda +const char* password = "cekduluaja"; // Ganti dengan password WiFi Anda + +const int TRIGPIN = D5; +const int ECHOPIN = D3; +const int oneWireBus = D6; //DS18B20 +const int irSensorPin = D1; //Sensor IR +const int LOADCELL_DOUT_PIN = D7; +const int LOADCELL_SCK_PIN = D8; +const int relay = D2; + +const float beratKalengkosong = 56.9; +const float beratKalengpenuh = 471.0; +const float tinggiTabung = 36; +const float persentaseTinggiTabung = 0.3; // 30% +const float suhumin = 20; +const float suhumax = 75; + +long timer; +int jarak; +int suhu; +int statusRelay; +HX711 scale; + +OneWire oneWire(oneWireBus); +DallasTemperature sensors(&oneWire); + +void setupLoadcell() { + Serial.println("Before setting up the scale:"); + Serial.print("read: \t\t"); + Serial.println(scale.read()); + + Serial.print("read average: \t\t"); + Serial.println(scale.read_average(20)); + + Serial.print("get value: \t\t"); + Serial.println(scale.get_value(5)); + + Serial.print("get units: \t\t"); + Serial.println(scale.get_units(5), 1); + + scale.set_scale(209.984); + scale.tare(); + + Serial.println("After setting up the scale:"); + + Serial.print("read: \t\t"); + Serial.println(scale.read()); + + Serial.print("read average: \t\t"); + Serial.println(scale.read_average(20)); + + Serial.print("get value: \t\t"); + Serial.println(scale.get_value(5)); + + Serial.print("get units: \t\t"); + Serial.println(scale.get_units(5), 1); + + Serial.println("Successful Initialization of HX711"); +} + +void kirim_data(){ + + String postData = (String)"ultrasonic=" + jarak + "&infrared=" + readIrsensor() + + "&valve=" + statusRelay + "&loadcell=" + suhu + "&suhu=" + suhu; + + HTTPClient http; + http.begin(WiFiClient,"http://192.168.137.10/TestApi/update.php"); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + int httpCode = http.POST(postData); + String payload = http.getString(); + + Serial.println(postData); + Serial.println(payload); + + http.end(); +} + +void setup() { + Serial.begin(115200); + digitalWrite(relay, HIGH); + + WiFi.mode(WIFI_OFF); + delay(1000); + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, password); + Serial.println(""); + + Serial.print("Connecting"); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.print("Connected to "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); //IP address assigned to your ESP + + Serial.println("STARTING"); + delay(1500); + Serial.println("PASTIKAN TIDAK ADA BENDA PADA LOADCELL"); + delay(3000); + Serial.println("Starting Initialization of HX711"); + delay(1000); + scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); + pinMode(ECHOPIN, INPUT); + pinMode(TRIGPIN, OUTPUT); + pinMode(relay, OUTPUT); + + setupLoadcell(); + sensors.begin(); +} + +void readUltrasonic() { + digitalWrite(TRIGPIN, LOW); + delayMicroseconds(2); + digitalWrite(TRIGPIN, HIGH); + delayMicroseconds(10); + digitalWrite(TRIGPIN, LOW); + + timer = pulseIn(ECHOPIN, HIGH); + jarak = timer/58; + + Serial.print("Jarak = "); + Serial.print(jarak); + Serial.print(" cm"); +} + +void readSuhu() { + sensors.requestTemperatures(); + suhu = sensors.getTempCByIndex(0); + + Serial.print(" Suhu: "); + Serial.print(suhu); + Serial.print("°C "); +} + +void readLoadcell() { + Serial.print("BERAT: "); + Serial.print(scale.get_units(), 1); +} + +bool readIrsensor() { + int irSensorValue = digitalRead(irSensorPin); + return (irSensorValue != HIGH); +} + +void loop() { + readUltrasonic(); + readSuhu(); + readLoadcell(); + + if (jarak <= tinggiTabung * persentaseTinggiTabung) { + Serial.println(" Silakan isi sauce."); + digitalWrite(relay, HIGH); + statusRelay = 1; + } else { + if (suhu >= suhumin && suhu <= suhumax) { + if (!readIrsensor() && scale.get_units() < beratKalengkosong) { + Serial.println(" Silakan letakkan kaleng."); + digitalWrite(relay, HIGH); + statusRelay = 1; + } + else { + if (scale.get_units() >= beratKalengkosong && scale.get_units() <= beratKalengpenuh) { + Serial.println(" Mengisi kaleng."); + digitalWrite(relay, LOW); + statusRelay = 0; + } + else { + Serial.println(" Tidak Mengisi Kaleng"); + digitalWrite(relay, HIGH); + statusRelay = 1; + } + } + } else { + Serial.println(" Sauce Panas"); + digitalWrite(relay, HIGH); + statusRelay = 1; + } + + kirim_data(); + } + + delay(1000); +} diff --git a/lifimfirebase.ino b/lifimfirebase.ino new file mode 100644 index 0000000..d84ddf3 --- /dev/null +++ b/lifimfirebase.ino @@ -0,0 +1,213 @@ +//MEMAKAI RELAY ACTIVE LOW, JADI KALAU HIGH RELAY MATI +#include +#include "HX711.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Provide the token generation process info. +#include "addons/TokenHelper.h" +// Provide the RTDB payload printing info and other helper functions. +#include "addons/RTDBHelper.h" + +const char* ssid = "Asus_Rog_E"; // Ganti dengan SSID WiFi Anda +const char* password = "cekduluaja"; // Ganti dengan password WiFi Anda +#define API_KEY "AIzaSyDq2Xkt6qNszEzq_pCzEkO7QfjvGRALlQU" +#define USER_EMAIL "fikriahdiars@gmail.com" +#define USER_PASSWORD "Lifim!234" +#define DATABASE_URL "https://lifim-3e7e2-default-rtdb.asia-southeast1.firebasedatabase.app/" + +// Define Firebase objects +FirebaseData fbdo; +FirebaseAuth auth; +FirebaseConfig config; + +// Variable to save USER UID +String uid; + +// Database main path (to be updated in setup with the user UID) +String databasePath; +// Database child nodes +String UltrasonicPath = "/ultrasonic"; +String irPath = "/infrared"; +String valvePath = "/valve"; +String suhuPath = "/suhu"; + +// Parent Node (to be updated in every loop) +String parentPath; +FirebaseJson json; + +// Define NTP Client to get time +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "id.pool.ntp.org"); + +// Variable to save current epoch time +int timestamp; + +const int TRIGPIN = D8; +const int ECHOPIN = D7; +const int oneWireBus = D6; //DS18B20 +const int irSensorPin = D1; //Sensor IR +const int relay = D2; + +const float tinggiTabung = 35; +const float persentaseTinggiTabung = 0.3; // 30% +const float suhumin = 20; +const float suhumax = 75; + +long timer; +int jarak; +int suhu; +int statusRelay; + +OneWire oneWire(oneWireBus); +DallasTemperature sensors(&oneWire); + +// Timer variables (send new readings every three minutes) +unsigned long sendDataPrevMillis = 0; +unsigned long timerDelay = 3000; + +unsigned long getTime() { + timeClient.update(); + unsigned long now = timeClient.getEpochTime(); + return now; +} + +void setup() { + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + int i = 0; + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(1000); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + Serial.println(); + + // Assign the api key (required) + config.api_key = API_KEY; + + // Assign the user sign in credentials + auth.user.email = USER_EMAIL; + auth.user.password = USER_PASSWORD; + + // Assign the RTDB URL (required) + config.database_url = DATABASE_URL; + + Firebase.reconnectWiFi(true); + fbdo.setResponseSize(4096); + + // Assign the callback function for the long running token generation task */ + config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h + + // Assign the maximum retry of token generation + config.max_token_generation_retry = 5; + + // Initialize the library with the Firebase authen and config + Firebase.begin(&config, &auth); + + // Getting the user UID might take a few seconds + Serial.println("Getting User UID"); + while ((auth.token.uid) == "") { + Serial.print('.'); + delay(1000); + } + // Print user UID + uid = auth.token.uid.c_str(); + Serial.print("User UID: "); + Serial.println(uid); + + // Update database path + databasePath = "/UsersData/" + uid + "/readings"; + + Serial.println("STARTING"); + delay(1500); + pinMode(ECHOPIN, INPUT); + pinMode(TRIGPIN, OUTPUT); + pinMode(relay, OUTPUT); + digitalWrite(relay, HIGH); + + sensors.begin(); +} + +void readUltrasonic() { + digitalWrite(TRIGPIN, LOW); + delayMicroseconds(2); + digitalWrite(TRIGPIN, HIGH); + delayMicroseconds(10); + digitalWrite(TRIGPIN, LOW); + + timer = pulseIn(ECHOPIN, HIGH); + jarak = timer/58; + // jarak = tinggiTabung - jarak; + + Serial.print("Jarak = "); + Serial.print(jarak); + Serial.print(" cm"); +} + +void readSuhu() { + sensors.requestTemperatures(); + suhu = sensors.getTempCByIndex(0); + + Serial.print(" Suhu: "); + Serial.print(suhu); + Serial.print("°C "); +} + +bool readIrsensor() { + int irSensorValue = digitalRead(irSensorPin); + return (irSensorValue != HIGH); +} + +void loop() { + if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0)){ + sendDataPrevMillis = millis(); + readUltrasonic(); + readSuhu(); + + if (jarak <= tinggiTabung * persentaseTinggiTabung) { //jika jarak kurang dari 30% refill sauce + Serial.println(" Silakan refill sauce."); + digitalWrite(relay, HIGH); + statusRelay = 0; + } else { + if (suhu >= suhumin && suhu <= suhumax) { + if (readIrsensor()) { //Jika kaleng terdeteksi mengisi kaleng + Serial.println(" Mengisi kaleng."); + digitalWrite(relay, LOW); + delay(5000); // DELAY ISI KALENG SAMPAI PENUH + digitalWrite(relay, HIGH); + delay (3000); //DELAY TUNGGU KALENG PINDAH / GESER + digitalWrite(relay, HIGH); + statusRelay = 1; + } else { + Serial.println(" Tidak Mengisi Kaleng"); //Jika kaleng tidak terdeteksi + digitalWrite(relay, HIGH); + statusRelay = 0; + } + } else { + Serial.println(" Sauce Panas"); //Jika suhu melebihi suhumax + digitalWrite(relay, HIGH); + statusRelay = 0; + } + parentPath= databasePath + "/" + String(timestamp); + + json.set(UltrasonicPath.c_str(), String(jarak)); + json.set(irPath.c_str(), String(readIrsensor())); + json.set(valvePath.c_str(), String(statusRelay)); + json.set(suhuPath.c_str(), String(suhu)); + Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str()); + } + } +}