Start a new topic

Arduino Mega, no text on t0

 Hi,


 anything I do, I cannot read text on t0  Nextion,

if I change serial port to Serial 1 and there I have usb 2 RS232 adapter I can read the text with putty, so is the "t0" problem, I not declare it well or something missing, please some tips or help, to finish my project


 

#include "Nextion.h"


NexText t0 = NexText(0, 1, "t0");
char buffer[20] = {0};

const byte numChars = 18;
char receivedChars[numChars];

boolean newData = false;


void setup() {
  // initialize serial ports:
  Serial.begin(9600);   // USB 
  Serial1.begin(9600);
  Serial2.begin(9600);  //Nextion display
//  Serial3.begin(9600);

  nexInit();

  t0.setText(buffer);
  dbSerialPrintln("setup done");
  t0.getText(buffer, sizeof(buffer));
}

void loop() {

  
    recvWithStartEndMarkers();
    showNewData();
}
void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx =0;
    char startMarker ='FF FF FF 32';
    char endMarker ='FD';
    char rc;
 
 // if (Serial.available() > 0) {
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars;
 }
}
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
 }
}

        else if (rc == startMarker) {
            recvInProgress = true;         
    }
  }
}



void showNewData() {

  

    if (newData == true) {
        Serial2.print("FX =");
        Serial2.print(receivedChars);
        Serial2.print("Mhz");
        
        newData = false;
    
    }



        //   read from port 1, send to port 0:
     if (Serial2.available())
 {
    int inByte = Serial2.read();
         Serial.write(inByte);

       
    }

}

 Thank you !


ok,
  it is someone able to help me with a simple sketch example for Arduino (Mega) ?

need to send a simple text or numbers, from serial (or serial2,3) to serial1 (where Nextion is attached)  ,

page 0, t0 and Itead nextion library

 all I do, is not print on t0, I check serial with RS232 analyzer, and text is send it correct;

please a sketch tested..  

Hey Yo2ldk


Sorry I don't do Arduino.  But, start with endmarkers as FF FF FF 32 is invalid.

FF FF FF is a data terminator.  So it is looking like you are waiting for the termination (which should in theory invalidate what ever valid data was sent before the termination), and then expecting the next character to be 0x32, or decimal 50 -- a number 2.


I don't think there is such a scenario where the data starts with 0x32.


The Itead Nextion Library for the Arduino is configured for the Mega.  It has examples.

Has this library not worked for you, or maybe you haven't seen where it is located?

When you upload your HMI to the Nextion: is it at 9600 baud?  If it is higher, then the serial will need to match your Serial1.begin statement.  My Nextion is set at 115200 baud it would need Serial.begin(115200);


If this is not set to match your Mega wouldn't respond.

Please check the examples from the Itead Library located here:

https://github.com/itead/ITEADLIB_Arduino_Nextion


Thank you Peter for reply, yes, I need 9600 bauds. for serial.

  I read and try it all examples,  comp text is in fact for button more than text !

forget for all characters from my example is for an apart device. and only for test

 I need only that:
 if I send "TEST" letters via serial2 on Arduino mega module, to serial1 for Nextion, I want to see that word "TEST" on t0. That's all .

