WriteTextAndCommandsCode.ino 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * WriteTextAndCommands.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. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  18. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  19. // Set as parameter the Hardware Serial you are going to use
  20. void setup(){
  21. myNex.begin(9600); // Begin the object with a baud rate of 9600
  22. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  23. }
  24. void loop(){
  25. /* Use writeStr(String) for sending commands.
  26. * Use ONLY the first parameter
  27. * In the first parameter, write the command according to the instructions of Nextion's Instruction Set
  28. * Do NOT write ANYTHING in the second parameter. Leave it empty.
  29. * TIP: Write in the debug mode the command to check if it is written correctly
  30. * example: The command to change the page and go to page0, is: page page0(page0 is the name of the page)
  31. * or page 0(0 is the ID of the page)
  32. */
  33. myNex.writeStr("page page0"); // Sending this command to change the page we are on Nextion using pageName
  34. delay(50); // Some time for Nextion to execute the command
  35. /* Use writeStr(String, String) to change the text in a textbox
  36. * Use BOTH parameters
  37. * In the first parameter, write the objectName.textAttribute example: t0.txt or b0.txt
  38. * In the second parameter, write the text you want to "print"
  39. * Any previous text on the textbox is deleted
  40. */
  41. myNex.writeStr("t0.txt", "You are now transferred to page0"); // The text in t0 is now this
  42. delay(2950);
  43. myNex.writeStr("page 1"); // Sending this command to change the page we are on Nextion using pageId
  44. delay(50); // Some time for Nextion to execute the command
  45. /* By writing \\r, you send Nextion the change line character < \r >
  46. * The second \ is required, in order to print the \ as character
  47. * and not as an escape character.
  48. */
  49. myNex.writeStr("t0.txt", "You are now transferred to page1\\r");
  50. // Avoid using very big text Strings in the same command, as Nextion will not recognise them.
  51. // Istead use a second command and in order to add to the existing text, use the + symbol, after the .textAttribute("t0.txt+").
  52. myNex.writeStr("t0.txt+", "This is the:\\rWriteTextAndCommands example");
  53. myNex.writeStr("t0.txt+", "\\rEvery 3000ms we change page");
  54. myNex.writeStr("t0.txt+", "\\rAnd we print a text to t0");
  55. delay(4950);
  56. myNex.writeStr("page page2");
  57. delay(50); // Some time for Nextion to execute the command
  58. myNex.writeStr("t0.txt", "You are now transferred to page2\\r");
  59. myNex.writeStr("t0.txt+", "Thank you\\rfor choosing my library!!!");
  60. myNex.writeStr("t0.txt+", "\\rEnjoy the library!!!");
  61. myNex.writeStr("t0.txt+", "\\r\\rAthanasios Seitanis");
  62. myNex.writeStr("t0.txt+", "\\rseithagta@gmail.com");
  63. delay(7950);
  64. myNex.writeStr("t0.txt", "Screen will go to sleep mode in");
  65. myNex.writeStr("t0.txt+", "\\r3...");
  66. delay(1000);
  67. myNex.writeStr("t0.txt+", "2...");
  68. delay(1000);
  69. myNex.writeStr("t0.txt+", "1...");
  70. delay(1000);
  71. myNex.writeStr("t0.txt", "S L E E P\\rSee you in 10 seconds!!!");
  72. delay(1000);
  73. myNex.writeStr("sleep=1"); // Screen goes to sleep mode
  74. delay(10000);
  75. myNex.writeStr("sleep=0"); // Screen exits sleep mode
  76. delay(100); // Give some time to Nextion to Exit sleep mode
  77. // As these commands are using Serial to read and write,
  78. // it is more preferred not to run them in the loop() without delay();
  79. // or some other method of not running them with the frequency of the loop
  80. // and use them only when it is needed.
  81. // A delay in the loop can be noticed, especially when reading from Serial
  82. // And of course to avoid a Serial buffer overflow
  83. }