-
-
Save abdulrcs/01d3f060ad3a72929d1f5a413d81476f to your computer and use it in GitHub Desktop.
NodeMCU Home Alarm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClient.h> | |
#include <WiFiClientSecure.h> | |
#define TRIGGERPIN D1 // Triggerpin kita pasang di pin D1 NodeMCU | |
#define ECHOPIN D2 // Echopin kita pasang di pin D2 NodeMCU | |
long duration; | |
int distance; | |
const char* ssid = "NAMA WIFI"; | |
const char* password = "PASSWORD WIFI"; | |
void setup() { | |
pinMode(TRIGGERPIN, OUTPUT); // Mensetting TRIG pin pada sensor menjadi output | |
pinMode(ECHOPIN, INPUT); // Mensetting ECHO pin pada sensor menjadi input | |
Serial.begin(9600); | |
WiFi.begin(ssid, password); | |
Serial.println("Connecting"); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Telah terkoneksi ke WiFi dengan IP Address: "); | |
Serial.println(WiFi.localIP()); | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, LOW); // Menyalakan LED selama 1 detik jika sudah terkoneksi WiFi | |
delay(1000); | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
void loop() { | |
digitalWrite(TRIGGERPIN, LOW); // Clear Triggerpin | |
delayMicroseconds(2); | |
digitalWrite(TRIGGERPIN, HIGH); // Menyalakan triggerpin | |
delayMicroseconds(20); | |
digitalWrite(TRIGGERPIN, LOW); | |
duration = pulseIn(ECHOPIN, HIGH); // Membaca echo pin, dan akan me-return waktu perjalanan gelombang suara | |
distance= duration*0.034/2; // Rumus untuk mendapatkan distance dari waktu perjalanan gelombang suara dalam CM | |
if(distance < 50) { // Jika sensor mendeteksi < 50cm | |
digitalWrite(LED_BUILTIN, LOW); // Menyalakan LED | |
Serial.println("Sensor mendeteksi seseorang!"); | |
HTTPClient http; | |
WiFiClientSecure client; | |
client.setInsecure(); | |
http.begin(client, "https://maker.ifttt.com/trigger/sensor_detected/with/key/APIKEY"); | |
int httpResponseCode = http.GET(); | |
String payload = http.getString(); | |
Serial.println(payload); | |
Serial.println(httpResponseCode); | |
digitalWrite(LED_BUILTIN, HIGH); // Mematikan LED (Untuk indikasi sudah mengirim request ke API) | |
delay(10000); // Delay untuk mencegah pemanggilan API berterusan | |
} | |
delay(500); // Delay setiap pembacaan distance sensor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment