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.