HMI Code
So the purpose of this interface is send a message to the Arduino microcontroller (over UART) telling it to turn on or off one of the relays. Thus we need to send a message that states the relay to control and which state for it to switch to. We also need to know which state the switch is in order to know which message to send. This means we need to create an event that executes some lines of code when the switch is pressed.
The cool thing is that the Nextion is constantly looking for events to happen such as Touch Press Events and Touch Release Events from the switches. So if you click on the switch widget you will notice that in the event toolbar these two types of events will appear where we can write the code we want to execute when said event happens.
I am going to use a Touch Release Event meaning, when I remove my finger from the switch it will execute the event code.
The first thing we want to look at is what state is the switch in. Is it ON (equal to 1) or OFF (equal to 0). To do this I will simply use an if..else statement to check the value of the switch.
Thus if the value of the switch is equal to 1 then we want to send this message to the Arduino; R1:ON. If that condition is not true it will then run the else statement and send this message to the Arduino; R1:OFF.
I will do the same thing for the second switch except the messages to send will be "R2:ON" and "R2:OFF".
Now I will run the simulator and press the switches to verify that the correct messages are being sent. To view the messages in string click the S in the Simulator Return box to view HMI outputs.
The HMI output the messages as expected. Now I need to develop code on the Arduino to actually recieve and process this messages. Then to do something with the commands. First lets go ahead and load this HMI code onto the HMI itself.
Last updated