easyNexReadCustomCommand.ino 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * easyNexReadCustomCommand.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. // Compatible for Arduino
  18. // As example we use 2 arrays (tables) of integers where we are going
  19. // to change their values with custom commands.
  20. /*
  21. The format is the known: # len cmd id id2
  22. where the id referred to the position (element) of the array we want to write on
  23. And id2 carries the value to write on the element of array
  24. the custom command from Nextion: printh 23 03 4C 00 0A
  25. 4C is the Hex for letter L and we referred to the array dataL[]
  26. 00 Hex of Dec number 0 used as the index for each array element
  27. 0A Hex of Dec number 10 is the value we are going to write on element 0
  28. After the command executed by our code, the values on dataL[] array will be
  29. dataL[4] = {10,0,0,0}
  30. Same for the dataS[] intead that cmd is the 53 Hex for letter S
  31. Documentation to the end of the code
  32. */
  33. int dataL[4] = {0,0,0,0}; //values 0 or 1
  34. // printh 23 03 4C 00 00
  35. int dataS[4] = {0,0,0,0}; // values from 0 to 100
  36. // printh 23 03 53 00 00
  37. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  38. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  39. // Set as parameter the Hardware Serial you are going to use
  40. void setup(){
  41. myNex.begin(9600); // Begin the object with a baud rate of 9600
  42. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  43. delay(500); // give Nextion some time to finish initialize
  44. myNex.writeStr("page 0"); // For synchronizing Nextion page in case of reset to Arduino
  45. delay(50);
  46. myNex.lastCurrentPageId = 1; // At the first run of the loop, the currentPageId and the lastCurrentPageId
  47. // must have different values, due to run the function firstRefresh()
  48. }
  49. void loop(){
  50. myNex.NextionListen(); // WARNING: This function must be called repeatedly to response touch events
  51. // from Nextion touch panel. Actually, you should place it in your loop function.
  52. firstRefresh();
  53. }
  54. void easyNexReadCustomCommand(){
  55. int arrayPlace; // temp variable
  56. int value; // temp variable
  57. String numericAttribute;
  58. switch(myNex.cmdGroup){
  59. case 'L': // Or <case 0x4C:> If 'L' matches
  60. // we are going to write values in specific places in the dataL[] table
  61. // read the next byte that determines the position on the table
  62. arrayPlace = myNex.readByte();
  63. // read the next byte that keeps the value for the position
  64. value = myNex.readByte();
  65. // update the array with the new values
  66. dataL[arrayPlace] = value;
  67. // show the updated values of the array on Nextion to check them
  68. // use this Line to update only the one that you have change
  69. numericAttribute = "n"; numericAttribute += arrayPlace; numericAttribute += ".val";
  70. myNex.writeNum(numericAttribute, dataL[arrayPlace]);
  71. // Uncomment this block to update all the values on Nextion
  72. /*
  73. myNex.writeNum("n0.val", dataL[0]);
  74. myNex.writeNum("n1.val", dataL[1]);
  75. myNex.writeNum("n2.val", dataL[2]);
  76. myNex.writeNum("n3.val", dataL[3]); */
  77. break;
  78. case 'S': // Or <case 0x53:> If 'S' matches
  79. // ********* PAGE2 ON NEXTION ********
  80. // we are going to write values in specific places in the dataS[] table
  81. // from Nextion printh 23 03 53 00 and after that prints n0.val,1 in order to have one byte with the value of n0 attribute.
  82. // The value of n0 can take values from 0 to 255 as it is only one byte
  83. // read the next byte that determines the position on the table
  84. arrayPlace = myNex.readByte();
  85. // read the next byte that keeps the value for the position
  86. value = myNex.readByte();
  87. // update the array with the new values
  88. dataS[arrayPlace] = value;
  89. // show the updated values of the array on Nextion to check them
  90. // use this Line to update only the one that you have change
  91. numericAttribute = "n"; numericAttribute += (arrayPlace + 4); numericAttribute += ".val";
  92. myNex.writeNum(numericAttribute, dataS[arrayPlace]);
  93. /*
  94. myNex.writeNum("n4.val", dataS[0]);
  95. myNex.writeNum("n5.val", dataS[1]);
  96. myNex.writeNum("n6.val", dataS[2]);
  97. myNex.writeNum("n7.val", dataS[3]);
  98. */
  99. break;
  100. }
  101. }
  102. void firstRefresh(){ // This function's purpose is to update the values of a new page when is first loaded,
  103. // and refreshing all the components with the current values as Nextion shows the Attribute val.
  104. if(myNex.currentPageId != myNex.lastCurrentPageId){ // If the two variables are different, means a new page is loaded.
  105. switch(myNex.currentPageId){
  106. case 0:
  107. myNex.writeNum("n0.val", dataL[0]);
  108. myNex.writeNum("n1.val", dataL[1]);
  109. myNex.writeNum("n2.val", dataL[2]);
  110. myNex.writeNum("n3.val", dataL[3]);
  111. break;
  112. case 1:
  113. myNex.writeNum("n4.val", dataS[0]);
  114. myNex.writeNum("n5.val", dataS[1]);
  115. myNex.writeNum("n6.val", dataS[2]);
  116. myNex.writeNum("n7.val", dataS[3]);
  117. myNex.writeNum("n0.val", dataS[0]);
  118. myNex.writeNum("n1.val", dataS[1]);
  119. myNex.writeNum("n2.val", dataS[2]);
  120. myNex.writeNum("n3.val", dataS[3]);
  121. break;
  122. }
  123. myNex.lastCurrentPageId = myNex.currentPageId; // Afer the refresh of the new page We make them equal,
  124. // in order to identify the next page change.
  125. }
  126. }
  127. /* More for custom protocol and commands https://seithan.com/Easy-Nextion-Library/Custom-Protocol/
  128. easyNexReadCustomCommand() has a weak attribute and will be created only when user
  129. declare this function on the main code
  130. More for custom protocol and commands https://seithan.com/Easy-Nextion-Library/Custom-Protocol/
  131. our commands will have this format: <#> <len> <cmd> <id> <id2>
  132. and we must send them from Nextion as HEX with the printh command
  133. like: printh 23 03 4C 01 01
  134. <#> start marker, declares that a command is followed
  135. <len> declares the number of bytes that will follow
  136. <cmd> declares the task of the command or command group
  137. <id> declares the properties of the command
  138. <id2> a second property of the command
  139. When we send a custom command with the above format, the function NextionListen() will capture the start marker # and the len (first 2 bytes)
  140. and it will wait until all the bytes of the command, as we have declared with the len byte, arrive to the Serial buffer and inside the timeout limits.
  141. After that, the function will read the next byte, which is the command group and the function readCommand() takes over and through a switch command
  142. tries to match the _cmd variable that holds the command group value with the statements of the cases.
  143. If we do NOT have a match with the predefined, cmd of P for page and T for triggers, it will continue to the default where we store the _cmd and _len to the public variables
  144. cmdGroup and cmdLenght as we are going to need access to them from the main code in the next step.
  145. Next we call the the easyNexReadCustomCommand() with the precondition and ONLY if we have declared the function in the main code.
  146. From this point we can handle the assign of cmdGroup and IDs from the easyNexReadCustomCommand() in the user code, where we can go on with a switch case
  147. for the cmdGroup, the one that we have stored the _cmd for public use and we can call it with myObject.cmdGroup. This is why we made cmdGroup a public variable.
  148. */