0Pricing
Arduino & IoT Academy · 课时

启用 ArduinoOTA

添加 OTA 支持,并通过 WiFi 上传

启用 ArduinoOTA 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Meet ArduinoOTA

The simplest way to update over WiFi is the ArduinoOTA library. It makes your board appear as a network port inside the Arduino IDE.

Include the Library

OTA rides on WiFi, so you pull in both headers at the top of your sketch before anything else runs.

#include <WiFi.h>
#include <ArduinoOTA.h>

Connect First

OTA can only work once you are online. In setup you join WiFi with your network name and password before starting OTA.

WiFi.begin("MyNetwork", "secret123");
while (WiFi.status() != WL_CONNECTED) { delay(500); }

Start the Service

One call wakes the updater. ArduinoOTA.begin() registers your board on the network so the IDE can find it.

ArduinoOTA.begin();

Give It a Name

Set a clear hostname so your device is easy to spot in the IDE's port list, especially when several boards share one network.

ArduinoOTA.setHostname("living-room-sensor");

Protect It With a Password

Always set an OTA password so a stranger on your WiFi cannot push their own code to your hardware.

ArduinoOTA.setPassword("flashMe!");

Handle OTA in the Loop

The updater is not automatic. You must call ArduinoOTA.handle() on every pass of loop so it can listen for an incoming upload.

void loop() {
  ArduinoOTA.handle();
}

Never Block the Loop

A long delay starves handle() and the upload fails. Keep loop quick so OTA always gets a chance to respond.

Watch the Progress

You can attach callbacks like onProgress to print how much of the new firmware has arrived during an upload.

ArduinoOTA.onProgress([](unsigned int p, unsigned int t) {
  Serial.printf("%u%%\n", (p * 100) / t);
});

Find the Network Port

After uploading once by USB, open the IDE's port menu. Your named board appears under network ports, ready for wireless flashing. 📶

Upload Wirelessly

Pick the network port, hit upload, and enter your password. From now on, new code flows entirely over the air.

Quick Check

One method must run constantly for OTA to work.

Recap

Include ArduinoOTA, connect to WiFi, call begin() with a hostname and password, then run handle() every loop to flash your board wirelessly. ✅

常见问题解答

「启用 ArduinoOTA」课时是免费的吗?

是的 — 「启用 ArduinoOTA」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。

「启用 ArduinoOTA」这节课中我会学到什么?

添加 OTA 支持,并通过 WiFi 上传 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Arduino & IoT Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「启用 ArduinoOTA」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?

能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么 OTA 对现场设备很重要
  2. 启用 ArduinoOTA
  3. 托管网页更新页面
  4. 回滚错误的更新
← 返回 Arduino & IoT Academy