How to create minimal web server example with a dropdown menu?

I want to create a three-item menu: Verizon, T-Mobile, AT&T. Since the example uses LEDs, I'll do the same--except with a dropdown menu to select among the three.

I'm trying to combine these two examples:

  1. Build an ESP8266 Web Server - Code and Schematics (NodeMCU) | Random Nerd Tutorials
  2. W3Schools Tryit Editor

Except with phone carriers instead of cars:

enter image description here

I'm struggling on the polyglot part of the programming, combining the HTML and Arduino program in the middle:

        // Web Page Heading
        client.println("<form action='/action_page.php'>");
        client.println("  <select id='providor' name='providor'>");
        client.println("    <option value='verizon'>Verizon</option>");
        client.println("    <option value='tmobile'>T-Mobile</option>");
        client.println("    <option value='atnt'>AT&T</option>");
        client.println("  </select>");
        client.println("  <input type='submit'>");
        client.println("</form>");
        
        if (providor==verizon)
          client.println("<p>Providor Selected: Verizon</p>");
        else if (providor==tmobile)
          client.println("<p>Providor Selected: T-Mobile"</p>");
        else if (providor==atnt)
          client.println("<p>Providor Selected: AT&T"</p>");    

Here is the rest of the program:

/*

Based from 
https://randomnerdtutorials.com/esp8266-web-server

HTML tool
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_elem_select
*/

#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
//String output5State = "off";
//String output4State = "off";

// Assign output variables to GPIO pins
const int output5 = 14;    // D5 verizon  
const int output6 = 12;    // D6 tmobile
const int output7 = 13;    // D7 atnt

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output5, OUTPUT);
  pinMode(output6, OUTPUT);
  pinMode(output7, OUTPUT);  
  // Set outputs to LOW
  digitalWrite(output5, LOW);
  digitalWrite(output6, LOW);
  digitalWrite(output7, LOW);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop()
{
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();


            
            // turns the GPIOs on and off
            if (header.indexOf("GET /5/on") >= 0) 
            {
              Serial.println("D5 on");
              output5State = "on";
              digitalWrite(output5, HIGH);
            }
            else if (header.indexOf("GET /5/off") >= 0) 
            {
              Serial.println("D5 off");
              output5State = "off";
              digitalWrite(output5, LOW);
            }
            else if (header.indexOf("GET /6/on") >= 0) 
            {
              Serial.println("D6 on");
              output4State = "on";
              digitalWrite(output6, HIGH);
            } 
            else if (header.indexOf("GET /6/off") >= 0) 
            {
              Serial.println("D6 off");
              output4State = "off";
              digitalWrite(output6, LOW);
            }
            else if (header.indexOf("GET /7/on") >= 0) 
            {
              Serial.println("D7 on");
              output4State = "on";
              digitalWrite(output7, HIGH);
            } 
            else if (header.indexOf("GET /7/off") >= 0) 
            {
              Serial.println("D7 off");
              output4State = "off";
              digitalWrite(output7, LOW);
            }
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
            '>Verizon</option>");

            // HERE
            // ↓
            // ↓
            // ↓
            // ↓         
              
            // Web Page Heading
            client.println("<form action='/action_page.php'>");
            client.println("  <select id='providor' name='providor'>");
            client.println("    <option value='verizon'>Verizon</option>");
            client.println("    <option value='tmobile'>T-Mobile</option>");
            client.println("    <option value='atnt'>AT&T</option>");
            client.println("  </select>");
            client.println("  <input type='submit'>");
            client.println("</form>");
            
            if (providor==verizon)
              client.println("<p>Providor Selected: Verizon</p>");
            else if (providor==tmobile)
              client.println("<p>Providor Selected: T-Mobile"</p>");
            else if (providor==atnt)
              client.println("<p>Providor Selected: AT&T"</p>");    
                
            // ↑
            // ↑
            // ↑
            // ↑
            
            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

Any suggestions or solutions on how to change or the correct code?

I long ago gave up on handling all the web stuff at a low level. Look at the ESP8266WebServer and related examples.

Is there a dropdown menu in there? I don't see one.

That is not how you compare strings in C/C++. You use strcmp().

Set your SSI and password. On the browser go to dropdown.local

/* ***********************************************************************************************
 * 
 * This sketch is an ESP8266 test of a WiFi interface 
 *
 * An mDNS server is started to handle the dropdown.local URL
 * 
 * A WiFi server is started to provide a user interface via a web browser
 * URL handlers are provided to
 *    Root page
 *    Unknown URL
 *
 * NOTES:
 *    I am not a web programmer. The web interface is a WEB101 level. If you think you can
 *    do better you may be right.
 *    
 * WARNING:
 *   This is proof of concept code - not for prime time. 
 *     Error handling is sparse 
 *     "Best practices" are not necessarily followed 
*********************************************************************************************** */

// Include the libraries we need

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>


const char* ssid = "your ssi";
const char* password = "your password";
const char* station_name = "dropdown"; // station name url will be dropdown.local

ESP8266WebServer server(80);

void handleRoot() {
  
  if (server.hasArg("carrier")){
    // Handle return parameter
    Serial.print("Carrier is ");
    Serial.println(server.arg("carrier"));
    // whatever else you want to do...
  }  
    // build html page
    String htmlPage =
        String("<!DOCTYPE HTML>\n")
        + "<html>\n"  
        + "<head>\n"
        +    "<title>Dropdown Demo</title>\n"
        +"</head>\n"
        + "<body>\n\n"
        + "<h1 style='text-align: center;'>Drop Down Demo</h1>\n"
        + " <form>\n"
        + "    <label for='carriers'>Choose a carrier:</label>\n"
        + "    <select name='carrier' id='carriers'>\n"
        + "       <option value='Verizon'>Verizon</option>\n"
        + "       <option value='T-mobile'>T-Mobile</option>\n"
        + "       <option value='AT&T'>AT&T</option>\n"
        + "    </select>\n"
        + "    <input type='submit' value='Submit'>\n"
        + "  </form>\n"         
        + "</body>\n"+
        "</html>\n";
    server.send(200, "text/html",htmlPage);
}

void handleNotFound() {

    // no clue what they sent
    String message = "File Not Found\n\n";
    message += "URI: ";
    message += server.uri();
    message += "\nMethod: ";
    message += (server.method() == HTTP_GET) ? "GET" : "POST";
    message += "\nArguments: ";
    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 setup() {
 
  Serial.begin(9600);

  // Set up WiFi network
  Serial.println();
  Serial.print("Creating Station ");
  Serial.println(station_name);

  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: ");
  Serial.println(WiFi.localIP());

  // Set up mDNS responder:
  if (!MDNS.begin(station_name)) {
    Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");

  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);
  
  // Start the server
  server.begin();
  Serial.println("Server started");
  
   // Add service to MDNS-SD
  MDNS.addService("http", "tcp", 80);

 }

void loop() {
  MDNS.update();
  server.handleClient(); 
 
  }
2 Likes

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