ผมสนใจทำมินิโปรเจคด้วย ESP32

ไม่ผ่านครับ :disappointed_relieved:

เอาโค้ตตัวเต็มที่เราแก้ล่าสุดมาดูหน่อยครับ

// *** (2) SYSTEM CONFIGURATION ***

//WIFI CONFIG
char ssid[] = "PC-Faster-8093";
char pass[] = "00000000";

//BLYNK TOKEN
char auth[] = "e7f4cf21602844349219dc86b5679918";
char server[] = "blynk.honey.co.th";
unsigned int port = 9443;

/* ---------------------------------------------------------------*/

// *** WATERING CONFIG ***

//ค่าเปอร์เซ็นต์ความชื้นในดินที่ต้องการให้เริ่มรดน้ำต้นไม้
#define PercentWatering = 40

//ตั้งระยะเวลาการรดน้ำต้นไม้แต่ละครั้ง
//15000 = 15 วินาที
#define TimeWatering = 15000

//ตั้งระยะห่างของเวลาที่ต้องการเช็คความชื้นในดิน
//60000 = 6 นาที
#define TimeSoilCheck = 360000

/* ---------------------------------------------------------------*/

// *** TIMER CONFIG ***

//ตั้งเวลาส่งข้อมูลให้ BLYNK SERVER ทุกๆ 2 นาที
//120 Secounds = 120000 Milliseconds
#define sendSensorTime = 120000

//ตั้งเวลาส่งข้อมูลให้ BLYNK SERVER ทุกๆ 3 นาที
//180 Secound = 180000 Milliseconds
#define sendSensor2Time = 180000

//ตั้งเวลาให้เช็คว่า Blynk เชื่อมต่ออยู่หรือไม่ทุกๆ 10 วินาที
#define checkConnectionTime = 10000

// *** (3) LIBRARY INCLUDE ***

//ไลบรารี่เกี่ยวกับ WIFI
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>

//ไลบรารี่ของ BLYNK
#include <BlynkSimpleEsp32.h>

//ไลบรารี่ของเซ็นเซอร์ DHT11
#include <SimpleDHT.h>

//ไลบรารี่สำหรับดึงเวลาจากอินเตอร์เน็ต NTP
#include <time.h>

// *** (4) GPIO PIN SETUP ***

//กำหนดให้เซ็นเซอร์ DHT11 ใช้ขา GPIO ที่ 32
#define DHTPin 32

//กำหนดให้เซ็นเซอร์วัดความชื้นในดิน ใช้ขา GPIO ที่ 34
#define SoilSensorPin 34

//กำหนดให้เซ็นเซอร์วัดความเข้มของแสง ใช้ขา GPIO ที่ 35
#define LdrSensorPin 35

//กำหนดให้สวิตซ์ปุ่มกด ใช้ขา GPIO ที่ 25
#define SwitchPin 25

//กำหนดให้หลอดไฟ LED แสดงสถานะการส่งข้อมูล blynk server ใช้ขา GPIO ที่ 26
#define LEDSensorPin 26

//กำหนดให้ขาอ้างอิงการสั่งรดน้ำต้นไม้ ใช้ขา GPIO ที่ 27
#define WateringPin 27

//กำหนดให้หลอดไฟ LED บนบอร์ดแสดงสถานะ WIFI ใช้ขา GPIO ที่ 2
#define LEDWiFiPin 2

//กำหนดขาอ้างอิงการรดน้ำต้นไม้ ใช้ขา GPIO ที่ 14
#define LEDWateringPin 14 

//กำหนดหให้ขาควบคุม Relay ใช้ขา GPIO ที่ 5
#define RelayPin 5

// *** (5) BLYNK PARAMETER SETUP ***

//ตั้งค่าตัวแปรระบบของ BLYNK
#define BLYNK_TIMEOUT_MS  750
#define BLYNK_HEARTBEAT   17
#define BLYNK_PRINT Serial

