*Figured Out*Where do I place serial baud rate within this code?

So I am in the starting stages of a Nextion HMI project and after discussion with the Nextion people, this example code was given. I added in the Speed++ section in the loop to test the screen and it functions although after about 10 seconds or so, it starts tripping over itself.

#include <Arduino.h>
#include "Nextion.h"


// declare Nextion MCU side variables
uint8_t values[9] = {0,0,0,0,0,0,0,0,0};
  // in order battery, engine, left_turn, right_turn, hibeams,
  // water, oil_pressure, fuel, and volts.
uint16_t speed, rpm = 0;
uint8_t ndt[3] = {255,255,255};
uint32_t next;
// OTHER MCU SIDE VARIABLES

// we expect no touch events - there is no listen_list
NexTouch **nex_listen_list = {
  NULL
};

void update_status() {
  uint8_t x;
  for(x=0;x<9;x++) {
    nexSerial.print("bt"); 
    nexSerial.print(x); 
    nexSerial.print(".val="); 
    nexSerial.print(values[x]); 
    nexSerial.write(ndt,3); 
  } 
  nexSerial.print("n0.val="); 
  nexSerial.print(speed);
  nexSerial.write(ndt,3);
  nexSerial.print("va0.val=");
  nexSerial.print(rpm);
  nexSerial.write(ndt,3);
  sendCommand("click m0,0");
}

// OTHER MCU SIDE FUNCTIONS

void setup(void) { 
  nexInit();
  // OTHER SETUP
  next = millis(); 
}

void loop(void) {
  nexLoop(nex_listen_list);
  if (millis() <= next) {
    next = millis() + 200;
    update_status();
  }
  // OTHER LOOP CODE
  
  speed++;  // Increase the value of the variable by 1.
  if(speed == 100){  // If the variable reach 201...
    speed = 0;  // Set the variable to 0 so it starts over again.
  }
}

I have other code for the Nextion where you can set higher baud rates, but the code is written in what I would consider to be more traditional layout as shown below

void setup() {  // Put your setup code here, to run once:
  
  Serial.begin(9600);  // Start serial comunication at baud=9600. For Arduino mega you would have to add a
                       // number (example: "Serial1.begin(9600);").
                       // If you use an Arduino mega, you have to also edit everything on this sketch that
                       // says "Serial" and replace it with "Serial1" (or whatever number you are using).




  
  // I am going to change the Serial baud to a faster rate (optional):
  //delay(500);  // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
  Serial.print("baud=115200");  // Set new baud rate of nextion to 38400, but is temporal. Next time nextion is
                               // power on, it will retore to default baud of 9600.
                               // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
                               // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
                               // If you change default, everytime the nextion is power on is going to have that baud rate.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to nextion.
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.end();  // End the serial comunication of baud=9600.

  Serial.begin(115200);  // Start serial comunication at baud=115200.

Ive been trying different methods to past this in to the setup area and while it will compile, it will not work on the screen. Im not all that advanced in programming, so the layout of the first code set is a bit different than Im used to and could use a little help. BTW, the Nextion people arent all that excited to help on the Arduino side of things so I'd rather not bother them unless it is last resort.

1 Like

It looks like you may need

nexSerial.begin(your baud rate);

in setup(), but I know nothing about Nextion

What do the "Nextion people" say about it ?

UKHeliBob:
It looks like you may need

nexSerial.begin(your baud rate);

in setup(), but I know nothing about Nextion

What do the "Nextion people" say about it ?

As I said above, they aren't what I'd call enthusiastic to provide help with anything other than their screen itself and tell you that Arduino (or any other MCU) code is on you, not them. They are actually pretty snippy when you ask about their stuff as well.

Ill see if doing it in nexSerial will work and get back.

Doing this compiles, unfortuantely, it doesnt allow the code to work on the screen

#include <Arduino.h>
#include "Nextion.h"


// declare Nextion MCU side variables
uint8_t values[9] = {0,0,0,0,0,0,0,0,0};
  // in order battery, engine, left_turn, right_turn, hibeams,
  // water, oil_pressure, fuel, and volts.
uint16_t speed, rpm = 0;
uint8_t ndt[3] = {255,255,255};
uint32_t next;
// OTHER MCU SIDE VARIABLES

// we expect no touch events - there is no listen_list
NexTouch **nex_listen_list = {
  NULL
};

void update_status() {
  uint8_t x;
  for(x=0;x<9;x++) {
    nexSerial.print("bt"); 
    nexSerial.print(x); 
    nexSerial.print(".val="); 
    nexSerial.print(values[x]); 
    nexSerial.write(ndt,3); 
  } 
  nexSerial.print("n0.val="); 
  nexSerial.print(speed);
  nexSerial.write(ndt,3);
  nexSerial.print("va0.val=");
  nexSerial.print(rpm);
  nexSerial.write(ndt,3);
  sendCommand("click m0,0");
}

// OTHER MCU SIDE FUNCTIONS

void setup() { 
  nexSerial.begin(9600);
  nexSerial.print("baud=115200");
  nexSerial.write(ndt,3);
  nexSerial.end();
  nexSerial.begin(115200);
  
  
  nexInit();
  // OTHER SETUP
  next = millis(); 
}

void loop(void) {
  nexLoop(nex_listen_list);
  if (millis() <= next) {
    next = millis() + 200;
    update_status();
  }
  // OTHER LOOP CODE
  
  speed++;  // Increase the value of the variable by 1.
  if(speed == 100){  // If the variable reach 201...
    speed = 0;  // Set the variable to 0 so it starts over again.
  }
}

I have a feeling it has to do with the void update_status() portion being ahead of setup, then again, maybe not.

mattyb0:
As I said above, they aren't what I'd call enthusiastic to provide help with anything other than their screen itself and tell you that Arduino (or any other MCU) code is on you, not them.

Sounds like a product to avoid then.

They sell the product, make money, and expect free support forums and volunteers to support their products.

Had I known, it wouldve changed my mind about buying it.

using nexSerial didnt do it in any form so I posted to them and will see what they say.

Well figured it out.

Apparently, the Nextion library is not set up to allow baud rate initialization in the setup, it needs to have the default baud edited inside the Config file of the library and then preinitialize the screen with the same baud on it seperately.

That means if you are using their library for more than one project, you need to either rename the library in all its possible baud rates with the permanent modifications, or you must change it when you compile different projects with different baud rates.

That is a horrible way to do it IMO, but they think it works great.