ลอง Code ชุดนี้ดูครับ
TaskHandle_t Task1;
TaskHandle_t Task2;
TaskHandle_t Task3;
TaskHandle_t Task4;
TaskHandle_t Task5;
TaskHandle_t Task6;
TaskHandle_t Task7;
TaskHandle_t Task8;
TaskHandle_t Task9;
TaskHandle_t Task10;
TaskHandle_t Task11;
TaskHandle_t Task12;
void setup() {
Serial.begin(115200);
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
0); /* pin task to core 1 */
delay(500);
//create a task that will be executed in the Task3code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task3code, /* Task function. */
"Task3", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task3, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task4code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task4code, /* Task function. */
"Task4", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task4, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task5code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task5code, /* Task function. */
"Task5", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task5, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task6code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task6code, /* Task function. */
"Task6", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task6, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task7code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task7code, /* Task function. */
"Task7", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task7, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task8code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task8code, /* Task function. */
"Task8", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task8, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task9code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task9code, /* Task function. */
"Task9", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task9, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task10code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task10code, /* Task function. */
"Task10", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task10, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task11code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task11code, /* Task function. */
"Task11", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task11, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task12code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task12code, /* Task function. */
"Task12", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task12, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
}
//Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters ) {
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(1000);
}
}
//Task2code: blinks an LED every 700 ms
void Task2code( void * pvParameters ) {
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(700);
}
}
//Task3code: blinks an LED every 2000 ms
void Task3code( void * pvParameters ) {
Serial.print("Task3 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task4code: blinks an LED every 2000 ms
void Task4code( void * pvParameters ) {
Serial.print("Task4 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task5code: blinks an LED every 2000 ms
void Task5code( void * pvParameters ) {
Serial.print("Task5 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task6code: blinks an LED every 2000 ms
void Task6code( void * pvParameters ) {
Serial.print("Task6 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task7code: blinks an LED every 2000 ms
void Task7code( void * pvParameters ) {
Serial.print("Task7 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task8code: blinks an LED every 2000 ms
void Task8code( void * pvParameters ) {
Serial.print("Task8 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task9code: blinks an LED every 2000 ms
void Task9code( void * pvParameters ) {
Serial.print("Task9 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task10code: blinks an LED every 2000 ms
void Task10code( void * pvParameters ) {
Serial.print("Task10 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task11code: blinks an LED every 2000 ms
void Task11code( void * pvParameters ) {
Serial.print("Task11 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task12code: blinks an LED every 2000 ms
void Task12code( void * pvParameters ) {
Serial.print("Task12 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
void loop() {
}
อันนี้ตัวเต็มที่ผมทดสอบอยู่ AP_WiFi_Manager_Multicore 15_01_2563
หมายเหตุให้ดูเป็นตัวอย่างในเรื่องของตำแหน่งการวางครับ
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <SPIFFS.h>//เพิ่ม
#include <WiFi.h> //https://github.com/esp8266/Arduino
#include <WiFiClient.h>
//needed for library
#include <DNSServer.h>
#include <WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //Ver 5.13.4 //https://github.com/bblanchon/ArduinoJson
//---------------------------------- กำหนดหมายเลขของขาของ Node MCU ESP32 --------------------------------------------//
const int Ledblynk = 2; // ใช้ไฟ LED สีฟ้า ของบอร์ด MCU ESP32 ให้มีสัญญาณไฟกระพริบ ตาม Code ที่เขียน
const int AP_Config = 23; // ใช้เป็นปุ่มกด เพื่อเข้า AP Config ได้ตามความต้องการของผู้ใช้
//------------------------------------------------------------------------------------------------------------------------//
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
//------------------------------------------------------------------------------------------------------------------------//
//-------------สำหรับ Server local ที่แจกให้ เพิ่ม **** แค่ 2 บรรทัดนี้-------------- (ถ้าเป็น Blynk Server ปกติไม่ต้องใส่) ----------//
//char server[] = "oasiskit.com";
//int port = 8080;
//------------------------------------------------------------------------------------------------------------------------//
TaskHandle_t Task1;
TaskHandle_t Task2;
TaskHandle_t Task3;
TaskHandle_t Task4;
TaskHandle_t Task5;
TaskHandle_t Task6;
TaskHandle_t Task7;
TaskHandle_t Task8;
TaskHandle_t Task9;
TaskHandle_t Task10;
TaskHandle_t Task11;
TaskHandle_t Task12;
//------------------------------------------------------------------------------------------------------------------------//
//********************************************* void setup **************************************************//
//------------------------------------------------------------------------------------------------------------------------//
void setup() {
//-------IO NODE MCU Esp32-------//
pinMode(Ledblynk, OUTPUT); //กำหนดโหมดใช้งานให้กับขา Ledblynk เป็นขา สัญญาณไฟ ในสภาวะต่างๆ
pinMode(AP_Config, INPUT_PULLUP);//กำหนดโหมดใช้งานให้กับขา AP_Config เป็นขา กดปุ่ม ค้าง เพื่อตั้งค่า AP config
// ให้ LED ทั้งหมดดับก่อน
digitalWrite(Ledblynk, LOW);//ให้หลอด LED สีฟ้าดับก่อน
//-------------------------------//
Serial.begin(115200);
//-------------------------------//
//************************* การ อ่าน เขียนค่า WiFi + Password ]ลงใน Node MCU ESP32 ************//
//read configuration from FS json
Serial.println("mounting FS...");//แสดงข้อความใน Serial Monitor
if (SPIFFS.begin(true)) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
} else {
Serial.println("failed to load json config");//แสดงข้อความใน Serial Monitor
}
}
}
} else {
Serial.println("failed to mount FS");//แสดงข้อความใน Serial Monitor
}
//end read
//************************* จบการ อ่าน เขียนค่า WiFi + Password ]ลงใน Node MCU ESP32 **********//
//************************** AP AUTO CONNECT ********************************************//
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
for (int i = 5; i > -1; i--) { // นับเวลาถอยหลัง 5 วินาทีก่อนกดปุ่ม AP Config
digitalWrite(Ledblynk, HIGH);
delay(500);
digitalWrite(Ledblynk, LOW);
delay(500);
Serial.print (String(i) + " ");//แสดงข้อความใน Serial Monitor
}
if (digitalRead(AP_Config) == LOW) {
digitalWrite(Ledblynk, HIGH);
Serial.println("Button Pressed");//แสดงข้อความใน Serial Monitor
// wifiManager.resetSettings();//ให้ล้างค่า SSID และ Password ที่เคยบันทึกไว้
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); //load the flash-saved configs
esp_wifi_init(&cfg); //initiate and allocate wifi resources (does not matter if connection fails)
delay(2000); //wait a bit
if (esp_wifi_restore() != ESP_OK)
{
Serial.println("WiFi is not initialized by esp_wifi_init ");
} else {
Serial.println("WiFi Configurations Cleared!");
}
}
if (!wifiManager.autoConnect("PUYIOT ESP32 AP CONFIG")) {
Serial.println("failed to connect and hit timeout");//แสดงข้อความใน Serial Monitor
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();//แก้ เดิม ESP.reset(); ใน Esp8266
delay(5000);
}
Serial.println("Connected.......OK!)");//แสดงข้อความใน Serial Monitor
//strcpy(blynk_token, custom_blynk_token.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");//แสดงข้อความใน Serial Monitor
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
//************************** จบ AP AUTO CONNECT *****************************************//
Serial.println("local ip"); //แสดงข้อความใน Serial Monitor
delay(100);
Serial.println(WiFi.localIP());//แสดงข้อความใน Serial Monitor
Serial.println("gateway");
delay(100);
Serial.println(WiFi.gatewayIP());
Serial.println("subnetMask");
delay(100);
Serial.println(WiFi.subnetMask());
Serial.println("SSID");
delay(100);
Serial.println(WiFi.SSID());
Serial.println("Password");
delay(100);
Serial.println(WiFi.psk());
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
0); /* pin task to core 1 */
delay(500);
//create a task that will be executed in the Task3code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task3code, /* Task function. */
"Task3", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task3, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task4code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task4code, /* Task function. */
"Task4", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task4, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task5code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task5code, /* Task function. */
"Task5", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task5, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task6code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task6code, /* Task function. */
"Task6", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task6, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task7code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task7code, /* Task function. */
"Task7", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task7, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task8code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task8code, /* Task function. */
"Task8", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task8, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task9code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task9code, /* Task function. */
"Task9", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task9, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task10code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task10code, /* Task function. */
"Task10", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task10, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task11code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task11code, /* Task function. */
"Task11", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task11, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task12code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task12code, /* Task function. */
"Task12", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task12, /* Task handle to keep track of created task */
1); /* pin task to core 0 */
delay(500);
}
//------------------------------------------------------------------------------------------------------------------------//
//********************************************* จบ void setup **************************************************//
//------------------------------------------------------------------------------------------------------------------------//
//Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters ) {
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(1000);
}
}
//Task2code: blinks an LED every 700 ms
void Task2code( void * pvParameters ) {
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(700);
}
}
//Task3code: blinks an LED every 2000 ms
void Task3code( void * pvParameters ) {
Serial.print("Task3 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task4code: blinks an LED every 2000 ms
void Task4code( void * pvParameters ) {
Serial.print("Task4 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task5code: blinks an LED every 2000 ms
void Task5code( void * pvParameters ) {
Serial.print("Task5 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task6code: blinks an LED every 2000 ms
void Task6code( void * pvParameters ) {
Serial.print("Task6 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task7code: blinks an LED every 2000 ms
void Task7code( void * pvParameters ) {
Serial.print("Task7 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task8code: blinks an LED every 2000 ms
void Task8code( void * pvParameters ) {
Serial.print("Task8 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task9code: blinks an LED every 2000 ms
void Task9code( void * pvParameters ) {
Serial.print("Task9 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task10code: blinks an LED every 2000 ms
void Task10code( void * pvParameters ) {
Serial.print("Task10 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task11code: blinks an LED every 2000 ms
void Task11code( void * pvParameters ) {
Serial.print("Task11 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//Task12code: blinks an LED every 2000 ms
void Task12code( void * pvParameters ) {
Serial.print("Task12 running on core ");
Serial.println(xPortGetCoreID());
for (;;) {
delay(2000);
}
}
//------------------------------------------------------------------------------------------------------------------------//
//********************************************* void Loop ***************************************************//
//------------------------------------------------------------------------------------------------------------------------//
void loop() {
}
//------------------------------------------------------------------------------------------------------------------------//
//********************************************* จบ void Loop **************************************************//
//------------------------------------------------------------------------------------------------------------------------//