Start a new topic

Using "nexLoop" and having issues...

I have a fairly simple code for running my 7 inch Nextion display with my aftermarket automobile ECU. It works great. The way I have it setup is the second page is DS buttons used to operate a relay board to turn things on and off in the car. That works great also. The problem is it will only work with nexLoop in my main loop. When I insert it at the beginning of the main loop it renders my first page of display values virtually worthless. If I comment nexLoop out of the first page it works great but then I lose relay control. I am a programming beginner so please keep that in mind. Any suggestions on my error OR a workaround? Thanks. 


From the examples folder, NexLoop(nex_listen_list) inside the main loop should be correct.


Do you want to post HMI and ino files?

Attached is the code.  I really wish I could just take the serial input from the nextion button events and use them to activate my relays. Then I wouldnt need to call the nextion library and i could get rid of nexLoop. I tried using the recievemessages.ino I found in one of the examples but it keeps telling me myNextion is not declared on this scope,. I have been researching it for a while and I'm getting a little frustrated. :(

ino

I took a peek at your code:
I am not going to go through all, just point out a few


You have 4 DualState Buttons defined at the top, which you listen for their Release Event in Setup()


I am not going to do a byte count gauge_display()

but here you deviate from the library constructs


-- you didn't just declare the NexGauge? and the use setValue()

-- you didn't just declare the NexText components?


Part of what you miss here is that before sending out on serial you should be receiving any info that the Nextion has sent through first, once received is complete, then comes your turn to send


This is clearly overridden in your direct writes.


I have to wonder how fast the 1024 bit buffers (128 bytes) is exhausted in your loop.

It would seem to me, that depending on how many loops are iterated per second that

you could quickly have exceeded this buffer.  A delay(5) will minimize the loop to a max of 200 iterations per second and perhaps this is too high.  But you also must address receiving your incoming first.


This is taken care of for you when you use the proper infrastructure - such as was done with the

four dual states, and not done with any of the other components.


Note that sendCommand in NexHardware.cpp receives first before sending

Note that setValue, set_font_color_pco in NexGauge.cpp uses sendCommand

Note that setText  in NexText also uses sendCommand


Also in your dual state button declarations, if the page changes on the HMI screen, I didn't see how you will catch the page changes, therefore either bt0 on page 2 is unavailable or the three bt1,bt2,bt3 on page 0 will be unavailable.  What I do to ensure this is to prefix the .objname with the pagename in your code.

bt0 becomes page2.bt0 and bt1 becomes page0.bt1

(assuming page 0 is called page0 an page 2 is called page2)


This is only some issues you must look at.



Thanks Patrick. I may be jumping into this all too soon and getting ahead of myself. 

Any idea why the receive messages wont compile? It tells me myNextion doesnt name a type. I am wondering if I have my Nextion.h setup correct or if there is something I need to change?

ino
(486 Bytes)

What is the contents of NexConfig.h?

You define your serial to the nextion here.

By default it is called nexSerial ... this can be anyname you want


But don't make the mistake of naming from everywhere

The reason for naming  is so that you can see it in your code

you bounce between serial2, myNextion, nexSerial ... no good.

- the library is nexSerial ... use nexSerial and save yourself a mass headache


So I have multiple Nextion devices, I might be interested in multiple serial via a software serial


First you have to read about software serial at the Arduino site and look at the library

You will see they do good with some things, fail at others.

They suggest a different software serial, it does better at some, fails at others


So you have to learn about picking and choosing your battles.

When you have a hardware serial available - best to make use of it.



I am working on 2 automotive control touch screen projects right now using Nextion displays but only 1 per display. I can't see the need for 2 yet. 


I have my nextion communicating successfully on my native serial2. Here is my nexConfig


#ifndef __NEXCONFIG_H__

#define __NEXCONFIG_H__

 

/**

 * @addtogroup Configuration

 * @{

 */

 

/**

 * Define DEBUG_SERIAL_ENABLE to enable debug serial.

 * Comment it to disable debug serial.

 */

#define DEBUG_SERIAL_ENABLE

 

/**

 * Define dbSerial for the output of debug messages.

 */

#define dbSerial Serial

 

/**

 * Define nexSerial for communicate with Nextion touch panel.

 */

#define nexSerial Serial2

 

 

#ifdef DEBUG_SERIAL_ENABLE

#define dbSerialPrint(a) dbSerial.print(a)

#define dbSerialPrintln(a) dbSerial.println(a)

#define dbSerialBegin(a) dbSerial.begin(a)

#else

#define dbSerialPrint(a) do{}while(0)

#define dbSerialPrintln(a) do{}while(0)

#define dbSerialBegin(a) do{}while(0)

#endif

 

/**

 * @}

 */

 

#endif /* #ifndef __NEXCONFIG_H__ */


then based on your config, I would use nexSerial


nexSerial is the alias naming as defined

Serial2 is the natural naming as defined


Using in your code nexSerial and dbSerial helps you see which is being used

reverting to Serial2 and Serial can be confusing,

this is the reason why aliases are created - to avoid confusion.


If you really desired to use myNextion, then it is in this nexConfig.h

   where you would swap myNextion for nexSerial ... but there may be other code

   relying on the nexSerial ... so changing it, you would have to change all occurrences of

I get what you're saying and it makes perfect sense. I changed out myNextion with mySerial and still get the "Nextion does not name a type" when I try to compile my recieve message.ino I attached. I need more practice lol.

I am not the Arduino Guru, so I certainly can't solve all.


You will get a conflict using SoftwareSerial.h and Nextion.h

Here, you use one or the other, but not both for the same Nextion device

(unless you have multiple serials and multiple devices ... more advanced)

Login or Signup to post a comment