214 lines
6.0 KiB
C++
214 lines
6.0 KiB
C++
//MEMAKAI RELAY ACTIVE LOW, JADI KALAU HIGH RELAY MATI
|
|
#include <Arduino.h>
|
|
#include "HX711.h"
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ArduinoJson.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <Firebase_ESP_Client.h>
|
|
#include <WiFiUdp.h>
|
|
#include <NTPClient.h>
|
|
|
|
// 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());
|
|
}
|
|
}
|
|
}
|