ReadFromStringExample.ino 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * ReadFromStringExample.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 example aims to show how you can use the readStr() function of the Library
  18. * With this Function you can read a string component from Nextion
  19. */
  20. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  21. // Download the latest version https://github.com/Seithan/EasyNextionLibrary
  22. // or from Arduino's IDE Library Manager
  23. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  24. // Set as parameter the Hardware Serial you are going to use
  25. const int REFRESH_TIME = 1000; // time to refresh the Nextion data every 1000 ms
  26. unsigned long refresh_timer = millis(); // timer for refreshing Nextion's page
  27. String stringFromNextion;
  28. void setup(){
  29. myNex.begin(9600); // Begin the object with a baud rate of 9600
  30. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  31. }
  32. void loop(){
  33. myNex.NextionListen(); // WARNING: This function must be called repeatedly to response touch events
  34. // from Nextion touch panel. Actually, you should place it in your loop function.
  35. if((millis() - refresh_timer) > REFRESH_TIME){ //IMPORTANT do not have serial print commands in the loop without a delay
  36. // or an if statement with a timer condition like this one.
  37. stringFromNextion = myNex.readStr("t0.txt");
  38. myNex.writeStr("t1.txt",stringFromNextion);
  39. refresh_timer = millis(); // Set the timer equal to millis, create a time stamp to start over the "delay"
  40. }
  41. }