Sponsor Link
環境&対象
- macOS Big Sur 11.1
- Visual Stduio Code 1.52.1
- PlatformIO Core:5.0.3 Home:3.3.1
- NodeMCU 1.0
OTA で ESP8266 のアップデート
Web 検索で うまく見つからなかったので改めて書いてみました。
環境セットアップ
Visual Studio Code 上に PlatformIO を セットアップしていることを想定してます。
ESP32 向けですが、以下の記事にまとめてます。
[IoT] VisualStudio + PlatformIO + ESP32 での環境構築
PlatformIO で プロジェクト作成
PlatformIO の Home から、”+ New Project” を押下し、Name に適当な名前を設定後、Board で “NodeMCU 1.0(ESP-12E Module)” を選択します。
Framework は Arduino が自動で選択されるのでそのままにして、”Finish” を押下します。
環境の動作確認:L チカ動作
まずは、環境がセットアップできていることを確認します。
以下のコードを書き込めることを確認してみます。main.cpp にまるっとコピーして、書き込んでみます。
#include <Arduino.h>
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(1000); // Wait for two seconds (to demonstrate the active low LED)
}
いわゆる Lチカ です。
このコードが書き込めて、ボード上の青い LED が1秒ごとに点滅すれば、環境セットアップができていることがわかります。
OTA コードの書き込み
Arduino 環境では、[ファイル]-[スケッチ例]-[ArduinoOTA]-[BasicOTA] とすることで、サンプルを参照することができます。
まるっと PlatformIO の main.c にコピーしてきます。 自分のネットワークに接続できるように STASSID と STAPSK を適宜変更してください。
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#ifndef STASSID
#define STASSID "自分の環境のSSID"
#define STAPSK "WIFIパスワード"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
void setup() {
Serial.begin(115200);
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();
}
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
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);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.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();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// setup ping in/out
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
ArduinoOTA.handle();
// LED On/Off
digitalWrite(LED_BUILTIN, HIGH) ;
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
モニタースピード設定
そのままでは、PlatformIO 上で モニターできません。
ログにも表示されてますが、PlatformIO では、デフォルト 9600 らしいので、これをコード上に指定している 115200 にする必要があります。
プロジェクトに含まれる platform.ini ファイルで設定を追加・変更します。
以下のように行を追加します。
; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html [env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino monitor_speed = 115200 ; 追加した行
この状態でも、OTA による更新ができるのですが、ESP8266 の起動時に設定された IP を記録しておかないといけません。
少し不便なので、IP 固定で起動するように変更します。
IP アドレス指定
Main.cpp のコードを 以下のように修正することで、IP アドレス指定で起動することができます。
指定したい IP アドレスや、Gateway の IP アドレス等 は、環境に合わせて設定してください。
// .. snip ..
const char* password = STAPSK;
IPAddress ip(192,168,1,100); // 設定したい IP アドレス
IPAddress gw(192,168,1,1); // gateway IP アドレス
IPAddress subnet(255,255,255,0); // subnet
IPAddress DNS(192,168,1,1); // DNS IP アドレス
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.config(ip, gw, subnet); // IPアドレス等の設定
WiFi.begin(ssid, password);
// .. snip ..
上記のコードをコンパイルし、upload して以下のような表示が出れば成功です。
Ready IP address: 192.168.1.100 // 設定した IP アドレス
この IP は固定のため、これ以降は この IP アドレスの機器をアップデートするように 設定していきます。
OTA upload 設定
Platform.ini のファイルに以下を追加することで、シリアルポート経由のアップロードではなく、IP 指定の OTA アップデートとなります。
[env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino monitor_speed = 115200 upload_protocol = espota ; OTA 指定 upload_port = 192.168.1.100 ; OTA 対象とする IP 指定
OTA のために、残しておかなければいけないコード
現時点でのほとんどのコードは、WIFI 設定や OTA 設定のためのコードなので 自分のプログラムを追加していく際に消してはいけません。
言い換えると、消してしまっても構わないのは、Lチカのためのコードだけです。
まとめ:ESP8266 を PlatformIO 環境で OTA するまでの手順
- ESP8266 向けの環境を PlatformIO 上で構築する
- Arduino 環境から、OTA コードを持ってくる
- シリアルモニタのスピードを設定する
- (必要であれば)IP指定する
- PlatformIO 上で OTA を使った upload を設定する
説明は以上です。
不明な点やおかしな点ありましたら、こちらまで。
Sponsor Link


