IRCode
IRCode
h>
#include <WebServer.h>
#include <IRremote.h>
#include <EEPROM.h>
IRrecv IR(15);
const int stepPin = 2;
const int dirPin=4;
const int sensorPin=13;
const int feedopen=14;
int isopen=0;
int autoMode=0; // if the automatic mode of opening the door when an object is
detected activated, the value of this variable would be 1;
int SpeedCounter=0;
unsigned int deltime=100;
int StepsTaken=0;
WebServer server(80);
void handleRoot() {
//The Local Web Page Structure:
String htmlContent = "<!DOCTYPE html><html><head><style>";
htmlContent += "body { text-align: center; }";
htmlContent += ".button { width: 100px; height: 100px; font-size: 20px; margin:
10px; }";
htmlContent += "</style></head><body>";
htmlContent += "<h1>ESP32 Control</h1>";
htmlContent += "<button class='button' id='upButton'>Up</button>";
htmlContent += "<button class='button' id='downButton'>Down</button>";
htmlContent += "<button class='button' id='leftButton'>Left</button>";
htmlContent += "<button class='button' id='rightButton'>Right</button>";
htmlContent += "<script>";
htmlContent += "document.getElementById('upButton').addEventListener('click',
function () { sendCommand('up'); });";
htmlContent += "document.getElementById('downButton').addEventListener('click',
function () { sendCommand('down'); });";
htmlContent += "document.getElementById('leftButton').addEventListener('click',
function () { sendCommand('left'); });";
htmlContent +=
"document.getElementById('rightButton').addEventListener('click', function () {
sendCommand('right'); });";
htmlContent += "function sendCommand(command) {";
htmlContent += "var xhr = new XMLHttpRequest();";
htmlContent += "xhr.open('GET', '/control?cmd=' + command, true);";
htmlContent += "xhr.send();";
htmlContent += "}";
htmlContent += "</script>";
htmlContent += "</body></html>";
void ccwRotation(){
digitalWrite(stepPin, HIGH);
delayMicroseconds(deltime); //time of rotation
digitalWrite(stepPin, LOW); //stopping the rotaion (the step)
delayMicroseconds(deltime); // time before starting the next step
StepsTaken=+1; //the steps counter counts negative values when the door
is closing
//so that if something happens and the closing got
interrupted, the system
// knows how much steps to take in closing or opening the
door
}
isopen=1;
EEPROM.write(0,isopen);
EEPROM.commit();
}
}
//This Function is responsible for opening the door when the ir sensor detects an
object
void openSensor(){
if (autoMode){ // if the automatic mode is activated
//if the sensor detects an object, it waits, if the object is still detected
it opens the door
while(!digitalRead(sensorPin)){ //while you detect something, wait, open the
door, wait longer, close
if (isopen){ //If this function opens the door, it closes it. Otherwise it has
no control over the system whatsoever.
delay(1000);
if(!digitalRead(sensorPin)){
cwRotation();
Serial.println("Sensor detects an object, Opening");
delay(1000);
}
if (digitalRead(sensorPin)){
delay(1000);
delay(1000);
delay(1000);
delay(1000);
ccwRotation();
break;
}
// After Openning the door, if the sensor does not detect anything, the door is
closed
}
}
}
}
void handleCommand() {
//Handling the input from the buttons on the local web page
if (server.hasArg("cmd")) {
String command = server.arg("cmd");
// Handle the command received from the web interface here
// You can perform actions based on the command, e.g., controlling your ESP32
if (server.hasArg("cmd")) {
String command = server.arg("cmd");
if (command == "up") {
Serial.println("Up");
ccwRotation();
}
else if (command == "down") {
Serial.println("Down");
cwRotation();
}
else if (command == "left") {
Serial.println("Left");
}
else if (command == "right") {
Serial.println("Right");
Serial.begin(9600);
//Serial2.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
void loop() {
server.handleClient();
if (IR.decode()) {
Serial.println(IR.decodedIRData.decodedRawData, HEX);
if (IR.decodedIRData.decodedRawData == 0xA55AFF00) {//right button
Serial2.println("Right");
if(deltime>=50)
deltime-=10;
else deltime=50;
Serial.println(deltime);
} else if (IR.decodedIRData.decodedRawData == 0xF708FF00) {//left button
Serial2.println("Left");
if(deltime<=300)
deltime +=10;
else deltime=300;
Serial.println(deltime);
} else if (IR.decodedIRData.decodedRawData == 0xE718FF00) { //up
ccwRotation();
} else if (IR.decodedIRData.decodedRawData == 0xAD52FF00) {//down
digitalWrite(stepPin, LOW);
cwRotation();
}
else if (IR.decodedIRData.decodedRawData == 0xE31CFF00) {//OK Button
autoMode=1; //Activating the Automatic Opening Mode
}
delay(10);
IR.resume();
}
Serial.println("Right");
}
}