2024-4-2 21:22:00 [显示全部楼层]
177浏览
查看: 177|回复: 0

[ESP8266/ESP32] FireBeetle 2 ESP32 C6+小白手把手教你制作屏幕+温湿度扩展板 2

[复制链接]
本帖最后由 lihuahua 于 2024-4-2 21:22 编辑

小白手把手教你制作 “FireBeetle 2 ESP32 C6开发板”的屏幕+温湿度扩展板-2
上一篇文章,小白教大家制作了ESP32 C6的扩展板,含有dht11传感器模块和0.96tft彩屏。链接

本篇文章教大家在已有硬件上,实现电池电压采集、dht11传感器数据读取,tft彩屏上显示数据功能。

1、代码总体思想:
代码使用freertos,创建两个线程:

  • 温湿度传感器数据读取
创建一个线程用于dht11温湿度读取,该线程通过定时器唤醒;

  1. #include <Arduino.h>
  2. #include <Ticker.h>
  3. #include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
  4. /** Initialize DHT sensor 1 */
  5. DHTesp dhtSensor1;
  6. /** Task handle for the light value read task */
  7. TaskHandle_t tempTaskHandle = NULL;
  8. /** Pin number for DHT11 1 data pin */
  9. int dhtPin1 = 17;
  10. /** Ticker for temperature reading */
  11. Ticker tempTicker;
  12. /** Flags for temperature readings finished */
  13. bool gotNewTemperature = false;
  14. /** Data from sensor 1 */
  15. TempAndHumidity sensor1Data;
  16. /* Flag if main loop is running */
  17. bool tasksEnabled = false;
  18. /**
  19. * Task to reads temperature from DHT11 sensor
  20. * @param pvParameters
  21. *                pointer to task parameters
  22. */
  23. void tempTask(void *pvParameters) {
  24.         Serial.println("tempTask loop started");
  25.         while (1) // tempTask loop
  26.         {
  27.                 if (tasksEnabled && !gotNewTemperature) { // Read temperature only if old data was processed already
  28.                         // Reading temperature for humidity takes about 250 milliseconds!
  29.                         // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
  30.                         sensor1Data = dhtSensor1.getTempAndHumidity();        // Read values from sensor 1
  31.                         gotNewTemperature = true;
  32.                 }
  33.                 vTaskSuspend(NULL);
  34.         }
  35. }
  36. /**
  37. * triggerGetTemp
  38. * Sets flag dhtUpdated to true for handling in loop()
  39. * called by Ticker tempTicker
  40. */
  41. void triggerGetTemp() {
  42.         if (tempTaskHandle != NULL) {
  43.                  xTaskResumeFromISR(tempTaskHandle);
  44.         }
  45. }
复制代码


  • 电池电压采集、显示器显示
创建线程进行电池电压采集、驱动显示器显示温湿度数据和电压值。
线程1的优先级高于线程2的优先级。

  1. #include <Adafruit_GFX.h>    // Core graphics library
  2. #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
  3. #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
  4. #include <SPI.h>
  5. #define TFT_CS        1
  6. #define TFT_RST        14 // Or set to -1 and connect to Arduino RESET pin
  7. #define TFT_DC         8
  8. #define TFT_MOSI 22  // Data out
  9. #define TFT_SCLK 23  // Clock out
  10. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  11. int tft_blk = 15;
  12. TaskHandle_t tftTaskHandle = NULL;
  13. void tftTask(void *pvParameters) {
  14.         while (1) // tempTask loop
  15.         {
  16.     vTaskDelay(2000);
  17. //                Serial.println("Sensor 1 data:");
  18. //                Serial.println("Temp: " + String(sensor1Data.temperature,2) + "'C Humidity: " + String(sensor1Data.humidity,1) + "%");
  19.    
  20.     //数据更新
  21. //    tft.fillRect(5+11*6,15,12*5,15,ST77XX_BLACK);
  22. //    tft.fillRect(5+11*10, 40,12*2,15,ST77XX_BLACK);
  23.     tft.setTextSize(2);tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
  24.     tft.setCursor(5+11*6, 15);  tft.print(String(sensor1Data.temperature,2));
  25.     tft.setCursor(5+11*10, 40); tft.print(String(sensor1Data.humidity,0)+"%");
  26.     //电池电压显示:
  27.     //int analogValue = analogRead(0);
  28.     int analogVolts = analogReadMilliVolts(0);
  29.     tft.setTextSize(1);
  30.     tft.setCursor(3, 3);  
  31.     tft.print(String(analogVolts*0.002,2)+"V");
  32.     gotNewTemperature = false;
  33.         }
  34. }
