0% found this document useful (0 votes)
29 views7 pages

Lập trình FreeRTOS VỚI ESP32 theo yêu cầu

The document outlines a project using FreeRTOS with the ESP32, detailing the requirements for tasks involving sensors, LCD display, buzzer, and LED control. It includes a schematic for connections, libraries used, and the programming code for implementing the tasks. The code features multiple tasks for reading sensor data, controlling LEDs, and managing semaphores for button interactions.

Uploaded by

nguyenkluan957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

Lập trình FreeRTOS VỚI ESP32 theo yêu cầu

The document outlines a project using FreeRTOS with the ESP32, detailing the requirements for tasks involving sensors, LCD display, buzzer, and LED control. It includes a schematic for connections, libraries used, and the programming code for implementing the tasks. The code features multiple tasks for reading sensor data, controlling LEDs, and managing semaphores for button interactions.

Uploaded by

nguyenkluan957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FreeRTOS VỚI ESP32

Nhóm trình bày: Nguyễn Khắc Luân 22119099


Võ Duy Nghĩa 22119108
Ngô Trần Tuấn Vỹ 22119158
1. Yêu cầu đặt ra:
- 1 loại cảm biến (No outputDigital) (1 task) (lưu vào biến toàn cục).
- LCD hiển thị giá trị 2 cảm biến (tùy chọn) (Softimer 1s/ lần).
- 1 task chạy buzzer tần số (thụ động).
- 2 button bật/ tắt 1 led (1 task cần đáp ứng liền).
- 1 task chạy 1 button cấp Semaphore (max 5 lần).
- 1 task để chớp tắt 1 led (chớp tắt 3 lần, T = 1s), được cấp Semaphore.
2. Sơ đồ kết nối mô phỏng trên Wokwi:

3. Thư viện sử dụng:


- Các thư viện cơ bản có sẵn và chỉ việc khai báo:
o Arduino.h
o Wire.h
o Adafruit_Sensor.h
- Các thư viện khác:
o FreeRTOSConfig.h
o DHT.h
o LiquidCrystal_I2C.h
o Buzzer.h

4. Code lập trình:


#include <Arduino.h>
#include <FreeRTOSConfig.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <Buzzer.h>
//#include <esp_task_wdt.h>

DHT dht(15,DHT22);
LiquidCrystal_I2C lcd(0x27,16,2);
Buzzer buzzer(25);

float t = 0;
float h = 0;

#define btn_sem 12
#define led_sem 13
#define btn_on 27
#define btn_off 14
#define ledd 26

TimerHandle_t Mytimer = NULL;


void btn_sema (void *parameters);
void led_sema(void *parameters);
void On_Off_Led(void *parameters);
void hienthiLCD(TimerHandle_t xMytimer);
//void CrTimer(void *parameters);
void read_sensor(void *parameters);
void TASK_buzzer(void *parameters);
xSemaphoreHandle Sem_Hanle;

void Phatnhac() {
while(1){
buzzer.begin(100); // Khởi
tạo Buzzer tần số
Serial.println("Khởi tạo Buzzer thành công");
buzzer.begin(100);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_C8, 240);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_G7, 160);
buzzer.sound(NOTE_C8, 160); buzzer.sound(NOTE_G7, 160);
buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_D7, 80); buzzer.sound(NOTE_A7, 240);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_E7, 320);
buzzer.sound(NOTE_E7, 160); buzzer.sound(NOTE_D7, 160);
buzzer.sound(NOTE_E7, 160); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_C7, 160); buzzer.sound(NOTE_F7, 160);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_AS7, 160);
buzzer.sound(NOTE_AS7, 80); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_G7, 320);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_C8, 240);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_G7, 160);
buzzer.sound(NOTE_C8, 160); buzzer.sound(NOTE_G7, 160);
buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_D7, 80); buzzer.sound(NOTE_D7, 160);
buzzer.sound(NOTE_E7, 80); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_C7, 320);
buzzer.sound(NOTE_C7, 160); buzzer.sound(NOTE_D7, 80);
buzzer.sound(NOTE_E7, 160); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_C7, 160); buzzer.sound(NOTE_F7, 160);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_AS7, 160);
buzzer.sound(NOTE_AS7, 80); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_F7, 430);

buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_AS7, 80);


buzzer.sound(NOTE_C8, 160); buzzer.sound(NOTE_C8, 160);
buzzer.sound(NOTE_C8, 160);
buzzer.sound(NOTE_C8, 160); buzzer.sound(NOTE_C8, 80);
buzzer.sound(NOTE_D8, 80); buzzer.sound(NOTE_C8, 80);
buzzer.sound(NOTE_AS7, 80);
buzzer.sound(NOTE_A7, 160); buzzer.sound(NOTE_A7, 160);
buzzer.sound(NOTE_A7, 160); buzzer.sound(NOTE_A7, 160);
buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_AS7, 80); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_F7, 160);
buzzer.sound(NOTE_F7, 160);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_E7, 80);
buzzer.sound(NOTE_D7, 160); buzzer.sound(NOTE_E7, 160);
buzzer.sound(NOTE_E7, 80);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_G7, 160);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_G7, 320);

buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_AS7, 80);


buzzer.sound(NOTE_C8, 160); buzzer.sound(NOTE_C8, 160);
buzzer.sound(NOTE_C8, 160);
buzzer.sound(NOTE_C8, 160); buzzer.sound(NOTE_C8, 80);
buzzer.sound(NOTE_D8, 80); buzzer.sound(NOTE_C8, 80);
buzzer.sound(NOTE_AS7, 80);
buzzer.sound(NOTE_A7, 160); buzzer.sound(NOTE_A7, 160);
buzzer.sound(NOTE_A7, 160); buzzer.sound(NOTE_A7, 80);
buzzer.sound(NOTE_AS7, 80);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_E7, 80);
buzzer.sound(NOTE_D7, 160);
buzzer.sound(NOTE_D7, 80); buzzer.sound(NOTE_E7, 80);
buzzer.sound(NOTE_F7, 80); buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_C7, 160);
buzzer.sound(NOTE_F7, 160); buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_A7, 80); buzzer.sound(NOTE_G7, 240);
buzzer.sound(NOTE_G7, 80);
buzzer.sound(NOTE_G7, 80); buzzer.sound(NOTE_F7, 80);
buzzer.sound(NOTE_F7, 320);

buzzer.end(2000);

vTaskDelay(pdMS_TO_TICKS(1000)); // Hàm delay trong freeRTOS (0.1s)


}
}

void setup() {
Sem_Hanle = xSemaphoreCreateCounting(5,0); // cấp tối đa 5 sem, khởi
tạo = 0;
Serial.begin(9600);
//lcd.clear();
lcd.init();
lcd.backlight();
pinMode(btn_sem,INPUT_PULLUP);
pinMode(led_sem,OUTPUT);
pinMode(btn_on,INPUT_PULLUP);
pinMode(btn_off,INPUT_PULLUP);
pinMode(ledd,OUTPUT);
dht.begin();
Mytimer = xTimerCreate("Mytimer", pdMS_TO_TICKS(1000), pdTRUE, 0,
hienthiLCD);
if (Mytimer == NULL) {
Serial.println("Timer creation failed!");
} else {
xTimerStart(Mytimer, portMAX_DELAY);
Serial.println("Timer started!");
}

xTaskCreate(read_sensor, "TASK6", 4096, NULL, 1, NULL);


//xTaskCreate(CrTimer, "TASK5", 4096, NULL, 1, NULL);
xTaskCreate(On_Off_Led, "TASK3", 4096, NULL, 3, NULL);
xTaskCreate(btn_sema, "TASK1", 4096, NULL, 1, NULL);
xTaskCreate(led_sema, "TASK2", 4096, NULL, 1, NULL);
xTaskCreate(TASK_buzzer, "TASK4", 4096, NULL, 1, NULL);
}

void loop() {}

void read_sensor(void *parameters){


while (1)
{
t = dht.readTemperature();
h = dht.readHumidity();
Serial.println(h);
Serial.println(t);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

void On_Off_Led(void *parameters){


TaskHandle_t TASK3;
TASK3 = xTaskGetCurrentTaskHandle();
while (1){
if((digitalRead(btn_on)==0)){
digitalWrite(ledd,1);
while(digitalRead(btn_on)==0){vTaskDelay(pdMS_TO_TICKS(30));}
}
if((digitalRead(btn_off)==0)){
digitalWrite(ledd,0);
while(digitalRead(btn_on)==0){vTaskDelay(pdMS_TO_TICKS(30));}
}
vTaskDelay(pdMS_TO_TICKS(30));
}
}

void TASK_buzzer(void *parameters){


TaskHandle_t TASK4;

while (1){
Phatnhac();

}
}

void btn_sema(void *parameters){


TaskHandle_t TASK1;

while (1){
TASK1 = xTaskGetCurrentTaskHandle();
if(digitalRead(btn_sem)==0){
Serial.printf("GIVE SEM\n");
xSemaphoreGive(Sem_Hanle);
Serial.print("So semaphore da give la: ");
Serial.println(uxSemaphoreGetCount(Sem_Hanle));
while (digitalRead(btn_sem)==0){
vTaskDelay(pdMS_TO_TICKS(30));
}
}
vTaskDelay(pdMS_TO_TICKS(30));
}
}

void led_sema(void *parameters){


TaskHandle_t TASK2;

while (1){
TASK2 = xTaskGetCurrentTaskHandle();
if(xSemaphoreTake(Sem_Hanle, pdMS_TO_TICKS(portMAX_DELAY))== pdTRUE )
{
Serial.print("so semaphore con lai la: ");
Serial.println(uxSemaphoreGetCount(Sem_Hanle));
for(uint8_t i = 0; i < 3; i++){
digitalWrite(led_sem,1);
vTaskDelay(pdMS_TO_TICKS(500));
digitalWrite(led_sem,0);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
}
}

void hienthiLCD(TimerHandle_t xMytimer) {


// Timer callback function

if (xMytimer != NULL) {

lcd.setCursor(0, 0);
lcd.printf("Temp:%.1fC",t);
lcd.setCursor(0,1);
lcd.printf("Humid:%.1f%%",h);
}
}

You might also like