Start a new topic

Arduino / Nextion Question

Good morning again, great help was achieved yesterday, thanks  Patrick Martin


I now need a hand taking this a little bit further, I have changed the buttons from DS to single buttons, for some reason the Arduino was not acting on every serial bit received, which means the buttons were sometimes on when they were off outputs,


1. what causes that, I am still finding it with the b0's that the Nextion misses or does not respond the serial bit, I can see it in the serial monitor for every press, but the processor responds to the second, third or forth receive ????


2. I can successfully turn the relay on and off via on button or an off button (CODE 1  below) on the Nextion when the Arduino is driving the digital output directly, or conversely via a single button (CODE 2 below) using the else command, they work great, thanks again Patrick


3. with the point 2 above being said, I really need the Arduino to use the serial input as an internal bit, ie button (b0) sets internal bit to HIGH or LOW, using the same logic as setting a digiatal output. (CODE 3 below does not work) I can then use the internal bit to drive the output but cant get it to turn off on the next press or the off button press. later I will be using logic to use the bits to work out what output to drive, hence the direct 1 to 1 will not be required to progress the project


thanks in advance Ben


**********************************************

CODE 1 Pin13 on / off by two buttons on the nextion bo and b1


**********************************************

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

 // parameters  page #, component .id, component .objname

NexButton myButton0 = NexButton(0,17, "b0");
NexButton myButton1 = NexButton(0,18, "b1");
 uint32_t digitalstate11, digitalstate12 = 0; 

 const int relay1Pin = 13;
 byte pump1;
 byte pump2;

NexTouch *nex_listen_list[] = {
  &myButton0, &myButton1,
  NULL
 };

 void myButton0ReleaseCallback(void *ptr) {
  digitalstate11 = 1-digitalstate11;
  if(digitalstate11==1){
  digitalWrite(relay1Pin, HIGH);
  }
 }
  void myButton1ReleaseCallback(void *ptr) {
  digitalstate12 = 1-digitalstate12;
 if(digitalstate12==1){
 digitalWrite(relay1Pin, LOW);

 }
  }
 void setup(void) {
 pinMode(relay1Pin,OUTPUT);
  nexInit();
  myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
  myButton1.attachPop(myButton1ReleaseCallback, &myButton1);
 }
 
 void loop(void) {
  nexLoop(nex_listen_list);
 
 }


*********************************************************************

CODE 2 turns Pin13 on / off by a single button on the nextion bo

*********************************************************************

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

 // parameters  page #, component .id, component .objname

NexButton myButton0 = NexButton(0,17, "b0");

 uint32_t digitalstate11 = 0;
 
 const int relay1Pin = 13;
 
NexTouch *nex_listen_list[] = {
  &myButton0,
  NULL
 };
 void myButton0ReleaseCallback(void *ptr) {
  digitalstate11 = 1-digitalstate11;
  if(digitalstate11==1){
  digitalWrite(relay1Pin, HIGH);
  }else{
  digitalWrite(relay1Pin, LOW);
  } 
 }

 void setup(void) {
 pinMode(relay1Pin,OUTPUT);
  nexInit();
  myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
 }

 void loop(void) {

  nexLoop(nex_listen_list);
 }


********************************************************

CODE 3 I need to turn relay1Pin by an internal bit that the Nextion sets HIGH or LOW, can get it to work past the first on, output stays on or the output pulses or cant drive the output at all, I have tried the code in the top section or in the void loop section

????

********************************************************

********************************************************

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

 // parameters  page #, component .id, component .objname

NexButton myButton0 = NexButton(0,17, "b0");
NexButton myButton1 = NexButton(0,18, "b1");
 uint32_t digitalstate11, digitalstate12 = 0; 

 const int relay1Pin = 13;
 byte pump1;


NexTouch *nex_listen_list[] = {
  &myButton0, &myButton1,
  NULL
 };

 void myButton0ReleaseCallback(void *ptr) {
  digitalstate11 = 1-digitalstate11;
  if(digitalstate11==1){
  digitalWrite(pump1, HIGH);
  }
 }
  void myButton1ReleaseCallback(void *ptr) {
  digitalstate12 = 1-digitalstate12;
 if(digitalstate12==1){
 digitalWrite(pump1, LOW);

 }
  }
 void setup(void) {
 pinMode(relay1Pin,OUTPUT);
  nexInit();
  myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
  myButton1.attachPop(myButton1ReleaseCallback, &myButton1);
 }

 void loop(void) {

  pump1 = digitalRead(pump1);


  if (pump1==HIGH){
  digitalWrite(relay1Pin, HIGH);
  }
  if (pump1==LOW){
  digitalWrite(relay1Pin, LOW);
  }
  nexLoop(nex_listen_list);
 
 }

