Nextion communication works half
Hello everyone,
I'm working on a project using a Nextion Enhanced 7" display. I have set up serial communication over D0 and D1 and the display is powered using an external power source. I've just begun testing communication using a button on the touch screen to switch a relay on and off again.
The problem is: not every touch of the button results in a switch of the relay. I get about a 2/3 succes rate. The button on the screen turns green, indicating that the touch is registered, but often this does not trigger a response on the Industruino Ind. I/O 21G.
I have tried:
- Lowering baud rate from 9600 to 2400 on both devices, resulting in no communication at all
- Adding a delay(20) in the loop
- Using a different button type on the Nextion
The code is pasted below. Thanks in advance to whoever can help!
#include <Nextion.h>
#include <UC1701.h>
#include <Indio.h>
#include <Wire.h>
#include <Nextion.h>
static UC1701 lcd;
//Declare Nextion objects
NexDSButton b1 = NexDSButton(0, 3, "b1");
//Register buttons to touch event list
NexTouch *nex_listen_list[] =
{
&b1,
NULL
};
bool pumpstate = false;
const int CHpump = 1;
const int CHwaterlevel = 3;
const int CHemergency = 5;
const int CHerrorlight = 7;
const int CHflowsensor = 1;
const int CHpressuresensor = 2;
const int CHtemperaturesensor = 3;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void b1callback(void *ptr)
{
pumpstate = !pumpstate;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(26, OUTPUT); //LCD backlight
digitalWrite(26, HIGH);
Indio.digitalMode(CHwaterlevel,INPUT); //Set CH3 as input
Indio.digitalMode(CHpump,OUTPUT); //Set CH1 as output
Indio.digitalWrite(CHpump,LOW);
Indio.digitalMode(CHemergency,OUTPUT); //Set CH5 as output
Indio.digitalWrite(CHemergency,LOW);
Indio.digitalMode(CHerrorlight,OUTPUT); //Set CH7 as output
Indio.digitalWrite(CHerrorlight,LOW);
nexInit();
b1.attachPush(b1callback);
lcd.begin();
Serial.begin(9600);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void loop()
{
delay(20);
nexLoop(nex_listen_list);
if(Indio.digitalRead(CHwaterlevel) == HIGH){
Indio.digitalWrite(CHemergency,HIGH);
}
Indio.digitalWrite(CHpump, pumpstate);
}
Update: Using the serial monitor via USB I sent "check" everytime ""void b1callback(void *ptr)"" was activated, and I did not receive check on every push of the button. Is it possible that the wiring is too long? it is about 70 tot 80cm
Update: I've monitored the serial input in the industruino (coming from the display) when pressing the button. The returning pattern is 101 0 3 0 255 255 255 But more often than not I will get a soup of completely random bytes, example below: 101 0 3 0 255 (2 "255" bytes missing) 101 0 131 (No idea what happened here) 255 255 63 (Another mystery number) 3 0 255 255 255 101 0 3 192 (again, no idea what happened here) 255
Hi, i think you're on the right track to have a look at the bytes coming in from the Nextion. I don't think the cable length is necessarily a problem below 1m, but i guess it's easy to test with a shorter connection. Have you seen my blog post on using RS485 for Nextion? that allows for much longer cables and seemed quite stable. https://industruino.com/blog/our-news-1/post/nextion-hmi-with-industruino-42 In that example i used dual state buttons, and they worked well. One way to make it fail safe would be to use a normal button, and in the callback function send an instruction to change its colour, so the user can be sure the button press has reached the MCU, and keep pressing until it works, similar to this example https://github.com/jyberg/Enhanced-Nextion-Library/blob/master/examples/CompButton/CompButton_v0_32.ino (updating the button label via the mcu)
Also a reminder, if you are using D0/D1 on the INDIO, you need to switch off the RS485 chip by moving the hardware switch on the baseboard upwards, away from the RS485 terminals. By default it is configured to have the RS485 active.
Thanks again for your reply, Tom. I did check on your blog post using RS485 and it's been a good guide to setting up the code. I have also moved the hardware switch to Serial instead of RS485 a while ago. The good news: I've managed to get rid of the packet loss! The answer lies in one simple thing I completely forgot. The GND of the external power supply for the display -had- to be connected to the GND of the MCU pins. Would have been easier to troubleshoot with an oscilloscope though... Thanks for your tips!
Your answer
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 3/20/20, 12:46 PM |
Seen: 1915 times |
Last updated: 3/21/20, 6:24 AM |
Update: I have changed NexDSButton to NexButton as it is no dual state button, no succes however...