Skip to content

webserial.print displays truncated value for long String value #110

@Tomgault1959

Description

@Tomgault1959

Hi,

I am migrating all my sketches from version 1.4 to version 2.0.8 and values ​​assigned to String are truncated on display or cause a system crash when the values ​​are very long. I create a testing sketch for this subject.

I use this library with ESP32

Version Webserial 2.0.8
ESPAsyncWebServer by Mathieu Carbou 3.6.0
AsyncTCP by Mathieu Carbou 3.3.2
ESP32 core 3.1.1
Compile with Arduino IDE 2.3.4 on Windows

For testing purpose, enter the command ? to display help information store in bufferPrint
string. The result will give a truncated value on webserial html view or a system crash depend how long the string is. The serial monitor display value correctly all the time. All work fine with on version 1.4

The V2.0.8 test sketch here :
V2.0.8.txt

#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP32)
#include <AsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>

AsyncWebServer server(80);

const char* ssid = ""; // Your WiFi SSID
const char* password = ""; // Your WiFi Password
const char* http_username = "admin"; //Login (must be set)
const char* http_password = "admin"; //Login password (must be set)

void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}

// Once connected, print IP
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! This is WebSerial demo. You can access webserial interface at http://" + WiFi.localIP().toString() + "/webserial");
});

// WebSerial is accessible at "/webserial" in browser
WebSerial.begin(&server);
// Set Authentication Credentials
//WebSerial.setAuthentication(http_username, http_password);

/* Attach Message Callback */
WebSerial.onMessage([&](uint8_t *data, size_t len) {
Serial.printf("Received %lu bytes from WebSerial: ", len);
Serial.write(data, len);
Serial.println();
WebSerial.println("Received Data...");
String d = "";
for(size_t i=0; i < len; i++){
d += char(data[i]);
}
WebSerial.println(d);

String bufferPrint = "";  
  
if (d == "?")
{
  bufferPrint += " R              : Redémarrage du service\n";
  bufferPrint += " N              : Liste des réseaux disponibles\n";
  bufferPrint += " L              : Lister les paramètres réseaux modifiables\n";
  bufferPrint += " E              : Effacer tous les paramètres de configuration réseau\n";
  bufferPrint += " T              : Afficher les données RTC\n";
  bufferPrint += " ST             : Synchroniser l'horloge RTC à partir du temps NTP\n";
  bufferPrint += " D              : Démarrer la génératrice à essence\n";
  bufferPrint += " P              : Arrêt de la génératrice à essence\n";
  bufferPrint += " BS             : Power OFF du robot qui gère la génératrice à esssence\n";
  bufferPrint += " BD             : Power ON du robot qui gère la génératrice à esssence\n";

//Uncomment next line cause system to crash and reboot
/*
bufferPrint += " RRSI : Force du signal Wi-Fi\n";
bufferPrint += " LOAD : LOAD - Lister la valeur du controleur 1=ON 0=OFF\n";
bufferPrint += " S : SLEEP MODE 0=Mode normal sans interruption 1=Deep Sleep activé\n";
bufferPrint += " HA : Heure avancée, valeur possible 0 ou 1. 0 = Heure normale, 1 = heure avancée\n";
bufferPrint += " GMT : Correction de l'heure UTC\n";
bufferPrint += " PING : Vérification de la connexion internet et du serveur chalet\n";
bufferPrint += " TEMP : Température maximale déclenchant l'arrêt de la génératrice\n\n";
bufferPrint += " setSSID : Modifier le nom du réseau Wi-Fi\n";
bufferPrint += " setPASS : Modifier le mot de passe du réseau Wi-Fi\n";
bufferPrint += " setLOAD : LOAD Assigner la valeur du controleur - 1=ON 0=OFF\n";
bufferPrint += " setDEBUG : DEBUG Assigner la valeur du controleur - 0=prd 1=debug\n";
bufferPrint += " setSLEEP : SLEEP MODE Assigner le mode d'économie d'énergie - 0=Mode normal 1=Deep Sleep\n";
bufferPrint += " setGMT : Modifier la correction de l'heure UTC\n";
bufferPrint += " setHA : Heure avancée, valeur possible 0 ou 1. 0 = Heure normale, 1 = heure avancée\n";
bufferPrint += " setTEMP : Modifier la température maximale permise avant l'arrêt de la génératrice\n";
bufferPrint += "\n- Toujours redémarrer le service pour relire les nouveaux paramètres de configuration réseau IP\n";
*/
WebSerial.println(bufferPrint);
Serial.println(bufferPrint);

}
});

// Start server
server.begin();
}

void loop() {
static unsigned long last_print_time = millis();

// Print every 20 seconds (non-blocking)
if ((unsigned long)(millis() - last_print_time) > 20000) {
WebSerial.print(F("IP address: "));
WebSerial.println(WiFi.localIP());
WebSerial.printf("Uptime: %lums\n", millis());
WebSerial.printf("Free heap: %u\n", ESP.getFreeHeap());
last_print_time = millis();
}

WebSerial.loop();
}

The V1.4 test sketch here :
V1.4.txt

Regards,

Jean

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions