Conversion of data to print on my Nextion screen

Hello all

This is just a 'conversion question'. Don't really want to post the entire code, because its HUGE and full of peoples phone numbers etc. But, if we can't get a solution, then I will have to try and edit it down.

My code receives data over Websockets and displays the results in the serial monitor as follows:
I have routine that obtains the Websockets data...

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { // When a WebSocket message is received

This is then checked to see if its a disconnect request, connect request or text request. If it's a text request then....

  • case WStype_TEXT: // if new text data is received*
  • Serial.printf("[%u] get Text: %s\n", num, payload);*

Each set of data arriving uses the first 3 characters to determine what I want to do with the data. In this case here, 'TXT' means it's my text data (1000+ characters).
This prints perfectly once it's arrived on the serial debug screen:

  • Serial.println("Incoming .txt data....");*
  • Serial.printf("%s\n", (&payload[3])); // This prints the payload, minus the first three characters ('TXT')*

My stumbling point is how to convert this data to something I can print on the Nextion screen.
This short routine here prints to the Nextion fine:

  • NextionSerial.print("t0.txt="");*
  • NextionSerial.print("Test text - Hello!");*
  • NextionSerial.print(F(""")); NextionSerial.write(0xff); NextionSerial.write(0xff); NextionSerial.write(0xff); // Send the text to the Nextion text page*

Now I just need to convert that 'Test text - Hello!' to be the contents of my Websockets data

I just get constant conversion type errors.
Ideas?

phoneystark2020:
Now I just need to convert that 'Test text - Hello!' to be the contents of my Websockets data

not sure what you're looking for. seems that you know how to format text and data using printf

i have an project using an OLED. rather than embed OLED function calls throughout my code, i have a routine that prints string arguments. i stripped out the code that optionally displays the string to the serial monitor

// display up to 4 lines of text
void dispOled(
    const char  *s0,
    const char  *s1,
    const char  *s2,
    const char  *s3,
    bool         clr )
{
    if (clr)
        display.clear();

    display.setTextAlignment(TEXT_ALIGN_LEFT);

    if (s0)
        display.drawString(0, DISP_Y0,  s0);
    if (s1)
        display.drawString(0, DISP_Y1, s1);
    if (s2)
        display.drawString(0, DISP_Y2, s2);
    if (s3)
        display.drawString(0, DISP_Y3, s3);
    display.display();
}

this code formats strings for display.

        sprintf (s, "%2d:%02d   %d", timeSec / 60, timeSec % 60, dccAdr);
        sprintf (s0, " %3d Thr  %s%02d Rev",
            throttle, DIR_REV == dir ? "-" : " ", cutoff);

        sprintf (s1, "  %5s %5s", airBrkStr [brakeAir], indBrkStr [brakeInd]);
        sprintf (s2, "  %3d MPH  %3d DCC-Spd", int(mph), dccSpd);

        dispOled (   s, s0, s1, s2, CLR);

i use the following to append string data to a larger buffer that will generate a webpage

// display inputs
static void dispInputs (void)
{
    byte  encA = 10 * digitalRead (Enc_A_Dt) + digitalRead (Enc_A_Clk);
    byte  encB = 10 * digitalRead (Enc_B_Dt) + digitalRead (Enc_B_Clk);

    sprintf (s,  "%02d:%02d %s", timeSec / 60, timeSec % 60, name);
    sprintf (s0, "encA %02d, encB %02d", encA, encB);
    sprintf (s1, "%3d, %3d, %3d, %3d",
                            throttle, reverser, whistle, slope);
    if (button)
        sprintf (s2, "key %02d", button);
    else
        sprintf (s2, "key __");

    dispOled(s, s0, s1, s2, CLR);
}

sprintf() is useful for combining text and formating data values by simply managing string on various devices

Serial.printf("[%u] get Text: %s\n", num, payload);

NextionSerial.print(num);
NextionSerial.print(" get Text:");
NextionSerial.write(payload,length);
NextionSerial.print("\n");

Seems to do what you want

Thanks
Tried that... nope :confused:

I will keep trying

Hi phoneystark2020,

Your question assumes that someone trying to help would know what the data you are trying to send to a Nextion looks like, and I don't. Please post an example of the data including the variable type that holds it and some typical data (but not 1000 characters!!!), then I'll have a look.

It would help if your example included what you wanted it to look like on the Nextion.

What board are you using?

Hi Perry.

An ESP8266 is receiving a locally chosen demo.txt file from my Android phone. It sends that file as a string using websockets.
It's a standard NotePad text file.

The ESP8266 is printing that file to serial debug fine, including all the line breaks etc.

Serial.println("Incoming .txt data....");
Serial.printf("%s\n", (&payload[3])); // This prints the payload, minus the first three characters ('TXT')

I am guessing the %s is a string (or is it String?).... I can never remember.

I basically want to reproduce the text file as it was sent on the Nextion screen, I think it's a case of converting that string to something the Nextion can understand.

BUT... I have to say these Nextion screen are pretty terrible. The quality of the text is appalling.
I am looking at other touchscreen options with better support.

Eventually, this prototype needs moving over to a Mega with a Wifi module.

Hello phoneystark2020,

Sorry but I know nothing about web sockets or what a standard notepad text file would look like in an Arduino or ESP8266 environment, so I don't understand what you have. I do know about Nextion displays though. Without a proper understanding of what you are trying to print I can't help much.

The Nextion understands ASCII text sent over the serial port, along with the identifier for which text box you want it to go to, so you have to present to the serial port that data in ASCII format. If I understood what you are starting with I might be able to make it work. My usual way to do this is with sprintf. In sprintf % precedes the formatting characters. I would use sprintf to put the required string (not String) into a char array then print that to the serial port that the Nextion is connected to. Here's an example:

//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d", clock.hour, clock.minute, clock.second);
  Serial1.print(F("t1.txt=\""));
  Serial1.print(timestring);
  Serial1.print(F("\""));
  Serial1.print(F("\xFF\xFF\xFF"));         // Sends 0xff 0xff 0xff, which tell the Nextion that it has a complete string of data
}

Here's a different example without using sprintf:

//This displays the page number
void HMI_display_page(uint8_t page) {
 Serial1.print(F("t0.txt=\""));
 Serial1.print(F("This is page "));
 Serial1.print(page);
 Serial1.print(F("\""));
 Serial1.print(F("\xFF\xFF\xFF"));
}

I don't understand what you mean by the quality of the text being terrible, you have to generate the font you want to use so the quality of the text depends on the font you choose, nothing to do with the Nextion.

phoneystark2020:
The ESP8266 is printing that file to serial debug fine, including all the line breaks etc.

Serial.println("Incoming .txt data....");
Serial.printf("%s\n", (&payload[3])); // This prints the payload, minus the first three characters ('TXT')

you're using both Serial.println() and printf().

at least the esp32 supports printf() (no need to say Serial.printf())

while serial.println() will properly print either a c-string or a String, printf %s expects a c-string.
to print a String with printf, i believe you need to convert it to a c-string
printf (" %s\n", String.c_str())

If you use Serial.println() you will insert a line feed into the data, the Nextion won't like that and won't display it. You need Serial.print(), as per my examples.

Thanks guys. When I get home tonight I will have a play around and see what I can achieve

Not much luck. The Nextion screen doesn't seem to deal with '\n' in any fashion.

Can't seem to get it to force a new line.

Not all that impressed with these Nextion screens or their support/text abilities

Part 4 of this tutorial tells you how to use multi line text boxes: Using Nextion displays with Arduino - Displays - Arduino Forum

Well, after literally hours trying to make this horrible screen read my text, I have abandoned it.
I am sure it can be done, but it's not worth spending any more time on it.

A few good features on the Nextion, but some of its features (numbers, fonts etc) are pretty poor.

Ordered a different screen that apparently works far better. I'll try that!

Thanks for the advice.

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