复制代码


  • 总体代码
  1. #include <Arduino.h>
  2. #include <Ticker.h>
  3. #include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
  4. #include <Adafruit_GFX.h>    // Core graphics library
  5. #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
  6. #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
  7. #include <SPI.h>
  8. #define TFT_CS        1
  9. #define TFT_RST        14 // Or set to -1 and connect to Arduino RESET pin
  10. #define TFT_DC         8
  11. #define TFT_MOSI 22  // Data out
  12. #define TFT_SCLK 23  // Clock out
  13. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  14. int tft_blk = 15;
  15. /** Initialize DHT sensor 1 */
  16. DHTesp dhtSensor1;
  17. /** Task handle for the light value read task */
  18. TaskHandle_t tempTaskHandle = NULL;
  19. TaskHandle_t tftTaskHandle = NULL;
  20. /** Pin number for DHT11 1 data pin */
  21. int dhtPin1 = 17;
  22. /** Ticker for temperature reading */
  23. Ticker tempTicker;
  24. /** Flags for temperature readings finished */
  25. bool gotNewTemperature = false;
  26. /** Data from sensor 1 */
  27. TempAndHumidity sensor1Data;
  28. /* Flag if main loop is running */
  29. bool tasksEnabled = false;
  30. /**
  31. * Task to reads temperature from DHT11 sensor
  32. * @param pvParameters
  33. *                pointer to task parameters
  34. */
  35. void tempTask(void *pvParameters) {
  36.         Serial.println("tempTask loop started");
  37.         while (1) // tempTask loop
  38.         {
  39.                 if (tasksEnabled && !gotNewTemperature) { // Read temperature only if old data was processed already
  40.                         // Reading temperature for humidity takes about 250 milliseconds!
  41.                         // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
  42.                         sensor1Data = dhtSensor1.getTempAndHumidity();        // Read values from sensor 1
  43.                         gotNewTemperature = true;
  44.                 }
  45.                 vTaskSuspend(NULL);
  46.         }
  47. }
  48. void tftTask(void *pvParameters) {
  49.         while (1) // tempTask loop
  50.         {
  51.     vTaskDelay(2000);
  52. //                Serial.println("Sensor 1 data:");
  53. //                Serial.println("Temp: " + String(sensor1Data.temperature,2) + "'C Humidity: " + String(sensor1Data.humidity,1) + "%");
  54.    
  55.     //数据更新
  56. //    tft.fillRect(5+11*6,15,12*5,15,ST77XX_BLACK);
  57. //    tft.fillRect(5+11*10, 40,12*2,15,ST77XX_BLACK);
  58.     tft.setTextSize(2);tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
  59.     tft.setCursor(5+11*6, 15);  tft.print(String(sensor1Data.temperature,2));
  60.     tft.setCursor(5+11*10, 40); tft.print(String(sensor1Data.humidity,0)+"%");
  61.     //电池电压显示:
  62.     //int analogValue = analogRead(0);
  63.     int analogVolts = analogReadMilliVolts(0);
  64.     tft.setTextSize(1);
  65.     tft.setCursor(3, 3);  
  66.     tft.print(String(analogVolts*0.002,2)+"V");
  67.     gotNewTemperature = false;
  68.         }
  69. }
  70. /**
  71. * triggerGetTemp
  72. * Sets flag dhtUpdated to true for handling in loop()
  73. * called by Ticker tempTicker
  74. */
  75. void triggerGetTemp() {
  76.         if (tempTaskHandle != NULL) {
  77.                  xTaskResumeFromISR(tempTaskHandle);
  78.         }
  79. }
  80. /**
  81. * Arduino setup function (called once after boot/reboot)
  82. */
  83. void setup() {
  84.         Serial.begin(115200);
  85.   
  86.   //set the resolution to 12 bits (0-4096)
  87.   analogReadResolution(12);
  88.   //Init ST7735S mini display
  89.   tft.initR(INITR_MINI160x80_PLUGIN);  
  90.   tft.setRotation(1);  tft.fillScreen(ST77XX_BLACK);
  91.   pinMode(tft_blk,OUTPUT);  digitalWrite(tft_blk,HIGH);
  92.   //输出固定文字
  93.   tft.setTextSize(2); tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
  94.   tft.setCursor(5, 15);  tft.print("Temp: ");
  95.   tft.setCursor(5, 40);  tft.print("Humidity: ");
  96.         // Initialize temperature sensor 1
  97.         dhtSensor1.setup(dhtPin1, DHTesp::DHT11);
  98.         xTaskCreatePinnedToCore(
  99.                         tftTask,                                                                                         /* Function to implement the task */
  100.                         "tftTask ",                                                                                /* Name of the task */
  101.                         4000,                                                                                                         /* Stack size in words */
  102.                         NULL,                                                                                                         /* Task input parameter */
  103.                         4,                                                                                                                        /* Priority of the task */
  104.                         &tftTaskHandle,                                                                /* Task handle. */
  105.                         1);                                                                                                                 /* Core where the task should run */
  106.         // Start task to get temperature
  107.         xTaskCreatePinnedToCore(
  108.                         tempTask,                                                                                         /* Function to implement the task */
  109.                         "tempTask ",                                                                                /* Name of the task */
  110.                         4000,                                                                                                         /* Stack size in words */
  111.                         NULL,                                                                                                         /* Task input parameter */
  112.                         5,                                                                                                                        /* Priority of the task */
  113.                         &tempTaskHandle,                                                                /* Task handle. */
  114.                         1);                                                                                                                 /* Core where the task should run */
  115.         if (tempTaskHandle == NULL) {
  116.                 Serial.println("[ERROR] Failed to start task for temperature update");
  117.         } else {
  118.                 // Start update of environment data every 30 seconds
  119.                 tempTicker.attach(2, triggerGetTemp);
  120.         }
  121.         // Signal end of setup() to tasks
  122.         tasksEnabled = true;
  123. } // End of setup.
  124. /**
  125. * loop
  126. * Arduino loop function, called once 'setup' is complete (your own code should go here)
  127. */
  128. void loop() {
  129. } // End of loop
复制代码



2、效果图:
FireBeetle 2 ESP32 C6+小白手把手教你制作屏幕+温湿度扩展板 2图1FireBeetle 2 ESP32 C6+小白手把手教你制作屏幕+温湿度扩展板 2图2

如果大家和我一样,遇到tft彩屏边缘有彩条,则需要更改库文件Adafruit_ST7735.cpp中的屏幕偏移值,操作步骤如下:
FireBeetle 2 ESP32 C6+小白手把手教你制作屏幕+温湿度扩展板 2图3   FireBeetle 2 ESP32 C6+小白手把手教你制作屏幕+温湿度扩展板 2图4



3、参考链接:
  • https://wiki.dfrobot.com.cn/_SKU_DFR1075_FireBeetle_2_Board_ESP32_C6
  • https://wiki.dfrobot.com.cn/_SKU_DFR1075_FireBeetle_2_Board_ESP32_C6_Basic_Tutorial
  • https://wiki.dfrobot.com.cn/_SKU_DFR1075_FireBeetle_2_Board_ESP32_C6_Advanced_Tutorial


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail