#include <Wire.h>
#include <Blynk.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
char auth[] = "YyGqtuOYYo77d1snQoM8XTX7zdZDRcih";
char ssid[] = "KAAASYAH";
char pass[] = "01135907790";
#define inSensor1 14 // D14
#define inSensor2 26 // D27
#define inSensor3 32 // D32
#define inSensor4 12 // D34
#define physicalButtonOut1 13 // Change this to the pin you connected the physical button for outSensor1 to
#define physicalButtonOut2 27 // Change this to the pin you connected the physical button for outSensor2 to
#define physicalButtonOut3 33 // Change this to the pin you connected the physical button for outSensor3 to
#define physicalButtonOut4 25 // Change this to the pin you connected the physical button for outSensor4 to
int inStatus1, inStatus2, inStatus3, inStatus4;
int outStatus1, outStatus2, outStatus3, outStatus4;
int countin1 = 0, countin2 = 0, countin3 = 0, countin4 = 0;
int countout1 = 0, countout2 = 0, countout3 = 0, countout4 = 0;
int in1, in2, in3, in4;
int out1, out2, out3, out4;
int now1, now2, now3, now4;
Servo servo1, servo2, servo3, servo4;
int servoPin1 = 2; // D2
int servoPin2 = 4; // D4
int servoPin3 = 5; // D5
int servoPin4 = 15; // D15
#define relay 18 // D3
WidgetLED light(V0);
#define buttonPin1 V2 // Virtual button pin for Sensor 1
#define buttonPin2 V5 // Virtual button pin for Sensor 2
#define buttonPin3 V8 // Virtual button pin for Sensor 3
#define buttonPin4 V11 // Virtual button pin for Sensor 3
BLYNK_WRITE(buttonPin1) {
int buttonState = param.asInt();
if (buttonState == 0) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo1.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo1.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
// Button is pressed, simulate inSensor1 triggered
out1 = countout1++;
Blynk.virtualWrite(buttonPin1, 0); // Reset button state
}
}
BLYNK_WRITE(buttonPin2) {
int buttonState = param.asInt();
if (buttonState == 0) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo2.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo2.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
// Button is pressed, simulate inSensor2 triggered
out2 = countout2++;
Blynk.virtualWrite(buttonPin2, 0); // Reset button state
}
}
BLYNK_WRITE(buttonPin3) {
int buttonState = param.asInt();
if (buttonState == 0) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo3.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo3.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
// Button is pressed, simulate inSensor3 triggered
out3 = countout3++;
Blynk.virtualWrite(buttonPin3, 0); // Reset button state
}
}
BLYNK_WRITE(buttonPin4) {
int buttonState = param.asInt();
if (buttonState == 0) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo4.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo4.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
// Button is pressed, simulate inSensor4 triggered
out4 = countout4++;
Blynk.virtualWrite(buttonPin4, 0); // Reset button state
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass, IPAddress(128, 199, 159, 242), 8080);
delay(200);
pinMode(physicalButtonOut1, INPUT_PULLUP); // Configure the physical button pin for outSensor1 as INPUT_PULLUP
pinMode(physicalButtonOut2, INPUT_PULLUP); // Configure the physical button pin for outSensor2 as INPUT_PULLUP
pinMode(physicalButtonOut3, INPUT_PULLUP); // Configure the physical button pin for outSensor3 as INPUT_PULLUP
pinMode(physicalButtonOut4, INPUT_PULLUP); // Configure the physical button pin for outSensor4 as INPUT_PULLUP
pinMode(inSensor1, INPUT);
pinMode(inSensor2, INPUT);
pinMode(inSensor3, INPUT);
pinMode(inSensor4, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
servo1.setPeriodHertz(50);
servo1.attach(servoPin1, 1000, 2000);
servo2.setPeriodHertz(50);
servo2.attach(servoPin2, 1000, 2000);
servo3.setPeriodHertz(50);
servo3.attach(servoPin3, 1000, 2000);
servo4.setPeriodHertz(50);
servo4.attach(servoPin4, 1000, 2000);
Serial.println("PILL Counter ");
// Set up the buttons in the Blynk app
Blynk.virtualWrite(buttonPin1, 0);
Blynk.virtualWrite(buttonPin2, 0);
Blynk.virtualWrite(buttonPin3, 0);
Blynk.virtualWrite(buttonPin4, 0);
}
void loop()
{
Blynk.run(); // Initiates Blynk
inStatus1 = digitalRead(inSensor1);
inStatus2 = digitalRead(inSensor2);
inStatus3 = digitalRead(inSensor3);
inStatus4 = digitalRead(inSensor4);
int physicalButtonOut1State = digitalRead(physicalButtonOut1);
int physicalButtonOut2State = digitalRead(physicalButtonOut2);
int physicalButtonOut3State = digitalRead(physicalButtonOut3);
int physicalButtonOut4State = digitalRead(physicalButtonOut4);
if (inStatus1 == 0)
{
in1 = countin1++;
delay(1);
}
if (inStatus2 == 0)
{
in2 = countin2++;
delay(1);
}
if (inStatus3 == 0)
{
in3 = countin3++;
delay(1);
}
if (inStatus4 == 0)
{
in4 = countin4++;
delay(1);
}
if (physicalButtonOut1State == LOW)
{
out1 = countout1++;
delay(1); // Add a small delay to debounce the physical button
}
if (physicalButtonOut2State == LOW)
{
out2 = countout2++;
delay(1); // Add a small delay to debounce the physical button
}
if (physicalButtonOut3State == LOW)
{
out3 = countout3++;
delay(1); // Add a small delay to debounce the physical button
}
if (physicalButtonOut4State == LOW)
{
out4 = countout4++;
delay(1); // Add a small delay to debounce the physical button
}
if (physicalButtonOut1State == LOW) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo1.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo1.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
}
if (physicalButtonOut2State == LOW) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo2.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo2.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
}
if (physicalButtonOut3State == LOW) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo3.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo3.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
}
if (physicalButtonOut4State == LOW) {
// Button is pressed, move servo1 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
servo4.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 180 degrees
// Move servo1 back to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servo4.write(pos);
delay(1);
}
delay(500); // Wait for 1 second at 0 degrees
}
now1 = in1 - out1;
now2 = in2 - out2;
now3 = in3 - out3;
now4 = in4 - out4;
// Assuming relay and light control based on the first set of sensors
if (now1 <= 0)
{
digitalWrite(relay, HIGH);
light.off();
Serial.println("No PILL! Light Off");
delay(1);
}
else
{
digitalWrite(relay, LOW);
light.on();
Serial.print("Current Visitor (Sensor 1): ");
Serial.println(now1);
Serial.print("IN (Sensor 1): ");
Serial.println(in1);
Serial.print("OUT (Sensor 1): ");
Serial.println(out1);
delay(1);
}
Blynk.virtualWrite(V1, in1); // Visitors In (Sensor 1)
Blynk.virtualWrite(V2, out1); // Visitors Out (Sensor 1)
Blynk.virtualWrite(V3, now1); // Current Visitors (Sensor 1)
Blynk.virtualWrite(V4, in2); // Visitors In (Sensor 2)
Blynk.virtualWrite(V5, out2); // Visitors Out (Sensor 2)
Blynk.virtualWrite(V6, now2); // Current Visitors (Sensor 2)
Blynk.virtualWrite(V7, in3); // Visitors In (Sensor 3)
Blynk.virtualWrite(V8, out3); // Visitors Out (Sensor 3)
Blynk.virtualWrite(V9, now3); // Current Visitors (Sensor 3)
Blynk.virtualWrite(V10, in4); // Visitors In (Sensor 4)
Blynk.virtualWrite(V11, out4); // Visitors Out (Sensor 4)
Blynk.virtualWrite(V12, now4); // Current Visitors (Sensor 4)
delay(1);
}