I have a ESP32 runing a webserver and access point on demand.
When i try to send data from the webpage to server, every thing is working fine if it's a number i send, but if i try to send data with string values, the esp32 is rebooting.
<script>
function saveMidi(){
toggleCheckbox(document.getElementById('mac1').value,document.getElementById('mac2').value,document.getElementById('mac3').value,document.getElementById('mac4').value,document.getElementById('mac5').value,document.getElementById('mac6').value,document.getElementById('pack').value);
}
function toggleCheckbox(mac1, mac2, mac3, mac4, mac5, mac6, beltpack){
var xhr = new XMLHttpRequest();
xhr.open("GET", "/update?mac1="+mac1+"&mac2="+mac2+"&mac3="+mac3+"&mac4="+mac4+"&mac5="+mac5+"&mac6="+mac6+"&beltpack="+beltpack, true);
xhr.send();
}
</script>
It all works if i change this inputMessage2 = request->getParam(PARAM_INPUT_2)->value(); tol inputMessage2 = request->getParam(PARAM_INPUT_2)->value().toInt;
you check for the presence of PARAM_INPUT_1 and PARAM_INPUT_2 and try to read other parameters like PARAM_INPUT_3 (and 4, 5, 6, 7) and apply to these a function calls (->value()) without knowing if the getParam() function returned an valid pointer or nullptr
if you don't check for the presence of the parameters, you would need to do something like
AsyncWebParameter * param3 = request->getParam(PARAM_INPUT_3);
if (param3 != nullptr) {
inputMessage3 = param3->value().toInt();
} else {
inputMessage3 = 0; // or whatever default value you want to have
}
You have to show more code.
I guess you have something like: const char* PARAM_INPUT_1 = "mac1"; somewhere.
If you include strings in the querystring: xhr.open("GET", "/updatemac1="+mac1+"&mac2="+mac2+... ); you have to ensure these contain no spaces or other illegal characters. Otherwise, wrap the whole thing into a JSON string.
I would love to show more code.
I'm trying to pass a mac address, so only 2 char.
The truth, is that i could'nt get a json string to work.
I have seen a lot of post on the interweb, many are using the same solution.
Depending on where you got ESPAsyncWebServer, you may have to fix one compiler error by making one variable a const . I'll attempt to fix that sometime.
I still don't see where or how, for example, PARAM_INPUT_1 is defined. However, it must be defined otherwise you'll get a compiler error.
From this name, I'm guessing your code is based on the Rui Santos model say at ESP32 Async Web Server – Control Outputs | Random Nerd Tutorials or some similar code. That model does not really scale up very well if you have a lot of HTML fields which you want to transfer back to the web server and then it's probably better to use the approach of using an HTML form and submitting it.
The way you appear to be transferring data with an explicit HTTP GET it probably doesn't matter but I don't believe such fields as below will be visible to the web server because there is no name attribute. <td><input class="input" type="tekst" id="mac1" align="center" value=%MAC1%></td>
I just tried your Async Web server, and that worked after i changed AsyncWebParameter* p = request->getParam(i); to const AsyncWebParameter* p = request->getParam(i);
And your example is doing, what i want.
I use a string processor to set values.
My script works fine when i'm only working with Int. but when i try to change it to run with String, it fails.
The base for my webserver is indeed from is from Rui Santos, but it's not from the script you link to, but that scriot seems to have error handling i could use.
I'm a big fan of Random Nerds.