[SOLVED] Trouble with HTTP GET

RudolfAtRTC
Posts: 3
Joined: Wed Dec 11, 2019 9:55 am

[SOLVED] Trouble with HTTP GET

Postby RudolfAtRTC » Wed Dec 11, 2019 10:12 am

I'm working on a WEB server at a ESP32 to control a 3-color-LED.
- The color picker ("colorpicker") from HTML5 is working
- I can input a color with an input field named "selectedcolor"
- But no GET request is done when the button (named "button") is pressed!
- I do'nt use a form
Can I get help to find my mistake?

Code: Untitled.cpp Select all

/*======================================================================
WEB-Server with NodeESP (ESP32) - Set colours for a 3-color-LED
----------------------------------------------------------------------
Base:
Rui Santos: Complete project details at http://randomnerdtutorials.com
----------------------------------------------------------------------
Author: Rudolf Schenke (aus Basis Rui Santos)
Stand: 10.12.2019
======================================================================*/

bool xDebug = true; // Debug when true

#include <WiFi.h> // Load Wi-Fi library

//---- WLAN-SSID and password - later from a file ----
const char* ssid = "...";
const char* password = "....";

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

String header; // Variable to store the HTTP request

// Auxiliar variables to store the current output state
String stateBlue = "Aus";
String stateGreen = "Aus";
String stateRed = "Aus";

// Assign output variables to GPIO pins
const int pinBlue = 0;
const int pinGreen = 2;
const int pinRed = 4;

void setup() {

Serial.begin(115200);
while(!Serial) delay(10);
Serial.println("WEB-Server zum Ein-Ausschalten der 3-Farben-LED");

// Initialize the output variables as outputs
pinMode(pinBlue, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinRed, OUTPUT);
// Set outputs to LOW
digitalWrite(pinBlue, HIGH);
digitalWrite(pinGreen, HIGH);
digitalWrite(pinRed, HIGH);

// 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
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (xDebug) 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();

//---- Hier muss die Auswertung rein! ----
Serial.println("Auswertung fehlt!");

// 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:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}</style>");
client.println("<title>HTML5 Color Picker Demonstration</title>");

client.println("<script language=\"javascript\">var actColor; function newColor(color)");
client.println("{ document.getElementById(\"selectedcolor\").value = color; ");
client.println("actColor = color;");
client.println("console.log(actColor); ");
client.println("document.getElementById(\"button\").href=\"/\" + actColor + \"&\"; }");

client.println("function setColor(color)");
client.println("{ document.getElementById(\"colorpicker\").value = color; ");
client.println("actColor = color;");
client.println("console.log(actColor); ");
client.println("document.getElementById(\"button\").href=\"/\" + actColor + \"&\"; }");

client.println("</script></head>");

// Web Page Body
client.println("<body><h1>NodeESP Web Server</h1>");
client.println("<h2>Color Picker f&uuml;r 3-Farben-LED</h2>");
//---- Form mit Farbpicker und Anzeige Farbwert ----
client.println("<p>Farbe ausw&auml;hlen: ");
client.println("<input id=\"colorpicker\" type=\"color\" ");
client.println("onchange=\"newColor(colorpicker.value);\"></p>");
client.println("<p>Ausgew&auml;hlt: <input id=\"selectedcolor\" type=\"text\" ");
client.println("onchange=\"setColor(selectedcolor.value);\"></p>");

client.println("<p><a href=\"#\" id=\"button\" role=\"button\">&Uuml;bernehmen</a></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("--------------------");
}
}
Last edited by RudolfAtRTC on Thu Dec 12, 2019 3:37 pm, edited 1 time in total.

Sprite
Espressif staff
Espressif staff
Posts: 10619
Joined: Thu Nov 26, 2015 4:08 am

Re: Trouble with HTTP GET

Postby Sprite » Thu Dec 12, 2019 3:29 am

> - I do'nt use a form
(also, no JS to do AJAX, looking at your code)

Then why do you expect a GET request to be generated?

RudolfAtRTC
Posts: 3
Joined: Wed Dec 11, 2019 9:55 am

Re: Trouble with HTTP GET

Postby RudolfAtRTC » Thu Dec 12, 2019 3:35 pm

Yes - you are right! I have changed to a "form" and it works now. Learning by doing....

Who is online

Users browsing this forum: ChatGPT-User and 0 guests