HMI/NEXTION

[NEXTION] ESP32 연결하기

기하 2021. 12. 4. 14:10

NEXTION HMI를 ESP32와 사용하기 위한 설정방법을 설명하려고합니다.

 

[사용되는 재료]

- ESP_WOOM-32 (DOIT의 ESP DEVKIT v1 30핀 버전)

- Basic Nextion 3.2"  NX4024T032_011

- Nextion 4선식 전원 / UART 케이블포함

- lteadlib Arduino Nextion Library v0.90

- 이 ESP32에 필요한 CP210X 브릿지 드라이브

- Nextion Editor v0.35

- Arduino IDE v1.80

 

1 단계 :  lteadlib Arduino Nextion Library  다운로드

   ① Iteadlib Arduino Nextion 라이브러리 github 페이지로 이동
      > https://github.com/itead/ITEADLIB_Arduino_Nextion
  ② 복제 또는 다운로드를 클릭 하고 ZIP 다운로드
      > https://github.com/itead/ITEADLIB_Arduino_Nextion/archive/master.zip을 선택합니다.
  ③ 하드 드라이브에서 기억할 위치에 저장합니다(다음 단계의 경우).

 

2 단계 :  lteadlib Arduino Nextion Library  설치하기

   Arduino IDE 스케치 > 라이브러리 포함 > .ZIP 라이브러리 추가 에서

   라이브러리 zip 파일을 선택하고 엽니다.
   지시 사항을 보려면 라이브러리 readme.md 파일을 읽으십시오.

 

3 단계 :  Nextion Library  구성하기

 lteadlib는 ATMega 2560용으로 미리 구성되어 있습니다.

ESP32는 많은 동일한 구성을 사용하지만 모두가 필요한 것은 아니므로

작은 변경 사항에 세심한 주의를 기울이십시오.

 

① ESP32에는 3개의 직렬이 있으므로 다음처럼 조정한다 (Nexconfig.h)

    - dbSerial enable

    - nexSerial 을 Serial2

#define DEBUG_SERIAL_ENABLE
#define nexSerial Serial2   //  Serial2 : GPIO16/GPIO17

② NexHardware.cpp 화일을 수정한다.

    nexInit() 함수에서 Serial Monitor 와 Nextion 의 buadrate를 다음처럼 조정한다.

// dbSerial.begin(9600);
// nexSerial.begin(9600);

dbSerial.begin(250000);
nexSerial.begin(115200);

③ Nextion.h 을 다음처럼 조정한다. 

#include NexUpload.h

//#include NexUpload.h

④ 라이브러리 폴더에 있는  NexUpload 파일이름을 수정한다

–  NexUpload.h 을 NexUpload.h.txt 으로
–  NexUpload.cpp 을 NexUpload.cpp.txt 으로

 

 

The  Nextion HMI

4 단계 :  HMI  만들기

Picture Resources

    - 백그라운드로 사용할  화일을   Picture resource #0에 추가

Font Resources

   -  

Page 세팅 (page0, page1)

page0 page1
Button 컴포넌트    2개
Text 컴포넌트        3개 
Number 컴포넌트  1개
Timer 컴포넌트     1개
Button 컴포넌트 1개

      

5 단계 :  백그라운드 이미지 추가하기

6 단계 :  백그라운드 이미지 속성 변경하기

 ① page0.sta 값을 dropdown 에서 image로 변경

 ② page0.pic 값을 클릭 후 Picture Resource #0 를 선택

 ③ Event 패널의 Preinitialize Event에  다음 처럼 추가

bauds = 115200 // 3단계 세팅값과 같이 조정

7 단계 :  폰트 추가하기

8 단계 :  버튼 추가하기

 ① Button 컴포넌트 b0(30x100) 을 0,0 에 추가하기

 ② .txt 값에 Page1 입력후 클릭

 ③ Touch Release Event 에서 Send Component ID 를 체크하기

9 단계 :  텍스트0 추가하기

 ① Text 컴포넌트 t0(60x30) 을 0,82 에 추가하기

 ② .txt 값에 Read 입력후 클릭

 

10 단계 :  텍스트2 추가하기

 ① Text 컴포넌트 t2(80x30) 을 142,82 에 추가하기

 ② .txt 값에 Normal 입력후 클릭

 ③ .objname 에 t2

 ④ .bco 에 65504  (노랑)

 

11 단계 :  타이머 추가하기

 ① Timer 컴포넌트 tm0(80x30)를 142,82 추가하기

 ② .tim 에 50 

 ③ Timer Event 코드에 다음처럼 입력

if(n0.val>50)
{
  n0.bco=63488    // 빨강
  t2.txt="Too Hot!"
}else
{
  n0.bco=2016
  t2.txt="Normal" // 녹색
}

 

12 단계 :  Number Component  n0 추가하기

 ① Number 컴퍼넌트 n0(45x30)를 80, 82에 추가하기

13 단계 :  Text Component  t1 추가하기

 ① Text 컴포넌트 t1(140x30)을 0,154 에 추가

 ② .txt.maxl ---> 20     .txt ------> Arduino Control

 

14 단계 :  Button Component b1 추가하기

 ① Button 컴퍼넌트 b1 (100x30) 을 0,220 에 추가

 ② .txt --> D2 LED

 ③ Event 패널에서 Send Component ID 체크 (Press, Release 모두)

15 단계 :  page1 추가

 ① .bco -----> 0  // 검정색

 

16 단계 :  버튼0 추가하기

 ① Button 컴퍼넌트 b0 (100x30) 을 0,0 에 추가

 ② .txt --> Back

 ③ Event 패널에서 Send Component ID 체크 (Release )

       

17,18,19 단계 :  컴파일하여 SD 카드에 복사, 업로더

 

ESP32  Side

