LoginSignup
3
5

More than 5 years have passed since last update.

ESP-IDFでArduino用のライブラリ関数を使用するためにやったこと

Last updated at Posted at 2019-04-14

はじめに

ESP-IDFのESP32環境でArduinoの関数を使いたいと思ってその環境を構築しました。
そのための情報が散在していて少し手間取ったため、本記事を作成することにしました。

※Serial.println()で文字列を表示するサンプルプログラムを動作させるところまでの手順です

使用したもの

手順

※ESP-IDFの開発環境を構築していることを前提としています。(「ここ」の手順2の1~5を行った状態に該当します)

1. 以下のコマンドを上から順に実行する 

$ cd ~/esp
$ cp -r $IDF_PATH/examples/get-started/hello_world .
$ cd hello_world
$ mkdir -p components
$ cd components
$ git clone https://github.com/espressif/arduino-esp32.git arduino
$ cd arduino
$ git submodule update --init --recursive
$ cd ~/esp/hello_world
$ make menuconfig

2. menuconfigの画面にて、設定値を以下の通りに変更してSaveしてExitする

・Arduino Configuration -> 
  [*] Autostart Arduino setup and loop on boot
・Compiler options -> 
  [*] Enable C++ exceptions
・Serial flasher config -> Flash size ->
  [*] 4 MB
・Component Config -> mbedTLS -> TLS Key Exchange Methods ->
  [*] Enable pre-shared-key ciphersuits
    [*] Enable PSK based ciphersuite modes

3. hello_world/main下の「hello_world_main.c」を削除し、代わりに以下の内容で「main.cpp」を作成する

#include "Arduino.h"
void setup(){
  Serial.begin(115200);
}

void loop(){
  Serial.println("loop");
  delay(1000);
}

4. 実機にサンプルプログラムを書き込んで動作をモニターする

$ make
$ make flash
$ make monitor

画像:モニターの様子(「loop」が1秒間隔で表示される)
実行の様子.png

終わりに

今後はAPモードでサーバーとして色々させたいので、Arduinoの色々な関数を使用する中で問題が出てくるかもしれません。(その時は以下の「おまけ」に追記して行こうかな)

それにしても、menuconfigで「mbedTLS」の設定が出てきたときには「mbed!?」と思いましたが、組込でTLSを実装する際にはよく使われているのかもしれません。

また、git cloneでarduinoの環境を持ってきているので、それが変わってしまったらmenuconfigの設定を変えなければいけない恐れがあります。自身のリポジトリでgit submoduleでバージョンを紐づけておいた方が良いかもしれません。(参考URL1参考URL2
追記しました(2019/04/23)

見ていただいてありがとうございました。
тнайк чoμ_〆(・ω・。)

おまけ

ウオッチドッグタイマのリセットについて

本環境でWi-Fiのアプリケーションを作っている中で、ウオッチドッグタイマ(システムの監視機能)によるシステムリセットが発生することがありました。

ウオッチドッグタイマによるリセット時のメッセージ例:

Task watchdog got triggered. The following tasks did not reset the watchdog in time:
- IDLE (CPU 0)
Tasks currently running:
...

そこで、loop関数に以下のようにvTaskDelayを入れたところ、ウオッチドッグタイマをクリアする余地が入って現象を解消することができました。

void loop()
{
  WiFiClient client = server.available();
  if (client.connected()) {
   :
    client.stop();
  }
  vTaskDelay(1);
}

※ちなみにserverはWiFiServerクラスのオブジェクトです。

ESP32プロジェクトにArduinoライブラリのプロジェクトを紐づける

gitのsubmoduleを使用して、自分のESP32(ESP-IDF)プロジェクトのリポジトリにArduinoライブラリのプロジェクト(外部リポジトリ)を紐づけます。

動作確認済みのバージョンのArduinoライブラリと自分のプロジェクトを紐づけることによって、そのArduinoライブラリが更新されて動作しなくなる、という心配を軽減することができます。(cloneする際に紐づけたバージョンが使用されるため)

使用したもの:
PCのOS:Windows7 Pro 64bit
ツール1:Git for Windows 2.14.1
ツール2:source tree 1.9.13.7

前提として、上記のhello_worldプロジェクトで扱ったような以下の階層でリポジトリを既に作成しているとします。ここのcomponentsにArduinoライブラリのプロジェクト(外部リポジトリ)を紐づけます。

hello_world
┣ build
┣ components
┗ main

source tree単体でもsubmoduleのメニューはありますが、エラーが出て上手くいかないのでコマンドでgitを使います。componentsフォルダに移動して以下のコマンドを実行します。

C:\WorkSpace\esp32examples\hello_world\components> git submodule add https://github.com/espressif/arduino-esp32.git arduino

数分待つと完了します(以下のコマンド実行結果参照)

C:\WorkSpace\esp32examples\hello_world\components>git submodule add https://github.com/espressif/arduino-esp32.git arduino
Cloning into 'C:/WorkSpace/esp32examples/hello_world/components/arduino'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 12465 (delta 0), reused 1 (delta 0), pack-reused 12461
Receiving objects: 100% (12465/12465), 186.76 MiB | 664.00 KiB/s, done.
Resolving deltas: 100% (7306/7306), done.
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory.

そのリポジトリをsource treeで開くと以下のように認識されていますので、コミット&プッシュします。

図:source tree画面
sourcetree3.png

そうするとgithub上に登録されています。

図:github画面
github.png

この「@f8eebb5」というは、Arduinoライブラリのプロジェクト(外部リポジトリ)の コミット f8eebb5 を外部モジュール(submodule)として登録していることを指しています。

そして、上記でプッシュしたリポジトリをクローンする際は以下のようにコマンドを実行します。

git clone --recursive https://github.com/xxx/esp32examples.git .

ESP32 WiFiClientSecure ライブラリのハングアップ問題

「ESP32 WiFiClientSecure ライブラリのハングアップ問題」というものがあるようで、その解決方法が以下のサイトに記載されていました。私がいま使っている中では影響はありませんが、参考まで。

リンク:Arduino – ESP32 WiFiClientSecure ライブラリのハングアップ問題がついに解決!

更新履歴

  • 2019-04-14:新規作成
  • 2019-04-20:「ウオッチドッグタイマのリセットについて」を追加
  • 2019-04-23:「ESP32プロジェクトにArduinoライブラリのプロジェクトを紐づける」を追加
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5