From 823f988ab317cbc3da4505367b0831e96f0889df Mon Sep 17 00:00:00 2001 From: ArdianHilmiPramulintang Date: Tue, 21 May 2024 11:10:44 +0700 Subject: [PATCH] Upload files to "/" --- smart_home.ino | 178 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 smart_home.ino diff --git a/smart_home.ino b/smart_home.ino new file mode 100644 index 0000000..2fd9298 --- /dev/null +++ b/smart_home.ino @@ -0,0 +1,178 @@ +#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" + +// Insert your network credentials +#define WIFI_SSID "Comingsoon" +#define WIFI_PASSWORD "hilmi123" + +// Insert Firebase project API Key +#define API_KEY "AIzaSyCmquF32M-zZDpW_Hswg9ZvREgikmteJMY" + +// Insert RTDB URLefine the RTDB URL */ +#define DATABASE_URL "https://smart-home-e64ae-default-rtdb.firebaseio.com/" + +// Pin untuk mengontrol lampu +#define TENGAH 5 +#define DEPAN 16 +#define SAMPING 4 + +//Define Firebase Data object +FirebaseData fbdo; + +FirebaseAuth auth; +FirebaseConfig config; + +//some importent variables +String sValue, sValue2, sValue3; +bool signupOK = false; + +bool depanState = false; +bool sampingState = false; +bool tengahState = false; + +unsigned long lastResponseTimeHigh = 0; +unsigned long lastResponseTimeLow = 0; + +void setup() { + Serial.begin(115200); + + pinMode(DEPAN,OUTPUT); + pinMode(TENGAH,OUTPUT); + pinMode(SAMPING,OUTPUT); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + Serial.print("Connecting to Wi-Fi"); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(300); + } + Serial.println(); + Serial.print("Connected with IP: "); + Serial.println(WiFi.localIP()); + Serial.println(); + + /* Assign the api key (required) */ + config.api_key = API_KEY; + + /* Assign the RTDB URL (required) */ + config.database_url = DATABASE_URL; + + /* Sign up */ + if (Firebase.signUp(&config, &auth, "", "")) { + Serial.println("ok"); + signupOK = true; + } + else { + Serial.printf("%s\n", config.signer.signupError.message.c_str()); + } + + /* Assign the callback function for the long running token generation task */ + config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h + + Firebase.begin(&config, &auth); + Firebase.reconnectWiFi(true); +} + +void loop() { + if (Firebase.ready() && signupOK ) { + if (Firebase.RTDB.getString(&fbdo, "/DEPAN")) { + if (fbdo.dataType() == "string") { + sValue = fbdo.stringData(); + int a = sValue.toInt(); + Serial.println(a); + if (a == 1){ + digitalWrite(DEPAN,HIGH); + if (!depanState) { + unsigned long now = millis(); + Serial.print("DEPAN switched to HIGH, response time: "); + Serial.print(now - lastResponseTimeLow); + Serial.println(" ms"); + lastResponseTimeHigh = now; + } + depanState = true; + }else{ + digitalWrite(DEPAN,LOW); + if (depanState) { + unsigned long now = millis(); + Serial.print("DEPAN switched to LOW, response time: "); + Serial.print(now - lastResponseTimeHigh); + Serial.println(" ms"); + lastResponseTimeLow = now; + } + depanState = false; + } + } + } + else { + Serial.println(fbdo.errorReason()); + } + + if (Firebase.RTDB.getString(&fbdo, "/SAMPING")) { + if (fbdo.dataType() == "string") { + sValue2 = fbdo.stringData(); + int b = sValue2.toInt(); + Serial.println(b); + if (b == 1){ + digitalWrite(SAMPING,HIGH); + if (!sampingState) { + unsigned long now = millis(); + Serial.print("SAMPING switched to HIGH, response time: "); + Serial.print(now - lastResponseTimeLow); + Serial.println(" ms"); + lastResponseTimeHigh = now; + } + sampingState = true; + }else{ + digitalWrite(SAMPING,LOW); + if (sampingState) { + unsigned long now = millis(); + Serial.print("SAMPING switched to LOW, response time: "); + Serial.print(now - lastResponseTimeHigh); + Serial.println(" ms"); + lastResponseTimeLow = now; + } + sampingState = false; + } + } + } + else { + Serial.println(fbdo.errorReason()); + } + + if (Firebase.RTDB.getString(&fbdo, "/TENGAH")) { + if (fbdo.dataType() == "string") { + sValue3 = fbdo.stringData(); + int c = sValue3.toInt(); + Serial.println(c); + if (c == 1){ + digitalWrite(TENGAH,HIGH); + if (!tengahState) { + unsigned long now = millis(); + Serial.print("TENGAH switched to HIGH, response time: "); + Serial.print(now - lastResponseTimeLow); + Serial.println(" ms"); + lastResponseTimeHigh = now; + } + tengahState = true; + }else{ + digitalWrite(TENGAH,LOW); + if (tengahState) { + unsigned long now = millis(); + Serial.print("TENGAH switched to LOW, response time: "); + Serial.print(now - lastResponseTimeHigh); + Serial.println(" ms"); + lastResponseTimeLow = now; + } + tengahState = false; + } + } + } + else { + Serial.println(fbdo.errorReason()); + } + } +}