Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Characters after "#" getting sliced in getParam #1457

Open
shivanimakxenia opened this issue Dec 30, 2024 · 0 comments
Open

Characters after "#" getting sliced in getParam #1457

shivanimakxenia opened this issue Dec 30, 2024 · 0 comments

Comments

@shivanimakxenia
Copy link

shivanimakxenia commented Dec 30, 2024

I got this code from an online forum (https://randomnerdtutorials.com/esp32-async-web-server-espasyncwebserver-library/) which I modified to get a string data when the button is pressed on the HTML Page.
When the button is pressed, the data which is expected is "Abc#123". But the response received is "Abc".

`// Import required libraries
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

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

const char* PARAM_INPUT_1 = "output";
const char* PARAM_INPUT_2 = "state";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(

<title>ESP Web Server</title> <style> html {font-family: Arial; display: inline-block; text-align: center;} h2 {font-size: 3.0rem;} p {font-size: 3.0rem;} body {max-width: 600px; margin:0px auto; padding-bottom: 25px;} .switch {position: relative; display: inline-block; width: 120px; height: 68px} .switch input {display: none} .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px} .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px} input:checked+.slider {background-color: #b30000} input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)} </style>

ESP Web Server

%BUTTONPLACEHOLDER% <script>function toggleCheckbox(element) { var xhr = new XMLHttpRequest(); if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=Abc#123", true); } else { xhr.open("GET", "/update?output="+element.id+"&state=Abc#1234", true); } xhr.send(); } </script> )rawliteral";

// Replaces placeholder with button section in your web page
String processor(const String& var){
//Serial.println(var);
if(var == "BUTTONPLACEHOLDER"){
String buttons = "";
buttons += "

Output - GPIO 2

<label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="2" " + outputState(2) + "><span class="slider">";
buttons += "

Output - GPIO 4

<label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="4" " + outputState(4) + "><span class="slider">";
buttons += "

Output - GPIO 33

<label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="33" " + outputState(33) + "><span class="slider">";
return buttons;
}
return String();
}

String outputState(int output){
if(digitalRead(output)){
return "checked";
}
else {
return "";
}
}

void setup(){
// Serial port for debugging purposes
Serial.begin(115200);

// pinMode(2, OUTPUT);
// digitalWrite(2, LOW);
// pinMode(4, OUTPUT);
// digitalWrite(4, LOW);
// pinMode(33, OUTPUT);
// digitalWrite(33, LOW);

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}

// Print ESP Local IP Address
Serial.println(WiFi.localIP());

// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});

// Send a GET request to <ESP_IP>/update?output=&state=
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
// GET input1 value on <ESP_IP>/update?output=&state=
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
// digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else {
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
Serial.print("SSID: ");
Serial.print(inputMessage1);
Serial.print(" - Pass: ");
Serial.println(inputMessage2);
request->send(200, "text/plain", "OK");
});

// Start server
server.begin();
}

void loop() {

}`
Screenshot 2024-12-30 at 11 28 54
ESP_ASYNC___testcode.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant