#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define ssid "Wokwi-GUEST"
#define pass ""
#define server "mqtt-dashboard.com"
#define port 1883
WiFiClient client;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_MQTT_Client mqtt(&client, server, port);
Adafruit_MQTT_Publish in1 = Adafruit_MQTT_Publish(&mqtt, "in1");
Adafruit_MQTT_Publish in2 = Adafruit_MQTT_Publish(&mqtt, "in2");
Adafruit_MQTT_Subscribe data2 = Adafruit_MQTT_Subscribe(&mqtt, "data2");
Servo servo;
int sa = 0;
int sb = 0;
int counter = 0;
void data2callback(char *data, uint16_t len) {
JsonDocument doc;
deserializeJson(doc, data);
const char* sw0 = doc["sw0"];
if (sw0[0] == 't') digitalWrite(4, HIGH);
else digitalWrite(4, LOW);
const char* sw1 = doc["sw1"];
if (sw1[0] == 't') digitalWrite(16, HIGH);
else digitalWrite(16, LOW);
const char* sw2 = doc["sw2"];
if (sw2[0] == 't') digitalWrite(17, HIGH);
else digitalWrite(17, LOW);
const char* sw3 = doc["sw3"];
if (sw3[0] == 't') digitalWrite(5, HIGH);
else digitalWrite(5, LOW);
const char* sw4 = doc["sw4"];
if (sw4[0] == 't') digitalWrite(18, HIGH);
else digitalWrite(18, LOW);
int an1 = doc["an1"];
servo.write(an1 * 9);
delay(10);
lcd.setCursor(4, 0);
if (an1 < 10) lcd.print(" ");
if (an1 < 100) lcd.print(" ");
lcd.print(an1);
int an2 = doc["an2"];
lcd.setCursor(12, 0);
lcd.print(an2);
int an3 = doc["an3"];
lcd.setCursor(4, 1);
if (an3 < 10) lcd.print(" ");
if (an3 < 100) lcd.print(" ");
lcd.print(an3);
int an4 = doc["an4"];
lcd.setCursor(12, 1);
if (an4 < 10) lcd.print(" ");
if (an4 < 100) lcd.print(" ");
lcd.print(an4);
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000);
retries--;
if (retries == 0) {
while (1);
}
}
Serial.println("MQTT Connected!");
}
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(23, OUTPUT);//konveyor
data2.setCallback(data2callback);
mqtt.subscribe(&data2);
delay(1000);
}
void loop() {
MQTT_connect();
mqtt.processPackets(5000);
if (! mqtt.ping()) {
mqtt.disconnect();
}
int s1 = !digitalRead(15);
int s2 = !digitalRead(2);
if (s1 != sa) {
Serial.println(s1);
if (s1) in1.publish("{\"in1\":true}");
else in1.publish("{\"in1\":false}");
} sa = s1;
if (s2 != sb) {
Serial.println(s2);
if (s2) in2.publish("{\"in2\":true}");
else in2.publish("{\"in2\":false}");
} sb = s2;
}
Conveyor Motor
Guide Rail Servo
S1
S2