20 단계 :  Nextion Library 추가하기

#include "Nextion.h"

21 단계 :  HMI 컴포넌트 객체 생성하기

NexPage   p0    = NexPage  (0,0,"page0");
NexButton p0_b0 = NexButton(0,1,"b0");
NexButton p0_b1 = NexButton(0,7,"b1");
NexNumber p0_n0 = NexNumber(0,5,"n0");
NexText   p0_t1 = NexText  (0,6,"t1");

NexPage   p1    = NexPage  (1,0,"page1");
NexButton p1_b0 = NexButton(1,1,"b0");

22 단계 :  nex_listen_list[] 설정하기

// Send Componect ID 체크한 버튼들
NexTouch *nex_listen_list[] = { 
  &p0_b0, &p0_b1, &p1_b0, NULL
};

23 단계 :  변수 설정하기

uint32_t next, myInt = 0; // myInt : for updating number n0
                          // next : for when to trigger intermittent function
#define ledPin 2          // used to identify LED pin GPIO2

24 단계 :  버튼 클릭시 실행 함수 만들기

void p0_b1_Press(void *ptr) {     // LED on
  digitalWrite(ledPin, HIGH);
}

void p0_b1_Release(void *ptr) {   // LED off
  digitalWrite(ledPin, LOW);
}

void p0_b0_Release(void *ptr) {   // Page 1 으로가기
  p1.show();
}

void p1_b0_Release(void *ptr) {  // Page 1 으로가기
  p0.show();
}

25 단계 :  Intermittent 함수 만들기 n0 업데이트 위한

// myInt값이 0 에서 100 까지 증가하다 0으로 감소
void do_every_so_often() {
  myInt = (myInt + 1) % 101;
  p0_n0.setValue(myInt);   // page0 n0 값에 할당
}

26 단계 :  Setup() 만들기

void setup() 
{
  nexInit();                             // 초기화
  pinMode          (ledPin,OUTPUT);
  digitalWrite     (ledPin,LOW);
  p0_b0.attachPop  (p0_b0_Release, &p0_b0);
  p0_b1.attachPush (p0_b1_Press,   &p0_b1);
  p0_b1.attachPop  (p0_b1_Release, &p0_b1);
  p1_b0.attachPop  (p1_b0_Release, &p1_b0);
  p0_t1.setText    ("Arduino Text");
  next = millis();
}

27 단계 :  loop() 만들기

void loop() 
{
  nexLoop(nex_listen_list);

  if(millis() >= next) {
    next = millis()+500;
    do_every_so_often();
  }
}

28 단계 :   아두이노 IDE 컴파일

#include "Nextion.h"

NexPage   p0    = NexPage  (0,0,"page0");
NexButton p0_b0 = NexButton(0,1,"b0");
NexButton p0_b1 = NexButton(0,7,"b1");
NexNumber p0_n0 = NexNumber(0,5,"n0");
NexText   p0_t1 = NexText  (0,6,"t1");

NexPage   p1    = NexPage  (1,0,"page1");
NexButton p1_b0 = NexButton(1,1,"b0");

NexTouch *nex_listen_list[] = {
  &p0_b0, &p0_b1, &p1_b0, NULL
};

uint32_t next, myInt = 0;
#define ledPin 2

void p0_b1_Press(void *ptr) {
  digitalWrite(ledPin, HIGH);
}

void p0_b1_Release(void *ptr) {
  digitalWrite(ledPin, LOW);
}

void p0_b0_Release(void *ptr) {
  p1.show();
}

void p1_b0_Release(void *ptr) {
p0.show();
}

void do_every_so_often() {
  myInt = (myInt + 1) % 101;
  p0_n0.setValue(myInt);
}

void setup() 
{
  nexInit();                             // 초기화
  pinMode          (ledPin,OUTPUT);
  digitalWrite     (ledPin,LOW);
  p0_b0.attachPop  (p0_b0_Release, &p0_b0);
  p0_b1.attachPush (p0_b1_Press,   &p0_b1);
  p0_b1.attachPop  (p0_b1_Release, &p0_b1);
  p1_b0.attachPop  (p1_b0_Release, &p1_b0);
  p0_t1.setText    ("Arduino Text");
  next = millis();
}

void loop() {
  nexLoop(nex_listen_list);
  if(millis() >= next) {
    next = millis()+500;
    do_every_so_often();
  }
}

29 단계 : 

Nextion RX    (yellow) connected to ESP32 Serial2 TX2
Nextion TX    (blue)    connected to ESP32 Serial2 RX2
Nextion GND (black)   connected to ESP32 GND
Nextion 5V    (red)     connected to ESP32 VIN (Powered from USB)

 

Nextion Button page0 b0            – notifies the ESP32, the ESP32 changes page to page 1
Nextion Button page1 b0            – notifies the ESP32, the ESP32 changes page to page 0
Nextion Button page 0 b1 press   – notifies the ESP32, and LED is lit while pressed
Nextion Button page 0 b1 release – notifies the ESP32, and LED turns off when released

Nextion side timer code evaluates n0 – updates n0 background and t2.txt based on value

 

the ESP32 updates page0 t1 to “Arduino Text” from setup In loop, periodic conditional and bridled

– update MCU side myInt and updates page0 n0

[인용번역] https://nextion.ca/portfolio-items/nextion-iteadlib-and-esp32-step-by-step/

 

Nextion, Iteadlib and ESP32:

In this post, I will show how to setup an ESP32 for use with Iteadlib with a basic Nextion HMI. This basic project will meet the following requirements – MCU to change pages in response to Button release – MCU to toggle LED on GPIO2 on Button press/rel

nextion.ca

 

'HMI > NEXTION' 카테고리의 다른 글

[NEXTION] 아두이노 우노와 연결하기  (0) 2021.12.04