#include <ESP32Servo.h> // Allow ESP32 to control servo
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
Servo myservo; // Create Servo Object to control a servo
const int ServoPin = 23; // Setting pin 23 for servo
const int red = 17;
const int green = 16;
int angle = 0; // Current angle of Servo
const unsigned int incrementRate = 2; // Increment rate
const unsigned int sweepDelay = 100; // Delay for smooth movement
const int maxAngle = 180; // Maximum angle for the servo
const int minAngle = 0; // Minimum angle for the servo
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "GroupProject/4996968n";
int PORTNUM = 1883;
void setup_wifi()
{
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED )
{
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Given IP by the router to ESP32 is ");
Serial.println(WiFi.localIP());
}
void connectMQTT()
{
while(!client.connected() )
{
Serial.println("Connecting to MQTT ...");
if (client.connect(espClientName) )
{
Serial.println("Connected");
MQTTSubscribe();
}
else
{
Serial.print("Failed with state ");
Serial.print(client.state() );
delay(1500);
}
}
}
void callback(String topic, byte* payload, unsigned int length)
{
String messageTemp;
Serial.print("Message received in topic: ");
Serial.print(topic);
Serial.print(" length is: ");
Serial.println(length);
Serial.print("Data received from broker: ");
for (int i = 0; i<length; i++)
{
Serial.print( (char)payload[i] );
messageTemp += (char)payload[i];
}
if (topic == "GroupProject/4996968n")
{
if (messageTemp == "Up")
{
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
}
else if (messageTemp == "Down")
{
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
}
}
}
void MQTTSubscribe()
{
client.subscribe("GroupProject/4996968n");
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup()
{
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
Serial.begin(115200); // Begin Serial Monitor
myservo.attach(ServoPin); // Set up ServoPin using Servo library
myservo.write(angle); // Initialize servo to starting angle
setup_wifi();
setup_MQTT();
}
void loop ()
{
if ( !client.connected() )
{
connectMQTT();
}
client.loop();
unsigned int ADC_value; // Variable to read the value from analog pin
ADC_value = analogRead(A5);
Serial.print("Voltage now is ");
Serial.println(ADC_value);
if (ADC_value < 2500)
{
// Clockwise rotation: Move servo from current angle to 180 degrees
if (angle < maxAngle)
{
angle += incrementRate;
if (angle > maxAngle) angle = maxAngle; // Ensure angle does not exceed max
myservo.write(angle);
Serial.print("Moving to ");
Serial.println(angle);
delay(sweepDelay); // Adjust delay for smooth movement
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
{
client.publish("GroupProject/4996968n","Down");
}
}
else if (ADC_value >= 2500 && ADC_value < 4000)
{
// Counter-clockwise rotation: Move servo from current angle to 0 degrees
if (angle > minAngle)
{
angle -= incrementRate;
if (angle < minAngle) angle = minAngle; // Ensure angle does not go below min
myservo.write(angle);
Serial.print("Moving to ");
Serial.println(angle);
delay(sweepDelay); // Adjust delay for smooth movement
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
{
client.publish("GroupProject/44996968n","Up");
}
char str[4];
sprintf(str, "%d", ADC_value);
client.publish("GroupProject/4996968n", str);
}
}