How to get data to and from Nextion display

Hello!

I am sure that most probably I am making some sort of basic newbie mistake, so I would appreciate, if somebody could point me in the correct direction.
The thing is that I cannot get the Nextion display to communicate with Arduino and do something.
I tried this example and I had the clock ticking that was posted in these forums: Clock example

Here is my code for Arduino:

#include <doxygen.h>
#include <NexButton.h>
#include <NexCheckbox.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexGpio.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexRadio.h>
#include <NexRtc.h>
#include <NexScrolltext.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>
#include <NexWaveform.h>


#include <Wire.h>
#include <AccelStepper.h>
AccelStepper stepper(1, 2, 3); // pin 2 = step, pin 3 = direction


#include <EEPROM.h>
#define EEPROM_SIZE 20

#define nexSerial Serial1

const int IN_home_switch = 4; // Pin 4 connected to inductive Homing Switch

// Stepper Travel Variables
int scale = 189;


int profila_augstums;
int profila_garums;

int pos_cmd;
long mot_pos_cmd;
long mot_pos_scaled;
int home_pos;
long home_pos_scaled;
int min_limit;
int max_limit;
int acceleration;
int velocity;
int home_search_vel;
int home_latch_vel;


int STAT_homing = 0;

bool STAT_test = 0;
bool go_pos = 0;
bool go_home = 0;
bool done_homing = 0;

////////
//Lapa0 - sākums
///////

NexPage p0 = NexPage(0, 0, "page0"); //sākuma ekrans
NexButton p0_b0 = NexButton(0, 3, "b0"); //Iestatijumi
NexButton p0_b1 = NexButton(0, 4, "b1"); //Meklet majas poz.
NexButton p0_b2 = NexButton(0, 5, "b2"); //Iet uz majas poz.
NexButton p0_b3 = NexButton(0, 8, "b3"); //Braukt (zāģi uz uzdoto pozīciju)

NexText p0_t0 = NexText(0, 1, "t0"); //Profila garums
NexText p0_t1 = NexText(0, 6, "t1"); //Profila augstums
NexText p0_t2 = NexText(0, 8, "t2"); //Zaga pozicija

NexNumber p0_n0 = NexNumber(0, 2, "n0"); //uzdotais profila garums, milimetros
NexNumber p0_n1 = NexNumber(0, 7, "n1"); //iestatītais profila augstums
NexNumber p0_n2 = NexNumber(0, 9, "n2"); //aprēķinātā zāģa pozīcija

////////
//Lapa1 - iestatījumi
///////

NexPage p1 = NexPage(1, 0, "page1");
NexButton p1_b0 = NexButton(1, 1, "b0"); //Atpakal
NexButton p1_b1 = NexButton(1, 14, "b1"); //Saglabat

NexText p1_t0 = NexText(1, 2, "t0"); //Min limits
NexText p1_t1 = NexText(1, 3, "t1"); //Max limits
NexText p1_t2 = NexText(1, 4, "t2"); //Brauks. atrums
NexText p1_t3 = NexText(1, 5, "t3"); //Majas mekl. atrums 1 (ātrums ar kādu virzās uz mājas slēdzi)
NexText p1_t4 = NexText(1, 6, "t4"); //Majas mekl. atrums 2 (ātrums ar kādu virzās nost no mājas slēdža)
NexText p1_t5 = NexText(1, 7, "t5"); //Paatrinajums
NexText p1_t6 = NexText(1, 15, "t6"); //Majas poz.

NexNumber p1_n0 = NexNumber(1, 8, "n0"); //mazākā iespējamā zāģa pozīcija
NexNumber p1_n1 = NexNumber(1, 9, "n1"); //maksimālā zāģa pozīcija
NexNumber p1_n2 = NexNumber(1, 10, "n2"); //zāģa pārvietojuma ātrums
NexNumber p1_n3 = NexNumber(1, 11, "n3"); //mājas pozīcijas meklēšanas ātrums (virzās uz mājas slēdzi)
NexNumber p1_n4 = NexNumber(1, 12, "n4"); //ātrums, ar kādu virzās nost no mājas slēdža
NexNumber p1_n5 = NexNumber(1, 13, "n5"); //kustības paātrinājums
NexNumber p1_n6 = NexNumber(1, 16, "n6"); //mājas pozīcija

NexDSButton p1_bt0 = NexDSButton(1, 17, "bt0"); //testa poga, lai pārbaudītu komunikāciju starp ekrānu un Arduino

NexTouch * nex_listen_list[] = {
  &p0_b0, &p0_b1, &p0_b2, &p0_b3, &p1_b0, &p1_b1, &p1_bt0, NULL
};



// "Iestatijumi" poga sakuma ekranam
void p0_b0_Release(void *ptr) {
  digitalWrite(LED_BUILTIN, LOW);
}

// "Meklet majas poz." poga sakuma ekranam
void p0_b1_Release(void *ptr) {

  //Nolasa Home pogas nospiesanu
   while (digitalRead(IN_home_switch) != LOW) //Iet uz maju poziciju pirmo reizi
   {    
     mot_pos_scaled = mot_pos_scaled--;    
     stepper.move(mot_pos_scaled);
     stepper.setAcceleration(acceleration);
     stepper.setMaxSpeed(home_search_vel);
     stepper.run();
   } 
 
   while (digitalRead(IN_home_switch) != HIGH) // Attalinas no maju pozicijas
   {     
     mot_pos_scaled = mot_pos_scaled++;    
     stepper.move(mot_pos_scaled);
     stepper.setAcceleration(acceleration);
     stepper.setMaxSpeed(home_latch_vel);
     stepper.run();
   }

   while (digitalRead(IN_home_switch) != LOW) //Iet uz maju poziciju otro reizi
   {    
     mot_pos_scaled = mot_pos_scaled--;      
     stepper.move(mot_pos_scaled);
     stepper.setAcceleration(acceleration);
     stepper.setMaxSpeed(home_latch_vel);
     stepper.run();
   }
   stepper.setCurrentPosition(home_pos_scaled);  //Nonullee motora poziciju
   mot_pos_scaled = home_pos_scaled;              //

}


