#include <Arduino.h>
#define LED_PIN 4
#define BUZZER_PIN 16
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200); // Debugging on Serial Monitor
Serial.println("ESP32 Receiver Initialized.");
}
void loop() {
if (Serial.available()) {
// Read sensor data as a string
String receivedData = Serial.readStringUntil('\n');
// Parse the data
int separatorIndex = receivedData.indexOf(',');
if (separatorIndex > 0) {
int alcoholLevel = receivedData.substring(0, separatorIndex).toInt();
int irStatus = receivedData.substring(separatorIndex + 1).toInt();
// Control buzzer and LED
if (alcoholLevel >= 2048) {
tone(BUZZER_PIN,3000,1000);
} else {
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
}
if (irStatus == 0) {
digitalWrite(LED_PIN, HIGH); // Turn on LED
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
}
}
delay(100); // Small delay
}