*********************************************************



Ben the code in the Code #3 button b0 release and b1 release


1  digitalstate11 = 1-digitalstate11; 

2  if(digitalstate11==1){

3     digitalWrite(pump1, HIGH);

4  }


line 1 is a toggle, if value was 1 it will be 0, if it was 0 it will be 1

but the logic that follows is incomplete.


You must choose

a) button press turns on and next press turns off

b) button b0 turns on and button b1 turns off


If a) your logic would need to add

4   else {

5       digitalWrite(pump1, LOW);

6    }


If b) your logic between buttons b0 and b1 need to drop the toggle

  and b0 sets HIGH while b0 sets LOW.


I will try to assist answering the user Nextion side questions

but the MCU and coding side - you need to dig in a bit


There are a number of reasons outside of code why an MCU

may not be able to drive things by code alone, there will be

many many manuals and datasheets to read as you go.


Pulsing can be a sign of improper power supply, you will need to

investigate.  MCU may be asked to supply more than it can.

But many of the Arduino side questions - Arduino Forum


1 person likes this

thanks, got it going, removed the digitalWrite part, just set the bits High or low and removed the


pump1 = digitalRead(pump1)




 void myButton0ReleaseCallback(void *ptr) {
  digitalstate11 = 1-digitalstate11;
  if(digitalstate11==1){
  pump1=HIGH;
  } else {
  pump1= LOW; 
  }
  }

Patrick - HELP, what have I done, please sir please


I got it working then ????????? I have changed the Nextion then I could not get any TXT to read out on the screen


I have gone back to square 1, used you original code from yesterday and stated again, re designed the nextion and re configured the code for single buttons not DS


the Arduino code works, no issue, the outputs toggle, but for the life of me I cant get the screen to show the bit on or off like I did, the ID and config numbers match


sample code below, as stated above the code works, the digital output works, just not the Text to the nextion


 // PUMP8 OUTPUT ********************************************************


  if (pump8==HIGH) {
  pump8txt=HIGH;
  digitalWrite(relay8Pin, HIGH);
  } else{
  pump8txt=LOW;
  digitalWrite(relay8Pin, LOW);
  }

//PUMP 8 TEXT***********************************************************
 
  if (pump8txt == HIGH){ 
  t7.setText("ON");
  }
if (pump8txt == LOW){
  t7.setText("OFF");
  }
  nexLoop(nex_listen_list);
 }




below is the entire code,


what have I missed, again, it worked before, the Nextion, has 8 buttons b1-b7 and ID1-8


and 8 TXT fields t1-t7 and ID1-8


the buttons operate the digital outputs via the code meaning up to the setText bit works in the Arduino, is the issue in the Nextion


many thanks Ben



********************************************


#include <Arduino.h>
 #include "Nextion.h"
 // parameters  page #, component .id, component .objname


NexText t0 = NexText(0, 9, "t0");
NexText t1 = NexText(0, 10, "t1");
NexText t2 = NexText(0, 11, "t2");
NexText t3 = NexText(0, 12, "t3");
NexText t4 = NexText(0, 13, "t4");
NexText t5 = NexText(0, 14, "t5");
NexText t6 = NexText(0, 15, "t6");
NexText t7 = NexText(0, 16, "t7");

NexButton myButton0 = NexButton(0,1,"b0");
NexButton myButton1 = NexButton(0,2,"b1");
NexButton myButton2 = NexButton(0,3,"b2");
NexButton myButton3 = NexButton(0,4,"b3");
NexButton myButton4 = NexButton(0,5,"b4");
NexButton myButton5 = NexButton(0,6,"b5");
NexButton myButton6 = NexButton(0,7,"b6");
NexButton myButton7 = NexButton(0,8,"b7");


 uint32_t digitalstate0, digitalstate1, digitalstate2, digitalstate3,
  digitalstate4, digitalstate5, digitalstate6, digitalstate7 = 0;
 const int relay1Pin = 13;
 const int relay2Pin = 12;
 const int relay3Pin = 11;
 const int relay4Pin = 10;
 const int relay5Pin = 9;
 const int relay6Pin = 8;
 const int relay7Pin = 7;
 const int relay8Pin = 6;