If it is somewhere that example, I miss it (but I don't think) 
 Arduino board, like I say, is doing his job : on output serial1 TX I can see word TEST with analyzer, just not going on t0 :(


Yo2ldk


I apologize again that I just don't do Arduino

(I am a pascal programmer, MikroPasca;, Kylix 3, Delphi 2009)

The Arduino Library - well, I just don't use it.


But reading through allot of the library code has me wondering.

Their examples have used a Callback, this is heavily intertwined with receiving to ensure the receive buffer doesn't overflow and then it appears after the receive buffer is empty then take its turn at sending out to the Nextion device.  The callback for every component is there to ensure this sending/receiving is not broken.  Your example is not using it, and I hate to circumvent one practise to break another.


But here is what I know from a raw serial point of view.

If I push 'get t0.txt' and the data terminator 0xFF 0xFF 0xFF across the serial line the Nextion device has received the command to send back the contents of t0.txt - and will do so upon receiving the data terminator.  Everything the Nextion sends back is going to _also_ end with that data terminator of three bytes of 0xFF.  The data terminator can also be represented by characters ÿÿÿ as ÿ is the character for decimal 255 / hex 0xFF in iso-8859-1.  So in a shortcut:  sending 'get t0.txtÿÿÿ' accomplishes the required according to the Nextion Instruction Set.


Looking deeper into the Arduino Library .. the sendCommand adds this data terminator after sending out the cmd string parameter.  So sendCommand("get t0.txt") would also accomplish the same.  This means that to set the t0.txt component on the Nextion sendCommand("t0.txt=123") will set t0.txt to "123".


And looking deeper yet - this is what the getText and setText are actually doing.

So I have to go back to the text component example.


I have no way of testing all this, please excuse me.


I have to think that your setText and getText actually worked with empty buffer. in the setup function.

Perhaps it did not refresh in which case sendCommand("ref t0") may do the task of bringing the t0 text component up-to-date.


I think your real culprit is your start and end markers.


Maybe something I said may be useful, sorry.






1 person likes this

These are merely things for you to consider why your code may not work as expected.


In your showNewData() routine.

  you are printing to serial2

  this is the Nextion port as defined above.

  I certainly think that "FX =" is going to fail as a Nextion cmd.


Likewise are you intending to capture input from the debug serial line in recvWithStartEndMarkers?




1 person likes this

Patrick,


  You do not  apologize, I Thank you for you spend it time to check all and for your desire to help !

The hmi page is simple, page0, t0 id 1. About he code, you have right, I take a bit from here and there, that because my beginner and limited knowledge in  Arduino language; but this is do what I need, except I cannot see it on t0  :)
 I attach again code and capture from RS232 to see is working, and it is signal output on serial for display.

the letters is only for example so, not get it literally , 

Also, i have another simple code where I try it, but still nothing. 

  All I need is a simple working code, then I see it how I do it. I try more  with your advice's.
 About Nextion libraries, I found at least 4 different types, modified with others needs, and many hmi and ino example (without text) where is not specified  witch is used, now I see it more clear from ino code.

 In the 111.ino file, if inside "" from myNextion.setComponentText("t0", "") put 1234, then I can see on t0 1234, but what it come from serial, no.

ino
(556 Bytes)
ino

The four different libraries is going to be an issue - I have read in the forum here of people having problems with multiple libraries.


If the 111.ino is functioning to transfer text to the t0 component - it could be the base you build up from.


I noticed #include <SoftwareSerial.h> in 111.ino that is not in the other one.

I am wondering if you can alter serialtexttest.ino to include the softserial. and see if it helps.


1 person likes this

I'll jump in if I may.


Seems like a simple project, I'll write up some Arduino code shortly, and I won't be using any libraries

there really is no need.


If I read this right, we just need a serial 'text' capture on the Arduino (Incoming from a treminal).

Then output the same to the Nextion t0 component.


Simples, I'll have my evening meal and be right on it.



1 person likes this

Yo2ldk,


Whoa!  Neither of your code files will work for what you want.  The SERIALTESTTEXT.ini file won't work in any environment.

It's obvious you need much more study into the C language before you go further into the Arduino world.


First, learn about C chars and strings.  That will save you tons of trouble.

Second, don't use ANY of the Nextion libraries for Arduino.  They all are poorly written.

Third, I created and attached a file that will give you exactly what you want (in this limited text capacity).


Notes:

1) The attached file will handle ONLY text data from the Nextion.  It will NOT handle numeric data nor any other control codes.  You will need to tailor the code to your specific needs if you want anything more than to simply read/write to the txt field of an element.

2) The Nextion element can be either a string component or string variable.

3) The elements MUST be large enough to hold the data you want to store in it or the Nextion will give you problems.


Cheers

ino
(2.69 KB)

1 person likes this

thanks Steve - you know me and Arduino.


He has a really nice Amateur Radio Frequency/Band project going on with the Nextion

Somewhere between several libraries and I have none of the same hardware, things have gone awry.



Try this


  

#include <SoftwareSerial.h>


SoftwareSerial NexSerial(10, 11); // Nextion TX->10  RX->11.

String NextComm;
byte ter [4] = {0x22, 0xff, 0xff, 0xff};//Terminating characters

void printNextion() {
    
	NexSerial.print("t0.txt=\"");
	NexSerial.print(NextComm);
	NexSerial.write(ter,4);
  Serial.print("To Nextion -> ");
  Serial.print("t0.txt=\"");
  Serial.print(NextComm);
  Serial.write(ter,4);
  Serial.println();
	}

void setup() {

    Serial.begin(9600);
    NexSerial.begin(9600);
     
    Serial.println("Type some text and press Enter");      
}

void loop() { 

  while (Serial.available()) {
    char inChar = (char)Serial.read();
    NextComm += inChar;
    if (inChar == '\n') {//Put your own end character here if not using newline. Note \r\n will also be sent to Nextion     
      printNextion();
      NextComm = "";
    }
  }
}

  


1 person likes this

Try this


 

#include <SoftwareSerial.h>


SoftwareSerial NexSerial(10, 11); // Nextion TX->10  RX->11.

String NextComm;
byte ter [4] = {0x22, 0xff, 0xff, 0xff};//Terminating characters

void printNextion() {
    
	NexSerial.print("t0.txt=\"");
	NexSerial.print(NextComm);
	NexSerial.write(ter,4);
  Serial.print("To Nextion -> ");
  Serial.print("t0.txt=\"");
  Serial.print(NextComm);
  Serial.write(ter,4);
  Serial.println();
	}

void setup() {

    Serial.begin(9600);
    NexSerial.begin(9600);
     
    Serial.println("Type some text and press Enter");      
}

void loop() { 

  while (Serial.available()) {
    char inChar = (char)Serial.read();
    NextComm += inChar;
    if (inChar == '\n') {//Put your own end character here if not using newline. Note \r\n will also be sent to Nextion     
      printNextion();
      NextComm = "";
    }
  }
}

 


1 person likes this
Login or Signup to post a comment