What do I have to do to get Nextion.h to work

I am trying to use a Nextion library to talk to my nextion screen. It works fine in Arduino, but I can’t get it to work in XOD. Here’s my program:
image
This is the error message I get.
/tmp/ccpjPIXo.ltrans0.ltrans.o: In function xod::runTransaction()': <artificial>:(.text+0x2ec): undefined reference to xod::____emit_value::Nextion::Nextion(HardwareSerial&, unsigned long)

The Nextion library I am using is here: https://github.com/bborncr/nextion.
I have commented out the #define USE_SOFTWARE_SERIAL as I am using a hardware serial on my Mega (Serial1) to connect my screen.

What do I have to do to get this to work?

Thanks, Ian

Try this, I’m not sure if the SoftSerial definition goes there or between “global”

#pragma XOD require "https://github.com/bborncr/nextion"

{{#global}}
#include <Nection.h>
#include <SoftSerial.h>
{{/global}}

struct State {
};

SoftwareSerial nextion(2, 3);

{{GENERATE_CODE}}

Thanks @cesars. Only problem is that I modified the library. It has hard coded in the header to use software serial, but I want to use hardware serial. It can handle both but the default is software serial. (There’s a #define hard coded in the header, it really shouldn’t be there). Second issue is that the function unpacks the data returned from the screen into a string with spaces inserted between the characters. So if the screen returns 0x65 0x00 0x03 0x00 0xFF 0xFF 0xFF the output string from the function should be character “65 0 3 0 ffff ffff ffff”. I’d rather see the hex values (in case the other parameters get to two digits, I’d have to scan for spaces… could get ugly). So anyway I modified the library to not unpack the string, just return the hex string as is, and then I will unpack it when I receive it in the program. Works a treat in the Arduino version.
I added the pragma and it helpfully downloaded the library for me. I already had it in the ardulib folder but with the wrong name, so fixed all that. I tried it with the #include inside the global tags, it worked both ways. I guess it doesn’t really matter where in the program it ends up, it still works.

However it still won’t compile. It seems XOD doesn’t like the Nextion library. I get this error.

/tmp/ccTa6yAD.ltrans0.ltrans.o: In function `xod::runTransaction()':
<artificial>:(.text+0x440): undefined reference to `xod::____nextion_lib::Nextion::Nextion(HardwareSerial&, unsigned long)'
<artificial>:(.text+0x4f2): undefined reference to `xod::____nextion_lib::Nextion::init(char const*)'
<
artificial>:(.text+0x508): undefined reference to `xod::____nextion_lib::Nextion::listen(unsigned long)'
collect2: error: ld returned 1 exit status

Here’s the latest version.

#pragma XOD require "https://github.com/bborncr/nextion"
#include <Nextion.h>

struct State {
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    Nextion myNextion(Serial1, 9600);
    String newstring = "...";
    String mid = "message id: ";
    auto inValue = getValue<input_IN>(ctx);
//
    if (isSettingUp()) {
        Serial.begin(9600);
        myNextion.init();
    }
    
    String message = myNextion.listen(); //check for message
    
    if(message != "") { // if a message is received...
        Serial.println(mid += message.substring(1,2)); //...print it out
        for (int i = 0; i < message.length(); i++) {
            newstring += String(message[i], HEX);
            newstring += ",";
            Serial.println(message);
        }
    } else {
        emitValue<output_TRG>(ctx, true);
    }
//        dump(message, newstring); // this didn't work either

//    emitValue<output_OUT>(ctx, inValue);
//    emitValue<output_STR>(ctx, newstring);
//    newstring = "";
}

I’ve still got some more questions after I get past this problem. Mainly I’m not sure about executing the “Nextion myNextion(Serial1, 9600)”. Does it do this every time the node is executed? I’ve got the “myNextion.init()” in an isSettinpUp block so it will only execute once, but if I put the “Nextion myNextion(Serial1, 9600)” command in there too, I get errors later saynig myNextion is not defined in t this scope.

Thanks, Ian

try it

{{#global}} //edited ...added {  missing
#include <Nextion.h>
{{/global}}

As I said in my previous post

I’m sorry, using the translator I missed that part

No problem. I really value your help.

:wink: Ian

Hi @cesars,
Well, this is embarrassing. I’ve been trying it again, and found that in fact it doesn’t work without the “global” tags. I think what happened was in my original testing I had tried to comment the global tags by putting “//” in front of them, but I’m thinking maybe the pre-processor (or whatever processes those commands) doesn’t recognise the “//” string to mean a comment. It shows in the editor as a comment. It seems the pre-processor will process the global tags regardless of whether they are in a comment or not.
I finally got the clue because I was having a problem with the compiler not working. It just stopped. I started deleting code to try to identify what was causing the problem. I eventually found it was the global tags, as when I deleted them the compile worked, but generated an error. Then I noticed one of the global tags had one of the curly brackets missing! it was {#global}} instead of {{#global}}. Wasted hours on this! But when I deleted the tags completely the compile ran and generated a compile error. That’s when I found it wouldn’t work without the global tags, and generated those funny errors. The program compiles successfully only if the global tags are wrapped around the #include.
So, another lesson learned.

:wink: Ian

there I edited it so as not to confuse another, when I put it in code format a {
sorry…

Hardly your fault my friend.

:wink: Ian