Start a new topic

ESP8266 Serial/Serial1 loop demo

@Grzegorz Fitrzyk


Hi, here is a little demo you can try on your ESP8266.


Hitting the start button will enter both the ESP and Nextion into an endless loop scenario, where both devices are running as fast as they can to complete the programmed task. That task is to read in a 4 byte (uint32_t) value sent from Nextion to ESP's primary RX pin (Serial). ESP assembles the 4 bytes, adds 1 to the result and sends straight back to Nextion on Serial1 (GPIO2) for display in a number component. Then ESP asks for the value to be sent back again over Serial RX, the cycle repeats......

You will be very surprised how fast the loop runs :) 

uint32_t retLong = 0;

void setup() {
    
  Serial.begin(115200);
  Serial1.begin(115200);  
  while (Serial.available()) Serial.read(); //Flush initialising data from Nextion

}

void loop() {
  
  if (Serial.available() == 4) {//If there are 4 bytes in the buffer....
    uint8_t a = Serial.read();//Read 1st byte.
    uint16_t b = Serial.read();//Read 2nd byte.
    uint32_t c = Serial.read();//Read 3rd byte.
    uint32_t d = Serial.read();//Read 4th byte.    
    retLong = a + (b << 8) + (c << 16) + (d << 24) + 1;//Re-assemble into a 32 bit Long and add 1.

    Serial1.print("n0.val=");
    Serial1.print(retLong);//Print the Long back to Nextion
    Serial1.print("\xFF\xFF\xFF");

    Serial1.print("click n0,1");//Ask Nextion to execute n0's touch press event (Send another 4 bytes)
    Serial1.print("\xFF\xFF\xFF");        
  }   
}

 

HMI
(25.9 KB)

1 person likes this idea

@indev2 


Thank you for this example but I'm afraid it's not working for me.

I assume that "serial" means standard serial/USB on my Wemos board - so at this moment I don't understand line:


while (Serial.available()) Serial.read(); //Flush initialising data from Nextion

I tried also to change it to Serial1 (which means for me that Wemos D4 is connected to Nextion RX (yellow wire) and Wemos RX is connected to Nextion TX (blue wire)).


So there is also imposible to receive anything from nextion in main loop:


if (Serial.available() == 4) {//If there are 4 bytes in the buffer....

 But I tried also to change it to Serial1 and finally sketch was looking like this:  

uint32_t retLong = 0;

void setup() {

  Serial.begin(115200);
  Serial1.begin(115200);
  while (Serial1.available()) Serial1.read(); //Flush initialising data from Nextion

}

void loop() {

  if (Serial1.available() == 4) {//If there are 4 bytes in the buffer....
    uint8_t a = Serial1.read();//Read 1st byte.
    uint16_t b = Serial1.read();//Read 2nd byte.
    uint32_t c = Serial1.read();//Read 3rd byte.
    uint32_t d = Serial1.read();//Read 4th byte.
    retLong = a + (b << 8) + (c << 16) + (d << 24) + 1;//Re-assemble into a 32 bit Long and add 1.

    Serial1.print("n0.val=");
    Serial1.print(retLong);//Print the Long back to Nextion
    Serial1.print("\xFF\xFF\xFF");

    Serial1.print("click n0,1");//Ask Nextion to execute n0's touch press event (Send another 4 bytes)
    Serial1.print("\xFF\xFF\xFF");
  }
}

  And there was no reaction for clicking button.

Maybe I'm too stupid for this? I don't get it.

Hi Grzegorz,


The sketch should work as I posted originally (see revised below). No need to change Serial or Serial1.

Do please make sure that Nextion baud rate is set to 115200. 

Nextion TX (Blue wire) should be connected to your Wemos RX pin (Serial RX).

This line just clears initialising data on that wire when Nextion boots up. 

while (Serial.available()) Serial.read();

Nextion RX (Yellow wire) needs connecting to your Wemos D4 (GPIO2 Serial1 TX) then the sketch is self explanatory. Wemos reads Nextion on Serial, Wemos sends to Nextion on Serial1.


Here is a slightly modified version that gives a little time before we clear down Serial.read() prior to starting the loop with the button on the display. 

uint32_t retLong = 0;

void setup() {

  Serial.begin(115200);
  uint32_t startTime = millis();
  while (millis() - startTime < 3000) {
    yield();
  }
  Serial1.begin(115200);
  while (Serial.available()) Serial.read(); //Flush initialising data from Nextion
}

void loop() {

  if (Serial.available() == 4) {//If there are 4 bytes in the buffer....
    uint8_t a = Serial.read();//Read 1st byte.
    uint16_t b = Serial.read();//Read 2nd byte.
    uint32_t c = Serial.read();//Read 3rd byte.
    uint32_t d = Serial.read();//Read 4th byte.
    retLong = a + (b << 8) + (c << 16) + (d << 24) + 1;//Re-assemble into a 32 bit Long and add 1.

    Serial1.print("n0.val=");
    Serial1.print(retLong);//Print the Long back to Nextion
    Serial1.print("\xFF\xFF\xFF");

    Serial1.print("click n0,1");//Ask Nextion to execute n0's touch press event (Send another 4 bytes)
    Serial1.print("\xFF\xFF\xFF");
  }
} 

Regards


Steve.

Short video clip.....

mp4

1 person likes this

@indev2


Hi Steve! Thank you for explanation - and now it's working!

Quite fast it is :) And now I'm wonder how to use it.

I'm using standard Nextion library (0.7.0 Latest stable version) and have no idea how to configure NexConfig.h to use Serial & Serial1 instead of Serial and SoftwareSerial.

I think that there will be problem with it because of this common RX line.

So this trick is interesting - but need a lot of work for serious project.


Best regards, Greg

@Greg


Pleased you have it working :)

As it is the sketch has no useful purpose, it was just to demonstrate an unusual configuration. Also to give you an idea of the speed at which ESP and Nextion can swap data. I think ~100 cycles per second in this case is quite good, and usually much more than would be required in an average project.

Data integrity is also good, the sketch has run for days in tests without error.

Your challenge now as you say, is to use some of what you have learned in a practical project.

I have shown how you can construct Nextion commands without using the Itead library, so both methods are now at your disposal if you can figure out the library :)

Have fun with your projects.


Regards


Steve.


1 person likes this
Login or Signup to post a comment