#define BLYNK_TEMPLATE_ID "TMPL64AQ4mn3P"
#define BLYNK_TEMPLATE_NAME "embedded"
#define BLYNK_AUTH_TOKEN "2IyciabkqQtOdxLxIodrWu62prR5Qtbs"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
// #include <TridentTD_LineNotify.h>
char auth[] = "2IyciabkqQtOdxLxIodrWu62prR5Qtbs"; //ป้อน BLYNK_AUTH_TOKEN
char ssid[] = "Wokwi-GUEST"; //Wokwi-GUEST KUWIN-AIS
char pass[] = "";
#define LINE_TOKEN "ZwczY7lqOQpkqrKOM8LET26E2fNnvSheiG2fVzBvAUs" // LINE Token
BlynkTimer timer;
// ตั้งค่าที่ต้องการสำหรับ LCD
const int lcdColumns = 16;
const int lcdRows = 2;
const int lcdAddress = 0x27; // ค่าที่ใช้สำหรับแต่ละจอ LCD I2C อาจแตกต่างกันไป
// สร้างอ็อบเจกต์ของคลาส LiquidCrystal_I2C
LiquidCrystal_I2C lcd(lcdAddress, lcdColumns, lcdRows);
// สุทธิดา รองสุพรรณ
int measurePin = 13; // กำหนดขาที่ใช้ต่อกับเซ็นเซอร์ GP2Y1010AU0F
int ledPower = 27; // กำหนดขาที่ใช้สำหรับเปิด/ปิดไฟ LED ในเซ็นเซอร์ GP2Y1010AU0F
// Variable in monitor task
int Buzzer = 15;
int Lock_voMeasured = 0;
// Variable in sersor task
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
// ตัวแปรสำหรับส่งค่าระหว่าง task
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
// task handle
TaskHandle_t sensor; // จัดการ task sensor
TaskHandle_t monitor; // จัดการ task monitor
void Monitor() {
Serial.println();
Serial.print("Monitor Raw Signal Value (0-4095): ");
Serial.print(voMeasured);
Serial.print(" - Monitor ---> Voltage: ");
Serial.print(calcVoltage);
Serial.print(" - Monitor ---> Dust Density: ");
Serial.println(dustDensity);
lcd.setCursor(0,0);
lcd.print("raw: " + String(voMeasured));
Blynk.virtualWrite(V2 , voMeasured);
if( voMeasured >= 1100 ){
digitalWrite(Buzzer , LOW);
delay(500);
digitalWrite(Buzzer , HIGH);
delay(250);
}
else if(voMeasured < 1100){
digitalWrite(Buzzer , LOW);
}
if( voMeasured >= 1100 && Lock_voMeasured == 0 ){
// LINE.notify("แจ้งเตือนตรวจพบฝุ่นเกินกว่ากำหนด");
Lock_voMeasured = 1 ;
}
else if( voMeasured < 1100 && Lock_voMeasured == 1 ){
Lock_voMeasured = 0 ;
}
}
void setup() {
Serial.begin(9600);
pinMode(ledPower, OUTPUT); // กำหนดขาที่ใช้สำหรับเปิด/ปิดไฟ LED ในเซ็นเซอร์ GP2Y1010AU0F เป็น OUTPUT
pinMode(Buzzer , OUTPUT);
Serial.println();
Serial.println("Started");
// กำหนดความคืบหน้าให้กับจอ LCD
lcd.init();
// ส่งข้อความไปยังจอ LCD
lcd.backlight(); // เปิดไฟหน้าจอ LCD
lcd.setCursor(0, 0); // ตำแหน่งแถวและคอลัมน์
lcd.print("Hello, som!");
digitalWrite(Buzzer , LOW);
// LINE.setToken(LINE_TOKEN); // เริ่มต้นใช้ LINE Notify
Blynk.begin(auth, ssid, pass);
// LINE.notify("เซนเซอร์ตรวจจับฝุ่นเริ่มทำงาน");
timer.setInterval(1000L, Monitor); //ตั้ง Timer ให้ทำงานทุก 1 วินาที
//(function_task, task_name, stack_size, task_parameter, task_piority, task_handle, cpu_core)
xTaskCreatePinnedToCore(sensor_task, "Sensor", 10000, NULL, 1, &sensor, 0); // attach task to core
delay(500);
xTaskCreatePinnedToCore(monitor_task, "Monitor", 10000, NULL, 1, &monitor, 1); // attach task to core
delay(500);
}
void sensor_task(void* pvParameters) {
while (true) {
for (int i=0; i<4096; i++) {
Serial.print("Sensor ---> Raw Signal Value (0-4095): ");
Serial.print(voMeasured);
Serial.print(" - Sensor ---> Voltage: ");
Serial.print(calcVoltage);
Serial.print(" - Sensor ---> Dust Density: ");
Serial.println(dustDensity);
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
// voMeasured = analogRead(measurePin); // read the dust value
voMeasured = i; // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 3.3V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (3.3 / 1024);
dustDensity = 0.17 * calcVoltage - 0.1;
}
}
}
void monitor_task(void* pvParameters) {
while (true) {
Blynk.run();
timer.run();
}
}
void loop() {
}