Loading...

You have no items in your shopping cart.

 
 
 
×

Registration

Profile Informations

Login Datas

or login

First name is required!
Last name is required!
First name is not valid!
Last name is not valid!
This is not an email address!
Email address is required!
This email is already registered!
Password is required!
Enter a valid password!
Please enter 6 or more characters!
Please enter 16 or less characters!
Passwords are not same!
Terms and Conditions are required!
Email or Password is wrong!
Captcha is required!
Captcha is wrong!

Nextion Tutorial-Based on Nextion Arduino Library

Hi,everybody! From this tutorial,you will learn how to use the ITEADLIB_Arduino_Nextion_Library to work with Nextion.

There are 3 chapters in the tutorial.

At first,you'd better download the Library and HMI files and Nextion Editor below:

1. ITEADLIB Arduino Nextion Library

2.HMI files: Chapter1 Chapter2  Chapter3

3. The latest Nextion Editor 

Second,prepare below hardwares:

1. An Iteaduino UNO or MEGA2560

2. 1-4pcs Light bricks or Relay modules or Buzzer bricks

3. An RTC module

4. A 10k NTC Temperature Sensor with Steel Head and Plastic Connector and a 6.4K resistor

5. A breadboard or protoboard

6. 20pcs 1pin dupons(female&male or headers)

7. USB wire for UNO or MEGA

8. A Nextion display,you also need a USB-TTL module to download the project,here we use Foca.

