Go Down

Topic: ESP32 problème de WebServer. (Read 460 times) previous topic - next topic

simonlere

Code: [Select]
#include <Arduino.h>
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <DHT.h>
#include <ESPAsyncWebServer.h>

int Pin1 = 13;//IN1 is connected to 13
int Pin2 = 12;//IN2 is connected to 12  
int Pin3 = 14;//IN3 is connected to 14
int Pin4 = 27;//IN4 is connected to 27
int finDeCourseOuverte = 26;
int finDeCourseFermer = 25;
int fDCOuvert = 0;
int fDCFermer = 0;
boolean porteOuverte = false; // Déclaration variable Fin de Course Haut
boolean porteFermer = false; // Déclaration variable Fin de Course Bas
 
int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values
int statusPorte = 0;
int poleStep = 0;
int dirStatus = 3;// stores direction status 3= stop (do not change)

String buttonTitle1[] ={"OUVERTURE", "FERMETURE"};
String buttonTitle2[] ={"OUVERTURE", "FERMETURE"};
const String argId[] ={"ouverture", "fermeture"};
const String argId2[] ={"Porte ouverte!","Porte fermer!"};

const char *ssid = "XXX";
const char *password = "XXX";

WebServer server(80);

void handleRoot() {
   //Robojax.com ESP32 Relay Motor Control
  String HTML ="<!DOCTYPE html>";
  HTML += "<html>\n";

  HTML += "<head>\n";

  HTML += "<title>Porte Poulailler</title>\n";

  HTML += "<meta http-equiv=\"refresh\" content=\"25;url=motor?\">\n";
  HTML += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n";

  HTML += "<style>";
  HTML += "html,body{\t\nwidth:100%;`\nheight:100%;\nmargin:0}\n*{box-sizing:border-box}\n.colorAll{\n\tbackground-color:#90ee90}\n.colorBtn{\n\tbackground-color:#add8e6}\n.angleButtdon,a{\n\tfont-size:30px;\nborder:1px solid #ccc;\ndisplay:table-caption;\npadding:7px 10px;\ntext-decoration:none;\ncursor:pointer;\npadding:5px 6px 7px 10px}a{\n\tdisplay:block}\n.btn{\n\tmargin:5px;\nborder:none;\ndisplay:inline-block;\nvertical-align:middle;\ntext-align:center;\nwhite-space:nowrap}";
  HTML += "</style>\n";

  HTML += "</head>\n";

  HTML +="<body>\n";
  HTML +="<h1>Porte Poulailler </h1>\n";

   if(dirStatus ==2){
    HTML +="\n\t<h2><span style=\"background-color: #FFFF00\">Porte en fermeture...</span></h2>\n";    
   }else if(dirStatus ==1){
    HTML +="\n<h2><span style=\"background-color: #FFFF00\">Porte en ouverture...</span></h2>\n";      
   }else{
    HTML +="\n<h2><span style=\"background-color: #FFFF00\">Moteur eteint</span></h2>\n";    
   }
   if(statusPorte ==1){
     HTML +="\n\t<h2><span style=\"background-color: #FFFF00\">";
     HTML += argId2[1];
     HTML += "</span></h2>\n";
   } else if(statusPorte ==2) {
     HTML +="\n\t<h2><span style=\"background-color: #FFFF00\">";
     HTML += argId2[0];
     HTML += "</span></h2>\n";
   }
  
      if(dirStatus ==1){
        HTML += "<div class=\"btn\">\n\t\t<a class=\"angleButton\" style=\"background-color:#f56464\"  href=\"/motor?";
        HTML += argId[0];
        HTML += "=off\">";
        HTML +=buttonTitle1[0]; //motor ON title
      }else{
        HTML += "<div class=\"btn\">\n\t\t<a class=\"angleButton \" style=\"background-color:#90ee90\"  href=\"/motor?";  
        HTML += argId[0];
        HTML += "=on\">";      
        HTML +=buttonTitle2[0];//motor OFF title  
      }  
        HTML +="</a>\n</div>\n\n";  
          
      if(dirStatus ==2){
        HTML +="  <div class=\"btn\">\n<a class=\"angleButton\" style=\"background-color:#f56464\"  href=\"/motor?";
        HTML += argId[1];
        HTML += "=off\">";
        HTML +=buttonTitle1[1]; //motor ON title
      }else{
        HTML +="  <div class=\"btn\">\n<a class=\"angleButton \" style=\"background-color:#90ee90\"  href=\"/motor?";  
        HTML += argId[1];
        HTML += "=on\">";      
        HTML +=buttonTitle2[1];//motor OFF title  
      }
  
     HTML +="</a>\n</div>\n";

     HTML +="\n</body>\n</html>\n";
     server.send(200, "text/html", HTML);  
}//handleRoot()