byte pump1;
byte pump1txt;
byte pump2;
byte pump2txt;
byte pump3;
byte pump3txt;
byte pump4;
byte pump4txt;
byte pump5;
byte pump5txt;
byte pump6;
byte pump6txt;
byte pump7;
byte pump7txt;
byte pump8;
byte pump8txt;
 
 NexTouch *nex_listen_list[] = {
  &myButton0, &myButton1, &myButton2, &myButton3,
  &myButton4, &myButton5, &myButton6, &myButton7,
  NULL
 };

 void myButton0ReleaseCallback(void *ptr) {
  digitalstate0 = 1-digitalstate0;
  if(digitalstate0==1) {
  pump1=HIGH;
  } else {
  pump1=LOW;
  }
 }

 void myButton1ReleaseCallback(void *ptr) {
  digitalstate1 = 1-digitalstate1;
  if(digitalstate1==1) {
  pump2=HIGH;
  } else {
  pump2=LOW;
  }
 }

 void myButton2ReleaseCallback(void *ptr) {
  digitalstate2 = 1-digitalstate2;
  if(digitalstate2==1) {
  pump3=HIGH;
  } else {
  pump3=LOW;
  }
 }
 void myButton3ReleaseCallback(void *ptr) {
  digitalstate3 = 1-digitalstate3;
  if(digitalstate3==1) {
  pump4=HIGH;
  } else {
  pump4=LOW;
  }
 }
 void myButton4ReleaseCallback(void *ptr) {
  digitalstate4 = 1-digitalstate4;
  if(digitalstate4==1) {
  pump5=HIGH;
  } else {
  pump5=LOW;
  }
 }
 void myButton5ReleaseCallback(void *ptr) {
  digitalstate5 = 1-digitalstate5;
  if(digitalstate5==1) {
  pump6=HIGH;
  } else {
  pump6=LOW;
  }
 }
 void myButton6ReleaseCallback(void *ptr) {
  digitalstate6 = 1-digitalstate6;
  if(digitalstate6==1) {
  pump7=HIGH;
  } else {
  pump7=LOW;
  }
 }
 void myButton7ReleaseCallback(void *ptr) {
  digitalstate7 = 1-digitalstate7;
  if(digitalstate7==1) {
  pump8=HIGH;
  } else {
  pump8=LOW;
  }
 }
char buffer[100] = {0};
 void setup(void) {
  pinMode(relay1Pin,OUTPUT);
  pinMode(relay2Pin,OUTPUT);
  pinMode(relay3Pin,OUTPUT);
  pinMode(relay4Pin,OUTPUT);
  pinMode(relay5Pin,OUTPUT);
  pinMode(relay6Pin,OUTPUT);
  pinMode(relay7Pin,OUTPUT);
  pinMode(relay8Pin,OUTPUT);

  nexInit();
  myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
  myButton1.attachPop(myButton1ReleaseCallback, &myButton1);
  myButton2.attachPop(myButton2ReleaseCallback, &myButton2);
  myButton3.attachPop(myButton3ReleaseCallback, &myButton3);
  myButton4.attachPop(myButton4ReleaseCallback, &myButton4);
  myButton5.attachPop(myButton5ReleaseCallback, &myButton5);
  myButton6.attachPop(myButton6ReleaseCallback, &myButton6);
  myButton7.attachPop(myButton7ReleaseCallback, &myButton7);
 }

 void loop() {

//PUMP1 OUTPUT ********************************************************
  if (pump1==HIGH) {
  pump1txt=HIGH;
  digitalWrite(relay1Pin, HIGH);
  } else{
  pump1txt=LOW;
  digitalWrite(relay1Pin, LOW);
  }

//PUMP 1 TEXT***********************************************************
 
  if (pump1txt == HIGH){ 
  t0.setText("ON");
  }
if (pump1txt == LOW){
  t0.setText("OFF");
  }
//PUMP2 OUTPUT ********************************************************
  if (pump2==HIGH) {
  pump2txt=HIGH;
  digitalWrite(relay2Pin, HIGH);
  } else{
  pump2txt=LOW;
  digitalWrite(relay2Pin, LOW);
  }

//PUMP 2 TEXT***********************************************************
 
  if (pump2txt == HIGH){ 
  t1.setText("ON");
  }
if (pump2txt == LOW){
  t1.setText("OFF");
  }
//PUMP3 OUTPUT ********************************************************
  if (pump3==HIGH) {
  pump3txt=HIGH;
  digitalWrite(relay3Pin, HIGH);
  } else{
  pump3txt=LOW;
  digitalWrite(relay3Pin, LOW);
  }

//PUMP 3 TEXT***********************************************************
 
  if (pump3txt == HIGH){ 
  t2.setText("ON");
  }
if (pump3txt == LOW){
  t2.setText("OFF");
  }
  //PUMP4 OUTPUT ********************************************************
  if (pump4==HIGH) {
  pump4txt=HIGH;
  digitalWrite(relay4Pin, HIGH);
  } else{
  pump4txt=LOW;
  digitalWrite(relay4Pin, LOW);
  }

//PUMP 4 TEXT***********************************************************
 
  if (pump4txt == HIGH){ 
  t3.setText("ON");
  }
if (pump4txt == LOW){
  t3.setText("OFF");
  }
  //PUMP5 OUTPUT ********************************************************
  if (pump5==HIGH) {
  pump5txt=HIGH;
  digitalWrite(relay5Pin, HIGH);
  } else{
  pump5txt=LOW;
  digitalWrite(relay5Pin, LOW);
  }

//PUMP 5 TEXT***********************************************************
 
  if (pump5txt == HIGH){ 
  t4.setText("ON");
  }
if (pump5txt == LOW){
  t4.setText("OFF");
  }
  //PUMP6 OUTPUT ********************************************************
  if (pump6==HIGH) {
  pump6txt=HIGH;
  digitalWrite(relay6Pin, HIGH);
  } else{
  pump6txt=LOW;
  digitalWrite(relay6Pin, LOW);
  }

//PUMP 6 TEXT***********************************************************
 
  if (pump6txt == HIGH){ 
  t5.setText("ON");
  }
if (pump6txt == LOW){
  t5.setText("OFF");
  }
  //PUMP7 OUTPUT ********************************************************
  if (pump7==HIGH) {
  pump7txt=HIGH;
  digitalWrite(relay7Pin, HIGH);
  } else{
  pump7txt=LOW;
  digitalWrite(relay7Pin, LOW);
  }

//PUMP 7 TEXT***********************************************************
 
  if (pump7txt == HIGH){ 
  t6.setText("ON");
  }
if (pump7txt == LOW){
  t6.setText("OFF");
  }
 // PUMP8 OUTPUT ********************************************************
  if (pump8==HIGH) {
  pump8txt=HIGH;
  digitalWrite(relay8Pin, HIGH);
  } else{
  pump8txt=LOW;
  digitalWrite(relay8Pin, LOW);
  }

//PUMP 8 TEXT***********************************************************
 
  if (pump8txt == HIGH){ 
  t7.setText("ON");
  }
if (pump8txt == LOW){
  t7.setText("OFF");
  }
  nexLoop(nex_listen_list);
 }




