This is the NEXTION HMI a Human Machine Interface device that provides a GUI control of instruments that feeds from your microcontroller. The Nextion is simplified interface to visualize the process between a human and machine, these device is useful on IOT projects or simply an display to your project instead using a traditional LCD or LED display. NEXTION included hardware and series of TFT boards along with a software editor to design your desired interface at the screen. The board uses a one serial communication port to communicate to your Arduino, RPI or AVR, and it works only RX/TX communication and let you avoid hassle of wirings, The NEXTION editor has an ability to provide a drag and drop component such as buttons, Pictures, toggle buttons, slider, progress bar, etc. to ensures that you spend less time in programming your development workloads with the help of WYSIWYG editor. As you can see the diagram we hookup a relay module work as a switch to any home, automotive, & industrial devices.
Required Components
- Arduino Microcontroller / Tenssy / NodeMCU (Arduino IDE Integration)
- USB TTL- UART Serial (CP2XX)/(PL2XX) will work
- Relay Module (4 to 8 channel)
- Jumper Wires / DuPont Wires
Wiring Guide
Uploading the HMI File with NEXTION GUI Editor
- Nextion editor can be download here > http://nextion.itead.cc/download.html
- Download the HMI File below
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
/* www.14core.com | 14CORE > Human Machine Interface Relay Commander */ #include "Nextion.h" int R1 = 2, R2 = 3, R3 = 4, R4 = 4, R5 = 5, R6 = 6; NexDSButton bt0 = NexDSButton(0, 1, "bt0"); NexDSButton bt1 = NexDSButton(0, 1, "bt1"); NexDSButton bt2 = NexDSButton(0, 1, "bt2"); NexDSButton bt3 = NexDSButton(0, 1, "bt3"); NexDSButton bt4 = NexDSButton(0, 1, "bt4"); NexDSButton bt5 = NexDSButton(0, 1, "bt5"); char buffer[100] = {0}; NexTouch *nex_listen_list[] = { &bt0, &bt1, &bt2, &bt3, &bt4, &bt5, NULL }; void setup(void) { nexInit(); bt0.attachPop(bt0PopCallback, &bt0); bt1.attachPop(bt0PopCallback, &bt1); bt2.attachPop(bt0PopCallback, &bt2); bt3.attachPop(bt0PopCallback, &bt3); bt4.attachPop(bt0PopCallback, &bt4); bt5.attachPop(bt0PopCallback, &bt5); dbSerialPrintln("setup done"); } void loop(void) { nexLoop(nex_listen_list); } void bt0PopCallback(void *ptr) { uint32_t dual_state; NexDSButton *btn = (NexDSButton *)ptr; dbSerialPrintln("Callback"); dbSerialPrint("ptr="); dbSerialPrintln((uint32_t)ptr); memset(buffer, 0, sizeof(buffer)); bt0.getValue(&dual_state); bt1.getValue(&dual_state); bt2.getValue(&dual_state); bt3.getValue(&dual_state); bt4.getValue(&dual_state); bt5.getValue(&dual_state); if(dual_state){digitalWrite(R1, HIGH);}else{digitalWrite(R1, LOW);} if(dual_state){digitalWrite(R2, HIGH);}else{digitalWrite(R2, LOW);} if(dual_state){digitalWrite(R3, HIGH);}else{digitalWrite(R3, LOW);} if(dual_state){digitalWrite(R4, HIGH);}else{digitalWrite(R4, LOW);} if(dual_state){digitalWrite(R5, HIGH);}else{digitalWrite(R5, LOW);} if(dual_state){digitalWrite(R6, HIGH);}else{digitalWrite(R6, LOW);} } |
Downloads
- Toggle Button | Download Link
- Push Button | Download Link
Download the GUI Editor software > http://nextion.itead.cc/download.html
Designer Starter Guide > http://wiki.iteadstudio.com/Nextion_Editor_Quick_Start_Guide
Super guideline. Everything included and very helpful. Thank you.
great Tutorial.
Some days ago i try to run/upload to Wemos D1 R2 ESP8266 and found some occured. Please help me to solve til it work. This Hardware and software i used :
– Nextion 3.2″ Standart/Basic
– Wemos D1 R2 ESP8266
– Arduino IDE
This is errors occured :
Arduino: 1.8.1 (Windows XP), Board: “WeMos D1 R2 & mini, 80 MHz, 921600, 4M (1M SPIFFS)”
sketch_apr03a:7: error: ‘NexDSButton’ does not name a type
NexDSButton bt0 = NexDSButton(0, 1, “bt0”);
^
sketch_apr03a:8: error: ‘NexDSButton’ does not name a type
NexDSButton bt1 = NexDSButton(0, 1, “bt1”);
sketch_apr03a:18: error: ‘bt0’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
C:\DOCUME~1\Me\LOCALS~1\Temp\arduino_modified_sketch_560165\sketch_apr03a.ino: In function ‘void setup()’:
sketch_apr03a:24: error: ‘bt0’ was not declared in this scope
bt0.attachPop(bt0PopCallback, &bt0);
^
C:\DOCUME~1\Me\LOCALS~1\Temp\arduino_modified_sketch_560165\sketch_apr03a.ino: In function ‘void bt0PopCallback(void*)’:
sketch_apr03a:41: error: ‘NexDSButton’ was not declared in this scope
NexDSButton *btn = (NexDSButton *)ptr;
^
sketch_apr03a:41: error: ‘btn’ was not declared in this scope
NexDSButton *btn = (NexDSButton *)ptr;
^
sketch_apr03a:49: error: ‘bt0’ was not declared in this scope
bt0.getValue(&dual_state);
^
exit status 1
‘NexDSButton’ does not name a type
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
Thanks a lot for shared with me.
Please use this library.
http://www.14core.com/wp-content/uploads/2017/03/ESP8266WiFi.zip
http://www.14core.com/wp-content/uploads/2017/03/ESP8266WebServer.zip
http://www.14core.com/flashing-upgrade-eps8266-v1-firmware-with-espressif-flash-tool/
I was try to uninstall arduino also libraries then install again, put the libraries ESP8266WiFi.zip, ESP8266WebServer.zip and ITEADLIB_Arduino_Nextion-0.7.0 (stable version one). But still not working.
This is errors message :
Arduino: 1.8.1 (Windows XP), Board: “WeMos D1 R2 & mini, 80 MHz, 115200, 4M (1M SPIFFS)”
Touch_Nextion_6_Channel:7: error: ‘NexDSButton’ does not name a type
NexDSButton bt0 = NexDSButton(0, 1, “bt0”);
^
Touch_Nextion_6_Channel:8: error: ‘NexDSButton’ does not name a type
NexDSButton bt1 = NexDSButton(0, 1, “bt1”);
^
Touch_Nextion_6_Channel:9: error: ‘NexDSButton’ does not name a type
NexDSButton bt2 = NexDSButton(0, 1, “bt2”);
^
Touch_Nextion_6_Channel:10: error: ‘NexDSButton’ does not name a type
NexDSButton bt3 = NexDSButton(0, 1, “bt3”);
^
Touch_Nextion_6_Channel:11: error: ‘NexDSButton’ does not name a type
NexDSButton bt4 = NexDSButton(0, 1, “bt4”);
^
Touch_Nextion_6_Channel:12: error: ‘NexDSButton’ does not name a type
NexDSButton bt5 = NexDSButton(0, 1, “bt5”);
^
Touch_Nextion_6_Channel:18: error: ‘bt0’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
Touch_Nextion_6_Channel:18: error: ‘bt1’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
Touch_Nextion_6_Channel:18: error: ‘bt2’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
Touch_Nextion_6_Channel:18: error: ‘bt3’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
Touch_Nextion_6_Channel:18: error: ‘bt4’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
Touch_Nextion_6_Channel:18: error: ‘bt5’ was not declared in this scope
&bt0, &bt1, &bt2, &bt3, &bt4, &bt5,
^
C:\Documents and Settings\Me\My Documents\Arduino\Touch_Nextion_6_Channel\Touch_Nextion_6_Channel.ino: In function ‘void setup()’:
Touch_Nextion_6_Channel:24: error: ‘bt0’ was not declared in this scope
bt0.attachPop(bt0PopCallback, &bt0);
^
Touch_Nextion_6_Channel:25: error: ‘bt1’ was not declared in this scope
bt1.attachPop(bt0PopCallback, &bt1);
^
Touch_Nextion_6_Channel:26: error: ‘bt2’ was not declared in this scope
bt2.attachPop(bt0PopCallback, &bt2);
^
Touch_Nextion_6_Channel:27: error: ‘bt3’ was not declared in this scope
bt3.attachPop(bt0PopCallback, &bt3);
^
Touch_Nextion_6_Channel:28: error: ‘bt4’ was not declared in this scope
bt4.attachPop(bt0PopCallback, &bt4);
^
Touch_Nextion_6_Channel:29: error: ‘bt5’ was not declared in this scope
bt5.attachPop(bt0PopCallback, &bt5);
^
C:\Documents and Settings\Me\My Documents\Arduino\Touch_Nextion_6_Channel\Touch_Nextion_6_Channel.ino: In function ‘void bt0PopCallback(void*)’:
Touch_Nextion_6_Channel:41: error: ‘NexDSButton’ was not declared in this scope
NexDSButton *btn = (NexDSButton *)ptr;
^
Touch_Nextion_6_Channel:41: error: ‘btn’ was not declared in this scope
NexDSButton *btn = (NexDSButton *)ptr;
^
Touch_Nextion_6_Channel:41: error: expected primary-expression before ‘)’ token
NexDSButton *btn = (NexDSButton *)ptr;
^
Touch_Nextion_6_Channel:41: error: expected ‘;’ before ‘ptr’
NexDSButton *btn = (NexDSButton *)ptr;
^
Touch_Nextion_6_Channel:49: error: ‘bt0’ was not declared in this scope
bt0.getValue(&dual_state);
^
Touch_Nextion_6_Channel:50: error: ‘bt1’ was not declared in this scope
bt1.getValue(&dual_state);
^
Touch_Nextion_6_Channel:51: error: ‘bt2’ was not declared in this scope
bt2.getValue(&dual_state);
^
Touch_Nextion_6_Channel:52: error: ‘bt3’ was not declared in this scope
bt3.getValue(&dual_state);
^
Touch_Nextion_6_Channel:53: error: ‘bt4’ was not declared in this scope
bt4.getValue(&dual_state);
^
Touch_Nextion_6_Channel:54: error: ‘bt5’ was not declared in this scope
bt5.getValue(&dual_state);
^
exit status 1
‘NexDSButton’ does not name a type
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
I am using an Arduino MEGA with this code. I changed pins 2-6 on the 14core output to pins 34-38 on the MEGA. I can see functionality on the serial out of the Arduino seen below, but the command is not sending to the relay switch.. any suggestions?
[17recvRetCommandFinished ok
recvRetCommandFinished ok
setup done
[1755:0,1,bt0]
Callback
ptr=1755
recvRetNumber :0
recvRetNumber :1
recvRetNumber :1
recvRetNumber :1
recvRetNumber :1
recvRetNumber :1
yes because there is never declare the output’s , that’s the reason !
just input like that exmp. pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);
pinMode(R5, OUTPUT);
pinMode(R6, OUTPUT);
in section : void (setup)
Hi there!
Where can I find the Nextion library, because with my library doesn’t work?
Error message:
Nextion_relay:21: error: no ‘void NexTouch::sendCommand(const char*)’ member function declared in class ‘NexTouch’
void NexTouch::sendCommand(const char* cmd)
^
exit status 1
no ‘void NexTouch::sendCommand(const char*)’ member function declared in class ‘NexTouch’
Thanks for your answer!
hye can you give me the source coding?
Get the source code from here >
http://www.14core.com/wiring-the-hmi-nextion-tft-touch-display-on-6-channel-relay-module-with-microcontroller/
Great and interesting tutorial
but i always have this message :
⸮⸮⸮bkcmd=1⸮⸮⸮recvRetCommandFinished err
14:22:08.791 -> page 0⸮⸮⸮recvRetCommandFinished err
14:22:08.791 -> setup done
i don’t know what that mean ?
when i press my Nextion screen, the red TX led on my arduino switch on briefly
i change in the Nextion .h : serial1 to serial for working with UNO
i use other project to get information from Arduino to Nextion with success but on the other side Nextion to Arduino looks not good ?
Thanks if you can help, regards
Hi All,
I tried to simulate this program for controlling my relay in Nextion HMI. Here I am using ESP32 Micro Controller. Earlier I had this problem ‘NexDSButton’ does not name a type and then I copied the files from unstable Nextion version to my stable Nextion version 7.0 and then this problem didn’t occur.
Now my problem whenever I press the toggle button in my nextion display the relay is not turning on or off. Below I have attached my code,
#include “NexDualStateButton.h”
#include “Nextion.h”
#include “DHT.h”
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int R1 = 2;
uint32_t dual_state ;
NexText tTempC = NexText(0, 4, “tTempC”);
NexText tHumidity = NexText(0, 6, “tHumidity”);
NexDSButton bt0 = NexDSButton(1, 2, “bt0”);
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
&bt0,
NULL
};
void bt0PopCallback(void *ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln(“bt0PopCallback”);
dbSerialPrint(“ptr=”);
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
bt0.getValue(&dual_state);
if(dual_state>0)
{
digitalWrite(R1, HIGH);
}
else
{
digitalWrite(R1, LOW);
}
}
void setup(void) {
dht.begin();
Serial.begin(9600);
pinMode(R1, OUTPUT);
nexInit();
bt0.attachPop(bt0PopCallback, &bt0);
Serial.println(“DHT Sensor Reading”);
dbSerialPrintln(“setup done”);
}
void loop(void) {
nexLoop(nex_listen_list);
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial2.println(F(“Failed to read from DHT sensor!”));
return;
}
static char temperatureCTemp[10];
dtostrf(t, 6, 2, temperatureCTemp);
tTempC.setText(temperatureCTemp);
char hTemp[10] = {0};
utoa(int(h), hTemp, 10);
tHumidity.setText(hTemp);
Serial.print(F(“Humidity: “));
Serial.print(h);
Serial.print(F(“% Temperature: “));
Serial.print(t);
Serial.print(“°C “);
//Serial2.print(F(“tTempC.txt=”));
//Serial.print(“”);
//Serial.print(F(“tHumidity.txt:\ “));
//Serial.print(h);
//Serial.write(0xff);
//Serial.write(0xff);
//Serial.write(0xff);
}
I am looking for a solution. Kindly help me to sort out this problem. Can you please send me your Arduino coding and your HMI file to the following mail id: [email protected]
Thanks in Advance.
Hi all,
I tried to control the relay switching in nextion using esp32. I used a dual state button to control the relay but whenever I tried to control the relay there is some delay to pass the control signal to the relay. Like for example when I press the dual state button in nextion HMI to turn off the relay there is some delay of 10 to 15 seconds to turn off the relay. I am looking for help to solve this issue. Here I have atta ched my Arduino code below,
#include “NexDualStateButton.h”
#include “Nextion.h”
#include “DHT.h”
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int R1 = 2;
uint32_t dual_state ;
NexText tTempC = NexText(0, 4, “tTempC”);
NexText tHumidity = NexText(0, 6, “tHumidity”);
NexDSButton bt0 = NexDSButton(1, 2, “bt0”);
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
&bt0,
NULL
};
void bt0PopCallback(void *ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln(“bt0PopCallback”);
dbSerialPrint(“ptr=”);
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
bt0.getValue(&dual_state);
if(dual_state>0)
{
digitalWrite(R1, HIGH);
}
else
{
digitalWrite(R1, LOW);
}
}
void setup(void) {
dht.begin();
Serial.begin(9600);
pinMode(R1, OUTPUT);
nexInit();
bt0.attachPop(bt0PopCallback, &bt0);
Serial.println(“DHT Sensor Reading”);
dbSerialPrintln(“setup done”);
}
void loop(void) {
nexLoop(nex_listen_list);
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial2.println(F(“Failed to read from DHT sensor!”));
return;
}
static char temperatureCTemp[10];
dtostrf(t, 6, 2, temperatureCTemp);
tTempC.setText(temperatureCTemp);
char hTemp[10] = {0};
utoa(int(h), hTemp, 10);
tHumidity.setText(hTemp);
Serial.print(F(“Humidity: “));
Serial.print(h);
Serial.print(F(“% Temperature: “));
Serial.print(t);
Serial.print(“°C “);
//Serial2.print(F(“tTempC.txt=”));
//Serial.print(“”);
//Serial.print(F(“tHumidity.txt:\ “));
//Serial.print(h);
//Serial.write(0xff);
//Serial.write(0xff);
//Serial.write(0xff);
}
Fantastic Tutorial!!! I am pretty new to the Nextion screen and have tried a few other tutorials and was not successful. But this worked like a PERFECTLY!!! Thank You!!
Does anyone know how to change the words on the buttons? Or can anyone point be to a tutorial to do this?
Thank You ~Randy~
Hello,
please help me. I used your code on Ardunio Mega. Unfortunately my relay switching is not working. I can see communication on the serial port but no response. I found here in the questions , that it is necessary to add pinMode(R1, OUTPUT); to void setup. Unfortunately that didn’t help either. Is it possible to advise ? I am a complete beginner and can’t move on. Thanks a lot with regards
serial com
recvRetCommandFinished ok
recvRetCommandFinished ok
setup done
[1760:0,2,bt0]
[1760:0,2,bt0]
[1760:0,2,bt0]
program
#include “Nextion.h”
int S1 = 30, S2 = 31;
NexDSButton bt0 = NexDSButton(0, 2, “bt0”);
NexDSButton bt1 = NexDSButton(0, 3, “bt1”);
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
&bt0, &bt1,
NULL
};
void setup(void)
{
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
nexInit();
bt0.attachPop(bt0PopCallback, &bt0);
dbSerialPrintln(“setup done”);
}
void loop(void)
{
nexLoop(nex_listen_list);
}
void bt0PopCallback(void *ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
dbSerialPrintln(“Callback”);
dbSerialPrint(“ptr=”);
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
bt0.getValue(&dual_state);
if(dual_state){digitalWrite(S1, LOW);}else{digitalWrite(S1, HIGH);}
}
Hi!, Petr Just follow this link, https://www.14core.com/wp-content/uploads/2023/02/nextion-hmi-examples.zip and extract the zip file and navigate to the example. cheers! :)