#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3I5h0zLCP"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "N8PDN3Uek8IJep22RT9tsNE-7-wm50A_"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Pin Definitions
#define LM35_PIN 34 // LM35 sensor analog input
#define IN1_PIN 32 // L298N IN1
#define IN2_PIN 33 // L298N IN2
#define ENA_PIN 25 // L298N ENA (PWM motor speed)
int motorPWM = 128; // Set initial motor PWM value (range 0-255)
float temperature;
void setup() {
// Start serial communication
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_SSID, WIFI_PASSWORD);
// Set motor pins as OUTPUT
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
pinMode(ENA_PIN, OUTPUT);
// Set PWM for motor driver
ledcAttachChannel(ENA_PIN, 30000, 8, 0);
ledcWrite(ENA_PIN, motorPWM); // Set initial motor speed (PWM value)
}
void loop() {
// Read temperature from LM35
int sensorValue = analogRead(LM35_PIN);
temperature = (sensorValue * 3.3 / 4095.0) * 100.0; // Convert analog value to Celsius
// Send temperature to Blynk app
Blynk.virtualWrite(V1, temperature); // Virtual Pin V1 for temperature
// Send motor PWM to Blynk app
Blynk.virtualWrite(V2, motorPWM); // Virtual Pin V2 for motor PWM
// Control motor direction (just an example)
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
// Run Blynk
Blynk.run();
}
//control motor speed from Blynk (V3)
BLYNK_WRITE(V3) {
motorPWM = param.asInt(); // Get PWM value from Blynk
ledcWrite(ENA_PIN, motorPWM); // Set PWM motor speed
}