Communicating with Nextion from the P2
kg1
Posts: 164
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.
Comments
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.
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.
Mike
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:
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.
For those wondering, the my P1 object has a waitms() method so that I can move this right over to the P2.
@Electrodude Thank you for your comment. It works in SPIN2 100%
@iseries Thank you for your comment Mike.
@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).
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.
... 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.
For SPIN2 code:
Code supplied by JonnyMac: Modified for P2. Works 100%