Start a new topic

nextion.listen doesn't receive after myNextion.set / send commands

Hi,

I just got a strange problem with my NX3224T024 Display. On the display are two buttons which send a Component ID to an Arduino Nano to increase and decrese a value that is then shown on the display.


What works is:

  #include <SoftwareSerial.h>  // Software Serial for Display

  #include <Nextion.h>  // Nextion Library to access Display

  SoftwareSerial nextion(3, 4);// Nextion TX to pin 3 and RX to pin 4 of Arduino 

  Nextion myNextion(nextion, 9600);

  float p_pump_set;

 void setup(){
  p_pump_set = 0;
  Serial.begin(9600);
  myNextion.init();  // Init Nextion Display Connection
  myNextion.setComponentText("t1", String(p_pump_set)); float p_pump_set;

}


void loop(){

  String message = myNextion.listen(); //check for message

  if (message != "")
  {
  Serial.println(message);// Button UP
  if (message == "65 0 2 1 ffff ffff ffff") {
  if (p_pump_set <= 95) p_pump_set += 5;
  myNextion.setComponentText("t1",  String(p_pump_set));
  }
  if (message == "65 0 1 1 ffff ffff ffff") {
  if (p_pump_set >= 5) p_pump_set -= 5;
  myNextion.setComponentText("t1", String(p_pump_set));
  }
  }

}


however, if I use then an additional myNextion.set / send like 

  myNextion.setComponentText("t1", String(p_pump_set));

or

  myNextion.setComponentText("t3", "42"));

or

  myNextion.sendCommand("vis t3,0");

the value on the display is updated, but I no longer receive anything from myNextion.listen() .


Why does this happen? I don't understand why the first   myNextion.setComponentText("t1",  String(p_pump_set)); inside the if-case doesn't cause this problem.

Hope someone can help me with this.


Thanks in advance and kind regards

Lars

Hey Lars


First disclaimer - I am a pascal programmer, not Arduino.

Second disclaimer - Support is limited to level 2, to ensure hardware is working, and not to debug code.

Third disclaimer - The Arduino Gods and Gurus don't visit to answer code questions very often.


So, I am going to humbly attempt to help where I can.

I am not sure of your coding capabilities, and understanding that I am not an Arduino guru

I am assuming this has compiled.  I am assuming you made no typos.


The code you presented as you state works - I can't see any fault in what you say is working. 

I might have had different personal preferences but they'd be just that - personal preferences.

(Such as not using Nextion and nextion, or including braces on your inner if statement)


When you are attempting to add another component statement and the code fails,

that is the code that you haven't included, so it is really hard to locate the error.

It could be a number of things from a programming point of view.


But I have to wonder if this is using the Itead Library?

as your code isn't following the format of the two button and text example from the CompText example.


Can you confirm that you are using the IteadLib or not?


LJ.


I was revisiting your code:


After finally installing Arduino 1.6.11

- and after installing ITEADLIB_Arduino_Nextion-0.7.0 and testing your code

  (Does not contain a Nextion type).

- and after also installing NeoNextion and testing your code

  (NeoNextion Nextion class does not have a setComponentText member)

Neither of these two libraries allow your code to compile without errors.


Knowing which library you are using is necessary to help find a solution for you.

Hey Patrick,
thanks for your help. It seems, I am using a modified version of the nextion library which can be found here: https://github.com/bborncr/nextion
Some days ago I got stuck trying to send and receive the values via serial and asked a collegue for help. We then installed the nextion library obove and go the same "class does not have member" error since there was another nextion lib installed in his visual studio programm. After removing the preinstalled one, the simple script worked until i added another command.
We prefered the modified library because it allows to set the software-serial pins since I need the A.. pins to read voltages from a laser system.
Here is my complete program code :

 

#include <SoftwareSerial.h>  // Software Serial for Display 
#include <Nextion.h>        // Nextion Library to access Display      

//#define DAC 0x62 //AdafruiteMCP4725 I2C Address


// Display Communication over Software Serial //////////
  SoftwareSerial nextion(6, 7);// Nextion TX to pin 3 and RX to pin 4 of Arduino
  Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps


