Shop Learn P1 Docs P2 Docs
Communicating with Nextion from the P2 — Parallax Forums

Communicating with Nextion from the P2

Using the Nextion Editor format: "t0.txt="Kevin"", 0

Here is how Arduino processes attribute changes:

For every component on Nextion, we can chance only attributes that are showed with GREEN color when Nextion is “running” with the following prototype .= . If attribute is text, the value must be inside <" "> quotes. Supposing we want to send to component t3 the text the command for Nextion is < t3.txt="HELLO">, from Arduino, we write < Serial.print("t3.txt=\"HELLO\"")>; When we type <\"> means that <"> is a character and it's going to print it over serial as character. Else <"> is a STRUCTURE. To change the font color t3, we write on Nextion: < t3.pco=BLUE > or < t3.pco=<31> > From Arduino:< Serial.print(“t3.pco=BLUE”); > or < Serial.print(“t3.pco=31”); >\

This aproach also has the same error:

Although the next attempt is compiled by the P2, the Nextion Editor does not like the syntact and returns an error message: "1Ä":

Although this last attempt is successful (using: hmi.fstr4(string("t0.txt=%c%s%c%3c"), $22, @command1, $22, $FF) I feel there must be a better way:

Here is the beginning display:

And the final screen:

Comments welcome.


  • ElectrodudeElectrodude Posts: 1,540
    edited 2022-11-10 03:22

    I'm afraid you just have to put the special characters in as numeric constants outside the string literal. At least on the P1, Spin doesn't support any kinds of escape characters within strings.

    command1      byte    "t0.txt=", $22, "Kevin", $22, 0
  • iseriesiseries Posts: 1,284

    If you want to program the P1/P2 in C I have library functions for Nextion to do that. Otherwise if your just trying to learn SPIN have at it.


  • JonnyMacJonnyMac Posts: 8,311
    edited 2022-11-10 15:59

    If you're using my serial library (jm_fullduplexserial.spin2) you can use \q for an embedded quote with any of the .fstrN() methods. Like this:

      hmi.fstr1(@"t0.txt=\qKevin\q%3c", $FF)

    I have asked Chip if he would allow the use of standard escape sequences in P2 strings -- he says he will, but it's not there yet.

    I have this simple method in a P1 object for the Nextion that I'm using in a new laser tag accessory. It will easily translate to Spin2.

    pub set_text(s_obj, p_str) : result
    '' Send string to text field of object defined by s_obj
    '' -- do not include '.txt' in object name
      fdsx.rxflush                                                  ' clear old messages
      fdsx.str(s_obj)                                               ' send object name
      fdsx.str(string(".txt=")                                      ' send .txt=
      fdsx.tx($22)                                                  ' quote
      fdsx.str(p_str)                                               ' new string
      fdsx.tx($22)                                                  ' quote
      terminate                                                     ' $FF $FF $FF
      txflush                                                       ' let message finish
      waitms(5)                                                     ' give nextion time to respond
      if (fdsx.available == 0)                                      ' no error?
        return true
        return false

    For those wondering, the my P1 object has a waitms() method so that I can move this right over to the P2.

  • kg1kg1 Posts: 151

    @Electrodude Thank you for your comment. It works in SPIN2 100%

  • kg1kg1 Posts: 151

    @iseries Thank you for your comment Mike.

  • kg1kg1 Posts: 151

    @JonnyMac Thank you for your fullduplexserial function. On line 318 I did see "double quote" but at the time I did not realize \q could be used "\q" i.e. embedded in a double quote. I have always been slow with grammar.

    Thank you for:
    "I have asked Chip if he would allow the use of standard escape sequences in P2 strings -- he says he will, but it's not there yet."
    This would be excellent.

    In the last picture above "\q" returns a 1A error message. If "\q" could be "\"" perhaps the Nextion would accept the command?

    Thank you for the set_text function. Here I have replaced hmi with term to check the building of the command.

    On the far right see the command. Next I changed term back to hmi. Although set_text compiles ok there is no change to the display.
    Next I will try Propeller Tool with Debug (first time).

  • JonnyMacJonnyMac Posts: 8,311
    edited 2022-11-11 02:14

    You can't have escape characters in the strings that replace %s in the control string -- this issue will be resolved by Chip adding standard escape sequences. I have a P1 project with Nextion on my desk, so I did it this way using jm_fullduplexserial to connect to the Nextion.

    hmi.fstr2(string("t0.txt=\q%s\q%3c"), @name, $FF)  

    ... where name is a simple string with no escape characters.

    Yes, I happen to have a screen in my project with a t0 object. I usually rename objects, but in my case, t0 is static -- I just used it in this case to test with your desired code.

    Note that my object method is somewhat verbose because the fdsx.spin object I use with it has a very small serial object that doesn't do formatting.

  • kg1kg1 Posts: 151

    For SPIN2 code:

    Code supplied by JonnyMac: Modified for P2. Works 100%

Sign In or Register to comment.