//การกำหนดขา Vitual Pin ที่สร้างขึ้นให้กับ Widget ต่างๆ ใน Blynk Mobile APP
#define Widget_Humidity V1
#define Widget_Temperature V2
#define Widget_SoilRawValue V3
#define Widget_SoilLevel V4
#define Widget_LightRawValue V5
#define Widget_LightLevel V6
#define Widget_WateringButton V7
#define Widget_SendSensorStatus V8
#define Widget_WateringStatus V9
#define Widget_WifiSignal V10
#define Widget_WifiRawValue V11
#define Widget_Terminal V12

WidgetLED LEDSensor(Widget_SendSensorStatus);
WidgetLED LEDWatering(Widget_WateringStatus);
WidgetTerminal terminal(Widget_Terminal);

// *** (6) NTP SERVER SETUP ***

char ntp_server1[20] = "time.navy.mi.th";
char ntp_server2[20] = "clock.nectec.or.th";
char ntp_server3[20] = "th.pool.ntp.org";

// *** (7) GENAREL SETUP ***

//ระบุรุ่นเซ็นเซอร์วัดความชื้นสัมพัทธ์และอุณมหภูมิเป็นรุ่น DHT11
SimpleDHT11 dht11;

//เรียกใช้การตั้งเวลาของ Blynk
BlynkTimer timer;

//กำหนดตัวแปรเริ่มต้น
int WifiSignal;
bool isFirstConnect = true;
byte oldtemperature;
byte oldhumidity;
int CurrentSoilValue;
int MapReadSoilValue;
int CurrentWiFiSignal;
int MapReadLightValue;
int CurrentLightValue;
int dhtdata[2];

// *** (8) BLYNK CONNECTED FUNCTION ***

BLYNK_CONNECTED() {
  
//ให้หลอดไฟ LED บนบอร์ดสีน้ำเงินติดเพื่อเป็นการแสดงสถานะว่าการเชื่อมต่อเสร็จสมบูรณ์
digitalWrite(LEDBUILIN, HIGH);

//ถ้าเป็นการเชื่อมต่อ Blynk Server ครั้งแรกหลังบอร์ดรีบูต
 if(isFirstConnect) {
  
   //ให้ซิงค์ข้อมูลทั้งหมดล่าสุดจาก Blynk Server
   Blynk.syncAll();
   
   //ให้ซิงค์สถานะของขา Virtual V7
   Blynk.syncVirtual(Widget_WateringButton);
   
   isFirstConnect = false;
   
 }
}

// *** (9) BLYNK BUTTON FUNCTION ***

BLYNK_WRITE(Widget_WateringButton) {
  
 //ค่าที่ได้จาก Widget_WateringButton (V7) จะเป็น 0 และ 1 ถ้ามีการปดปุ่ม
 if (param.asInt() == 1) {
  
   //ป้องกันไม่ให้กดซ้ำ
   if (digitalRead(WateringPin) != LOW) {
    
      //เรียกใช้ฟังก์ชันรดน้ำ
      digitalWrite(WateringPin, LOW);

   } //if
 } //if
}

// *** (10) FUNCTION SETUP() ***

