Start a new topic

Questions about buffer usage Nextion Display x Arduino

 Hello all,

I was trying to understand how the Fish Tank example should work with an Arduino Mega board and there is one point which I still not able to understand: the usage of buffers and some converters to store the temperature value.

I'll point the questions that I have:
1 - Why is it necessary to declare the variable buffer as a char list with 10 values?

char buffer[10] = {0};
char buffer_temp[10] = {0};


2 - After that, why is it necessary to use a secondary buffer to store the temperature and them rewrite the variable buffer with the buffer_temperature values?

    itoa(temperature, buffer, 10);
    
    if (strcmp(buffer, buffer_temperature))
    {
        t8.setText(buffer);
        strcpy(buffer_temperature, buffer);
    }
3 - How can I transmit the value of a Checkbox and Radiobuttons from the Nextion to the Arduino and with is the output type (boolean, string, int)?

4 - How can I convert the value of the buffer to a given variable in order to store into a SD card?

For now, thanks for the help!

Buffers are storage for the characters being received +1 for NULL termination.

The Nextion is going to return data formatted as per the Nextion Instruction Set

The longest of these is 9 bytes being returned + 1 null is 10 spaces used - initialized to null {0}


buffer_temp may or may not refer to buffer for temperature, but may refer to temporary

As I am not the author of the code, I can not state his reasons for doing so,

But even as an efficient coder, I am not immune from some occational inefficiencies

- this is probably true of us all.


As per the Nextion Instruction Set, (the attributes are either string .txt or numeric .val)

- communication data is terminated by three bytes of 0xFF

String data is returned in a 0x70 format 1 byte per char data terminated

Numeric data is returned in a 0x71 format little endian 32 bit value data terminated

The RadioButton and CheckBox Components use a numeric .val of either 0 or 1

Using the IteadLib Arduino Library, getValue or getText functions handle the retrieval and return


The current function of the onboard Nextion SD card is limited to the upload of a TFT file.

If referring to an Arduino SD module, you will refer to the Arduino SD library used



1 person likes this

Hello Patrick,


Thanks for the answers and clarifications. It's a little more clear to me now!


I'm trying to get a value from a Radiobutton and now the following error is presented:


TCC_ver_0:113: error: invalid use of non-static member function


I'm trying to get the value from the radiobutton by using the following command:


 myFile.print(rvlrconstclave.getValue);


Should I put an argument at the getValue function?


Concerning the SD card, I'm using an external module to store some configuration parameters that will be automatically loaded at the arduino power-up.


Thanks again!

the .getValue function is defined in your library inside NexCheckbox.cpp and NexRadioButton.cpp

This function takes an uint32_t as a parameter.


1 person likes this

Hi Martin,

Thanks again for the message.


Why I should put one parameter in order to just get a value of the Radiobutton/Checkbox state?

Is this parameter a variable to store the state of the Radiobutton/Checkbox?

As per my point of view (If I'm seeing this correctly), this function should receive no parameter.


Cheers


Hi Martin,

I'm still struggling with the Radiobuttons and Checkboxes. I found at the examples folder one example of the Dual state button and I've tried to adapt it to get the value of 2 radiobuttons.

This example almost worked and now I'm able to get the value of the first radiobutton, while the second one is just a copy of the value of the first one.


Based on the dual state button example I wrote the following code:


     uint32_t dual_state;

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

     rminmaxclave.getValue(&dualstate);

     if(dual_state)

     {

       myFile.print("1");

     }

     else

     {

       myFile.print("0");

     }

     myFile.println(" ");

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

     rvlrconstclave.getValue(&dual_state);

     if(dual_state)

     {

       myFile.print("1");

     }

     else

     {

       myFile.print("0");

     }


This code works however returns the following at the serial monitor: recvRetNumber err

Besides this error, both values (of the radiobuttons rminmaxclave and rvlrconstclave) are stored as 1 while I've selected just the first one to be active.


Thanks again

Ricardo



I would be more inclined to define my radio buttons in my function

This may be just a styling preference, but perhaps may provide a clarity you can borrow.

Where only r0 or r1 should be active at one time, this we need to set by code - not cover this here.

also for r0 and r1 to have ids of 1 and 2, they must be the first two components created on the page

 - or sent to 1 via the Bring Bottom down arrow on the Nextion Editor toolbar.


NexRadio r0 = NexRadio(0,1,"page0.r0");  // HMI component    class(page,comp,.objname)

uint32_t    r0value;                                        // local MCU variable

NexRadio r1 = NexRadio(0,2,"page0.r1");  // HMI component

uint32_t    r1value;                                        // local MCU variable


// you can now retrieve these HMI component values into MCU variables

r0.getValue(&r0value);

r1.getValue(&r1value);


// you can make changes to these values

//     and perform any other logic desired

if (r0value==0) {

   r1value = 1;

} else {

   r1value = 0;

}


// you can now sync local variable changes back to HMI components

r0.setValue(&r0value);

r1.setValue(&r1value);


Your recvRetNumber err could be reproduced if the rminmaxclave wasn't declared with its class

To me, it also didn't appear the "buffer" was actually used in the example file.

I also didn't deal with any touch events or handling.



1 person likes this

Patrick,


Now it finally worked! Thanks for the example!

The main issue I had was not declaring the page name before the component name while defining the radiobuttons. After I added the page name before the component name, they worked.


I'm not dealing with any touch events since a good part of all logic is implemented in the HMI file instead of the arduino. As my Nextion Display had arrived before the arduino, I was trying to make the most of the project at the display so I would focus at the arduino code when it arrived.


About the buffer, it is declared in another part of the code. 


Thanks again!


do remember that r0 and r1 in the HMI would need to be global, when the page changes in the HMI, the component itself will not be valid and present, but its value is retained and accessible as a global.


This also does not account for other HMI logic, but you can now retrieve and set



1 person likes this
Yes, they are global already and the logic is working pretty well.
Thanks!

 

Login or Signup to post a comment