#include <WiFi.h>
#include <PubSubClient.h>

#define PIN_TRIG 26
#define PIN_ECHO 25
#define LOWLED   18
#define MIDLED   19
#define HIGHLED  21
#define MOTOR    27

unsigned int level = 0;

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
const char* mqttTopicCM = "/ThinkIOT/distance_cm";
const char* mqttTopicInch = "/ThinkIOT/distance_inch";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  pinMode(LOWLED, OUTPUT);
  pinMode(MIDLED, OUTPUT);
  pinMode(HIGHLED, OUTPUT);
  pinMode(MOTOR, OUTPUT);
  digitalWrite(LOWLED, HIGH);
  digitalWrite(MIDLED, HIGH);
  digitalWrite(HIGHLED, HIGH);
  digitalWrite(MOTOR, LOW);
  Serial.begin(115200);
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);

  // Connect to Wi-Fi
  setup_wifi();

  // Set up MQTT
  client.setServer(mqtt_server, 1883);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  // Start a new measurement:
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);

  // Read the result:
  int duration = pulseIn(PIN_ECHO, HIGH);

  // Convert duration to distance in centimeters:
  int distanceCM = duration / 58;

  level = distanceCM;

  // Convert distance to string
  String distanceStringCM = String(distanceCM);
  String distanceStringInch = String(duration / 148.0, 2);

  // Publish data to MQTT
  client.publish(mqttTopicCM, distanceStringCM.c_str());
  client.publish(mqttTopicInch, distanceStringInch.c_str());

  // Print data in both units
  Serial.print("Distance in CM: ");
  Serial.println(distanceStringCM);
  Serial.print("Distance in inches: ");
  Serial.println(distanceStringInch);

  // Your logic for controlling LEDs and Motor based on the distance
  if (level < 100) {
    digitalWrite(LOWLED, LOW);
    digitalWrite(MOTOR, HIGH);
  } else if ((level > 200) && (level < 400)) {
    digitalWrite(LOWLED, HIGH);
    digitalWrite(MIDLED, LOW);
  } else if (level >= 400) {
    digitalWrite(HIGHLED, LOW);
    digitalWrite(MIDLED, HIGH);
  }

  delay(1000);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str())) {
      Serial.println("Connected");
      client.publish("/ThinkIOT/Publish", "Welcome");
      client.subscribe("/ThinkIOT/Subscribe");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module