HMI/NEXTION

[NEXTION] 아두이노 우노와 연결하기

기하 2021. 12. 4. 13:26

Nextion HMI 감압식터치스크린 NX8048P070-011R과 아두이 우노와 연결해보자.

 

1. Nextion 라이브러리 폴더에 있는 "Nexconfig.h" 수정하기

/** 
 * 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

#include <SoftwareSerial.h>
extern SoftwareSerial HMISerial;
#define nexSerial HMISerial

#define DEBUG_SERIAL_ENABLE
#define nexSerial 

을 사용하지 않기 위해 주석처리한다.

 

#define nexSerial Serial2 을 아래처럼 수정한다


#include <SoftwareSerial.h>
extern SoftwareSerial HMISerial;
#define nexSerial HMISerial

 

2.  아두이노 소프트웨어 시리얼 사용하기

 

아두이노에서 소프트웨어 시리얼을 다음처럼 지정한다.

SoftwareSerial HMISerial(10,11)  // RX, TX

 

3. Nextion rhk 아두이노 우노 핀 연결하기

Nextion 아두이노 우노
TX 10
RX 11

 

4. 아두이노 소스

#include "Nextion.h"
SoftwareSerial HMISerial(10, 11);

NexText     t0 =  NexText    (0, 2, "t0");
NexSlider   h0 =  NexSlider  (0, 1, "h0");
NexGauge    z0  = NexGauge   (0, 3, "z0");
NexDSButton bt0 = NexDSButton(0, 5, "bt0");

NexTouch *nex_listen_list[] = {&h0, &bt0, NULL};

void bt0PopCallback(void *ptr)
{
  uint32_t dual_state;
  NexDSButton *btn = (NexDSButton *)ptr;
  
  dbSerialPrintln    ("b0PopCallback");
  dbSerialPrint      ("ptr=");
  dbSerialPrintln    ((uint32_t)ptr);
  
  bt0.getValue(&dual_state);
  
  if (dual_state)               
  {
    digitalWrite(13, HIGH);           // if my dual state buton is pressed
  }
  else
  {
    digitalWrite(13, LOW);            // if not
  }

}
void h0PopCallback(void *ptr)
{
  int      numberx  = 0;
  int      gosterge = 0;
  uint32_t number   = 0;
  char     temp[10] = {0};
  
  dbSerialPrintln("h0PopCallback");
  h0.getValue    (&number);
  utoa           (number, temp, 10);
  t0.setText     (temp);
  numberx = number;
  gosterge = map (numberx, 0, 100, 0, 180);
  z0.setValue    (gosterge);
}

void setup(void)
{
  pinMode        (13, OUTPUT);
  nexInit        ();
  h0.attachPop   (h0PopCallback);
  bt0.attachPop  (bt0PopCallback, &bt0);
  dbSerialPrintln("setup done");
}

void loop(void)
{
  nexLoop(nex_listen_list);
}

 

Nextion_Arduino_UNO.HMI
4.35MB

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

[NEXTION] ESP32 연결하기  (0) 2021.12.04