HMI
(30.2 KB)

further to this, it appears when ever I try now to send a command to the Nextion I get this Error on the serial monitor, note can send commands from the Nexton to the Arduino ok, but from the Arduino to the Nextion, something has happened.


the serial link is ok as I can send commands one way but not the other


I get this error ????????? on any sketch


recvRetCommandFinished err

Ben what is connected to the pins 6 to 13?

What does the datasheets say for current?

Have you exceeded limits?

The same Happens when I do the temp probe Only the probe message pun is connected. (Pin 5 ). It happens for any sketch. With or without anything connected. Can't write to the Nextion.
From 13-6 is the 8 channel relay bank

What is the amount of current it is requesting

Does the data sheet show that the ports can deliver that much current?

I noticed you also looked at and commented about the YouTube thread.

The users issue was unrelated to yours - if you changed your NexConfig.h

this wold also cause issues

Patrick I understand the question. The answer is I don't know sorry . But the same issue presents with nothing connected at all also or just a single sensor.
Changed. No. Never have altered the library files. No need to. These are basic sketches.

Okay so one goes back to the drawing board and works forward from last known good.


(Sometimes USB to Serial connectors need to be unplugged and replugged if serial port is not closed properly)

The HMI and ino file are in the thread where I posted them to you.

If you need to find a starting point where it was working, they wont change and you can download again.


Check cables, ensure ground is common, no fractured wires, continuity exists on wires.


As far as the question of how much current it needs and what are max.

you need to look this up in the datasheets.  Each is different and specific

One I looked at was 20ma per relay but port of MCU could deliver top 100ma

 - so such a scenario means trying to draw 160ma (8x20) from a 100ma port - no go


This is in no way a guideline but every component is different

You need datasheets, you need to do calculations - you must know


Embedded is not like a plug and play USB mouse

- you can damage your MCU and components by doing it wrong.

How can I change to serial port 3. Ti see if there is an issue with 2.

Where single sensor fails, you need to resort back to simple port 13 flash LED

and try and make sure all is well with your MCU


IF using the ATMega 2560, the datasheet for the Mega is accessible via Arduino.cc

You need to find the data sheets for your relays.  Measure the draw with multimeters.


Login or Signup to post a comment