I try send temperature from DHT to Nextion through arduino
I send for example from arduino 24.6 C - I change it for 246 (x10) and sending by command:
- this working - but if I change 246 and conversion to "float"
In preinitialize event section on page 0
I see only temperature data when I change page from other to main :(
All variables I set to global - but it doesn't work because is run only one in the start ( refresh page)
I will modify my answer .. to answer your page preinitialization event - don't put code there
The Nextion STM32 mcu is integer based math - meaning there is no float
I would probably use in the HMI
1 Number Component for the whole number portion set .lenth=1
1 Text Component to hold the decimal .txt=.
1 Number Component for the partial number set .lenth=1
From your Arduino ... break your dht into whole number part of 24 dhtw and fractional part 6 dhtf
// code to break dht into dhtw and dhtf
Thank you for the answer, but one I can not understand:
Why variables can not be performed "online" in memory of the HMI.
if I send the number or text - can be displayed on a regular basis
If I want to convert them in nextion it does not work anymore
Patrick - yes I know that I can send two variables from arduino board - if I use them in the HMI as a (number, dot, decimal ) - it looks ugly :( Objects overlap or have a large distance between them
Gerry - yes I know that the page-preinitialize section is the wrong place for code because performed once , but I haven't other place in the nextion where I could operate on variables
If I am using any font size, the width is fixed to 1/2 of height during generation. So ...
The key to not having large distances is to change the attributes and placement
This will leave it with the same distances as if rendered by the Text Component as all in one 24.6 string.
If my font size is 12x24 and I want 2 digits 24, then set the .w to 24 height to 24 and right alight the Number
For the decimal point and fractional number, the same theory set w. to 12.
Align these (arrow left/right) will move 1 px until in perfect placement.
There are other places to operate on variables within the Nextion
Every Touch Pressed event or Touch Released Event becomes a place to operate on variables
A Timer Event is a place to operate on variables.
The Page component has 4: PreInit,PostInit,Pressed and Released
The Slider has Pressed Released and Move.
You must think strategically for your purpose.
What I suggested requires two commands over serial and it is done.
There are many means to be less efficient, jumble where it is found when it comes time to debug and upgrade
My suggestion is based on the fact that your dht value is received on you mcu
- so since you are already in a routine to send it to the Nextion
- why not send it at the same time as you receive your dht value
It becomes a real easy spot to locate your function when modifying your code
And you have the benefit of knowing that when it was received, it has been handled.
yes, You're right, the reason is the fact that I wanted to give simple instructions to LCD - so the code for arduino is very short :) also I collect information from 16 external devices and send them via the serial to HMI - so I depend on minimizing transmitted data
Thank You for simple solution
In programming there are always many methods.
If for the sake of having Nextion perform the tasks
create a hotspot (later you will resize it to .w of 5 and .h of 5) assumed m0
create a variable dht on your HMI page0 (perhaps .vscope Global is needed if multi-paged)
place your code for converting dht.val to Numbers or Text in this hotspot
You will send from your mcu your dht value * 10 as an integer to the Nextion.
You will send from your mcu SendCommand("click m0,1");
- this triggers the Pressed Event code for the hotspot m0, use click m0,0 for the Released Event
At this point this again is two serial commands to accomplish the task and Nextion now must finish the task.
Other people use a timer and put the code in the timer event - there are max 4 to 6 timers allowed
I am not a fan as I can trigger a click when I want which may be more than 20/sec, timer limited to 50ms
I am also not a fan of the timer method, as I may want my timers for something else
I will not run out of hotspots at 5x5 pixels and placed out of the way.
But having said that, if each of the external devices are represented by a variable value,
Update new value for external device to its corresponding Nextion variable
In the single event code, you can process all of these variables to the screen inside one event.
It will become more complex with multiple pages, you will need to set your dht variable to .vscope Global.
Hotspot its grate idea, but if I send from mcu Serial1.write("click m0, 1"); not change nothing.
If I type in Nextion console click m0,1 - work, page0.click... also don't work...
In my devices I changed 16bit LCD TFT for Nextion - but I do not know simple way without using function and declaration to send basic control commands to HMI
Do I have declared all in arduino? I'm a hardware engineer - I prepare the hrdware to work properly, the programming is not my strong point :( but I have to check it anyway....
it appears you put an extra space in there. click<ONE_SPACE>m0,1
unfortunately, does not work...
if ((tw_old) != (tw)) ///////////// sending data if only value changed
va5.setValue(tw_old); // work! I see it if I change page or press m0
Serial1.write("click m0, 1"); //double space
//Serial.print(tw_old); // it work
Looks like that the data is not received by nextion, or wrong sending to nextion ...
I give up....It is too easy to understand it
Serial1.write("ÿÿÿ"); // three bytes of 0xFF ... in this case represented as characters
Note that the only space is between click and the first parameter m0.
There is no space after the comma and the one.
Note the second line is required as a data terminator.
If you are using the IteadLib Arduino Nextion library
the two Serial.write lines above could have used
as sendCommand automatically includes the needed three terminating bytes
You are big... but ->
I use for prototype of course IteadLib Arduino Nextion
I admire you for wanting to help - but it does not work. You are incredibly dispositional
///////////// sending data if changed only
if ((tw_old) != (tw))
Serial1.write("click m0,1"); //double spacelick
of course work after changing page or call of m0 by pressing
Again why would you change the three bytes from char(255) - the ÿ character to Y and expect it to work
The Nextion Instruction Set clearly states three bytes of 0xFF to terminate a command.
Changing it to "YYY" you have now swapped out to effectively send three bytes of 0x59
I think that HMI doesn't interpreted correctly command from arduino
but on the other hand, why it works when performing physical action on LCD, or send instructions to the UART Nextion from Nextion Editor? I do not understand ......
Please forgive me my English language
Earlier I made modifications because also I programing Nextion HMI via the serial port by press the button, additionally changed a parameter in the library 115200 - maybe this is the reason...?