void setup() {
  
//เรียกใช้งาน Serial Monitor
Serial.begin(115200);

/* -------------------------------------------------------------- */

//กำหนดโหมดใช้งานให้กำขา GPIO
pinMode(LEDWiFiPin, OUTPUT);
pinMode(LEDSensorPin, OUTPUT);
pinMode(LEDWateringPin, OUTPUT);
pinMode(WateringPin, OUTPUT);
pinMode(RelayPin, OUTPUT);
pinMode(SwitchPin, INPUT_PULLUP);

/* -------------------------------------------------------------- */

//ตั้งสถานะเริ่มต้นให้กับขา GPIO เพื่อป้องกันการทำงานเองตอนที่บอร์ด ESP32 รีบูต
digitalWrite(LEDWiFiPin, LOW);
digitalWrite(LEDSensorPin, LOW);
digitalWrite(LEDWateringPin, LOW);
digitalWrite(LEDWateringPin, HIGH);
digitalWrite(RelayPin, HIGH);

/* -------------------------------------------------------------- */

// สร้างมัลติทาสก์ขึ้นมาใช้งานอย่างอิสระจำนวน 5 ทาสก์
//TASK1: ลูปเช็คค่าความชื้นดิน ถ้าต่ำกว่าที่กำหนดให้เร่ิมการรดน้ำต้นไม้
xTaskCreate(&Task1, "Task1",  2000, NULL, 9, NULL);

//TASK2: ลูปอ่านค่าการกดปุ่มสวิตซ์ ถ้ามีการกดปุ่มสวิตซ์ให้เริ่มการรดน้ำต้นไม้
xTaskCreate(&Task2, "Task2", 1000, NULL, 9, NULL);

//TASK3: ลูปให้หลอดไฟ LED กระพริบ ถ้ามีการรดน้ำต้นไม้
xTaskCreate(&Task3, "Task3", 3000, NULL, 8, NULL);

//TASK4: ลูปเช็คการเชื่อมต่อ WiFi และให้เชื่อมต่อใหม่เองแบบอัตโนมัติ
xTaskCreate(&Task4, "Task4", 3000, NULL, 10, NULL);

//TASK5: รดน้ำต้นไม้ตามเวลาที่กำหนด
xTaskCreate(&Task5, "Task5", 3000, NULL, 9, NULL);

/* -------------------------------------------------------------- */

//ทำการเชื่อมต่อ WiFi

Blynk.connectWiFi(ssid, pass);

/* -------------------------------------------------------------- */

//ตั้งเวลาส่งข้อมูลให้ BLYNK SERVER
timer.setInterval(sendSensorTime, sendSensor);

//ตั้งเวลาส่งข้อมูลให้ BLYNK SERVER
timer.setInterval(sendSensor2Time, sendSensor2);

//ตั้งเวลาให้เช็คว่า Blynk เชื่อมต่ออยู่หรือไม่
timer.setInterval(checkConnectionTime, CheckConnection);

/* -------------------------------------------------------------- */

//แสดงข้อความใน Serial Monitor
Serial.println();
Serial.println("Welcom to smartfram IoT");
Serial.print("WiFi Connected: ");
Serial.println(WiFi.localIP());
Serial.print("WiFi signal RSSI: ");
Serial.print(WiFi.RSSI());
Serial.println("dBm");

/* -------------------------------------------------------------- */

//ดึงเวลาจากอินเตอร์เน็ตจาก NTP Time Server
configTime(7 * 3600, 0, ntp_server1, ntp_server2, ntp_server3);

//แสดงข้อความใน Serial Monitor
Serial.println("Waiting for time.");

while (!time(nullptr)) {
  
Serial.print(".");
vTaskDelay(100 / portTICK_PERIOD_MS);

}

//แสดงข้อความใน Serial Monitor
Serial.println();
Serial.println("Current time: " + NowString());

/* -------------------------------------------------------------- */

//ทำการเชื่อมต่อ BLYNK SERVER
Blynk.config(auth, server, port);
Blynk.connect();

/* -------------------------------------------------------------- */

}

// *** (11) FUNCTION LOOP() ***

void loop() {
  
Blynk.run();
timer.run();

}

// *** (12) FUNCTION SENDSENSOR() ***

