// ----------------- Blynk Configuration (MUST come first!) -----------------
#define BLYNK_TEMPLATE_ID "TMPL6_0YVPGWC"
#define BLYNK_TEMPLATE_NAME "samosys"
#define BLYNK_AUTH_TOKEN "yVQSBOPhRlJlSpi4MLAFzP7t1YuiZfbN"
// ----------------- Include Libraries -----------------
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// ----------------- WiFi Credentials -----------------
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// ----------------- Pin Definitions -----------------
#define TRIG_PIN 5
#define ECHO_PIN 18
#define RED_LED_DISTANCE 19
#define GREEN_LED_DISTANCE 21
#define TEMP_SENSOR_PIN 34
#define RED_LED_TEMP 22
#define GREEN_LED_TEMP 23
#define SERVO_PIN 13
// ----------------- Variables -----------------
float distance_cm;
float temperature_celsius;
Servo myServo;
unsigned long previousMillis = 0;
const long servoInterval = 10000; // 10 seconds
bool servoAtZero = true;
// Blynk Virtual Pins
#define VIRTUAL_PIN_DISTANCE V0
#define VIRTUAL_PIN_TEMP V1
// ----------------- Setup Function -----------------
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_LED_DISTANCE, OUTPUT);
pinMode(GREEN_LED_DISTANCE, OUTPUT);
pinMode(RED_LED_TEMP, OUTPUT);
pinMode(GREEN_LED_TEMP, OUTPUT);
myServo.attach(SERVO_PIN);
myServo.write(0); // Start at 0 degrees
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Connect to Blynk
}
// ----------------- Loop Function -----------------
void loop() {
Blynk.run(); // Keep Blynk connected
readSensors();
controlLEDs();
controlServo();
sendToBlynk();
delay(500);
}
// ----------------- Read Sensors -----------------
void readSensors() {
long duration;
int temp_adc;
// Read Distance
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
if (duration == 0) {
distance_cm = 999; // Out of range
} else {
distance_cm = (duration * 0.0343) / 2;
}
// Read Temperature
temp_adc = analogRead(TEMP_SENSOR_PIN);
temperature_celsius = (temp_adc / 4095.0) * 100.0; // Simplified conversion
// Debugging output
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.print(" cm, ");
Serial.print("Temperature: ");
Serial.print(temperature_celsius);
Serial.println(" °C");
}
// ----------------- Control LEDs -----------------
void controlLEDs() {
if (distance_cm < 20) {
digitalWrite(RED_LED_DISTANCE, HIGH);
digitalWrite(GREEN_LED_DISTANCE, LOW);
} else {
digitalWrite(RED_LED_DISTANCE, LOW);
digitalWrite(GREEN_LED_DISTANCE, HIGH);
}
if (temperature_celsius > 30) {
digitalWrite(RED_LED_TEMP, HIGH);
digitalWrite(GREEN_LED_TEMP, LOW);
} else {
digitalWrite(RED_LED_TEMP, LOW);
digitalWrite(GREEN_LED_TEMP, HIGH);
}
}
// ----------------- Control Servo Motor -----------------
void controlServo() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= servoInterval) {
previousMillis = currentMillis;
if (servoAtZero) {
myServo.write(180); // Rotate to 180 degrees
servoAtZero = false;
} else {
myServo.write(0); // Rotate back to 0 degrees
servoAtZero = true;
}
}
}
// ----------------- Send Data to Blynk -----------------
void sendToBlynk() {
Blynk.virtualWrite(VIRTUAL_PIN_DISTANCE, distance_cm);
Blynk.virtualWrite(VIRTUAL_PIN_TEMP, temperature_celsius);
}