You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.4 KiB

7 months ago
  1. #include "Nextion.h"
  2. // LED pins
  3. const int led1 = 8;
  4. const int led2 = 9;
  5. // Declare your Nextion objects - Example (page id = 0, component id = 1, component name = "b0")
  6. NexText tState = NexText(0, 4, "tState");
  7. NexButton bOn = NexButton(0, 2, "bOn");
  8. NexButton bOff = NexButton(0, 3, "bOff");
  9. NexSlider h0 = NexSlider(0, 5, "h0");
  10. NexText tSlider = NexText(0, 6, "tSlider");
  11. NexText tTempC = NexText(1, 5, "tTempC");
  12. NexText tTempF = NexText(1, 4, "tTempF");
  13. NexProgressBar jHumidity = NexProgressBar(1, 8, "jHumidity");
  14. NexText tHumidity = NexText(1, 9, "tHumidity");
  15. NexButton bUpdate = NexButton(1,10, "bUpdate");
  16. // Register a button object to the touch event list.
  17. NexTouch *nex_listen_list[] = {
  18. &bOn,
  19. &bOff,
  20. &h0,
  21. &bUpdate,
  22. NULL
  23. };
  24. /*
  25. * Button bOn component pop callback function.
  26. * When the ON button is released, the LED turns on and the state text changes.
  27. */
  28. void bOnPopCallback(void *ptr) {
  29. tState.setText("State: on");
  30. digitalWrite(led1, HIGH);
  31. }
  32. /*
  33. * Button bOff component pop callback function.
  34. * When the OFF button is released, the LED turns off and the state text changes.
  35. */
  36. void bOffPopCallback(void *ptr) {
  37. tState.setText("State: off");
  38. digitalWrite(led1, LOW);
  39. }
  40. /*
  41. * Slider h0 component pop callback function.
  42. * When the slider is released, the LED brightness changes and the slider text changes.
  43. */
  44. void h0PopCallback(void *ptr) {
  45. uint32_t number = 0;
  46. char temp[10] = {0};
  47. // change text with the current slider value
  48. h0.getValue(&number);
  49. utoa(number, temp, 10);
  50. tSlider.setText(temp);
  51. // change LED brightness
  52. analogWrite(led2, number);
  53. }
  54. /*
  55. * Button bUpdate component pop callback function.
  56. * When the UPDATE button is released, the temperature and humidity readings are updated.
  57. */
  58. void bUpdatePopCallback(void *ptr) {
  59. // Reading temperature or humidity takes about 250 milliseconds!
  60. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  61. float h = dht.readHumidity();
  62. // Read temperature as Celsius (the default)
  63. float t = dht.readTemperature();
  64. // Read temperature as Fahrenheit (isFahrenheit = true)
  65. float f = dht.readTemperature(true);
  66. // Check if any reads failed and exit early (to try again).
  67. if (isnan(h) || isnan(t) || isnan(f)) {
  68. return;
  69. }
  70. // Update temperature in Celsius
  71. static char temperatureCTemp[6];
  72. dtostrf(t, 6, 2, temperatureCTemp);
  73. tTempC.setText(temperatureCTemp);
  74. // Update humidity percentage text and progress bar
  75. char hTemp[10] = {0};
  76. utoa(int(h), hTemp, 10);
  77. tHumidity.setText(hTemp);
  78. jHumidity.setValue(int(h));
  79. // Update temperature in Fahrenheit
  80. static char temperatureFTemp[6];
  81. dtostrf(f, 6, 2, temperatureFTemp);
  82. tTempF.setText(temperatureFTemp);
  83. }
  84. void setup(void) {
  85. dht.begin();
  86. Serial.begin(9600);
  87. // You might need to change NexConfig.h file in your ITEADLIB_Arduino_Nextion folder
  88. // Set the baudrate which is for debug and communicate with Nextion screen
  89. nexInit();
  90. // Register the pop event callback function of the components
  91. bOn.attachPop(bOnPopCallback, &bOn);
  92. bOff.attachPop(bOffPopCallback, &bOff);
  93. h0.attachPop(h0PopCallback);
  94. bUpdate.attachPop(bUpdatePopCallback, &bUpdate);
  95. // Set LEDs as outputs
  96. pinMode(led1, OUTPUT);
  97. pinMode(led2, OUTPUT);
  98. }
  99. void loop(void) {
  100. /*
  101. * When a pop or push event occured every time,
  102. * the corresponding component[right page id and component id] in touch event list will be asked.
  103. */
  104. nexLoop(nex_listen_list);
  105. }