void sendSensor() {
  
//ให้หลอดไฟ LED ติด
digitalWrite(LEDWiFiPin, HIGH);

//ให้หลอดไฟ LED บน BLYNK MOBILE APP ติด
LEDSensor.on();

//แสดงข้อความใน Serial Monitor
Serial.print(NowString());
Serial.println(", Send data to blynk server");

/* -------------------------------------------------------------- */

//อ่านข้อมูลเซ็นเซอร์ DHT11
GetDHT11(dhtdata);

Blynk.virtualWrite(Widget_Humidity, dhtdata[0]);
Blynk.virtualWrite(Widget_Temperature, dhtdata[1]);
Blynk.virtualWrite(Widget_SoilRawValue, analogRead(SoilSensorPin));
Blynk.virtualWrite(Widget_SoilLevel, SoilPercentValue());

/* -------------------------------------------------------------- */

//แสดงข้อความใน Serial Monitor
Serial.print(NowString());
Serial.println(", Send DHT11 / Soil sensors to blynk server.");

/* -------------------------------------------------------------- */

//ให้หลอด LED ที่แสดงสถานะดับ
digitalWrite(LEDWiFiPin, LOW);

//ให้หลอดไฟ LED บน BLYNK MOBILE APP ดับ
LEDSensor.off(); 

}

// *** (13) FUNCTION SENDSENSOR2() ***

void sendSensor2() {
  
//ให้หลอดไฟ LED ติด
digitalWrite(LEDWiFiPin, HIGH);

//ให้หลอดไฟ LED บน BLYNK MOBILE APP ติด
LEDSensor.on();

//แสดงข้อความใน Serial Monitor
Serial.print(NowString());
Serial.println(", Send data to blynk server");

/* -------------------------------------------------------------- */

//แสดงข้อความใน Serial Monitor
Serial.print(NowString());
Serial.print(", Wifi Signal: ");
Serial.print(WifiPercentSignal());
Serial.println("%");

/* -------------------------------------------------------------- */

//อัพเดทข้อมูลไปยัง Blynk server

Blynk.virtualWrite(Widget_LightRawValue, analogRead(LdrSensorPin));
Blynk.virtualWrite(Widget_LightLevel, LdrPercentValue());
Blynk.virtualWrite(Widget_WifiSignal, WifiPercentSignal());
Blynk.virtualWrite(Widget_WifiRawValue, WiFi.RSSI());

/* -------------------------------------------------------------- */

//แสดงข้อความใน Serial Monitor
Serial.print(NowString());
Serial.println(", Send LDR sensors / WIFI to blynk server.");

/* -------------------------------------------------------------- */

//ให้หลอด LED ที่แสดงสถานะดับ
digitalWrite(LEDWiFiPin, LOW);

//ให้หลอด LED บน app Blynk ดับ
LEDSensor.off();
}

ที่อยู่ ไลเบอรี่ผิดครับ แก้ตามที่มันบอก

1 Likes

ดูยังไงครับอย่างเช่นตัวนี้ใช่ไหมครับ

ลองตัดบรรทัดนี้ออกไปก่อนแล้วคอมไพรใหม่ว่าผ่านไหม

1 Likes

ไม่ผ่านเหมือนเดิมครับตัดออกก็เปลี่ยนไปเป็นตัวที่ error ตัวต่อไป
ข้อความที่คอมไพรไม่ผ่านครับ

Warning: Board espressif:esp32:esp32-gateway doesn't define a 'build.board' preference. Auto-set to: ESP32_ESP32-GATEWAY
E:\000\100\100.ino: In function 'void BlynkOnConnected()':

100:143:14: error: 'LEDBUILIN' was not declared in this scope

 digitalWrite(LEDBUILIN, HIGH);

              ^

E:\000\100\100.ino: In function 'void setup()':

100:206:14: error: 'Task1' was not declared in this scope

 xTaskCreate(&Task1, "Task1",  2000, NULL, 9, NULL);

              ^

100:209:14: error: 'Task2' was not declared in this scope

 xTaskCreate(&Task2, "Task2", 1000, NULL, 9, NULL);

              ^

100:212:14: error: 'Task3' was not declared in this scope

 xTaskCreate(&Task3, "Task3", 3000, NULL, 8, NULL);

              ^

100:215:14: error: 'Task4' was not declared in this scope

 xTaskCreate(&Task4, "Task4", 3000, NULL, 10, NULL);

              ^

100:218:14: error: 'Task5' was not declared in this scope

 xTaskCreate(&Task5, "Task5", 3000, NULL, 9, NULL);

              ^

