About APP control led on ESP32AP need help please

Hi all,
I am not sure if it's decent to open a new topic about using an old one of:

ESP32. WiFi. WebServer. LED on/off. Static IP. Soft Access Point - Tutorials and Guides / Internet of Things - MIT App Inventor Community by Juan A. Villalpando. thanks for it.

I tested the: 5.- App sends on/off LED12 and LED14. WiFi. WebServer. Static IP. and got html code printed in textbox, without led on when pressed ON LED16 button (was: 12/14), which ON/OFF well when controlled by web page in 4.

The ESP32 code used from: 4.- ESP32 Soft Access Point. Web Server. Web page on/off LED2. Check status LED2.
can this the problem? how to fix please?

the 'Check status' button works fine, you can see: LED16 now is OFF in the second last line.
Thanks
Adam

Use a WebViewer

192.168.1.115/consulta

1 Like

Thank you Juan.
I tested #4 and #5 as below with the same ESP32 code of #5 with pins resigned, #4 works well, #5 no thing happened pressed buttons, what can be?

if it is only work by web command button?

then how to do if I like to use MIT APP 's button to control the led ?

#4-1


CODE:

/* 
 *  
 */

 // Juan A. Villalpando.
// KIO4.COM
// Enciende y apaga LED. Botones.

#include <WiFi.h>
 
const char* ssid = "Nombre_red_WiFi";
const char* password = "contraseña_red";

// Setting Static IP.
        IPAddress local_IP(192, 168, 1, 115);
        IPAddress gateway(192, 168, 1, 1);
        IPAddress subnet(255, 255, 255, 0); 
        IPAddress primaryDNS(8, 8, 8, 8); //opcional 
        IPAddress secondaryDNS(8, 8, 4, 4); //opcional 

WiFiServer server(80); // Port 80

#define LED16  16    // LED12
#define LED17  17    // LED14
String estado = "";
int wait30 = 30000; // time to reconnect when connection is lost.

void setup() {
  Serial.begin(115200);
  pinMode(LED16, OUTPUT);
  pinMode(LED17, OUTPUT);

// Setting Static IP.
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Error in configuration.");
  }

// Connect WiFi net.
  Serial.println();
  Serial.print("Connecting with ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected with WiFi.");
 
  // Start Web Server.
  server.begin();
  Serial.println("Web Server started.");
 
  // This is IP
  Serial.print("This is IP to connect to the WebServer: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  
// If disconnected, try to reconnect every 30 seconds.
  if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
    Serial.println("Trying to reconnect WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    wait30 = millis() + 30000;
  } 
  // Check if a client has connected..
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  Serial.print("New client: ");
  Serial.println(client.remoteIP());
   
  // Espera hasta que el cliente envíe datos.
  // while(!client.available()){ delay(1); }

  /////////////////////////////////////////////////////
  // Read the information sent by the client.
  String req = client.readStringUntil('\r');
  Serial.println(req);

  // Make the client's request.
       if (req.indexOf("on16") != -1) {digitalWrite(LED16, HIGH); estado = "LED16 ON";}
       if (req.indexOf("off16") != -1){digitalWrite(LED16, LOW); estado = "LED16 OFF";}
       if (req.indexOf("on17") != -1) {digitalWrite(LED17, HIGH); estado = "LED17 ON";}
       if (req.indexOf("off17") != -1){digitalWrite(LED17, LOW); estado = "LED17 OFF";}
       if (req.indexOf("consulta") != -1){
           estado ="";
           if (digitalRead(LED16) == HIGH) {estado = "LED16 ON,";} else {estado = "LED16 OFF,";}
           if (digitalRead(LED17) == HIGH) {estado = estado + "LED17 ON";} else {estado = estado + "LED17 OFF";}
           }
           
//////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(estado); //  Return status.

  client.flush();
  client.stop();
  Serial.println("Client disconnected.");
}


Check

1 Like

Great!
Thank you Juan, It works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.