// "Iet uz majas poz." poga sakuma ekranam
void p0_b2_Release(void *ptr) {
     home_pos_scaled = home_pos * scale;
     stepper.move(home_pos_scaled);
     stepper.setAcceleration(acceleration);
     stepper.setMaxSpeed(velocity);
     stepper.run();
}

// "Braukt" poga sakuma ekranam
void p0_b3_Release(void *ptr) {
//     p1_n0.getValue(profila_garums);
//     p1_n1.getValue(profila_augstums);

//     pos_cmd = profila_garums - 2 * profila_augstums;

//     if (pos_cmd < min_limit) {mot_pos_cmd = min_limit;}
//     else if (pos_cmd > max_limit) {mot_pos_cmd = max_limit;}
//     else {mot_pos_cmd = pos_cmd;}     

//     p0_n2.setValue(mot_pos_cmd);
     
     mot_pos_scaled = mot_pos_cmd * scale;
     
     stepper.move(mot_pos_scaled);
     stepper.setAcceleration(acceleration);
     stepper.setMaxSpeed(velocity);
     stepper.run();
}


// "Atpakal" poga Iestatijumu ekranam
void p1_b0_Release(void *ptr) {
  
}

// "Saglabat" poga Iestatijumu ekranam
void p1_b1_Release(void *ptr) {
  p1_n0.getValue(min_limit);
  p1_n1.getValue(max_limit);
  p1_n2.getValue(velocity);
  p1_n3.getValue(home_search_vel);
  p1_n4.getValue(home_latch_vel);
  p1_n5.getValue(acceleration);
  p1_n6.getValue(home_pos);
  EEPROM.write(0, min_limit);
  EEPROM.write(1, max_limit);
  EEPROM.write(2, velocity);
  EEPROM.write(3, home_search_vel);
  EEPROM.write(4, home_latch_vel);
  EEPROM.write(5, acceleration);
  EEPROM.write(6, home_pos);
  //  EEPROM.commit();
}


void p1_bt0_Press(void *ptr) {
  STAT_test = 1;
}

void p1_bt0_Release(void *ptr) {
  STAT_test = 0;
}



void setup() {

    // initialize the serial communications:
    Serial1.begin(9600);  
    
    nexInit();
  p0_b0.attachPop(p0_b0_Release, &p0_b0);
  p0_b1.attachPop(p0_b1_Release, &p0_b1);
  p0_b2.attachPop(p0_b2_Release, &p0_b2);
  p0_b3.attachPop(p0_b3_Release, &p0_b3);
  p1_b0.attachPop(p1_b0_Release, &p1_b0);
  p1_b1.attachPop(p1_b1_Release, &p1_b1);
  p1_bt0.attachPop(p1_bt0_Press, &p1_bt0);
  p1_bt0.attachPop(p1_bt0_Release, &p1_bt0);

  pinMode(IN_home_switch, INPUT_PULLUP);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

//  digitalWrite(2,LOW);
//  digitalWrite(3,HIGH);

     stepper.setMaxSpeed(velocity);        //Motora parvietosana
     stepper.setAcceleration(acceleration);
     
  delay(50);  // Wait for EasyDriver wake up

  min_limit = EEPROM.read(0);
  max_limit = EEPROM.read(1);
  velocity = EEPROM.read(2);
  home_search_vel = EEPROM.read(3);
  home_latch_vel = EEPROM.read(4);
  acceleration = EEPROM.read(5);
  home_pos = EEPROM.read(6);

  p1_n0.setValue(min_limit);
  p1_n1.setValue(max_limit);
  p1_n2.setValue(velocity);
  p1_n3.setValue(home_search_vel);
  p1_n4.setValue(home_latch_vel);
  p1_n5.setValue(acceleration);
  p1_n6.setValue(home_pos);

}

void loop() {

    nexLoop(nex_listen_list);

     p1_n0.getValue(profila_garums);
     p1_n1.getValue(profila_augstums);

     pos_cmd = profila_garums - 2 * profila_augstums;

     if (pos_cmd < min_limit) {mot_pos_cmd = min_limit;}
     else if (pos_cmd > max_limit) {mot_pos_cmd = max_limit;}
     else {mot_pos_cmd = pos_cmd;}     

     p0_n2.setValue(mot_pos_cmd);

// for testing purposes only
     if (digitalRead(IN_home_switch)) {
      digitalWrite(LED_BUILTIN, LOW);
     } else if (!digitalRead(IN_home_switch)) {
      digitalWrite(LED_BUILTIN, HIGH);
     }   

     if (STAT_test){
     digitalWrite(LED_BUILTIN, LOW); 
     } else {
     digitalWrite(LED_BUILTIN, HIGH);
     }
}

I tried using the builtin LED for testing - it does turn on and off as I press the physical button connected to pin4, but it does not work from Nextion display.
What am I missing? Do I have to add all the numbers to nex_listen_list? I did not find any example or tutorial explaining this. All I could find are sending the numbers over serial port in HEX as a string of 4 bytes, but that is too complicated for me.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.