100:33:24: error: expected primary-expression before '=' token

 #define sendSensorTime = 120000

                        ^

E:\000\100\100.ino:229:19: note: in expansion of macro 'sendSensorTime'

 timer.setInterval(sendSensorTime, sendSensor);

                   ^

100:37:25: error: expected primary-expression before '=' token

 #define sendSensor2Time = 180000

                         ^

E:\000\100\100.ino:232:19: note: in expansion of macro 'sendSensor2Time'

 timer.setInterval(sendSensor2Time, sendSensor2);

                   ^

100:40:29: error: expected primary-expression before '=' token

 #define checkConnectionTime = 10000

                             ^

E:\000\100\100.ino:235:19: note: in expansion of macro 'checkConnectionTime'

 timer.setInterval(checkConnectionTime, CheckConnection);

                   ^

100:235:40: error: 'CheckConnection' was not declared in this scope

 timer.setInterval(checkConnectionTime, CheckConnection);

                                        ^

100:265:45: error: 'NowString' was not declared in this scope

 Serial.println("Current time: " + NowString());

                                             ^

E:\000\100\100.ino: In function 'void sendSensor()':

100:297:24: error: 'NowString' was not declared in this scope

 Serial.print(NowString());

                        ^

100:303:17: error: 'GetDHT11' was not declared in this scope

 GetDHT11(dhtdata);

                 ^

100:308:55: error: 'SoilPercentValue' was not declared in this scope

 Blynk.virtualWrite(Widget_SoilLevel, SoilPercentValue());

                                                       ^

E:\000\100\100.ino: In function 'void sendSensor2()':

100:337:24: error: 'NowString' was not declared in this scope

 Serial.print(NowString());

                        ^

100:345:32: error: 'WifiPercentSignal' was not declared in this scope

 Serial.print(WifiPercentSignal());

                                ^

100:353:55: error: 'LdrPercentValue' was not declared in this scope

 Blynk.virtualWrite(Widget_LightLevel, LdrPercentValue());

                                                       ^

Multiple libraries were found for "WiFi.h"
 Used: C:\Users\EPZTaylor\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.1\libraries\WiFi
 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
Multiple libraries were found for "BlynkSimpleEsp32.h"
 Used: C:\Users\EPZTaylor\Documents\Arduino\libraries\src
 Not used: C:\Users\EPZTaylor\Documents\Arduino\libraries\Blynk
 Not used: C:\Users\EPZTaylor\Documents\Arduino\libraries\Blynk
 Not used: C:\Users\EPZTaylor\Documents\Arduino\libraries\Blynk
 Not used: C:\Users\EPZTaylor\Documents\Arduino\libraries\Blynk
exit status 1
'LEDBUILIN' was not declared in this scope

จุดนี้แก้ เพิ่ม

#define LEDBUILIN 2

1 Likes

ไลเบอลี่ต้องใส่ในที่อยู่ที่ขีดเส้นเท่านั่นครับ

ที่อื่นลบออกตามที่มันบอก

1 Likes

ถูกไหมครับ

//ให้หลอดไฟ LED บนบอร์ดสีน้ำเงินติดเพื่อเป็นการแสดงสถานะว่าการเชื่อมต่อเสร็จสมบูรณ์
digitalWrite(LEDBUILIN, HIGH);
#define LEDBUILIN 2

คอมไพรเเล้วเหลือส่วนนี้ครับ

Warning: Board espressif:esp32:esp32-gateway doesn't define a 'build.board' preference. Auto-set to: ESP32_ESP32-GATEWAY
E:\000\100\100.ino: In function 'void setup()':

100:207:14: error: 'Task1' was not declared in this scope

 xTaskCreate(&Task1, "Task1",  2000, NULL, 9, NULL);

              ^

100:210:14: error: 'Task2' was not declared in this scope

 xTaskCreate(&Task2, "Task2", 1000, NULL, 9, NULL);

              ^