// Variables //////////////////////////////////
  boolean operation = 0;  // 1 = normal operation
  boolean laser_key = 0;
  float Pp_Ctrl;
  float Pp_Ctrl_pc;
  float P_laser_read;
  float p_pump_set;
  int p_nominal;
  float P_laser_W;
  float I_pump;
  float I_pump_pc;
  boolean Back_Ref;
  float temp;


// analog Inputs   (int i/o,function,pin on laser = Arduino Pin N°)
  char in_PpCtrl_8 = A0;    // ext. ctrl voltage to adj Pump Power (0-4V)
  char in_IpMon_6 = A1;     // Monitorvoltage PumpCurrent (0-0.4V)
  char in_PoutMon_25 = A2;  //Monitordiode for Laser Output Power (0-1V)
  char in_TCase_5 = A3;     // Temp of Laser (0.01V /°C)


// digital Inputs
  int in_BackRef_16 = 12;   // detect Backreflection
  int in_laser_key = 11;    // savety keyswitch to start laser


// digital output
  int out_mod_17 = 2;       // Modulation = 0 switches Laser on
  int led_laser = 10;       // Laser Status LED


/////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

  void setup(){
  p_pump_set = 0;               // Reset Laser Power for defined start value
  Serial.begin(9600); 

  myNextion.setComponentText("t1", String(p_pump_set));
  

  pinMode(11, INPUT);             // set pin to input
  digitalWrite(11, HIGH);         // turn on pullup resistors
  pinMode(out_mod_17, OUTPUT);     // define Modulation as output
  pinMode(led_laser, OUTPUT);      // define Laser Led as output
  }

/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////Main Program///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void loop(){

  // Touch Button input /////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////// 
  String message = myNextion.listen(5000); //check for message
//  if (message != "") 
//  {
//    Serial.println(message);
//    
//    Serial.println(message.length());
//    while (1) {} 
//  }
  if (message != "")
  { 
    Serial.println(message);// Button UP
    if (message == "65 0 2 1 ffff ffff ffff") { 
     if (p_pump_set <= 95) p_pump_set += 5;
     myNextion.setComponentText("t1",  String(p_pump_set));
    }
    if (message == "65 0 1 1 ffff ffff ffff") {
      if (p_pump_set >= 5) p_pump_set -= 5;
      myNextion.setComponentText("t1", String(p_pump_set));
    }
  }

  //set DAC 
  // is missing here



  // Read Laser Output Power SET (voltage from DAC) ////////////////////////////
  ///////////////////////////////////////////////////////////////////////////// 
    Pp_Ctrl = analogRead(in_PpCtrl_8) * (5.0 / 1023.0); // read the ctlr. voltage
    Pp_Ctrl_pc = Pp_Ctrl *100 / 4  ;    // 100% equals 4 V
  //Terminal Debug Output
    Serial.print("Power READ in %  ");
    Serial.println(Pp_Ctrl_pc); //output the read laser power in PerCent
    Serial.print("Power SET in %  ");
    Serial.println(p_pump_set); //output the set laser power in PerCent
  // Write P_Pump_set to Display
    myNextion.setComponentText("t1", String(p_pump_set));
   

  // Read Laser output Power from Monitor Diode //////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
    P_laser_read = analogRead(in_PoutMon_25) * (5.0 / 1023.0); // 1V = nominal Power
    p_nominal = 13;   // calibration factor
    P_laser_W = P_laser_read * p_nominal; 
  //Terminal Debug Output
    Serial.print("Laser Output in W  ");
    Serial.println(P_laser_W); //output the measured laser power in W  
    
  // Write measured Output Power to Display
    myNextion.setComponentText("t3", String(P_laser_W));

 
  
  // Read Pump Diode Current from Laser Head ////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
    I_pump = analogRead(in_IpMon_6)* (5.0 / 1023.0); // 0.4V=100%
    I_pump_pc = I_pump / 0.4 * 100;  // PumpCurrent in PerCent
  //Terminal Debug Output
    Serial.print("I_Pump in %  ");
    Serial.println(I_pump_pc); //output the measured PumpCurrent in PerCent

  
  // Read temperature /////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
    temp = analogRead(in_TCase_5)*100;
  //Terminal Debug Output
    Serial.print("Temp Laser in C   ");
    Serial.println(temp); 
  //Write measured Output Power to Display
    myNextion.setComponentText("t5", String(temp));


  // Check of Back_Reflection ////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////
    Back_Ref = digitalRead(in_BackRef_16); // high=OK, low=error
    // LASER HAS TO SHUT DOWN WHEN BR IS DETECTED !!!
    if (Back_Ref== 1){ operation = 1;}
    else {operation = 0;}
  //Terminal Debug Output
    Serial.print("Status 1=OK 0=Error : ");
    Serial.println(operation); //output the measured PumpCurrent in PerCent
  // Write Status to Display
   if (Back_Ref == 1)  myNextion.setComponentText("t7", "OK");
   if (Back_Ref == 0)  myNextion.setComponentText("t7", "Error1");
      
  
  // SW Laser ON and OFF with Modulation Signal ////////////////////////
  /////////////////////////////////////////////////////////////////////
    // low = laser on    high = laser off
    laser_key = digitalRead(in_laser_key); //pull down 
  //Terminal Debug Output
    Serial.print("Laserkey 0=ON 1=OFF : ");
    Serial.println(laser_key); //output satus of laser key
    Serial.println("-------------------------------------");


  // START THE LASER IF ALL CONDITIONS OK //////////////////////////////
  /////////////////////////////////////////////////////////////////////
    if (laser_key == 0 and operation == 1)  {
      digitalWrite(out_mod_17, 0); // switch laser on
      digitalWrite(led_laser,1);
      Serial.println("Laser ON"); 
  // Show Info on Display
      myNextion.sendCommand("vis t8,0");
       myNextion.sendCommand("vis t3,1");
    }     
    else {digitalWrite(out_mod_17, 1); //switch laser off
      digitalWrite(led_laser,0);
      Serial.println("Laser OFF"); 
  // Hide Info on Display
      myNextion.sendCommand("vis t8,1");
       myNextion.sendCommand("vis t3,0");
    }  
}

 

 

 

 

Okay.  I am beginning to see some of the issues.


When I examined the Nextion library you are using, I noted that it is

a) either a subset of only what was needed to drive their example project

b) since their compiled design .tft file is version 0.21 - rather old indeed

c) posted to showcase a single specific hmi design created using Google's Inkscape.


So I would truly approach using their "nextion library" with caution

- unless you are using version 0.21 of the Nextion Editor

- unless you were to continue to add the missing pieces for your HMI design.

- somehow their library skews 0xFF into ffff - technically incorrect

- many missing return codes such as numeric values will be passed over ie: 0x71 return code.

It doesn't appear that the two authors are working to bring it up-to-date

This library would need a fair amount of work.


IteadLib 0.7.0 (stable) advantage is that it has recently been brought up-to-date for new components

- advantage is it exposes the HMI component attributes that can be changed at runtime

- advantage is the attachPush (Touch Pressed) and attachPop (Touch Released) save you from having to fight with if (message == "65 1 2 4 ffff ffff ffff") type code making listen() much less complex once you have all your important component events attached and configured.

- disadvantage is that your direct serial is less obvious, but it is there in sendCommand

- disadvantage, a small learning curve to make the most of it

- need one serial to talk to the Nextion -- modify in NexConfig.h


So digging further into Arduino, their software serial library can only listen to one serial at a time.

Advantage - configure two whatever two pins happen to be available.

Advantage - complete control over what is written over serial

Disadvantage - complete control over what is written and responsibility for reading over serial

Disadvantage - learning curve to become intimate with HMI design and Nextion Instruction Set.


The IteadLib will want to use a hardware serial by default, unless you modified it for software serial

- modifying it to use software serial is untested at the moment - but you have an example to follow in the library source for Nextion.h and Nextion.cpp of your current library.  It may be worth a shot.


So going forward, there is going to be a bit of coding to do either way you look at it

And I see some learning curves in whichever direction you take.


Hi Patrick,

thanks a lot for examining the code. As soon as I find the time for it, i'll switch over to the IteadLib and let you know about the progress. However, for now - as a quick workaround - I will use a potentiometer to set the Laser pump power.

 

Login or Signup to post a comment