ChangePagesAndSendFloatValuesCode.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * ChangePagesAndSentFloatValuesCode.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 and WeMos D1 mini
  18. /* This project aims to show how to manage page changes,
  19. * how to send different data in different pages and
  20. * how to limit the data sent to Nextion to minimum
  21. * and to avoid unnecessary use of the Serial and
  22. * a possible buffer overflow.
  23. */
  24. /* This project also demonstrates the two ways you can send float values
  25. * on Nextion, as Nextion DOES NOT SUPPORT float numbers. Uses integer math
  26. * and does not have real or floating support.
  27. * The Xfloat component is used for signed 32-bit integer values.
  28. * The .vvs0 sets the number of digits shown to the left of the decimal (useful for leading zeros).
  29. * The .vvs1 sets the number of digits shown to the right of the decimal.
  30. * Go to the refreshPage0() function and see the comments on how we can send float values
  31. */
  32. /* The best way to demonstrate the change of the pages,
  33. * is to use the DHT11 sensor, as it is very common and
  34. * the chances of someone having one, are very high.
  35. * Also, it gives us at least 3 different types of data.
  36. * DHT11, DHT21 or DHT22 can be used.
  37. */
  38. //----------------------------------
  39. // EasyNextionLibrary Initialization
  40. //----------------------------------
  41. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  42. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  43. // Set as parameter the Hardware Serial you are going to use
  44. //--------------------------------------
  45. // DHT Library and Sensor Initialization
  46. //--------------------------------------
  47. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  48. #include "DHT.h" // Include DHT library
  49. #define DHTPIN 2 // Digital pin connected to the DHT sensor D2 for UNO, NANO, MEGA, D4 for WeMos D1 mini
  50. // Uncomment whatever type you're using!
  51. #define DHTTYPE DHT11 // DHT 11
  52. //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  53. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  54. #define GET_DATA_EVERY 2000 // DHT sensors can give us measurements every 2 seconds,
  55. // as Adafruit suggests
  56. unsigned long getDataTimer = millis(); // Timer for GET_DATA_EVERY
  57. DHT dht(DHTPIN, DHTTYPE);
  58. // Reading temperature or humidity takes about 250 milliseconds!
  59. // We create the lastXX value variables, in order to store the last value that we have sent on Nextion,
  60. // compare the last value with the new value (current value that we have take from sensor) and send
  61. // the new value ONLY if it is different from the last and not send data to Nextion if there is no need.
  62. // For the needs of this project, the last value comparison might be not needed,
  63. // but I write these, just to show how you can do it, if it is really needed, at more complicated projects.
  64. float humidity = 0.0; // Store the value of humidity
  65. float lastSentHumidity = 0.0; // Store the last value of humidity that we have Sent on Nextion
  66. float temperature = 0.0; // Store the value of the temperature in Celsius
  67. float lastSentTemperature = 0.0; // Store the last value of the temperature in Celsius that we have sent on Nextion
  68. float fahrenheit = 0.0; // Store the value of the temperature in Fahrenheit
  69. float lastSentFahrenheit = 0.0; // Store the last value of the temperature in Fahrenheit that we have sent on Nextion
  70. float heatIndexFahrenheit = 0.0; // Store the value of the heat index in Fahrenheit
  71. float lastSentHeatIndexFahrenheit = 0.0; // Store the last value of the heat index in Fahrenheit that we have sent on Nextion
  72. float heatIndexCelsius = 0.0; // Store the value of the heat index in Celsius
  73. float lastSentHeatIndexCelsius = 0.0; // Store the last value of the heat index in Celsius that we have sent on Nextion
  74. //----------------------------------
  75. // Change Page Initialization
  76. //----------------------------------
  77. /* In order to update currentPageId variable with the current Id of the page,
  78. * you must write at the Preinitialize Event of every page: `printh 23 02 50 XX` , where `XX` the id of the page in HEX.
  79. * For page0: `printh 23 02 50 00`
  80. * For page9: `printh23 02 50 09`
  81. * For page10: `printh 23 02 50 0A`
  82. */
  83. #define DATA_REFRESH_RATE 1000 // The time between each Data refresh of the page
  84. // Depending on the needs of the project, the DATA_REFRESH_RATE can be set
  85. // to 50ms or 100ms without a problem. In this example, we use 1000ms,
  86. // as DHT sensor is a slow sensor and gives measurements every 2 seconds
  87. unsigned long pageRefreshTimer = millis(); // Timer for DATA_REFRESH_RATE
  88. bool newPageLoaded = false; // true when the page is first loaded ( lastCurrentPageId != currentPageId )
  89. void setup(){
  90. myNex.begin(9600); // Begin the object with a baud rate of 9600
  91. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  92. dht.begin();
  93. delay(500); // give Nextion some time to finish initialize
  94. myNex.writeStr("page 0"); // For synchronizing Nextion page in case of reset to Arduino
  95. delay(50);
  96. myNex.lastCurrentPageId = 1; // At the first run of the loop, the currentPageId and the lastCurrentPageId
  97. // must have different values, due to run the function firstRefresh()
  98. }
  99. void loop(){
  100. myNex.NextionListen(); // This function must be called repeatedly to response touch events
  101. // from Nextion touch panel. Actually, you should place it in your loop function.
  102. readSensorValues();
  103. refereshCurrentPage();
  104. firstRefresh();
  105. }
  106. void firstRefresh(){ // This function's purpose is to update the values of a new page when is first loaded,
  107. // and refreshing all the components with the current values as Nextion shows the Attribute val.
  108. if(myNex.currentPageId != myNex.lastCurrentPageId){ // If the two variables are different, means a new page is loaded.
  109. newPageLoaded = true; // A new page is loaded
  110. // This variable is used as an argument at the if() statement on the refreshPageXX() voids,
  111. // in order when is true to update all the values on the page with their current values
  112. // with out run a comparison with the last value.
  113. switch(myNex.currentPageId){
  114. case 0:
  115. refreshPage0();
  116. break;
  117. case 1:
  118. refreshPage1();
  119. break;
  120. case 2:
  121. refreshPage2();
  122. break;
  123. case 3:
  124. refreshPage3();
  125. break;
  126. }
  127. newPageLoaded = false; // After we have updated the new page for the first time, we update the variable to false.
  128. // Now the values updated ONLY if the new value is different from the last Sent value.
  129. // See void refreshPage0()
  130. myNex.lastCurrentPageId = myNex.currentPageId; // Afer the refresh of the new page We make them equal,
  131. // in order to identify the next page change.
  132. }
  133. }
  134. void readSensorValues(){
  135. if((millis() - getDataTimer) > GET_DATA_EVERY){
  136. humidity = dht.readHumidity(); // Read relative humidity
  137. temperature = dht.readTemperature(); // Read temperature as Celsius (the default)
  138. fahrenheit = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)
  139. heatIndexFahrenheit = dht.computeHeatIndex(fahrenheit, humidity);// Compute heat index in Fahrenheit (the default)
  140. heatIndexCelsius = dht.computeHeatIndex(temperature, humidity, false); // Compute heat index in Celsius (isFahreheit = false)
  141. getDataTimer = millis();
  142. }
  143. }
  144. void refereshCurrentPage(){
  145. // In this function we refresh the page currently loaded every DATA_REFRESH_RATE
  146. if((millis() - pageRefreshTimer) > DATA_REFRESH_RATE){
  147. switch(myNex.currentPageId){
  148. case 0:
  149. refreshPage0();
  150. break;
  151. case 1:
  152. refreshPage1();
  153. break;
  154. case 2:
  155. refreshPage2();
  156. break;
  157. case 3:
  158. refreshPage3();
  159. break;
  160. }
  161. pageRefreshTimer = millis();
  162. }
  163. }
  164. void refreshPage0(){
  165. // Use lastSentTemperature, in order to update the components ONLY when their value has changed
  166. // and avoid sending unnecessary data over Serial.
  167. // Also with the newPageLoaded boolean variable, we bypass the if() argument of temperature != lastSentTemperature (last value comparison)
  168. // so as to update all the values on Nextion when a new page is loaded, independant of if the values have changed
  169. if(temperature != lastSentTemperature || newPageLoaded == true){
  170. String tempString = String(temperature, 1); // Convert the float value to String, in order to send it to t0 textbox on Nextion
  171. myNex.writeStr("t0.txt", tempString); //Write the String value to t0 Textbox component
  172. int tempInt = temperature*10; // We convert the float to int, in order to send it to x0 Xfloat component on Nextion
  173. // We multiply it x10, because Xfloat will put a comma automatically after the last digit
  174. // if vvs1 is set to 1
  175. myNex.writeNum("x0.val", tempInt);
  176. lastSentTemperature = temperature; // We store the last value that we have sent on Nextion, we wait for the next comparison
  177. // and send data only when the value of temperature changes
  178. }
  179. }
  180. void refreshPage1(){
  181. if(humidity != lastSentHumidity || newPageLoaded == true){
  182. String tempString = String(humidity, 1);
  183. myNex.writeStr("t0.txt", tempString);
  184. int tempInt = humidity*10;
  185. myNex.writeNum("x0.val", tempInt);
  186. lastSentHumidity = humidity;
  187. }
  188. }
  189. void refreshPage2(){
  190. if(fahrenheit != lastSentFahrenheit || newPageLoaded == true){
  191. String tempString = String(fahrenheit, 1);
  192. myNex.writeStr("t0.txt", tempString);
  193. int tempInt = fahrenheit*10;
  194. myNex.writeNum("x0.val", tempInt);
  195. lastSentFahrenheit = fahrenheit;
  196. }
  197. }
  198. void refreshPage3(){
  199. if(heatIndexCelsius != lastSentHeatIndexCelsius || newPageLoaded == true){
  200. String tempString = String(heatIndexCelsius, 1);
  201. myNex.writeStr("t0.txt", tempString);
  202. int tempInt = heatIndexCelsius*10;
  203. myNex.writeNum("x0.val", tempInt);
  204. lastSentHeatIndexCelsius = heatIndexCelsius;
  205. }
  206. if(heatIndexFahrenheit != lastSentHeatIndexFahrenheit || newPageLoaded == true){
  207. String tempString = String(heatIndexFahrenheit, 1);
  208. myNex.writeStr("t1.txt", tempString);
  209. int tempInt = heatIndexFahrenheit*10;
  210. myNex.writeNum("x1.val", tempInt);
  211. lastSentHeatIndexFahrenheit = heatIndexFahrenheit;
  212. }
  213. }