Ok, let`s get started!

Chapter 1-Touch_Switch: To Control Your MCU

In this tutorial, we take Iteaduino MEGA2560 for example. There are instructions for using Iteaduino UNO at the end of this post.

1.Open Nextion Editor and create a new .HMI project file. or open our original HMI file.

2.Choose a suitable background image, create 4 new control buttons and 5 text components as showed by the following image, you can modify and adjust according to your own need.

Nextion Editor Quick Start Guide

3.Note that to send the data of the events raised by new button components, you must tick “Send key value” as shown below:

4.Download the project file to Nextion with Foca.
Nextion     Foca(USB—TTL)
TX    →    RX
RX    →    TX
G    →     G
V    →     V

5. Download and install arduino IDE, and download Nextion_library from ITEAD github page and stored them in your
\arduino\arduino-1.x.x\libraries

6.Next, open arduino IDE, create a new project file, write programs based on Nextion library to get four buttons control switches.
Source Code as below:


#include "NexButton.h"
#include "NexText.h"

NexButton b0 = NexButton(0, 2, "b0");
NexButton b1 = NexButton(0, 3, "b1");
NexButton b2 = NexButton(0, 4, "b2");
NexButton b3 = NexButton(0, 5, "b3");

char buffer[10] = {0};

NexTouch *nex_Listen_List[] = 
{
    &b0,
    &b1,
    &b2,
    &b3,
    NULL
};

/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b0PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(9, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(9, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
   
}

/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b1PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
         digitalWrite(10, HIGH);
         strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(10, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}
/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b2PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(11, HIGH);
        strcpy(buffer, "ON");
     
    }
    else
    {
        digitalWrite(11, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}
/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b3PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(12, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(12, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}

void setup(void)
{
    nexInit();
    b0.attachPop(b0PopCallback, &b0);
    b1.attachPop(b1PopCallback, &b1);
    b2.attachPop(b2PopCallback, &b2);
    b3.attachPop(b3PopCallback, &b3);
//setting port OUTPUT
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
//initializing  port 
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
}

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


7. Note that when writing code, the components are operated by monitoring Page ID, Component ID, and Component Name, as shown:

8.In the code,you can see the hardwares connection:
Light LED    MEGA2560
Sinal    →     9
G       →       G
V       →       V

UV LED    MEGA2560
Sinal   →   10
G       →       G
V       →       V

Pump LED    MEGA2560
Sinal     →     11
G        →        G
V        →         V

Food LED    MEGA2560
Sinal    →     12
G        →       G
V        →        V

Nextion     MEGA2560
TX     →     RX2
RX     →     TX2
G       →       G
V       →       V

9. Download the source code to maga2560, then you can tap the screen to achieve effect demonstrated as below:

How to work with UNO?

1.Open the file “NexConfig.h” from ITEADLIB_Arduino_Nextion_Library.

* Comment out debug serial. #define DEBUG_SERIAL_ENABLE —> //#define DEBUG_SERIAL_ENABLE

* Modify the definition of serial 2 as the default serial. #define nexSerial Serial2  —> #define nexSerial Serial

2. Download from arduino IDE to UNO without Nextion display connection.

3. Can not use the simulation function of Nextion Editor with UNO connecting Nextion display.

5. Hardware connection is the same as Mega, but TX RX(UNO) are instead of TX2 RX2(MEGA).

Chapter 2-Time and Tem: Receve data from your MCU

Through studying the chapter 1, I believe you have known how to send commands to MCU, right? In this chapter, we will add clock and temperature real-time display on the basis of chapter 1.So if you have not learnt chapter 1, don’t be lazy, start to read it now.

1. Open your HMI file of chapter 1 or the original HMI file of chapter 2.

2. Creat t5-t11 text components as showed by the following image, you can modify according to your own need.

3.Download the HMI project file to your Nextion display with Foca and connect to your mega2560(the same as chapter 1)

4. Download the RTC library and store it in your arduino library folder.

5.Open arduino IDE to create a new project file, on which you can write clock display programs according to Nextion library and RTC library.
The source code is below:


#include "NexButton.h"
#include "NexText.h"
#include "Arduino.h"
#include "Wire.h"
#include "RTCLib.h"


NexButton b0 = NexButton(0, 2, "b0");
NexButton b1 = NexButton(0, 3, "b1");
NexButton b2 = NexButton(0, 4, "b2");
NexButton b3 = NexButton(0, 5, "b3");
NexText t5 = NexText(0, 10, "t5");
NexText t6 = NexText(0, 11, "t6");
NexText t7 = NexText(0, 12, "t7");
NexText t8 = NexText(0, 13, "t8");
NexText t9 = NexText(0, 14, "t9");

char buffer[10] = {0};
char buffer_temp[10] = {0};
char buffer_hour[10] = {0};
char buffer_week[10] = {0};
char buffer_second[10] = {0};
char buffer_year[10] = {0};
char buffer_temperature[10] = {0};

RTCLib rtc;

uint8_t number = 0;
uint16_t sensor_pin = A0;    // select the input pin for the potentiometer
uint16_t sensor_value = 0;  // variable to store the value coming from the sensor
uint16_t temperature = 0;
uint16_t temp = 0;

NexTouch *nexListenList[] = 
{
    &b0,
    &b1,
    &b2,
    &b3,
    &t5,
    &t6,
    &t7,
    &t9,
    &t8,
    NULL
};



/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b0PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(9, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(9, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
   
}

/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b1PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
         digitalWrite(10, HIGH);
         strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(10, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}
/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b2PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(11, HIGH);
        strcpy(buffer, "ON");
     
    }
    else
    {
        digitalWrite(11, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}
/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b3PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(12, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(12, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}

void updateTime()
{
    memset(buffer, 0, sizeof(buffer)); 
    number = rtc.hour();
    itoa(number, buffer, 10);
    strcat(buffer, ":");
    number = rtc.minute();
    itoa(number, buffer_temp, 10);
    if (rtc.minute() < 10)
    {
        strcat(buffer, "0");
    }
    strcat(buffer, buffer_temp);
    strcat(buffer, ":");
    if (strcmp(buffer_hour, buffer))
    {
        t6.setText(buffer);
        strcpy(buffer_hour, buffer); 
    }
        
    memset(buffer, 0, sizeof(buffer)); 
    memset(buffer_temp, 0, sizeof(buffer_temp)); 
        
    number = rtc.second();
    itoa(number, buffer_temp, 10);
    if (number < 10)
    {
        strcpy(buffer, "0");
        strcat(buffer, buffer_temp);
        if (strcmp(buffer, buffer_second))
        {
            t9.setText(buffer);
            strcpy(buffer_second, buffer);
         }
    } 
    else
    {
        if (strcmp(buffer_temp, buffer_second))
        {
            t9.setText(buffer_temp);
            strcpy(buffer_second, buffer_temp);
        }
    }
        
/*week's data dispaly*/
 
    switch (rtc.dayOfWeek())
    {
        case 1:
                strcpy(buffer, "Sunday");
                break;
        case 2: 
                strcpy(buffer, "Monday");
                break;
        case 3:
                strcpy(buffer, "Tuesday");
                break;
        case 4:
                strcpy(buffer, "Wednesday");
                break;
        case 5:
                strcpy(buffer, "Thursday");
                break;
        case 6:
                strcpy(buffer, "Friday");
                break;
        case 7:
                strcpy(buffer, "Saturday");
                break;
        default:
                strcpy(buffer, "fail");
                break;
    }

    if (strcmp(buffer_week, buffer))
    {
        t7.setText(buffer);
        strcpy(buffer_week, buffer);
    }

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));
      
/*receive  year's data*/
    number = rtc.year();
    strcat(buffer, "20");
    itoa(number, buffer_temp, 10);
    strcat(buffer, buffer_temp);
    strcat(buffer, "/");
        
/*receive  month's data*/ 
    number = rtc.month();
    itoa(number, buffer_temp, 10);
    strcat(buffer, buffer_temp);
    strcat(buffer, "/");

/*receive  day's data*/
    number = rtc.day();
    itoa(number, buffer_temp, 10);
    strcat(buffer, buffer_temp);
        
/*send to Nextion txt5*/
    if (strcmp(buffer_year, buffer))
    {
        t5.setText(buffer);
        strcpy(buffer_year, buffer);
    }
}
void ntcTemperature()
{
    sensor_value = analogRead(sensor_pin);
    temp = sensor_value-185;
    temperature = 100-temp/5.4;

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));

    itoa(temperature, buffer, 10);
    
    if (strcmp(buffer, buffer_temperature))
    {
        t8.setText(buffer);
        strcpy(buffer_temperature, buffer);
    }
    delay(100);
    
  
}
void setup(void)
{
    dbSerial.begin(9600);
    nexInit();
    b0.attachPop(b0PopCallback, &b0);
    b1.attachPop(b1PopCallback, &b1);
    b2.attachPop(b2PopCallback, &b2);
    b3.attachPop(b3PopCallback, &b3);
    dbSerial.println("setup done");
/*setting port OUTPUT*/
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
/*initializing  port */
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
/* Only used once, then disabled
 *rtc.set(0, 5, 16, 6, 28, 7, 15);
 *RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte
 *dayOfMonth, byte month, byte year);*/

 }

void loop(void)
{
    unsigned long nowtime = millis(); 
    unsigned long update = 10000;
    if (nowtime > update)
    {
        ntcTemperature();
        update+=10000;
    }
    nexLoop(nexListenList);
    rtc.refresh();
    updateTime();
    
}

6.After download the code to your MEGA,we are going to put a 6.4K resisitor in series to the NTC. As below:

view the source code for details. (Temperature details are for reference only–Temperature Coefficient=(the highest known temperature – the lowest known temperature) / (the read value of change in adc )) (available in serial port monitoring) as shown below:

7. In the code,you can see the connection of hardwares(the connection of the four LED bricks are not changed ):

RTC     MEGA2560
SDA   →    SDA
SCL   →    SCL
V      →      V
G      →      G

NTC     MEGA2560
V     →      A0
V     →      V
G     →      G

8.Now,everything is done! Power on your maga2560, and you can see the effect demonstrated as below(48℃ is for a cup of warm water):

Chapter 3-Interactions between Nexiton and MCU

In last two chapters we realize one-way send and receive data. In this chapter, we will learn how to implement Nextion and MUC two-way communication. For example: achieve MCU clock settings from Nextion display.

1. Open the HMI file of chapter2 or the original HMI file of chapter 3. Then create one “set” button and split the time and date by more text components as showed by the following image.

2. Create page l, on which create 3 button and 12 text components, then modify as below(random naming):

(Note that for events raised by the buttons or text components, you must tick”Send key value” )

3.Download the HMI project file to your Nextion display with Foca, then connect Nextion display to your mega2560(the same as chapter 1)

4. Modify the source code based on last two chapters, complete time-setting code writing.(You must pay attention to date line issue while writing code. ) The soure code is below:


#include "NexButton.h"
#include "NexText.h"
#include "Arduino.h"
#include "Wire.h"
#include "RTCLib.h"
#include "NexPage.h"


NexButton b0 = NexButton(0, 2, "b0");
NexButton b1 = NexButton(0, 3, "b1");
NexButton b2 = NexButton(0, 4, "b2");
NexButton b3 = NexButton(0, 5, "b3");
NexButton b4 = NexButton(0, 17, "b4");
NexText t5 = NexText(0, 10, "t5");
NexText t6 = NexText(0, 11, "t6");
NexText t7 = NexText(0, 12, "t7");
NexText t8 = NexText(0, 13, "t8");
NexText t9 = NexText(0, 14, "t9");
NexText t13 = NexText(0, 19, "t13");
NexText t16 = NexText(0, 22, "t16");
NexText t18 = NexText(0, 24, "t18");
NexButton d6 = NexButton(1, 15, "d6");
NexText d7 = NexText(1, 14, "d7");
NexPage page0 = NexPage(0, 0, "page0");
NexPage page1 = NexPage(1, 0, "page1");
NexButton k1 = NexButton(1, 2, "k1");
NexButton k2 = NexButton(1, 3, "k2");
NexButton k3 = NexButton(1, 4, "k3");
NexButton k4 = NexButton(1, 5, "k4");
NexButton k5 = NexButton(1, 6, "k5");
NexButton k6 = NexButton(1, 7, "k6");
NexButton k7 = NexButton(1, 8, "k7");
NexButton k8 = NexButton(1, 9, "k8");

char buffer[10] = {0};
char buffer_temp[10] = {0};
char buffer_year[10] = {0};
char buffer_month[10] = {0};
char buffer_day[10] = {0};
char buffer_hour[10] = {0};
char buffer_minute[10] = {0};
char buffer_second[10] = {0};
char buffer_week[10] = {0};

int8_t settime_type = -1;
int8_t settime_up = -1;
int8_t number_settime_year = 0;
int8_t number_settime_month = 0;
int8_t number_settime_day = 0;
int8_t number_settime_hour = 0;
int8_t number_settime_minute = 0;
int8_t number_settime_week = 0;

char buffer_temperature[10] = {0};

RTCLib rtc;

uint8_t number = 0;
uint16_t sensor_pin = A0;    /*select the input pin for the potentiometer*/
uint16_t sensor_value = 0;  /* variable to store the value coming from the sensor*/
uint16_t temperature = 0;
uint16_t temp = 0;

NexTouch *nex_Listen_List[] = 
{
    &b0, &b1, &b2, &b3, &b4,
    &t5, &t6, &t7, &t9, &t8,
    &t13, &t16, &t18, &d7,
    &k1, &k2, &k3, &k4, &k5,
    &k6, &k7, &k8, &d6, &page0,
    &page1,
    NULL
};

void b4PopCallback(void *ptr)
{
    page1.show();
    k3.setText(buffer_year);
    k4.setText(buffer_month);
    k5.setText(buffer_day);
    k6.setText(buffer_hour);
    k7.setText(buffer_minute);
    k8.setText(buffer_week);
    number_settime_year = atoi(buffer_year);
    number_settime_month = atoi(buffer_month);
    number_settime_day = atoi(buffer_day);
    number_settime_hour = atoi(buffer_hour);
    number_settime_minute = atoi(buffer_minute);
    if(strcmp(buffer_week, "Sunday") == 0)
    {
        number_settime_week = 1;
    }
    if(strcmp(buffer_week, "Monday") == 0)
    {
        number_settime_week = 2;
    }
    if(strcmp(buffer_week, "Tuesday") == 0)
    {
        number_settime_week = 3;
    }
    if(strcmp(buffer_week, "Wednesday") == 0)
    {
        number_settime_week = 4;
    }
    if(strcmp(buffer_week, "Thursday") == 0)
    {
        number_settime_week = 5;
    }
    if(strcmp(buffer_week, "Friday") == 0)
    {
        number_settime_week = 6;
    }
    if(strcmp(buffer_week, "Saturday") == 0)
    {
        number_settime_week = 7;
    }
    if(strcmp(buffer_week, "Fail") == 0)
    {
        number_settime_week = -1;
    }
    
    
}

void d6PopCallback(void *ptr)
{
    rtc.set(0, number_settime_minute, number_settime_hour, 
            number_settime_week, number_settime_day, 
            number_settime_month, number_settime_year);
}

void d7PopCallback(void *ptr)
{
    page0.show();
    t5.setText(buffer_year);
    t16.setText(buffer_month);
    t18.setText(buffer_day);
    t6.setText(buffer_hour);
    t13.setText(buffer_minute);
    t7.setText(buffer_week);
}


void k1PopCallback(void *ptr)
{
    settime_up = 1;
    setTime();
}

void k2PopCallback(void *ptr)
{
    settime_up = 0;
    setTime();
}

void k3PopCallback(void *ptr)
{
    settime_type = 1;   
}

void k4PopCallback(void *ptr)
{
    settime_type = 2;
}

void k5PopCallback(void *ptr)
{
    settime_type = 3;
}

void k6PopCallback(void *ptr)
{
    settime_type = 4;
}

void k7PopCallback(void *ptr)
{
    settime_type = 5;
}

void k8PopCallback(void *ptr)
{
    settime_type = 6;

}

void setTime(void)
{
    switch (settime_type)
    {
        case 1: 
            if (settime_up == 1)
            {
               number_settime_year++;
               if (number_settime_year > 99)
               {
                    number_settime_year = 10;
               }
               memset(buffer, 0, sizeof(buffer));  
               itoa(number_settime_year, buffer, 10);
               k3.setText(buffer);
            }   
            else if (settime_up == 0)
            {
                number_settime_year--;
                if (number_settime_year < 10)
                {
                    number_settime_year = 99;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_year, buffer, 10);
                k3.setText(buffer);
            }
            break;
        case 2:
            if (settime_up == 1)
            {
                number_settime_month++;
                if (number_settime_month > 12)
                {
                    number_settime_month = 1;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_month, buffer, 10);
                k4.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_month--;
                if (number_settime_month < 1)
                {
                    number_settime_month= 12;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_month, buffer, 10);
                k4.setText(buffer);
            }
            break;
        case 3:
        {
            memset(buffer, 0, sizeof(buffer)); 
            k4.getText(buffer,sizeof(buffer));
            uint8_t dayofmonth = atoi(buffer);
            if (settime_up == 1)
            {
                number_settime_day++;
                if (dayofmonth == 1 || dayofmonth == 3 || dayofmonth == 5 
                    || dayofmonth == 7 || dayofmonth == 8 || dayofmonth == 10
                    || dayofmonth == 12 )
                {
                    if (number_settime_day > 31)
                    {
                        number_settime_day = 1;
                    }
                }
                if (dayofmonth == 4 || dayofmonth == 6 || dayofmonth == 9 
                    || dayofmonth == 11)
                {
                    if (number_settime_day > 30)
                    {
                        number_settime_day = 1;
                    }
                }
                if (dayofmonth == 2)
                {
                    if(number_settime_day > 28)
                    {
                        number_settime_day = 1;
                    }
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_day, buffer, 10);
                k5.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_day--;
                if (dayofmonth == 1 || dayofmonth == 3 || dayofmonth == 5 
                    || dayofmonth == 7 || dayofmonth == 8 || dayofmonth == 10
                    || dayofmonth == 12 )
                {
                    if (number_settime_day < 1)
                    {
                        number_settime_day = 31;
                    }
                }
                if (dayofmonth == 4 || dayofmonth == 6 || dayofmonth == 9 
                    || dayofmonth == 11)
                {
                    if (number_settime_day < 1)
                    {
                        number_settime_day = 30;
                    }
                }
                if (dayofmonth == 2)
                {
                    if(number_settime_day < 1)
                    {
                        number_settime_day = 28;
                    }
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_day, buffer, 10);
                k5.setText(buffer);
            }
            break;
        }
        case 4:
            if (settime_up == 1)
            {
                number_settime_hour++;
                if (number_settime_hour > 23)
                {
                    number_settime_hour = 0;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_hour, buffer, 10);
                k6.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_hour--;
                if (number_settime_hour < 0)
                {
                    number_settime_hour = 23;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_hour, buffer, 10);
                k6.setText(buffer);
            }
            break;
        case 5:
            if (settime_up == 1)
            {
                number_settime_minute++;
                if (number_settime_minute > 59)
                {
                    number_settime_minute = 0;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_minute, buffer, 10);
                k7.setText(buffer);
            }
            else if (settime_up == 0)
            {
                number_settime_minute--;
                if (number_settime_minute < 0)
                {
                    number_settime_minute = 59;
                }
                memset(buffer, 0, sizeof(buffer));  
                itoa(number_settime_minute, buffer, 10);
                k7.setText(buffer);
            }
            break;                
        case 6:
            if (settime_up == 1)
            {
                number_settime_week++;
                if (number_settime_week > 7)
                {
                    number_settime_week = 1;
                }  
            }
            else if (settime_up == 0)
            {
                number_settime_week--;
                if (number_settime_week < 1)
                {
                    number_settime_week = 7;
                }
            }
            switch (number_settime_week)
            {
                case 1:
                        strcpy(buffer, "Sunday");
                        break;
                case 2: 
                        strcpy(buffer, "Monday");
                        break;
                case 3:
                        strcpy(buffer, "Tuesday");
                        break;
                case 4:
                        strcpy(buffer, "Wednesday");
                        break;
                case 5:
                        strcpy(buffer, "Thursday");
                        break;
                case 6:
                        strcpy(buffer, "Friday");
                        break;
                case 7:
                        strcpy(buffer, "Saturday");
                        break;
                default:
                        strcpy(buffer, "fail");
                        break;
            }
            k8.setText(buffer);
            break;
    }
}

/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b0PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(9, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(9, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
}

/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b1PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
         digitalWrite(10, HIGH);
         strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(10, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}
/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b2PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(11, HIGH);
        strcpy(buffer, "ON");
     
    }
    else
    {
        digitalWrite(11, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}
/**
 * Button to return the response.
 * 
 * @param ptr - the parameter was transmitted to pop event function pointer. 
 * 
 */
void b3PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"ON"))
    {
        digitalWrite(12, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(12, LOW);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}

void timeDisplay()
{
    memset(buffer, 0, sizeof(buffer)); 
    number = rtc.hour();
    itoa(number, buffer, 10);
    if (strcmp(buffer_hour, buffer))
    {
        t6.setText(buffer);
        strcpy(buffer_hour, buffer); 
    }

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));
    number = rtc.minute();
    itoa(number, buffer_temp, 10);
    if (rtc.minute() < 10)
    {
        strcat(buffer, "0");
    }
    strcat(buffer, buffer_temp);
    if (strcmp(buffer_minute, buffer))
    {
        t13.setText(buffer);
        strcpy(buffer_minute, buffer); 
    }
    
    memset(buffer, 0, sizeof(buffer)); 
    memset(buffer_temp, 0, sizeof(buffer_temp)); 
    number = rtc.second();
    itoa(number, buffer_temp, 10);
    if (number < 10)
    {
        strcpy(buffer, "0");
        strcat(buffer, buffer_temp);
        if (strcmp(buffer, buffer_second))
        {
            t9.setText(buffer);
            strcpy(buffer_second, buffer);
         }
    } 
    else
    {
        if (strcmp(buffer_temp, buffer_second))
        {
            t9.setText(buffer_temp);
            strcpy(buffer_second, buffer_temp);
        }
    }
        
 
    switch (rtc.dayOfWeek())
    {
        case 1:
                strcpy(buffer, "Sunday");
                break;
        case 2: 
                strcpy(buffer, "Monday");
                break;
        case 3:
                strcpy(buffer, "Tuesday");
                break;
        case 4:
                strcpy(buffer, "Wednesday");
                break;
        case 5:
                strcpy(buffer, "Thursday");
                break;
        case 6:
                strcpy(buffer, "Friday");
                break;
        case 7:
                strcpy(buffer, "Saturday");
                break;
        default:
                strcpy(buffer, "fail");
                break;
    }

    if (strcmp(buffer_week, buffer))
    {
        t7.setText(buffer);
        strcpy(buffer_week, buffer);
    }

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));
    number = rtc.year();
    itoa(number, buffer, 10);
    if (strcmp(buffer_year, buffer))
    {
        t5.setText(buffer);
        strcpy(buffer_year, buffer);
    }
    
    memset(buffer, 0, sizeof(buffer));     
    number = rtc.month();
    itoa(number, buffer, 10);
    if (strcmp(buffer_month, buffer))
    {
        t16.setText(buffer);
        strcpy(buffer_month, buffer);
    }
    
    memset(buffer, 0, sizeof(buffer));
    number = rtc.day();
    itoa(number, buffer, 10);
    if (strcmp(buffer_day, buffer))
    {
        t18.setText(buffer);
        strcpy(buffer_day, buffer);
    }
}

void ntcTemperature()
{
    sensor_value = analogRead(sensor_pin);
    temp = sensor_value-185;
    temperature = 100-temp/5.4;

    memset(buffer, 0, sizeof(buffer));
    memset(buffer_temp, 0, sizeof(buffer_temp));

    itoa(temperature, buffer, 10);
    
    if (strcmp(buffer, buffer_temperature))
    {
        t8.setText(buffer);
        strcpy(buffer_temperature, buffer);
    }
    delay(100);
}

void setup(void)
{
    nexInit();
    b0.attachPop(b0PopCallback, &b0);
    b1.attachPop(b1PopCallback, &b1);
    b2.attachPop(b2PopCallback, &b2);
    b3.attachPop(b3PopCallback, &b3);
    b4.attachPop(b4PopCallback);
    d6.attachPop(d6PopCallback);
    d7.attachPop(d7PopCallback);
    k1.attachPop(k1PopCallback);
    k2.attachPop(k2PopCallback);
    k3.attachPop(k3PopCallback);
    k4.attachPop(k4PopCallback);
    k5.attachPop(k5PopCallback);
    k6.attachPop(k6PopCallback);
    k7.attachPop(k7PopCallback);
    k8.attachPop(k8PopCallback);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT); 
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    /* RTCLib::set(byte second, byte minute, byte hour, 
     * byte dayOfWeek, byte dayOfMonth, byte month, byte year);
     */
    rtc.set(0, 5, 16, 3, 15, 2, 16); 
}

void loop(void)
{
    unsigned long nowtime = millis(); 
    unsigned long update = 0;
    if (nowtime > update)
    {
        ntcTemperature();
        update += 10000;
    }
    nexLoop(nex_Listen_List);
    rtc.refresh();
    timeDisplay();
    
}


5.This tutorial use two buttons to control the date’s plus and minus, which use settime_type to define current year_month_day or hour_minute. (When press the button of ‘year’, settime_type = 1)

6.After complete setting date and time, then jump to page 0, where you must send time information again to the screen, as follows:

7. Download the source code written in arduino software to maga2560, and you can see the effect demonstrated as below:

GOOD NEWS! Our smart fish tank V2.0 has completed now! Click to Learn more about it.