Help with Nextion Display for college student

You caught me. I had one course in each of the following: C, C++, Python, and one course where we used Arduino. All of my experience in real world though, outside of school, is ladder logic. That is why I am struggling so hard with interfacing the Arduino and Nextion. I am used to using matching HMI's and PLC's. Allan Bradley auto interfaces as does Seimens, Bechenoff, and Automation Direct. This exact program completely functioning in Automation Direct, took me about 45 minutes, to do both PLC and HMI. And it was only 30 rungs. But in that world communication is a simple HDMI or ethernet cable and all the tag names are quite literally drag and drop.

Now, I am easily at 200 hours and 300 lines of code so far and I am no farther now than I was a week ago. :disappointed: But I will get there. I have six weeks to figure it out and I am not stopping till I do.

1 Like

Just so you know I consider myself to be a reasonably good hobby programmer in C. I am not a professional and I struggle with some of the complexities of C++.

I appreciate that. I certainly do miss Visual Studio Plus. It really made programming easier. The drop down function where I could start spelling a variable and it would pop down, that was nice. I cannot even begin to tell you how many times I compile and simply forgot to capitalize something. Or forgot an underscore.

I think there are IDEs you can use for Arduino that have Intellisense. Maybe VS code. Generally, I've found though that Arduino programs are small enough (and therefore compile quickly) that I'll write a very few new lines before letting the compiler tell me I'm an idiot - I don't need fancier tools for that :wink:

2 Likes

So this is what I have got but now I keep getting "redefinition of "NexPage b0"" error. What am I doing wrong here?

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

//Nextion
int CurrentPage = 0;
//Page 0
NexButton b4 = NexButton(0, 6, "b4"); // ALL ON
NexButton b5 = NexButton(0, 7, "b5"); // ALL OFF
NexPage b0 = NexPage(0, 2, "b0");  // Home Page
NexPage b1 = NexPage(0, 3, "b1"); // Timer Page
NexPage b2 = NexPage(0, 4, "b2"); // Amperage Page
NexPage b3 = NexPage(0, 5, "b3"); // Faults Page

//Page 1
NexNumber n0 = NexNumber(1, 9, "n0"); // Coarse System Delay Off 
NexNumber n1 = NexNumber(1, 11, "n1"); // Mill Shop Delay Off 
NexNumber n2 = NexNumber(1, 12, "n2"); // Coarse System Delay Off 
NexPage b0 = NexPage(1, 2, "b0");  // Home Page
NexPage b1 = NexPage(1, 3, "b1"); // Timer Page
NexPage b2 = NexPage(1, 4, "b2"); // Amperage Page
NexPage b3 = NexPage(1, 5, "b3"); // Faults Page

//Page 2
NexNumber n9 = NexNumber(2, 26, "n9"); //Auger Current Reading
NexPage b0 = NexPage(2, 2, "b0");  // Home Page
NexPage b1 = NexPage(2, 3, "b1"); // Timer Page
NexPage b2 = NexPage(2, 4, "b2"); // Amperage Page
NexPage b3 = NexPage(2, 5, "b3"); // Faults Page

//Page 3

NexPage b0 = NexPage(3, 2, "b0");  // Home Page
NexPage b1 = NexPage(3, 3, "b1"); // Timer Page
NexPage b2 = NexPage(3, 4, "b2"); // Amperage Page
NexPage b3 = NexPage(3, 5, "b3"); // Faults Page

