Upload files to "/"
This commit is contained in:
parent
c6d0c12617
commit
f2511401a5
|
@ -0,0 +1,160 @@
|
|||
#include <WiFi.h>
|
||||
#include <Firebase_ESP_Client.h>
|
||||
#include <SPI.h>
|
||||
#include <LoRa.h>
|
||||
|
||||
#define WIFI_SSID "WIFI_SSID"
|
||||
#define WIFI_PASSWORD "WIFI_PASSWORD"
|
||||
#define API_KEY "API_KEY"
|
||||
#define DATABASE_URL "DATABASE_URL"
|
||||
#define USER_EMAIL "USER_EMAIL"
|
||||
#define USER_PASSWORD "USER_PASSWORD"
|
||||
|
||||
#define LORA_SS 5
|
||||
#define LORA_RST 14
|
||||
#define LORA_DIO0 13
|
||||
#define BUZZER_PIN 27
|
||||
|
||||
FirebaseData fbdo;
|
||||
FirebaseAuth auth;
|
||||
FirebaseConfig config;
|
||||
|
||||
String lastStatusKebakaran = "";
|
||||
|
||||
void setupLoRa() {
|
||||
delay(2000);
|
||||
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
|
||||
pinMode(LORA_DIO0, INPUT);
|
||||
if (!LoRa.begin(433E6)) {
|
||||
Serial.println("Gagal memulai LoRa");
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
Serial.println("LoRa Siap");
|
||||
}
|
||||
|
||||
void beepPendekDuaKali() {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
digitalWrite(BUZZER_PIN, HIGH);
|
||||
delay(1000);
|
||||
digitalWrite(BUZZER_PIN, LOW);
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void beepPanjang() {
|
||||
digitalWrite(BUZZER_PIN, HIGH);
|
||||
delay(5000);
|
||||
digitalWrite(BUZZER_PIN, LOW);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void periksaStatusKebakaran(String status) {
|
||||
if (status != lastStatusKebakaran) {
|
||||
if (status == "iya") {
|
||||
beepPanjang();
|
||||
}
|
||||
|
||||
lastStatusKebakaran = status;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
delay(3000);
|
||||
Serial.begin(115200);
|
||||
Serial.println("Memulai setup...");
|
||||
|
||||
pinMode(BUZZER_PIN, OUTPUT);
|
||||
digitalWrite(BUZZER_PIN, LOW);
|
||||
|
||||
Serial.println("Menghubungkan Wifi");
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("WiFi tersambung");
|
||||
|
||||
config.api_key = API_KEY;
|
||||
config.database_url = DATABASE_URL;
|
||||
auth.user.email = USER_EMAIL;
|
||||
auth.user.password = USER_PASSWORD;
|
||||
|
||||
Firebase.begin(&config, &auth);
|
||||
Firebase.reconnectWiFi(true);
|
||||
while (auth.token.uid == "") {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
setupLoRa();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int packetSize = LoRa.parsePacket();
|
||||
delay(100);
|
||||
if (packetSize) {
|
||||
String data = "";
|
||||
while (LoRa.available()) {
|
||||
data += (char)LoRa.read();
|
||||
}
|
||||
|
||||
Serial.println("Data LoRa: " + data);
|
||||
|
||||
if (data.startsWith("api:") && data.indexOf(",asap:") == -1) {
|
||||
String apiStatus = data.substring(4);
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/api", apiStatus);
|
||||
}
|
||||
|
||||
else if (data.startsWith("kebakaran:")) {
|
||||
String status = data.substring(10);
|
||||
Firebase.RTDB.setString(&fbdo, "/notif/kebakaran", status);
|
||||
periksaStatusKebakaran(status);
|
||||
Serial.println("Status kebakaran disimpan:" + status);
|
||||
}
|
||||
|
||||
else if (data.startsWith("asap:")) {
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/asap", data.substring(5));
|
||||
}
|
||||
|
||||
else if (data.startsWith("suhu:")) {
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/suhu", data.substring(5));
|
||||
}
|
||||
|
||||
else if (data.startsWith("tanah:")) {
|
||||
String tanahStr = data.substring(6);
|
||||
tanahStr.replace("%", "");
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/tanah", tanahStr);
|
||||
}
|
||||
|
||||
else if (data.startsWith("relay:")) {
|
||||
String relayStatus = data.substring(6);
|
||||
relayStatus.toLowerCase();
|
||||
Firebase.RTDB.setString(&fbdo, "/pompa/status", relayStatus);
|
||||
}
|
||||
|
||||
else if (data.startsWith("api:") && data.indexOf(",asap:") != -1) {
|
||||
int idxApi = data.indexOf("api:");
|
||||
int idxAsap = data.indexOf(",asap:");
|
||||
int idxSuhu = data.indexOf(",suhu:");
|
||||
int idxTanah = data.indexOf(",tanah:");
|
||||
int idxRelay = data.indexOf(",relay:");
|
||||
|
||||
if (idxApi >= 0 && idxAsap >= 0 && idxSuhu >= 0 && idxTanah >= 0 && idxRelay >= 0) {
|
||||
String api = data.substring(idxApi + 4, idxAsap);
|
||||
String asap = data.substring(idxAsap + 6, idxSuhu);
|
||||
String suhu = data.substring(idxSuhu + 6, idxTanah);
|
||||
String tanah = data.substring(idxTanah + 7, idxRelay);
|
||||
String relay = data.substring(idxRelay + 7);
|
||||
|
||||
tanah.replace("%", "");
|
||||
relay.toLowerCase();
|
||||
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/api", api);
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/asap", asap);
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/suhu", suhu);
|
||||
Firebase.RTDB.setString(&fbdo, "/sensor/tanah", tanah);
|
||||
Firebase.RTDB.setString(&fbdo, "/pompa/status", relay);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue