#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <ArduinoJson.h>
#define ssid "Wokwi-GUEST"
#define pass ""
#define server "mqtt-dashboard.com"
#define port 1883
WiFiClient espClient;
PubSubClient client(espClient);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
int sa = 0;
int sb = 0;
int counter = 0;
void callback(char* topic, byte* payload, unsigned int length) {
  JsonDocument doc;
  deserializeJson(doc, payload);
  bool lb0 = doc["lb0"];
  if (lb0) digitalWrite(4, HIGH);
  else digitalWrite(4, LOW);
  bool lb1 = doc["lb1"];
  if (lb1) digitalWrite(16, HIGH);
  else digitalWrite(16, LOW);
  bool lb2 = doc["lb2"];
  if (lb2) {
    digitalWrite(17, HIGH);
    digitalWrite(23, HIGH);
  }
  else {
    digitalWrite(17, LOW);
    digitalWrite(23, LOW);
  }
  bool lb3 = doc["lb3"];
  if (lb3) digitalWrite(5, HIGH);
  else digitalWrite(5, LOW);
  bool lb4 = doc["lb4"];
  if (lb4) digitalWrite(18, HIGH);
  else digitalWrite(18, LOW);
  int lw1 = doc["lw1"];
  servo.write(lw1 * 9);
  delay(10);
  lcd.setCursor(4, 0);
  if (lw1 < 10) lcd.print(" ");
  if (lw1 < 100) lcd.print(" ");
  lcd.print(lw1);
  int lw2 = doc["lw2"];
  lcd.setCursor(12, 0);
  lcd.print(lw2);
  int lw3 = doc["lw3"];
  lcd.setCursor(4, 1);
  if (lw3 < 10) lcd.print(" ");
  if (lw3 < 100) lcd.print(" ");
  lcd.print(lw3);
  int lw4 = doc["lw4"];
  lcd.setCursor(12, 1);
  if (lw4 < 10) lcd.print(" ");
  if (lw4 < 100) lcd.print(" ");
  lcd.print(lw4);
}
void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("MQTT Connected");
      // Once connected, publish an announcement...
      client.publish("wokwi/ebpro/input1", "{\"lb5\":false}");
      client.publish("wokwi/ebpro/input2", "{\"lb6\":false}");
      client.subscribe("ebpro/wokwi/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (counter > 10) {
      ESP.restart();
    }
    counter++;
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  servo.attach(19);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("an1:    an2:    ");
  lcd.setCursor(0, 1);
  lcd.print("an3:    an4:    ");
  pinMode(15, INPUT_PULLUP); //S1
  pinMode(2, INPUT_PULLUP); //S2
  pinMode(4, OUTPUT); //LED1
  pinMode(16, OUTPUT); //LED2
  pinMode(17, OUTPUT); //LED3
  pinMode(5, OUTPUT); //LED4
  pinMode(18, OUTPUT); //LED5
  pinMode(13, OUTPUT); //LED6
  pinMode(14, OUTPUT); //LED7
  pinMode(23, OUTPUT);//konveyor
  client.setServer(server, 1883);
  client.setCallback(callback);
  delay(1000);
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  int s1 = !digitalRead(15);
  int s2 = !digitalRead(2);
  if (s1 != sa) {
    Serial.println(s1);
    if (s1) {
      client.publish("wokwi/ebpro/input1", "{\"lb5\":true}");
      digitalWrite(13, HIGH);
    } else {
      client.publish("wokwi/ebpro/input1", "{\"lb5\":false}");
      digitalWrite(13, LOW);
    }
  } sa = s1;
  if (s2 != sb) {
    Serial.println(s2);
    if (s2) {
      client.publish("wokwi/ebpro/input2", "{\"lb6\":true}");
      digitalWrite(14, HIGH);
    } else {
      client.publish("wokwi/ebpro/input2", "{\"lb6\":false}");
      digitalWrite(14, LOW);
    }
  } sb = s2;
}
Loading
esp32-devkit-c-v4
Conveyor Motor
NOCOMNCVCCGNDINLED1PWRRelay Module
Guide Rail Servo
S1
S2