Start a new topic

Nextion Arduino Tutorial without Nextion library

There is nothing worse that trying to do something with a Nextion without the needed information. With more tabs open in your internet browser than the money spent on your house looking for information, you loose track of time and before you know its way past your bedtime. By then you realize that you have achieved nothing more than when you started with your bright idea for the next million dollar gadget. 


I have been there, done that, got the T-shirt and every trophy. I have spent so many hours in my office trying to figure out how to get the Nextion display to do what I want it to do that my own dog started barking at me.


That said, I am willing to share what I managed to figure out. When I started out, I did not use the Nextion.h Library for arduino. When I eventually downloaded the library, I was not in the mood to spend another week to learn how to use it.


So here we go.

----------------------------------------------------------------------------------------------------------------------

How to write information to you newly purchased Nextion.


Use the Nextion IDE to design your Nextion layout.

Add a text box on your display. 

imageLook at the ID in on the right hand side. In this case it is called t0. Upload to your Nextion display.


 Now  connect the RX wire on the Nextion to the TX on the Arduino. If you use an external power supply for the Nextion, remeber to connect ground from the Arduino to ground of the external power supply.


Lets have a look at the Arduino code.


void setup()

{

  Serial.begin(9600);

}


void loop()

{

    Serial.print("t0.txt=");

    Serial.write(0x22);

    Serial.print("Hello Nextion");

    Serial.write(0x22);

    Serial.write(0xff);

    Serial.write(0xff);

    Serial.write(0xff);  

}


----------------------------------------------------------------------------------------------------------------------


How to use the Crop feature

image


After designing your layout, use the + button, like above to add your picture.

For this example, the picture will change when a Input is triggered on the Arduino.

Add the next two pictures.


image

and

image


Click the Sti tab on the right like below and select "Image".Double click PIC tab and select the first picture without the yellow gear like below.

image


Next up, click the crop button on the left like below.

image


Stretch and place the block over the circle like below.

image


Double click the picc tab on the right and once again select the picture without the gear. Upload to your display.


Next up is to code your Arduino.


int button1 = 8

int buttonstate = 0;


void setup()

{

pinMode(button1,INPUT);

Serial.begin(9600);

}


void loop()

{

buttonstate = digitalRead(button1);


 if (buttonstate == HIGH)

{

      Serial.print("picq 134,28,217,219,1");

      Serial.write(0xff);

      Serial.write(0xff);

      Serial.write(0xff);

}


else

{

      Serial.print("picq 134,28,217,219,0"); 

      Serial.write(0xff);

      Serial.write(0xff);

      Serial.write(0xff);

}

}


Upload to your arduino and if everything is set up correctly, the picture should change once the input is set to HIGH and default when the input is LOW


The line Serial.print("picq 134,28,217,219,0"); works like this.

image

