alat
This commit is contained in:
commit
38eb3c8a36
|
@ -0,0 +1,197 @@
|
|||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Arduino.h>
|
||||
#include <NewPing.h>
|
||||
|
||||
//Hall Effect
|
||||
int countHall = 0;
|
||||
int currentHall = 0;
|
||||
int persenGate = 0;
|
||||
int currentGate = 0;
|
||||
#define hallPin 34
|
||||
#define LED 2
|
||||
#define RELAY 26
|
||||
#define RELAY1 25
|
||||
#define TRIGGER_PIN 16
|
||||
#define ECHO_PIN 17
|
||||
#define CALIBRATION_FACTOR 0.0327
|
||||
|
||||
NewPing sonar(TRIGGER_PIN, ECHO_PIN);
|
||||
|
||||
// WiFi
|
||||
const char *ssid = "Redmi";
|
||||
const char *password = "poiuytre";
|
||||
|
||||
// MQTT Broker
|
||||
const char *mqtt_broker = "habibigarden.com";
|
||||
const char *mqtt_username = "";
|
||||
const char *mqtt_password = "";
|
||||
const int mqtt_port = 1883;
|
||||
String messagemqtt = "";
|
||||
String SNalat = "001Irigate";
|
||||
bool autoMode = false;
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
int batasAtas = 60;
|
||||
int batasBawah = 80;
|
||||
bool isGateOpen = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000); // Delay for stability
|
||||
|
||||
// Connecting to a WiFi network
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.println("Connecting to WiFi...");
|
||||
}
|
||||
Serial.println("Connected to the WiFi network");
|
||||
|
||||
pinMode(RELAY, OUTPUT);
|
||||
pinMode(RELAY1, OUTPUT);
|
||||
pinMode(hallPin, INPUT_PULLUP);
|
||||
pinMode(LED, OUTPUT);
|
||||
digitalWrite(LED, LOW);
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
|
||||
client.setServer(mqtt_broker, mqtt_port);
|
||||
client.setCallback(callback);
|
||||
while (!client.connected()) {
|
||||
String client_id = "esp8266-client-";
|
||||
client_id += String(WiFi.macAddress());
|
||||
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
|
||||
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
|
||||
Serial.println("Public EMQX MQTT broker connected");
|
||||
} else {
|
||||
Serial.print("Failed with state ");
|
||||
Serial.print(client.state());
|
||||
delay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
client.publish("Irigate/Status", "Smart Irigate Connect");
|
||||
client.subscribe("Irigate/relay");
|
||||
client.subscribe("Irigate/AutoMode");
|
||||
|
||||
attachInterrupt(digitalPinToInterrupt(34), Hall, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(34), Aplikasi, CHANGE);
|
||||
}
|
||||
|
||||
void callback(char *topic, byte *payload, unsigned int length) {
|
||||
String message;
|
||||
for (int i = 0; i < length; i++) {
|
||||
message += (char) payload[i];
|
||||
}
|
||||
|
||||
if (String(topic) == "Irigate/relay") {
|
||||
persenGate = message.toInt();
|
||||
} else if (String(topic) == "Irigate/AutoMode") {
|
||||
if (message == "ON") {
|
||||
autoMode = true;
|
||||
} else if (message == "OFF") {
|
||||
autoMode = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Aplikasi() {
|
||||
if (currentGate < persenGate) {
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, LOW);
|
||||
if(countHall == persenGate){
|
||||
currentGate = persenGate;
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
}
|
||||
} else if(currentGate > persenGate){
|
||||
digitalWrite(RELAY, LOW);
|
||||
digitalWrite(RELAY1, LOW);
|
||||
if(countHall == persenGate){
|
||||
currentGate = persenGate;
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
}
|
||||
} else if(currentGate == persenGate){
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
void Hall(){
|
||||
if(digitalRead(RELAY) == HIGH && digitalRead(RELAY1) == LOW && digitalRead(hallPin) == LOW) {
|
||||
countHall++;
|
||||
Serial.println(countHall);
|
||||
} else if (digitalRead(RELAY) == LOW && digitalRead(RELAY1) == LOW && digitalRead(hallPin) == LOW) {
|
||||
countHall--;
|
||||
Serial.println(countHall);
|
||||
}
|
||||
}
|
||||
|
||||
void autoModeLoop() {
|
||||
unsigned int duration = sonar.ping();
|
||||
float distance = (duration / 2.0) * CALIBRATION_FACTOR;
|
||||
|
||||
if (distance <= batasAtas && !isGateOpen) {
|
||||
Serial.println("Gerbang terbuka");
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, LOW);
|
||||
delay(5000);
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
isGateOpen = true;
|
||||
}
|
||||
if (distance >= batasBawah && isGateOpen) {
|
||||
Serial.println("Gerbang tertutup");
|
||||
digitalWrite(RELAY, LOW);
|
||||
digitalWrite(RELAY1, LOW);
|
||||
delay(5000);
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
isGateOpen = false;
|
||||
}
|
||||
if (distance == 0.0) {
|
||||
digitalWrite(RELAY, HIGH);
|
||||
digitalWrite(RELAY1, HIGH);
|
||||
Serial.println("Gagal");
|
||||
} else {
|
||||
Serial.print("Jarak: ");
|
||||
Serial.print(distance);
|
||||
Serial.println(" cm");
|
||||
}
|
||||
}
|
||||
|
||||
void manualModeLoop() {
|
||||
Hall();
|
||||
Aplikasi();
|
||||
unsigned int duration = sonar.ping();
|
||||
float distance = (duration / 2.0) * CALIBRATION_FACTOR;
|
||||
String distanceString = String(distance);
|
||||
|
||||
if (distance == 0.0) {
|
||||
Serial.println("Gagal");
|
||||
} else {
|
||||
Serial.print("Jarak: ");
|
||||
Serial.print(distance);
|
||||
Serial.println(" cm");
|
||||
|
||||
String json = "{\"info\":{\"distance\":\"" + distanceString + "\",\"Hallefect\":\"" + countHall +"\" ,\"Status\":\"" + messagemqtt + "\"}}";
|
||||
client.publish("Irigate/SNalat", SNalat.c_str());
|
||||
client.publish("Irigate/Ultrasonik", json.c_str());
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
client.loop();
|
||||
if (autoMode) {
|
||||
autoModeLoop();
|
||||
} else {
|
||||
manualModeLoop();
|
||||
}
|
||||
delay(100);
|
||||
}
|
Loading…
Reference in New Issue