本帖最后由 lihuahua 于 2024-4-2 21:22 编辑
小白手把手教你制作 “FireBeetle 2 ESP32 C6开发板”的屏幕+温湿度扩展板-2 上一篇文章,小白教大家制作了ESP32 C6的扩展板,含有dht11传感器模块和0.96tft彩屏。链接
本篇文章教大家在已有硬件上,实现电池电压采集、dht11传感器数据读取,tft彩屏上显示数据功能。
1、代码总体思想: 创建一个线程用于dht11温湿度读取,该线程通过定时器唤醒;
- #include <Arduino.h>
- #include <Ticker.h>
- #include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
-
-
- /** Initialize DHT sensor 1 */
- DHTesp dhtSensor1;
-
- /** Task handle for the light value read task */
- TaskHandle_t tempTaskHandle = NULL;
-
- /** Pin number for DHT11 1 data pin */
- int dhtPin1 = 17;
-
- /** Ticker for temperature reading */
- Ticker tempTicker;
- /** Flags for temperature readings finished */
- bool gotNewTemperature = false;
- /** Data from sensor 1 */
- TempAndHumidity sensor1Data;
-
- /* Flag if main loop is running */
- bool tasksEnabled = false;
-
-
- /**
- * Task to reads temperature from DHT11 sensor
- * @param pvParameters
- * pointer to task parameters
- */
- void tempTask(void *pvParameters) {
- Serial.println("tempTask loop started");
- while (1) // tempTask loop
- {
- if (tasksEnabled && !gotNewTemperature) { // Read temperature only if old data was processed already
- // Reading temperature for humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
- sensor1Data = dhtSensor1.getTempAndHumidity(); // Read values from sensor 1
-
- gotNewTemperature = true;
- }
- vTaskSuspend(NULL);
- }
- }
-
-
- /**
- * triggerGetTemp
- * Sets flag dhtUpdated to true for handling in loop()
- * called by Ticker tempTicker
- */
- void triggerGetTemp() {
- if (tempTaskHandle != NULL) {
- xTaskResumeFromISR(tempTaskHandle);
- }
- }
复制代码
创建线程进行电池电压采集、驱动显示器显示温湿度数据和电压值。 线程1的优先级高于线程2的优先级。
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
- #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
- #include <SPI.h>
-
- #define TFT_CS 1
- #define TFT_RST 14 // Or set to -1 and connect to Arduino RESET pin
- #define TFT_DC 8
- #define TFT_MOSI 22 // Data out
- #define TFT_SCLK 23 // Clock out
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
- int tft_blk = 15;
-
- TaskHandle_t tftTaskHandle = NULL;
-
- void tftTask(void *pvParameters) {
- while (1) // tempTask loop
- {
- vTaskDelay(2000);
- // Serial.println("Sensor 1 data:");
- // Serial.println("Temp: " + String(sensor1Data.temperature,2) + "'C Humidity: " + String(sensor1Data.humidity,1) + "%");
-
- //数据更新
- // tft.fillRect(5+11*6,15,12*5,15,ST77XX_BLACK);
- // tft.fillRect(5+11*10, 40,12*2,15,ST77XX_BLACK);
- tft.setTextSize(2);tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
- tft.setCursor(5+11*6, 15); tft.print(String(sensor1Data.temperature,2));
- tft.setCursor(5+11*10, 40); tft.print(String(sensor1Data.humidity,0)+"%");
-
- //电池电压显示:
- //int analogValue = analogRead(0);
- int analogVolts = analogReadMilliVolts(0);
- tft.setTextSize(1);
- tft.setCursor(3, 3);
- tft.print(String(analogVolts*0.002,2)+"V");
-
- gotNewTemperature = false;
- }
- }
复制代码
- #include <Arduino.h>
- #include <Ticker.h>
- #include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
-
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
- #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
- #include <SPI.h>
-
-
- #define TFT_CS 1
- #define TFT_RST 14 // Or set to -1 and connect to Arduino RESET pin
- #define TFT_DC 8
- #define TFT_MOSI 22 // Data out
- #define TFT_SCLK 23 // Clock out
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
- int tft_blk = 15;
-
-
- /** Initialize DHT sensor 1 */
- DHTesp dhtSensor1;
-
- /** Task handle for the light value read task */
- TaskHandle_t tempTaskHandle = NULL;
- TaskHandle_t tftTaskHandle = NULL;
-
- /** Pin number for DHT11 1 data pin */
- int dhtPin1 = 17;
-
- /** Ticker for temperature reading */
- Ticker tempTicker;
- /** Flags for temperature readings finished */
- bool gotNewTemperature = false;
- /** Data from sensor 1 */
- TempAndHumidity sensor1Data;
-
- /* Flag if main loop is running */
- bool tasksEnabled = false;
-
-
- /**
- * Task to reads temperature from DHT11 sensor
- * @param pvParameters
- * pointer to task parameters
- */
- void tempTask(void *pvParameters) {
- Serial.println("tempTask loop started");
- while (1) // tempTask loop
- {
- if (tasksEnabled && !gotNewTemperature) { // Read temperature only if old data was processed already
- // Reading temperature for humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
- sensor1Data = dhtSensor1.getTempAndHumidity(); // Read values from sensor 1
-
- gotNewTemperature = true;
- }
- vTaskSuspend(NULL);
- }
- }
-
- void tftTask(void *pvParameters) {
- while (1) // tempTask loop
- {
- vTaskDelay(2000);
- // Serial.println("Sensor 1 data:");
- // Serial.println("Temp: " + String(sensor1Data.temperature,2) + "'C Humidity: " + String(sensor1Data.humidity,1) + "%");
-
- //数据更新
- // tft.fillRect(5+11*6,15,12*5,15,ST77XX_BLACK);
- // tft.fillRect(5+11*10, 40,12*2,15,ST77XX_BLACK);
- tft.setTextSize(2);tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
- tft.setCursor(5+11*6, 15); tft.print(String(sensor1Data.temperature,2));
- tft.setCursor(5+11*10, 40); tft.print(String(sensor1Data.humidity,0)+"%");
-
- //电池电压显示:
- //int analogValue = analogRead(0);
- int analogVolts = analogReadMilliVolts(0);
- tft.setTextSize(1);
- tft.setCursor(3, 3);
- tft.print(String(analogVolts*0.002,2)+"V");
-
- gotNewTemperature = false;
- }
- }
-
- /**
- * triggerGetTemp
- * Sets flag dhtUpdated to true for handling in loop()
- * called by Ticker tempTicker
- */
- void triggerGetTemp() {
- if (tempTaskHandle != NULL) {
- xTaskResumeFromISR(tempTaskHandle);
- }
- }
-
- /**
- * Arduino setup function (called once after boot/reboot)
- */
- void setup() {
- Serial.begin(115200);
-
- //set the resolution to 12 bits (0-4096)
- analogReadResolution(12);
-
- //Init ST7735S mini display
- tft.initR(INITR_MINI160x80_PLUGIN);
- tft.setRotation(1); tft.fillScreen(ST77XX_BLACK);
- pinMode(tft_blk,OUTPUT); digitalWrite(tft_blk,HIGH);
-
- //输出固定文字
- tft.setTextSize(2); tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
-
- tft.setCursor(5, 15); tft.print("Temp: ");
- tft.setCursor(5, 40); tft.print("Humidity: ");
-
- // Initialize temperature sensor 1
- dhtSensor1.setup(dhtPin1, DHTesp::DHT11);
-
- xTaskCreatePinnedToCore(
- tftTask, /* Function to implement the task */
- "tftTask ", /* Name of the task */
- 4000, /* Stack size in words */
- NULL, /* Task input parameter */
- 4, /* Priority of the task */
- &tftTaskHandle, /* Task handle. */
- 1); /* Core where the task should run */
-
- // Start task to get temperature
- xTaskCreatePinnedToCore(
- tempTask, /* Function to implement the task */
- "tempTask ", /* Name of the task */
- 4000, /* Stack size in words */
- NULL, /* Task input parameter */
- 5, /* Priority of the task */
- &tempTaskHandle, /* Task handle. */
- 1); /* Core where the task should run */
-
- if (tempTaskHandle == NULL) {
- Serial.println("[ERROR] Failed to start task for temperature update");
- } else {
- // Start update of environment data every 30 seconds
- tempTicker.attach(2, triggerGetTemp);
- }
-
- // Signal end of setup() to tasks
- tasksEnabled = true;
- } // End of setup.
-
-
- /**
- * loop
- * Arduino loop function, called once 'setup' is complete (your own code should go here)
- */
- void loop() {
-
- } // End of loop
复制代码
2、效果图:
如果大家和我一样,遇到tft彩屏边缘有彩条,则需要更改库文件Adafruit_ST7735.cpp中的屏幕偏移值,操作步骤如下:
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
|