Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SOURCE CODE: #include #include #include 9 | P a g e const char * ssid = Galaxy A 3 1 EB 6 1 ;

SOURCE CODE:
#include
#include
#include
9| P a g e
const char* ssid = "Galaxy A31EB61";
const char* password = "mutyam@1234";
const String validQRCode1= "valid_qr_code";
const String validQRCode2= "valid_qr_code_2";
WebServer server(80);
// Define the DC motor pins for the first motor on L298N
const int ENA =5; // PWM pin for speed control
const int IN1=19; // Direction pin 1
const int IN2=18; // Direction pin 2
// Define the DC motor pins for the second motor on L298N
const int ENB =4; // PWM pin for speed control
const int IN3=17; // Direction pin 1
const int IN4=16; // Direction pin 2
void handleRoot();
void handleQRRequest(); // Declare the handleQRRequest function
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED){
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin("esp32")){
Serial.println("Error setting up MDNS responder!");
while (1){
delay(1000);
}
}
Serial.println("MDNS responder started");
10| P a g e
// Set the motor control pins as outputs
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
server.on("/", handleRoot);
server.on("/qr", handleQRRequest); // Now handleQRRequest is declared
server.begin();
MDNS.addService("http","tcp",80);
Serial.println("HTTP server started");
}
void loop(){
server.handleClient();
}
bool validateQRCode(String qrData){
return qrData.equals(validQRCode1)|| qrData.equals(validQRCode2);
}
void dispenseItem(String qrData, int tablets){
Serial.println("Dispensing item...");
int rotationTime =1000; // Adjust this time based on your motor speed to achieve a 360 degree
rotation
int rotations = tablets; // Each tablet corresponds to one rotation
// Rotate the appropriate motor based on the QR code
for (int i =0; i < rotations; i++){
if (qrData.equals(validQRCode1)){
// Motor 1
digitalWrite(IN1, HIGH); // Set motor direction
digitalWrite(IN2, LOW);
analogWrite(ENA,255); // Set motor speed to maximum (255)
} else if (qrData.equals(validQRCode2)){
// Motor 2
digitalWrite(IN3, HIGH); // Set motor direction
digitalWrite(IN4, LOW);
analogWrite(ENB,255); // Set motor speed to maximum (255)
11| P a g e
}
delay(rotationTime); // Wait for the motors to complete one rotation
if (qrData.equals(validQRCode1)){
analogWrite(ENA,0); // Stop motor 1
} else if (qrData.equals(validQRCode2)){
analogWrite(ENB,0); // Stop motor 2
}
delay(500); // Wait before the next rotation
}
Serial.println("Item dispensed");
}
void handleQRCode(String qrData, int tablets){
if (validateQRCode(qrData)){
Serial.println("QR Data is valid");
dispenseItem(qrData, tablets);
} else {
Serial.println("QR Data is invalid");
}
}
void handleQRRequest(){
if (server.args()>0){
String qrData ="";
int tablets =1; // Default to 1 tablet if not specified
for (uint8_t i =0; i < server.args(); i++){
if (server.argName(i)=="qr"){
qrData = server.arg(i);
}
if (server.argName(i)== "tablets"){
tablets = server.arg(i).toInt();
}
}
handleQRCode(qrData, tablets);
}
server.send(200, "text/plain","QR Code processed");
}
void handleRoot(){
12| P a g e
const char webpage[] PROGMEM = R"=====(
Tablet Dispenser
Press the button to dispense an item:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions