#include <WiFi.h>
#include "PubSubClient.h"
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqttServer = "broker.emqx.io";
int port = 1883;
String stMac;
char mac[50];
char clientId[50];
WiFiClient espClient;
PubSubClient client(espClient);
Servo servo;
Servo servo2;
Servo servo3;
int pos = 0;
const int ledPin = 2;
const int servoPin = 13;
const int servoPin2 = 12;
const int servoPin3 = 14;
#define ECHO_PIN 27
#define TRIG_PIN 26
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0));
servo.attach(servoPin, 500, 2400);
servo2.attach(servoPin2, 500, 2400);
servo3.attach(servoPin3, 500, 2400);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
wifiConnect();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
stMac = WiFi.macAddress();
stMac.replace(":", "_");
Serial.println(stMac);
client.setServer(mqttServer, port);
client.setCallback(callback);
pinMode(ledPin, OUTPUT);
servo.write(0);
servo2.write(0);
servo3.write(0);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void wifiConnect() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void mqttReconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
long r = random(1000);
sprintf(clientId, "clientId-%ld", r);
if (client.connect(clientId)) {
Serial.print(clientId);
Serial.println(" connected");
client.subscribe("IKAN");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String stMessage;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
stMessage += (char)message[i];
}
Serial.println();
if (String(topic) == "IKAN") {
Serial.print("Changing output to ");
if(stMessage == "makan"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
for (pos = 0; pos <= 45; pos += 1) {
servo.write(pos);
delay(100);
}
for (pos = 45; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
}
else if(stMessage == "makan2"){
Serial.println("on2");
digitalWrite(ledPin, HIGH);
for (pos = 0; pos <= 45; pos += 1) {
servo2.write(pos);
delay(100);
}
for (pos = 45; pos >= 0; pos -= 1) {
servo2.write(pos);
delay(15);
}
}
else if(stMessage == "makan3"){
Serial.println("on3");
digitalWrite(ledPin, HIGH);
for (pos = 0; pos <= 45; pos += 1) {
servo3.write(pos);
delay(100);
}
for (pos = 45; pos >= 0; pos -= 1) {
servo3.write(pos);
delay(15);
}
}
else if(stMessage == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}
void loop() {
delay(10);
if (!client.connected()) {
mqttReconnect();
}
client.loop();
float distance = readDistanceCM();
bool isNearby = distance == 20.98;
if (isNearby){
client.publish("IKAN", " Pakan Ikan tinggal 50% ");
}
Serial.print("jarak: ");
Serial.println(readDistanceCM());
delay(100);
}