NexTouch *nex_listen_list[] = {
  &b4,
  &b5,
  &page0,
  &page1,
  &page2,
  &page3,

  NULL
};
`//** Middle code of no use at the moment. 
Current_Read_Value = analogRead(Current_Sensor_Read);
  ReadVoltage = (Current_Read_Value)*50000/1024;
  RealCurrent = ((offsetVoltage - ReadVoltage)/Sensitivity);
  
    if(digitalRead(Auger_Relay) == LOW){
     Serial.print(" Current Reading ");
     Serial.print(RealCurrent); 
     Serial.print(" mA");
     Serial.println();
     Serial1.print("n9.val=");
     Serial1.print(RealCurrent);
     Serial1.write(0xff);
     Serial1.write(0xff);
     Serial1.write(0xff);

How do I tell the serial print which page to print to?
Also struggling with the serial coms. I want to use serial 1. (I think).

Do you understand switch / case? If not search for it in the reference section of this website or on any C/C++ tutorial. Keep track of the current page using your variable CurrentPage then use that in switch so each case to print the appropriate data to each case.

Serial 1 is fine if that's what you are connected to.

I note you decided to use the Nextion official library, there is only limited help I can give you with this.

I apologize and mean no disrespect by it. Sadly I am unfamiliar with switch/ case. My projects in C and C++ consisted of hello world, a sports roster with addresses, and a functional payroll system that calculated taxes. And those projects were all serial printed in visual studio. All single page. Now I do have my old programming text books. I can certainly look into those instructions.

Struggling to use button press in nextion in conjunction with physical button press. Case is I want an all on button to turn all motor relays on. Those relays are also connected to physical buttons programmed with a debounce function. In a nutshell, I want to check if the physical button was pressed or if the button state changed from low to high or vice versa via the Nextion HMI.

IE:
if ((myInputPin1.fell() && Coarse_Button_State == LOW ) or (Coarse_Button_State was low and is now high)) {
do something
}

I don't think you need to complicate the code by combining the logic. Check the all on button and act if necessary. Then look at the physical buttons. I expect that you will need to use state change logic in each case so that you only act when a button becomes pressed.

//Molder Button
  if ((myInputPin3.fell() && Mold_Button_State == LOW)or(All_On_Button_State == HIGH))
  {
    digitalWrite(Molder_Blower_Relay, LOW );//instant turn on
    digitalWrite(Molder_Al_Relay, LOW);
    digitalWrite(Transfer_Blower_Relay, LOW );
    digitalWrite(Coarse_Powder_Al, LOW);
    digitalWrite(Coarse_Top_Al, LOW);
    
    Mold_Button_State = HIGH;
    Serial.println("Molder_On");
  }

  else if ((myInputPin3.fell()  && Mold_Button_State == HIGH)or (All_On_Button_State == LOW))
  {
    offDelayTimer_3 = true;
    lastDebounce_3 = millis();
  }

I wish I could Highlight what I added. Pretty much, I added an or to the end of my condition. My goal is to keep the timers active for either button press or HMI event. But I still have to get it connecting and talking to each other.

You need to think about what that really means. Except immediately after power up once a button has been pressed once then if it is high then the only thing it can have been before is low. So what do you really want? Some kind of timeout? Debouncing? What? This is a state machine type of thing possibly including a timer. Switch / case is good for that. In my reply #6 I linked to a tutorial about finite state machines, your project is definitely going to need one. Keeping track of which page is active is a typical finite state machined kind of activity. Please go and learn these basics before you try and solve any more of your problem. I'm sure you are familiar with the phrase "don't run before you can walk", well, you are trying to run a marathon without being able to walk. Basics first please.

Unfortunately, I do not have that sort of time. This project is do in six weeks and up until four weeks ago I thought I would be doing this project with a PLC and ladder logic. As I said before, I did this same project, including an HMI, in less than a day. Completely functional. (That is the power of ladder logic) But, due to supply timing constraints, I was unable to procure a PLC in that time. (Backordered up to six months) And I cannot graduate until this project works. SO here I am.

I added an or statement to my conditional executions with a new variable that the buttons in the HMI will set.

//Nextion Control
void b4PopCallback(void *ptr) { //If on button was pressed, turn all relays on
  All_On_Button_State = HIGH;
}

void b5PopCallback(void *ptr) { //If off button was pressed, start off sequence for all relays
  All_On_Button_State = LOW;
}
if ((myInputPin3.fell() && Mold_Button_State == LOW)or(All_On_Button_State == HIGH))

That compiled and should take care of that issue. Now I am trying to find the simplest way to send values from the Nextion to the Arduino. I tried following your work but admittedly, it is far more complex than I have time to digest. There has to be a simpler way to say hey, check this value in the HMI and place it in this variable in the Arduino.

values placed in n0, n1, and n2 need to be placed into

unsigned long debounceDelay_1 = 5000; // Coarse System Delay Off

unsigned long debounceDelay_2 = 5000; // Millshop Delay Off

unsigned long debounceDelay_3 = 5000; //<Molder Delay Off

Now I know I am trying to run here, but I am doing the best I can. Any help would be appreciated.

I wonder if anyone has written a library for Arduino that emulates a PLC. Such a thing sounds as if it would help you.

It may be indeed, that it's worth you writing one an then your project will be back in your comfort zone.

You need to think very hard about this, as far as I can tell you currently don't have the basic programming skills to complete your project. You can continue to waste time floundering and guessing or you can learn what you need to know and apply that knowledge to the problem. I believe the tutorials I suggested are the ones you need.

I wish you well with your project but I'm reluctant to offer further advice when you clearly don't want to act on it.

1 Like

I just need help communicating between the Arduino and the Nextion. I have always done hardwired buttons and such. How do you send data? How do you receive the data? Unlike PerryBebbington is suggesting, I have plenty of experience but I have never used more than a LCD display with arduino before. And those do not have inputs or outputs. Am I so wrong to ask for help in this scenario?

I am sorry you feel that way. But with out explanations in any of your code, no descriptions, no nothing. It useless to an individual such as myself who is trying to learn. Thank you for your time but perhaps someone else who can help will come along.

I haven't used Nextion displays (yet) so my advice on the subject is limited. I have though read multiple times that folks struggle with the libraries the manufacturer provides, hence the alternative approach that @PerryBebbington has taken.

One of the disadvantages of this forum is that although there are around a gazzilion people asking questions, there are only about fifty that answer them, at least in the English speaking sections. Specialist stuff like Nextion pares that pool down rather and I'm not aware of anyone but Perry who knows that stuff.

I do note though that you're in surprisingly good shape for a student. Calling for help with six weeks to go is an unusually sensible plan, we're more used to people needing to get a thing done in two days. Given that, you can afford a week or even two to do some learning that'll equip you to bang this thing out quite quickly, but I think you'll need to make that investment in time to be successful.

2 Likes

Thank you and if you look at the picture I posted at the beginning, all the hardware is already interfaced with the arduino and functions correctly. I am now adding in the HMI which needs only two buttons, and alarm screen and four values. And as I told Perry, this is not for a lack of trying. I have a 1100 page text book on C++ programming and a 1400 page textbook on C primer that has not left my side. But there is no mention in either text book of interfacing with any sort of display.

Forget C++ for now. At this level you can do everything you need with C. You can think of C++ as C with some extra bits called 'classes'*. Classes have their uses and are the reason Arduino has nice, tidy things like Serial.print(), but for this you can do perfectly well without knowing anything about classes and the other goodies inside C++. So, you can put your C++ book away for now.

*C++ experts will probably complain about this description :grimacing:

The tutorial was written on the assumption of some knowledge of C, including knowing what switch/case does and what a state machine does. There are comments in the code. It is impossible to write a tutorial that is right for every possible reader, you just have to do your best to put what you think is the necessary information in and hope it makes sense to at least some people. So far you've not asked me for an clarification about any part of my code so I don't know what you don't understand, but I do know that you don't know some of the important basics of programming, so it makes sense that you don't understand my code. I am here to answer questions, but if you ask 'tell me how the whole thing works' then I'm going to be reluctant to reply when I know you don't know and don't seem to want to learn the basics you need for my answer to make any sense.

No programming course book is going to tell you how to interface to a display, that is too product specific for a generic course.

For your college course do you have some kind of tutor / advisor / supervisor? Someone to go to for general advice about the whole course? If such a person exists then I suggest you discuss your difficulty with them and ask for advice. Maybe show them this discussion. Also, at your college is there a course on micro-controllers or similar? If so approach the person who leads that course, they should be able to give you good advice.

You could also try searching for any local electronics or maker groups and approach them for help. Near where I live we have this: https://nottinghack.org.uk/, maybe there's something similar near you. If you can find a group with someone who knows about micro-controllers then you will probably have found the help you need.

I am paying for a tutor. And my point is you used some functions that are not exactly beginner level. IE.
ENUM. I had to dig for that one and I still do not understand your reasoning for using it. Especially when I can accomplish the same thing with an array. So how about a comment, saying // "I used the function "ENUM" for this reason...."//
I told you from the beginning. I am not asking anyone to do this for me. I am trying to learn it. And 39 posts later I am growing tired of begging. Hence the reason I used the nextion library. There is explanations. I understand what each line is doing. Or would you rather I just copy yours word for word, and change the variables to avoid copy right and/ or plagiarism?

My man, for the last time. I am trying my hardest with the cards I have been dealt. I appreciate any help you have offered. Now, please let me work towards my goals.