ESP-01 webserver issues

Hi guys,

I’ve got an ESP-01 module serving a webpage to display some data that it reads from the serial port, using the popular ESP8266WebServer.h library.
This data is stored in a local circular byte array buffer, converted to a string of char and then replaced in the html where the flag {{}} is.

<p>Buffer Contents</p>
<textarea class='border' readonly accept-charset="UTF-8" >
{{dataBuffer}}
</textarea>

This data isn’t always well formatted, sometimes even transmitted at unexpected baudrates, so random special chars were breaking the webpage.
I’ve added a previous function to parse the data where it looks up for special chars and replaces by its html equivalents e.g ‘&quot’.

This seems to avoid the page from being broken, however every now and then, this is displayed in the textarea:

-8" >
>
>
UTF-8" >
"UTF-8" >
"UTF-8" >
-8" >
�������N B��B����N
B�� ��N
����B ��������B��������€n ������

It seems like a char or group of chars have the ‘ability’ to go back and forth and extract parts of the html code (?) if this makes any sense at all.
What the hell might be happening here?
Many thanks.

Please post the full code.

sorted.

I was using a simple conversion like "char(buffer.readbyte [i])" to extract 'readable' data from the buffer but I wasn't counting on non-printable characters like carriage return or end-of-line.

Also, the conversion function was adding a lot of extra data, eventually overrunning the available free heap space.

My solution was to read the data using String(buffer.readbyte[i], HEX) and send all data to the server hex formatted. There, I've added a simple script to decode all the data into html compatible chars e.g. '&#38;#x22;' ensuring that everything will be well understood by the browser.

Don't use Strings, use c-style strings (null-terminated char arrays). Also, use [code][/code] or [nobbc][/nobbc] tags to prevent your indices [i] from turning your entire post in italics.

Thanks Pieter, sorry about that.

I've read some articles regarding the evils of String() objects but a senior member on this topic Understanding the evil String() - Programming Questions - Arduino Forum said that the bugs with memory leaks have been corrected, despite being (obviously) less efficient than c-strings.

My struggle with c-strings is that most of the times I just don't know in advance how long or short my strings will be, so I cant find a point in using it. Also String() offers many handy methods that otherwise would need to be (poorly..) implemented by myself..

Memory leaks are not the problem, it’s memory fragmentation you’re fighting against.

The length doesn’t matter, just create a single buffer in static memory, that’s longer than your longest string.