TriggerCodeWeMos.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * TriggerCodeWemos.ino - Simple example code for EasyNextionLibrary
  3. * Copyright (c) 2020 Athanasios Seitanis < seithagta@gmail.com >.
  4. * https://www.seithan.com
  5. * All rights reserved. EasyNextionLibrary is licensed under the MIT License
  6. * https://opensource.org/licenses/MIT
  7. */
  8. /* I have invested time and resources providing open source codes, like this one.
  9. * Please do not hesitate to support my work!
  10. * If you found this work useful and has saved you time and effort,
  11. * Just simply paypal me at: seithagta@gmail.com
  12. */
  13. //********************************************************************************
  14. // You can find more examples, tutorials and projects with Nextion on my website
  15. // https://www.seithan.com
  16. //********************************************************************************
  17. /* This is the most important method of the library.
  18. * And this is because, it gives you the ability to use the predefined functions and run your code from there.
  19. * These predefined functions are named trigger0(), trigger1(), trigger2()... up to trigger50().
  20. * You can use them as a simple void out of the loop, in which you will have written a block of code to run every time it is called.
  21. * You can call those trigger() functions and run the code they contain anytime by simply writing in a Nextion Event the command:
  22. * `printh 23 02 54 XX` , where `XX` the id for the triggerXX() in HEX.
  23. * Example: printh 23 02 54 00 to call trigger0() ... printh 23 02 54 0A to call trigger10() and so on...
  24. */
  25. /*
  26. Declare the void by simply writing:
  27. void trigger0(){
  28. [ put your code here !!!!]
  29. }
  30. */
  31. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  32. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  33. // Set as parameter the Hardware Serial you are going to use
  34. void setup(){
  35. myNex.begin(9600); // Begin the object with a baud rate of 9600
  36. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  37. pinMode(LED_BUILTIN, OUTPUT); // The built-in LED is initialized as an output
  38. digitalWrite(LED_BUILTIN, HIGH);
  39. }
  40. void loop(){
  41. myNex.NextionListen(); // WARNING: This function must be called repeatedly to response touch events
  42. // from Nextion touch panel. Actually, you should place it in your loop function.
  43. }
  44. void trigger0(){
  45. // To call this void send from Nextion's component's Event: printh 23 02 54 00
  46. // In this exmaple, we send this command from the Release Event of b0 button (see the HMI of this example)
  47. // You can send the same `printh` command, to call the same function, from more than one component, depending on your needs
  48. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  49. if(digitalRead(LED_BUILTIN) == LOW){
  50. myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
  51. myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
  52. myNex.writeNum("p0.pic", 1); // Set picture 1 as background picture for p0
  53. }else if(digitalRead(LED_BUILTIN) == HIGH){
  54. myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
  55. myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "OFF"
  56. myNex.writeNum("p0.pic", 0); // Set picture 0 as background picture for p0 picture component
  57. }
  58. }
  59. void trigger1(){
  60. // To call this void send from Nextion's component's Event: printh 23 02 54 01
  61. // In this exmaple, we send this command from the Release Event of b1 button (see the HMI of this example)
  62. // You can send the same `printh` command, to call the same function, from more than one component, depending on your needs
  63. if(digitalRead(LED_BUILTIN) == LOW){
  64. digitalWrite(LED_BUILTIN, HIGH); // Start the function with the LED off
  65. myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
  66. myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "OFF"
  67. myNex.writeNum("p0.pic", 0); // Set picture 0 as background picture for p0 picture component
  68. }
  69. myNex.writeStr("t0.txt", "LED STROBE\\rON");
  70. for(int i = 0; i < 10; i++){
  71. for(int x = 0; x < 10; x++){
  72. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  73. delay(50);
  74. }
  75. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  76. delay(500);
  77. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  78. }
  79. myNex.writeStr("t0.txt", "LED STROBE\\rOFF"); // Setting the text of t0 textbox to "LED STROBE OFF"
  80. // \\r is the newline character for Nextion.
  81. // The text will look like this:
  82. // 1st line: LED STROBE
  83. // 2nd line: OFF
  84. }