picq is the name or ID of the picture. The numbers (134,28,217,219,0) represent the coordinates, width and height. The last digit is the image number (picc #) 

----------------------------------------------------------------------------------------------------------------------


How to change a picture with a Digital Input.


This example works more or less the same as the previous.


Load two pictures in the Nextion IDE.like below

image


Click the Picture button like below

image

Double click the PIC tab on the right

image


Select the first Picture. Upload to you Nextion.


Arduino code:


int button1 = 8

int buttonstate = 0;


void setup()

{

pinMode(button1,INPUT);

Serial.begin(9600);

}


void loop()

{

buttonstate = digitalRead(button1);


 if (buttonstate == HIGH)

{

   Serial.print("p0.pic=1");

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.write(0xff); 

}


else

{

   Serial.print("p0.pic=0");

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.write(0xff); 

}

}


Serial.print("p0.pic=1");  p0 is the picture number. 1 is pictures name or ID.

imageYou can see the picture numbers on the left like in the example above.


I hope this is helpful to anyone that feels the struggle. I will try to update this tutorial every once in a while. 

In church the other day, the guy sitting next to me lit up a cigarette. I got such a fright I nearly dropped my beer.


3 people like this idea

Sorry DaveyHi,


  Want to use a demo project made by two users and call it a library?

  - I can see where you have issues.


It isn't about reverse engineering someone else's project

 - it is about engineering (forward engineering) your own project


For this there is the Nextion Instruction Set

For this ST has reference manuals and a programming guide

For this ST will even pay for a 1 year Kiel C compiler licence (STM32F0/L0 series chips)
Heck many compilers will at least allow 8K of code - like mikroe - for free

  (large must pay for compiler)


Pump the following out over serial one byte at a time

   t0.txt="Hello World"ÿÿÿ


There is the Nextion version of Hello World

Nextion is used with 10,000 MCUs in over 100 programming languages


But electronics and programming in chosen language and chosen compiler a must.

You have to see why with such a broad range of MCUs and languages
    why it makes so less sense to say Nextion is too complicated


TTL Serial,  None parity, 1 Stop Bit, Eight Data Bits
  - no flow control

  - no parity checking

  - no error correction

  - no compression

Has to be the most simplest of protocols

  - only two wires 

  - not 40 pins


But no, there will not be a library made for

  each of the platforms of the 10,000+ MCUs in 

  every iterations of every programming language available.

I am not devoting my life to 1,000,000+ iterations even if it is Hello World


Reason why that bborncr project fails for STM is
  the #include SoftwareSerial.h is made for AVR chips, not ARM Cortex chips
But use a Hardware port on A9/A10 with 
   Serial.print("t0.txt=\"Hello World\"ÿÿÿ");
should probably suffice.

Read your documentation for what should be done rather than
   trying to debug someone else's demo project for your needs

But it sounds like you have some reading and some learning to do
  - in your chosen programming language

  - to understand your chosen compiler

  - to understand your chosen MCU

  - to understand HMI Human Machine Interfacing
  - to understand Nextion (Help menuitem of Help Menu)

But don't judge Nextion based on someone else's lack of skills, build your own skills

I'm sorry ... still sucks!  Not very analytical, qualitative or quantitative.

 - many have made replacement code

Specific to their specific HMI design requirements.


The Iteadlib Arduino Nextion is atleast structured


NexConfig - define which ports to use, a few debug macros.

NexObject - base object to hold

                      - page number, component id, and .objname
NexTouch - adds touch functions

                      - .attachPush used for SendComponentID in Press Event

                      - .attachPop  used for SendComponentID in Release Event

                      - .detachPush used for SendComponentID in Press Event

                      - .detachPop  used for SendComponentID in Release Event

                    attached function is called on corresponding touch event

                    triggered by the 0x65 code being received by the MCU

                    iterate function() to compare nex_listen_list to incoming

                     and if matched then goto function passed as parameter.
NexComponent

    (NexText for Text Component, NexNumber for Number Component)

                   - adds .getValue .setValue, change to other attributes for each


NexHardware - supporting functions

     - nexInit()

     - nexListen()

     - sendCommand()

     - recvRetNumber()

     - recvRetString()

     ... others


So seriously, with examples on how to use each component

  - define Nextion side components 

       NexComponent myobj = NexComponent(page, .id, .objname);

  - add to *nex_listen_list if want to respond to incoming touch events

       {&myobj, NULL}

  - in setup() then 

      nexInit();

      attachPush(functionA, &myobj) or attachPop(functionB, &myobj);

   - in loop() then

       nexListen()

 and add the functions to go to (functionA and functionB)


I am not sure how this sucks

 - not only is it an example of structured code

 - examples provided for each Component

But it also comes with an updated MIT license to change and modify

 - so it can be used however you like, as much or as little.


When the Serial TTL protocol hasn't changed in 2 years

  - the base of the library has no need to change

Changes in the last two years include:

  - a few new components, so new corresponding NexComponent files

  - a few new attributes, so new functions in component for attribute

  - a few new commands in the Nextion Instruction Set

     - so this would be sent over serial by sendCommand() - no change needed.


There have also been a few libraries given by users to the community

  - when there are questions on these, ask the authors for support.


So those that have created a different library that performs better for a need

  - these people maybe have a right to say one sucks.

Sorry, but someone that lacks prerequisite programming 

  - in their own chosen language

  - in their own chosen compiler

  - for their own chosen MCU

Is not a valid argument to say someone elses code sucks without understandings.

  - it can't get any easier

    - HMI design in minutes rather than hours (check Component Send ID)

    - basic no option straight TTL serial communications on two wires

    - user chose their MCU

    - user chose their language

    - user chose their compiler

    - commands are Text-Based and Human readable.  Few exceptions.


But I also highly doubt commercial code is going to be given away for free.

Again ... lack of skills is not a valid argument 

    Education eradicates ignorance.

You take a lot of time to say nothing of use. It's as understandable as the Nextion 'documentation' and examples. lol

You attack and expel so much anger over a simple post and then say that I'm the one with issues? Please take some deep breaths and go for a walk.

I agree with DaveyHi, Patrick, why is it that your responses are usually attacks and defense of often the defensable?  Perhaps a dose of Shakespear is in order " He protests to much me thinks"


This is a good product, but not a great product mainly due to the fact, in my opinion, the designers have missed the point in the interface to the user.. Lets just look at one item, the 0xF endings.. These have no use to the user, either in reception or as a send entity. They may have a use internally to the Nextion/ARM system. But so what, the user does not have access to the internals of the Nextion, nor should he. Most of the SCADA systems I have dealt with send reals, intergers and text in easy to recieve formats when you ask for it, without modification or the need for computation  Also to say


A product can not be compared as on/off beam based on lack of skill of some.
The way you present, all I2C and SPI devices should be banned


is a typical respose from you, dismissive, patronising to my abilities and in this case wrong.  I2C and SPI and serial are the data structures by which information is transferred, not the information. Reception of 0xF is not information


Peter, please wake up to the fact that the documentation, examples, and responses to the inputs  on this forum is extremely sub standard (and dont even get me started on the crappy font renditions)


Go have a look at AVR Freaks for a good open forum. Have a look at the tutorials, covers all the basics and in one tidy accessable position.


A cooperative Nextion forum, with an understanding that some users have limited ability would be more beneficial to all, starting with a huge clean up of the documentation. An open mind to improving this product must be beneficial and that will only come about if there is a responsible feedback from the forum administrators to the itead designers and not advisers, such as yourself that always seem to need to defend this product.



Great tutorials.

Great tutorials.

hi guys,

I want to send data from my Nextion Display to the Arduino via serial Port.

WHEN I press the button in my Display I want to sent specific Data 

like   

G28, G1 X0 Y0 Z0, .....


@Ahmad 

You can use the 'print' command: 

http://support.iteadstudio.com/support/search/topics?term=print


1 person likes this
Login or Signup to post a comment