SoftAP - creating Web server

fatih.okida
Posts: 2
Joined: Wed May 22, 2024 11:08 am

SoftAP - creating Web server

Postby fatih.okida » Thu May 23, 2024 7:25 am


Hi,
I have a program that used to work, but it no longer works the way I want it to. I am creating web server with Esp8266. After connecting Esp8266 normally when I open 192.168.6.1 needs to show me wifi page so I can enter my wifi name and password. It used to work this way but now it redirects me to 192.168.6.1/static. There is nothing on this page, it just says "Not found: /static/". I don't know what changed. I am adding the function where I created the web server below.
Regards Fatih

Code: Select all

void createWebServer (void){
  const char * headerkeys[] = {"User-Agent","Cookie"} ;
  size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  //ask server to track these headers
  server.collectHeaders(headerkeys, headerkeyssize );
  server.begin();
  server.on("/", [] 
  {
    if ((!is_authentified()) && (we_are == STATION))
    {
      header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
      server.sendContent(header);
      return;
    }
    header = "HTTP/1.1 301 OK\r\nLocation: /static/\r\nCache-Control: no-cache\r\n\r\n";
    server.sendContent(header);
    last_active = millis();  // SUAT

    mobcn = 1; //oc4100 tarafi wifi menusunden ciksin.
  });
  /*server.on("/static", [] 
  {
    if ((!is_authentified()) && (we_are == STATION))
    {
      header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
      server.sendContent(header);
      return;
    }
    header = "HTTP/1.1 301 OK\r\nLocation: /static/\r\nCache-Control: no-cache\r\n\r\n";
    server.sendContent(header);
  });  */
  server.on("/login", handleLogin);
  server.on("/apupdate", []()
  {
    /*if ((!is_authentified()) && (we_are == STATION))
    {
      header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
      server.sendContent(header);
      return;
    }*/

    //if (we_are == STATION)  //sadece networkde iken, ap reconfigureye izin veriyoruz. Cunku guvenlikten oturu. Komsuda ayni fırın varsa, girip bizim sifremizid degistirmesin diye...
    //{
      String apusid = server.arg("apssid");
      String apupass = server.arg("appphrs");

      if ((apusid.length() > 0) && (apupass.length() > 7) && (apusid.length()<12) && (apupass.length() < 9))
      {
        Serial.println("APSID: "); Serial.println(apusid);
        Serial.println("APPAS: "); Serial.println(apupass);

        //EEPROM.begin(512);

        EEPROM.write(2,  apusid.length());
        EEPROM.write(14, apupass.length());
        
        for (int i = 0; i < apusid.length(); ++i)
        {
          EEPROM.write(i+3, apusid[i]);
        }
        for (int i = 0; i < apupass.length(); ++i)
        {
          EEPROM.write(i+15, apupass[i]);
        }
  
        //EEPROM.write(0, 0xAB);
        //EEPROM.write(1, 0xCD);
  
        EEPROM.write(122, 0x01);  //custom AP
        Serial.println("EEPROM Updated with: ");
        
        Serial.print(EEPROM.read(122), HEX);
        
        EEPROM.commit();         
        delay(100);
        
      }
      else
      {
        Serial.println("Length Error");
      }
          
      String header = "HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
              server.sendContent(header);

      if (we_are == STATION)
      {
        delay(1);
      }
      else
      {
        delay(100);
        ESP.restart();
      }
  });
  server.on("/wifi", []() 
  {
    if ((!is_authentified()) && (we_are == STATION))
    {
      header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
      server.sendContent(header);
      return;
    }
    int n = WiFi.scanNetworks();
    content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"tr\" lang=\"tr\">";
    content += "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-16\" /><meta http-equiv=\"Content-Language\" content=\"tr\"/>";
    content += "<head> <title>OC4100</title> </head> <body style=\"padding:0px; margin:0px; overflow:hidden; background-color:#000000;\"> <div id=\"content\" style=\"position:absolute; overflow:hidden; width: 370px; margin-top:40px; margin-left:0px;\">";
    content += "<table id=\"scanTable\" cellpadding=\"2\" border=\"0\"><tbody><tr><td colspan=\"4\">";
    
    for (int x=0; x < n; x++) 
    {
      content += "<button type=\"button\" style=\"width:300px\" value=\"";
      content += WiFi.SSID(x);
      content += "\" onclick=\"get_ssid(this.value)\">";
      content += WiFi.SSID(x);
      content += "  Signal: ";
      content += WiFi.RSSI(x);
      content += "</button></br>";
    }

    switch(act_lang)
    {
      case 0:
              content += "<div id=\"ssids\"></p><form method='get' action='setting'><h3 style='color:white;'>A&#286;: </h3><input name='ssid' id=\"ss\" length=32 style=\"width:300px\"><h3 style='color:white;'>&#350;&#304;FRE: </h3><input name='pass' id=\"pass\" length=64 style=\"width:300px\"></br></br></br><input type='submit' style=\"width:300px\" value=\"G&#246;nder\"></form></div></td></tr></tbody><tr><td colspan=\"4\"><input style=\"width:300px\" id=\"retbtn\" type=\"button\" value=\"Geri\" onclick=\"location.href='/'\"/></td></tr></table></div>";
      break;
      case 1:
              content += "<div id=\"ssids\"></p><form method='get' action='setting'><h3 style='color:white;'>NETWORK: </h3><input name='ssid' id=\"ss\" length=32 style=\"width:300px\"><h3 style='color:white;'>PASSWORD: </h3><input name='pass' id=\"pass\" length=64 style=\"width:300px\"></br></br></br><input type='submit' style=\"width:300px\" value=\"Send\"></form></div></td></tr></tbody><tr><td colspan=\"4\"><input style=\"width:300px\" id=\"retbtn\" type=\"button\" value=\"Return\" onclick=\"location.href='/'\"/></td></tr></table></div>";
      break;
      case 2:
              content += "<div id=\"ssids\"></p><form method='get' action='setting'><h3 style='color:white;'>NETZWERK: </h3><input name='ssid' id=\"ss\" length=32 style=\"width:300px\"><h3 style='color:white;'>PASSWORT: </h3><input name='pass' id=\"pass\" length=64 style=\"width:300px\"></br></br></br><input type='submit' style=\"width:300px\" value=\"Senden\"></form></div></td></tr></tbody><tr><td colspan=\"4\"><input style=\"width:300px\" id=\"retbtn\" type=\"button\" value=\"Rückkehr\" onclick=\"location.href='/'\"/></td></tr></table></div>";
      break;
      case 3:
              content += "<div id=\"ssids\"></p><form method='get' action='setting'><h3 style='color:white;'>R&#201;SEAU: </h3><input name='ssid' id=\"ss\" length=32 style=\"width:300px\"><h3 style='color:white;'>MOT DE PASSE: </h3><input name='pass' id=\"pass\" length=64 style=\"width:300px\"></br></br></br><input type='submit' style=\"width:300px\" value=\"Envoyer\"></form></div></td></tr></tbody><tr><td colspan=\"4\"><input style=\"width:300px\" id=\"retbtn\" type=\"button\" value=\"Revenir\" onclick=\"location.href='/'\"/></td></tr></table></div>";
      break;
      case 4:
              content += "<div id=\"ssids\"></p><form method='get' action='setting'><h3 style='color:white;'>RETE: </h3><input name='ssid' id=\"ss\" length=32 style=\"width:300px\"><h3 style='color:white;'>PAROLA D'ORDINE: </h3><input name='pass' id=\"pass\" length=64 style=\"width:300px\"></br></br></br><input type='submit' style=\"width:300px\" value=\"Inviare\"></form></div></td></tr></tbody><tr><td colspan=\"4\"><input style=\"width:300px\" id=\"retbtn\" type=\"button\" value=\"Ritorno\" onclick=\"location.href='/'\"/></td></tr></table></div>";
      break;
      case 5:
              content += "<div id=\"ssids\"></p><form method='get' action='setting'><h3 style='color:white;'>RED: </h3><input name='ssid' id=\"ss\" length=32 style=\"width:300px\"><h3 style='color:white;'>CONTRASE&#209;A: </h3><input name='pass' id=\"pass\" length=64 style=\"width:300px\"></br></br></br><input type='submit' style=\"width:300px\" value=\"Enviar\"></form></div></td></tr></tbody><tr><td colspan=\"4\"><input style=\"width:300px\" id=\"retbtn\" type=\"button\" value=\"Regreso\" onclick=\"location.href='/'\"/></td></tr></table></div>";
      break;

    }
    
    content += "<script type=\"text/javascript\">var ssid; function get_ssid(ssid){console.log(ssid); var entry = document.getElementById(\"ss\"); entry.setAttribute(\"value\", ssid);};</script></body></html>";
    last_active = millis();  // SUAT
    server.send(200, "text/html", content);
  });
  server.on("/stat.xml", []() 
  {
    if ((!is_authentified()) && (we_are == STATION))
    {
      header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
      server.sendContent(header);
      return;
    }
    last_active = millis();  // SUAT
    SendStat();
  } );
 server.on("/setting", []() 
 {
  //Serial.println("setting req");
  if ((!is_authentified()) && (we_are == STATION))
  {
    header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
    server.sendContent(header);
    return;
  }
  last_active = millis();  // SUAT
  String qsid = server.arg("ssid");
  String qpass = server.arg("pass");
  content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">";
  if (qsid.length() > 0 && qpass.length() > 0) 
  {

    for (int i = 0; i < qsid.length(); ++i)
      {
        EEPROM.write(i+23, qsid[i]);
        //Serial.print("Wrote: ");
        //Serial.println(qsid[i]); 
      }
    for (int i = 0; i < qpass.length(); ++i)
      {
        EEPROM.write(56+i, qpass[i]);
        //Serial.print("Wrote: ");
        //Serial.println(qpass[i]); 
      }    
    network_recorded  = 0;
    
    main_stat = CMD_CONNECT;
    stat = WIFI_CONNECTING;
             
    EEPROM.write(121, 0x01);  //connecting
  
    EEPROM.commit();
    //Serial.println("Restarting for NETWORK");
    delay(200);
    ESP.restart();
  }
  else 
  {
    content += "<head> <title>OC4100</title><meta http-equiv=\"refresh\" content=\"0; url=http://" + WiFi.localIP().toString() + "/wifi\"> </head>";
    content += "<body style=\"padding:0px; margin:0px; overflow:hidden; background-color:#000000;\"> </body></html>";
    statusCode = 404;
    server.send(statusCode, "text/html", content);
  }
  
 });
  server.on("/buttons", []()
  {
    if ((!is_authentified()) && (we_are == STATION))
    {
      header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
      server.sendContent(header);
      return;
    }
    last_active = millis();  // SUAT
   if (server.hasArg("ckdly"))
   {
      String arg2 = server.arg("ckdly");
      int val2 = arg2.toInt();

      String arg1 = server.arg("ckdur");
      int val = arg1.toInt();      

      /*String arg3 = server.arg("ffonk");
      int val3 = arg3.toInt();

      String arg4 = server.arg("ctemp");
      int val4 = arg4.toInt();*/
        
      buttonpressed = 1;

      //Serial.println(val2);
      //Serial.println(val);
      
      command_byte = byte(231); //ckdly   
      data_byte[0] = (byte) (val / 256);   //Hi kisim
      data_byte[1] = (byte) (val % 256);  //Lo kisim
      data_byte[2] = (byte) (val2 / 256);   //Hi kisim
      data_byte[3] = (byte) (val2 % 256);  //Lo kisim    

      /*data_byte[4] = (byte) val3;
      data_byte[5] = (byte) (val4 / 256);
      data_byte[6] = (byte) (val4 % 256);*/
      
   }
   else if (server.hasArg("ckdly2"))
   {
      String arg2 = server.arg("ckdly2");
      int val2 = arg2.toInt();

      String arg1 = server.arg("ckdur2");
      int val = arg1.toInt();      

      /*String arg3 = server.arg("ffonk");
      int val3 = arg3.toInt();

      String arg4 = server.arg("ctemp");
      int val4 = arg4.toInt();*/
        
      buttonpressed = 1;

      //Serial.println(val2);
      //Serial.println(val);
      
      command_byte = byte(229); //ckdly     //escye basildi. gonderip geri gidecegiz.
      data_byte[0] = (byte) (val / 256);   //Hi kisim
      data_byte[1] = (byte) (val % 256);  //Lo kisim
      data_byte[2] = (byte) (val2 / 256);   //Hi kisim
      data_byte[3] = (byte) (val2 % 256);  //Lo kisim    

      /*data_byte[4] = (byte) val3;
      data_byte[5] = (byte) (val4 / 256);
      data_byte[6] = (byte) (val4 % 256);*/
      
   }   
   else if (server.hasArg("ffonk"))
   {
      String arg1 = server.arg("ffonk");
      int val1 = arg1.toInt();

      String arg2 = server.arg("ctemp");
      int val2 = arg2.toInt();

           
      data_byte[4] = (byte) val1;
      data_byte[5] = (byte) (val2 / 256);
      data_byte[6] = (byte) (val2 % 256);
               
      buttonpressed = 1;

          command_byte = byte(232); //fonktemp
   }
   else if (server.hasArg("almsethr"))
   {
      String arg1 = server.arg("almsethr");
      int val1 = arg1.toInt();

      String arg2 = server.arg("almsetmin");
      int val2 = arg2.toInt();

      data_byte[0] = (byte) val1;
      data_byte[1] = (byte) val2;
      

      buttonpressed = 1;

      command_byte = byte(233); //alm set
   }
   else if (server.hasArg("sethr"))
   {
      String arg1 = server.arg("sethr");
      int val1 = arg1.toInt();

      String arg2 = server.arg("setmin");
      int val2 = arg2.toInt();

      data_byte[0] = (byte) val1;
      data_byte[1] = (byte) val2;
      

      buttonpressed = 1;

      command_byte = byte(234); //clock set
   }   
   else if (server.hasArg("setyear"))
   {
      String arg1 = server.arg("setyear");
      int val1 = arg1.toInt();

      String arg2 = server.arg("setmonth");
      int val2 = arg2.toInt();

      String arg3 = server.arg("setday");
      int val3 = arg3.toInt();

      if (val1 >= 2000) val1 = val1-2000;
      data_byte[0] = (byte) val1;
      data_byte[1] = (byte) val2;
      data_byte[2] = (byte) val3;

      buttonpressed = 1;

      command_byte = byte(235); //date set
   }   
   else if (server.hasArg("mpset"))
   {
      String arg1 = server.arg("mpset");
      int val1 = arg1.toInt();
      
      String arg2 = server.arg("ffonk");
      int val2 = arg2.toInt();

      String arg3 = server.arg("ctemp");
      int val3 = arg3.toInt();

      data_byte[0] = (byte) val1;
           
      data_byte[1] = (byte) val2;
      data_byte[2] = (byte) (val3 / 256);
      data_byte[3] = (byte) (val3 % 256);

      
      buttonpressed = 1;

      command_byte = byte(236); //mp set
   }   
   else
   {
      if (buttonpressed  == 1) return;
      
      String but_id = server.arg("but");
      int sw = but_id.toInt();

      if ((sw == 0) || (sw == 0x30)) 
      {
        //Serial.println("error"); 
        return;
      }
      
      buttonpressed = 1;

      //Serial.println(85); 
      //Serial.println(sw); 
      
      switch (sw) {
        case 0:
        command_byte = byte(160);   // I/O Button
        break;
        case 1:
        command_byte = byte(161);   // ESC button
        break;
        case 2:
        command_byte = byte(162);    // Menu button
        break;
        case 3:
        command_byte = byte(163);   // Up button
        break;
        case 4:
        command_byte = byte(164);   // Down button
        break;
        case 5:
        command_byte = byte(165);   // OK button
        break;
        case 6:  // Exit Button
          if (server.hasArg("DISCONNECT")){
            if (we_are == STATION)  //AP isek logout bir ise yaramaz.
            {
              //Serial.println("Disconnection");
              String header = "HTTP/1.1 301 OK\r\nSet-Cookie: c=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
              server.sendContent(header);
            }
            else
            {
              String header = "HTTP/1.1 301 OK\r\nLocation:/ \r\nCache-Control: no-cache\r\n\r\n";
              server.sendContent(header);
            }
          
            buttonpressed = 0;
            return;
            //command_byte = byte(111);  //  buraya "client offline oldu" tarzı bisey koyabiliriz  
          }  
        break;
        case 7:
        command_byte = byte(167);   // Light button
        break;
        case 8:
        command_byte = byte(168);    // up arrow button
        break;
        case 9:
        command_byte = byte(169);   // Lock button
        break;
        case 10:
        command_byte = byte(170);   // x1 button
        break;
        case 11:
        command_byte = byte(171);   // fonk_left button
        break;
        case 12:
        command_byte = byte(172);   // fonk_right button
        break;
        case 13:
        command_byte = byte(173);   // temp_left button
        break;
        case 14:
        command_byte = byte(174);   // temp_right button
        break;
        default:
        command_byte = 0;
        break;
      }
   }
    server.send(200);
  });
}

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: SoftAP - creating Web server