100:213:14: error: 'Task3' was not declared in this scope

 xTaskCreate(&Task3, "Task3", 3000, NULL, 8, NULL);

              ^

100:216:14: error: 'Task4' was not declared in this scope

 xTaskCreate(&Task4, "Task4", 3000, NULL, 10, NULL);

              ^

100:219:14: error: 'Task5' was not declared in this scope

 xTaskCreate(&Task5, "Task5", 3000, NULL, 9, NULL);

              ^

100:33:24: error: expected primary-expression before '=' token

 #define sendSensorTime = 120000

                        ^

E:\000\100\100.ino:230:19: note: in expansion of macro 'sendSensorTime'

 timer.setInterval(sendSensorTime, sendSensor);

                   ^

100:37:25: error: expected primary-expression before '=' token

 #define sendSensor2Time = 180000

                         ^

E:\000\100\100.ino:233:19: note: in expansion of macro 'sendSensor2Time'

 timer.setInterval(sendSensor2Time, sendSensor2);

                   ^

100:40:29: error: expected primary-expression before '=' token

 #define checkConnectionTime = 10000

                             ^

E:\000\100\100.ino:236:19: note: in expansion of macro 'checkConnectionTime'

 timer.setInterval(checkConnectionTime, CheckConnection);

                   ^

100:236:40: error: 'CheckConnection' was not declared in this scope

 timer.setInterval(checkConnectionTime, CheckConnection);

                                        ^

100:266:45: error: 'NowString' was not declared in this scope

 Serial.println("Current time: " + NowString());

                                             ^

E:\000\100\100.ino: In function 'void sendSensor()':

100:298:24: error: 'NowString' was not declared in this scope

 Serial.print(NowString());

                        ^

100:304:17: error: 'GetDHT11' was not declared in this scope

 GetDHT11(dhtdata);

                 ^

100:309:55: error: 'SoilPercentValue' was not declared in this scope

 Blynk.virtualWrite(Widget_SoilLevel, SoilPercentValue());

                                                       ^

E:\000\100\100.ino: In function 'void sendSensor2()':

100:338:24: error: 'NowString' was not declared in this scope

 Serial.print(NowString());

                        ^

100:346:32: error: 'WifiPercentSignal' was not declared in this scope

 Serial.print(WifiPercentSignal());

                                ^

100:354:55: error: 'LdrPercentValue' was not declared in this scope

 Blynk.virtualWrite(Widget_LightLevel, LdrPercentValue());

                                                       ^

exit status 1
'Task1' was not declared in this scope

ตอนนี้ผมก็ยังติดตรงนี้อยู่เหมือนกันครับ ไม่แน่ใจว่าทำไมมัน อ้างอิง Task1 ไม่ได้ กำลังถามเจ้าของ โค้ตอยู่ครับ ส่วนตัวผมยังไม่ได้เล่น esp32 เต็มตัวนะครับ เพราะ esp32 กับ esp8266 มันจุดที่เขียนแตกต่างกันอยู่

1 Likes

ครับผม :smiley:

ได้หรือยังครับ ช่วงนี้ผมย้ายมาเล่น esp32 ผมผ่านจุดนี้มาแล้ว 5555(พึ่งจะผ่าน)

2 Likes

@Remy_Martin สวัสดีครับ ขอสอบถามหน่อยครับ ในคำถามล่าสุดในกระทู้นี้ เรื่อง task ที่อ้างอิงไม่ได้ แก้ไขยังไงครับ

ตั้งกระทู้ใหม่เลยครับ แล้วเอาข้อมูลมาคุยกัน

ขอตอบเพื่อปิด job กระทู้นี้เลยนะครับ code ที่ขาดหายไปคือตรงนี้ครับ

void Task1(void *pvParameter){
   for (;;) // A Task shall never return or exit.
  {

  }
}

ข้างในจะเป็น โปรแกรมในส่วนที่เราต้องการให้ทำงาน ในที่นี้คือว่าง ไม่มีการกำหนดการทำงานใดๆ

1 Likes