Start a new topic

Writing data to text box

I have loaded the program where you click a button on the screen and the number changes in a text box and it works fine.


I have a DHT11 connected to my Mega and am trying to figure out how to write the data from the DHT11 to the textbox without having to click any buttons.


Could anyone provide a simple sketch how to write data to a textbox on the Nextion?


Thank you


John307


When you have your value from the DHT11, you can either convert it to text on the Mega and send

t0.txt="12"

or you could send it to a numeric variable in your HMI design

va0.val=12

cov va0.val,t0.txt,0


The Arduino portion of the code found examples should be able to get you going.



1 person likes this
That part i knew how to do. It's the code to write that data to the display is where I'm stuck. I have looked through many sketches to see how the data is written to the display and I must not have something setup right. I even loaded the fish tank project but get a lot of errors i don't understand. Never got it to work. I just figured out the basic "push the button to change the number" sketch. Now i want go change the text without any button press.
Basically i have a sketch that displays the data from a DHT11 on a 4 line lcd display and want to move it over to the nextion display. So i just need to learn how to send the data to the display.

I will be as helpful as I can.  I don't do Arduino, so you will have to look at the examples and break the code down so that you understand where and what it is doing, just as in any programming language (I prefer Kylix 3 on a Linux kernel).  So the basics is that you are sending text over the Rx/Tx wires.


In the Arduino code, when you trace what happens when you set a text component, it will send the text you set over serial (I believe if you look deeper, you will find the code points to SendCommand followed by three 0xFF bytes to terminate the command)


In your routine that you are setting up where you retrieve your DHT11 value, you will add your additional code to send "t0.txt=24"0xFF0xFF0xFF.   This updates t0 on the Nextion with the text 24.


So if you have the CompText figured out, you are ignoring and strip out all the button code, insert your code to retrieve the value from your DHT11, convert to text and send it to the t0.


If you have a handle on serial communications, then sending the t0.txt=24 and the three 0xFFs is easy.

 

#include "Nextion.h" 

// Declare a text object [page id:0,component id:1, component name: "t0"]. 
NexText t0 = NexText(0, 1, "t0"); 

char buffer[100] = {0}; 
  
void setup(void) 
{ 
    /* Set the baudrate which is for debug and communicate with Nextion screen. */ 
    nexInit(); 
    dbSerialPrintln("setup done"); 
} 

void loop(void) 
{ 
  uint16_t dht11val;
  memset(buffer,0,sizeof(buffer));
  // inject your code to retrieve value from dht11
  // set value you retrieved from your dht11
  dht11val = 24;  
  itoa(dht11val,buffer,10);
  t0.settext(buffer);
} 

 




2 people like this

Patrick


Thank you for the sketch!! The one I was trying was similar to yours. When I was trying yours I couldn't get the serial to work. Then I went back to the original I had working and it didn't work either. It seems my USB power drivers are weak in the computer. I have to use an external supply to upload the file to the display. I thought it would be better to run that way but for some reason the serial doesn't work unless the display power is plugged in to the computer USB port. I'll have to work on that.


Anyway your sketch worked and I greatly appreciate your help!!! The only error was in t0.settext(buffer); it needs to be t0.setText(buffer);


>> In your routine that you are setting up where you retrieve your DHT11 value, you will add your additional code to send "t0.txt=24"0xFF0xFF0xFF


P.S. With your code, there was no need to add the 0xFF0xFF0xFF as I was using the Nextion library as it worked without it.


Thanks again


John



1 person likes this

I am glad you have it worked out.  Like I said, I am not an Arduino coder. (so I see it is case sensitive). 


Just so that you know, the 0xFFs are still going across the serial wire, the library is handling it for you a little deeper into the code -- you didn't need to specifically put them in yourself this time.  In the future you might like to code yourself a function to send across the serial for things like calling "click m1,0" or whatever command you wish, just to be flexible without having to describe your whole HMI to the Arduino Library.

Here is an updated version that I have been working on. Still working on cleaning up the sketch.

The one problem I'm having trouble with is the Minutes display. I'm having trouble displaying the extra "0" when the minutes are less than 10. Still new to Arduino coding, but learning .

Also I wish you could make the text boxes transparent. It would make the display look cleaner!!



HMI
ino
(6.07 KB)

1 person likes this

IF you were to have variables hr, mn on your page then you could send the codes to the Nextion

I think you may have to dig for the SendCommand() and see how it is used.


hr.val=tm.hour

mn.val=tm.minute

cov hr.val,tHour,2

cov mn.val,tMinutes,2


The last parameter of the cov command signifies the length where 0 is auto, and specifying a number (such as 2) will add leading zeros if the converted length is less.  This is perfect for cases like time.

I continue to make progress on the display. I have the time, date, temperature and humidity working. I'm not sure how well the humidity sensor, DHT11, is working. I had the windows open the other day because it was cool outside for once and the Humidity only rose 1 %. It is reading 32% and the humidity in the house was reading 55% on my mechanical humidity gauge.


I was playing around with the Fish Tank project to see how they display time and date. I finally got the sketch to work. There are 3 libraries included with the program and it reads and displays all the correct time and date readings. So I borrowed some of the parts of the sketch to make mine work. Thanks for the pointers Patrick. I wasn't sure how to implement them so I pulled part of the Fish Tank sketch to make the minutes display correctly is as follows.

 

 //====================
  //Display the Minutes
  //====================
  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))
  {
    tMinute.setText(buffer);
    strcpy(buffer_minute, buffer);
  }

 Kudos to the person who wrote the program. I just wish they would have documented the code to make it easier to understand. Once you start digging into it, the gray matter starts to understand how things work. I had to Google a lot of the commands to see what they did and how to use them.


John


Hey John


Just an update.  Creating a leading zero had been an issue in the past.  With Version 0.38 of the Nextion Editor, if you used a Number Component, you can set the .length attribute to 2 and it will automatically handle changing a 2 value to display 02.  An improvement for sure.

Thank you for the information Patrick. I may be redesigning my display knowing this.

Hi on line 21 dht11val = 24; is a pin number.....

i want to pass a string variable to t0.setText(variable).. this gives errors what is the issue?

Login or Signup to post a comment