Postby ESP_Sprite » Fri May 24, 2024 7:06 am

I mean, yeah? If you're 'authentified' or if the device is not a station, you'll get sent there by

Code: Select all

    header = "HTTP/1.1 301 OK\r\nLocation: /static/\r\nCache-Control: no-cache\r\n\r\n";

fatih.okida
Posts: 2
Joined: Wed May 22, 2024 11:08 am

Re: SoftAP - creating Web server

Postby fatih.okida » Fri May 24, 2024 1:38 pm


Yes you are right its directing to static page if conditions wrong. But it wasnt like that before. This very same program was working before without problem. Maybe something changed about HTML protocol or cookies. Do you have any idea ?

Code: Select all

void generate_api_key() {
  
  sessioncookie = "";
  
  for(int i = 0; i < 32; i++) sessioncookie += anchars[random(0, anchars.length())]; 
  //Serial.println(sessioncookie);
}

bool is_authentified(){ //This function checks for Cookie header in request, it compares variable c to sessioncookie and returns true if they are the same
  if (server.hasHeader("Cookie")){
    String cookie = server.header("Cookie"), authk = "c=" + sessioncookie;
    if (cookie.indexOf(authk) != -1) {try_apicnt = 0; return true;}
  }

  if (++try_apicnt > 10){
    try_apicnt = 0;
    last_active = millis();
    generate_api_key();
  }
  return false;
}

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: SoftAP - creating Web server

Postby ESP_Sprite » Sat May 25, 2024 1:33 am

Most logical reason was that your browser used to have a session cookie, but it got expired or deleted making 'is_authentified()' fail.

Who is online

Users browsing this forum: No registered users and 10 guests