# 15.The usage of Wi-Fi OTA(Over-The-Air)

### 1. What is OTA?

การกำหนดค่าของ BiBoard ของเราคือ 16MB Flash และพาร์ติชันเฉพาะมีดังนี้:

![](https://3127300255-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-Mcb-kSbVGJDM--UuUO3%2F-McbAsHwgKp0KZ3ryukX%2F13_01.png?alt=media\&token=e00e9a51-fb94-444b-9a80-efe4413ee94b)

OTA ดำเนินการพื้นที่ข้อมูล OTA เป็นหลัก ได้แก่ พื้นที่ APP1 และ APP2 หลักการคือ:

* BiBoard เรียกใช้เฟิร์มแวร์ด้วยฟังก์ชัน OTA ในขณะนี้ การบูตชี้ไปที่พื้นที่ APP1
* คำสั่ง OTA ถูกส่งไปยัง ESP32 ผ่าน Wi-Fi และไฟล์ไบนารีของโปรแกรมอัปเกรดจะถูกถ่ายโอนไปยังพื้นที่ APP2
* หากการส่ง APP2 เสร็จสิ้นและการตรวจสอบยืนยันสำเร็จ ข้อมูล OTA จะชี้ไปที่พื้นที่ APP2 และครั้งต่อไปที่เริ่มต้นจากพื้นที่เฟิร์มแวร์ที่อัปเดตแล้ว (APP2) ข้อมูล APP1 จะถูกเก็บไว้ ครั้งต่อไป OTA จะเขียนไปยังพื้นที่ APP1 เพื่อเขียนทับเฟิร์มแวร์เก่า
* หากการส่ง APP2 ไม่เสร็จสมบูรณ์เนื่องจากข้อผิดพลาดในการส่งเครือข่าย เนื่องจาก APP2 ไม่ผ่านการตรวจสอบ ข้อมูล OTA จะไม่ชี้ไปที่พื้นที่ APP2 โปรแกรมในพื้นที่ APP1 จะยังคงทำงานหลังจากเริ่มต้นการรีเซ็ต และพื้นที่ APP2 ที่เสียหายจะถูกลบและเขียนทับอย่างสมบูรณ์ระหว่าง OTA ถัดไป

## OTA operation in Arduino program

ในการสาธิต ก่อนอื่นให้กำหนดค่า WiFi และกำหนดค่าโหมด WiFi เป็น STA (สถานี โหมดสถานีฐาน) เปิดใช้งานฟังก์ชัน WiFi และป้อนรหัสผ่านบัญชี "WiFi.begin(ssid, password);"

```cpp
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
```

เมื่อเชื่อมต่อ Wi-Fi สำเร็จ ที่อยู่ IP จะถูกพิมพ์ผ่านพอร์ตอนุกรม หากการเชื่อมต่อผิดพลาด ESP32 จะรีสตาร์ท

![](https://3127300255-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-McbUjRuli8-pPewuOYB%2F-McbUtzzimUgoNQVVRQZ%2F13_02.png?alt=media\&token=a02e6db6-104a-44cd-b120-534638517de4)

ในการสาธิต คุณสามารถกำหนดค่าหมายเลขพอร์ต คีย์ OTA หรือแฮชของคีย์ และพื้นที่และประเภทของ OTA (ระบุโดยค่าเริ่มต้น)

```cpp
  // Port defaults to 3232
  // ArduinoOTA.setPort(3232);

  // Hostname defaults to esp3232-[MAC]
  // ArduinoOTA.setHostname("myesp32");

  // No authentication by default
  // ArduinoOTA.setPassword("admin");

  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
```

ต่อไปนี้เป็นข้อมูลโค้ดบางส่วนที่คล้ายกับฟังก์ชันการโทรกลับ ซึ่งใช้ในการตัดสินสถานะของแต่ละขั้นตอนของ OTA

```cpp
  ArduinoOTA
    .onStart([]() {
      String type;
      if (ArduinoOTA.getCommand() == U_FLASH)
        type = "sketch";
      else // U_SPIFFS
        type = "filesystem";

      // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
      Serial.println("Start updating " + type);
    })
    .onEnd([]() {
      Serial.println("\nEnd");
    })
    .onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    })
    .onError([](ota_error_t error) {
      Serial.printf("Error[%u]: ", error);
      if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
      else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
      else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
      else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
      else if (error == OTA_END_ERROR) Serial.println("End Failed");
    });

  ArduinoOTA.begin();
```

หลังจากกำหนดค่าตามการสาธิต ให้เรียก "ArduinoOTA.handle();" ในฟังก์ชันลูป ฟังก์ชัน analogWrite ต่อไปนี้ใช้เพื่อแยกแยะผลกระทบของการอัพเดตเฟิร์มแวร์ต่างๆ (โดยการเปลี่ยนค่า)

```cpp
void loop() {
  ArduinoOTA.handle();
  analogWrite(2, 127);    // test OTA firmware
}
```

ครั้งแรกที่คุณใช้พอร์ตอนุกรมเพื่อดาวน์โหลด เครื่องมือไพธอน "esptool" จะถูกเรียก คุณสามารถใช้ OTA ได้หลังจากการดาวน์โหลดเสร็จสิ้น ในตัวเลือกพอร์ต คุณจะพบพอร์ตพิเศษตามที่อยู่ IP ซึ่งเป็นที่อยู่ OTA

![](https://3127300255-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-McbYsOdZ9Y-1KD8QnT1%2F-McbZAImt5i9DlvwEWDd%2F13_03.png?alt=media\&token=78ebb5fc-4239-49ea-ae21-cfe799028812)

เลือกที่อยู่นี้ มุมขวาล่างคือที่อยู่ IP ของ ESP32 Dev Module บน BiBoard ของคุณ (192.168.1.178)

![](https://3127300255-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-McbYsOdZ9Y-1KD8QnT1%2F-McbZjg3mcFhhEYNiqVX%2F13_04.png?alt=media\&token=9a0119f6-ca74-4dec-959d-9ef9a414309e)

ในเวลาเดียวกัน คำเตือนจะปรากฏขึ้น: "ไม่รองรับการมอนิเตอร์แบบอนุกรมบนพอร์ตเครือข่าย เช่น 192.168.1.178 สำหรับโมดูล ESP32 Dev ในล่าสุดนี้"

![](https://3127300255-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-McbYsOdZ9Y-1KD8QnT1%2F-Mcb_EJC7UxNLNDYYXYX%2F13_05.png?alt=media\&token=58f62525-09a4-49fa-878e-332428c86630)

ESP32 OTA ของ Arduino เหมาะสำหรับการอัปเดตโปรแกรมเท่านั้น และไม่สามารถทำงานดีบักพอร์ตอนุกรมได้ หากคุณต้องการดีบัก BiBoard โปรดเชื่อมต่ออินเทอร์เฟซ USB-C

ดาวน์โหลดโปรแกรม ดังรูป

<br>

![](https://3127300255-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-Mcb_dpvQVRR-5r0kGbc%2F-Mcba3trjJWMdHDL1DXQ%2F13_06.png?alt=media\&token=7c2773e4-8cf8-4864-9677-6a022d8185ba)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.petoi.com/thai/biboard/demo-applications/15.the-usage-of-wifi-ota.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