void handleNotFound() {

  String message = "File Not Found";
  message += "URI: ";
  message += server.uri();
  message += "\n Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\n Arguments: ";
  message += server.args();
  message += "\n";

for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}

  server.send(404, "text/plain", message);
}

void Arret(){
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW);
     digitalWrite(Pin3, LOW);
     digitalWrite(Pin4, LOW);
}

void etatCourseFermer(){
  fDCFermer = digitalRead(finDeCourseFermer);
  if (fDCFermer == HIGH) // Changement d'état du fin de course haut (front montant ou descendant)
  {
    Serial.println("Porte fermée !"); // Affichage sur le moniteur série du texte
    statusPorte = 1;
    pinMode(Pin1, INPUT);//define pin for ULN2003 in1
    pinMode(Pin2, INPUT);//define pin for ULN2003 in2  
    pinMode(Pin3, INPUT);//define pin for ULN2003 in3  
    pinMode(Pin4, INPUT);//define pin for ULN2003 in4
    Arret();
  } else {
    pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
    pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2  
    pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3  
    pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4  
  }
}

void etatCourseOuverte(){
  fDCOuvert = digitalRead(finDeCourseOuverte);
  if (fDCOuvert == HIGH){
    Serial.println("Porte ouverte !");
    statusPorte = 2;
    pinMode(Pin1, INPUT);//define pin for ULN2003 in1
    pinMode(Pin2, INPUT);//define pin for ULN2003 in2  
    pinMode(Pin3, INPUT);//define pin for ULN2003 in3  
    pinMode(Pin4, INPUT);//define pin for ULN2003 in4
    Arret();
  } else {
    pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
    pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2  
    pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3  
    pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4
  }
}

void motorControl() {


    if(server.arg(argId[0]) == "on")
    {
      pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
      pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2  
      pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3  
      pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4  
      dirStatus = 1;// CCW

              
    }else if(server.arg(argId[0]) == "off"){
      
      dirStatus = 3;  // motor OFF
      
          
    }else if(server.arg(argId[1]) == "on"){
      pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
      pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2  
      pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3  
      pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4  
      dirStatus = 2;  // CW  
      
          
    }else if(server.arg(argId[1]) == "off"){
      
      
      dirStatus = 3;  // motor OFF
              
    }  

  handleRoot();
}//motorControl end

void driveStepper(int c)
{
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]);
     digitalWrite(Pin3, pole3[c]);
     digitalWrite(Pin4, pole4[c]);
}//driveStepper end here



simonlere

Code: [Select]
void setup(void) {
  //Robojax.com 28BYJ-48 Steper Motor Control
 
 pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
 pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2   
 pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3   
 pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4   
 pinMode(finDeCourseFermer, INPUT);
 pinMode(finDeCourseOuverte, INPUT);
 
  Serial.begin(115200);//initialize the serial monitor

  //Robojax.com 28BYJ-48 Steper Motor Control
   
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: http://");
  Serial.println(WiFi.localIP());

//multicast DNS   //Robojax.com 28BYJ-48 Steper Motor Control
  server.on("/", handleRoot);
  server.on("/motor", HTTP_GET, motorControl);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");

}//end of setup

void loop(void) {
    //Robojax.com 28BYJ-48 Steper Motor Control
  server.handleClient();
if(dirStatus ==1){

   poleStep--;
   driveStepper(poleStep);
   etatCourseOuverte();
   
 }else if(dirStatus ==2){

   poleStep++;
   driveStepper(poleStep);
   etatCourseFermer();

 }else{
   statusPorte = 0;
   driveStepper(8);   
 }
 if(poleStep>7){
   poleStep=0;
 }
 if(poleStep<0){
   poleStep=7;
 }

  delay(1);
  //Robojax.com 28BYJ-48 Steper Motor Control
}//end of loop

Go Up