Weird string conversion issue

Hello. I am making a wifi display using a max7219 and an esp32. I've got the basic code working, but there is an issue that I don't understand. Only one character will show up and when I send an "A", ":" gets displayed, when I send a "b", ">" will show up and only one character will show up. Do you get what I'm trying to say?

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

const char *ssid = "TheSecretMustNotBeRevealed";
const char *password = "SameHere";

uint8_t connectedDevices = 0;

WebServer server(80);
MD_Parola mx = MD_Parola(MD_MAX72XX::FC16_HW, 23, 18, 5, 4);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  char temp[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf(temp, 400,

           "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>ESP32 Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1>Hello from ESP32!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <img src=\"/test.svg\" />\
  </body>\
</html>",

           hr, min % 60, sec % 60
          );
  server.send(200, "text/html", temp);
  digitalWrite(led, 0);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void) {
  pinMode(led, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA); //WIFI_STA for non-AP mode, WIFI_AP for AP mode
  WiFi.begin(ssid, password);
  Serial.println("");
  mx.begin();
  // AP MODE
  // while (!WiFi.softAP(ssid, password)) { //WiFi.status() != WL_CONNECTED
  //   delay(500);
  // }
  // non-AP MODE
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  //Serial.println(WiFi.softAPIP());
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  } 

  server.on("/", handleRoot);
  server.on("/test.svg", drawGraph);
  server.on("/ledon", ledOn);
  server.on("/ledoff", ledOff);
  server.on("/display", displayHandler);
  server.on("/msgpost", HTTP_POST, postHandler);
  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  if(connectedDevices != server.client().connected()) {
    Serial.println("A page was visited");
  }
  connectedDevices = server.client().connected();
  mx.displayAnimate();
  delay(2);//allow the cpu to switch to other tasks
}

void postHandler() {
  String postResult = server.arg("text");
  const char *txt = postResult.c_str();
  mx.displayText(txt, PA_LEFT, 50, 2000, PA_SCROLL_LEFT);
  server.sendHeader("Connection", "close");
  server.send(200, "text/html", "<h1>" + postResult + "</h1>");
}

void displayHandler() {
  server.send(200, "text/html", "<form action=\"/msgpost\" method=\"post\"><input type=\"text\" name=\"text\"><button type=\"submit\">Display</button></form>");
}

void ledOn() {
  digitalWrite(4, HIGH);
  server.send(200, "text/plain", "led is ON");
}

void ledOff() {
  digitalWrite(4, LOW);
  server.send(200, "text/plain", "led is OFF");
}

void drawGraph() {
  String out = "";
  char temp[100];
  out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
  out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
  out += "<g stroke=\"black\">\n";
  int y = rand() % 130;
  for (int x = 10; x < 390; x += 10) {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
    out += temp;
    y = y2;
  }
  out += "</g>\n</svg>\n";

  server.send(200, "image/svg+xml", out);
}

Welcome to the forum

Please post it here, using code tags when you do

Many members are reluctant to visit other sites to download code when you could make it much easier to examine if you follow the advice regarding posting code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

ok, corrected

Thanks. Note how much easier it makes to see and copy the code

Does that library expect message to be a String object? ("text/plain" is a character string, not a String).

yes, it does know about the String class

can you clarify where does something shows up and how you send stuff?

You enter /display to enter the message (textbox and button using POST) and then it gets displayed on the max7219 matrix led screen. EDIT: behaviour is not what I thought. Every time you press Display, a different thing shows up on the display even if you don't change the message. Still, only one character will show up. I will post a video right now

The Parola library does not save a copy of the text, just a pointer to it. When you call mx.displayAnimate(), the pointer is no longer valid. You have to copy the text yourself to a static buffer.

void postHandler() {
  String postResult = server.arg("text");
  static char postBuffer[10];
  postResult.toCharArray(postBuffer, sizeof(postBuffer));
  mx.displayText(postBuffer, PA_LEFT, 50, 2000, PA_SCROLL_LEFT);
  server.sendHeader("Connection", "close");
  server.send(200, "text/html", "<h1>" + postResult + "</h1>");
}

Or you could make PostResult static:

  static String postResult;
  postResult = server.arg("text");

Thank you for answering. I will edit this answer if anything doesn't work. If it does, I'll set your answer as the solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.