2021年9月29日水曜日

ESP32 超入門

ESP32 超入門

概要

ESP32 は Wifi と Bluetooth を備えた Arduino 互換のマイコンボードです
Amazon で 1,000 円ほどで購入できます

今回は開発のセットアップ方法とサンプルのスケッチを書き込んで動作を確認してみたいと思います

環境

  • macOS 11.6
  • Arduino IDE 1.8.16
  • ESP-WROOM-32

開発ボードの追加

Arduino -> Preferences から追加します

ツール -> ボード -> ボードマネージャ で「esp32」と検索すると ESP32 用のボードが出てくるようになるのでインストールします

ボードのインストールができたら一度 Arduino IDE を再起動しましょう

ボードの変更

ツール -> ボード -> ESP32 Arduino -> ESP32 Dev Module

を選択します
ここは各自の ESP32 の開発ボードのタイプに合わせて変更してください
冒頭で紹介した ESP32 と同じ商品の場合は「ESP32 Dev Module」で OK です

サンプルスケッチの書き込み

とりあえず BLE デバイスとして動作するスケッチを書き込んでみます

#include "SimpleBLE.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

SimpleBLE ble;

void onButton(){
    String out = "BLE32 name: ";
    out += String(millis() / 1000);
    Serial.println(out);
    ble.begin(out);
}

void setup() {
    Serial.begin(115200);
    Serial.setDebugOutput(true);
    pinMode(0, INPUT_PULLUP);
    Serial.print("ESP32 SDK: ");
    Serial.println(ESP.getSdkVersion());
    ble.begin("ESP32 SimpleBLE");
    Serial.println("Press the button to change the device's name");
}

void loop() {
    static uint8_t lastPinState = 1;
    uint8_t pinState = digitalRead(0);
    if(!pinState && lastPinState){
        onButton();
    }
    lastPinState = pinState;
    while(Serial.available()) Serial.write(Serial.read());
}

書き込み時のオプションは以下の通りです
基本的にはデフォルトのままでシリアルポートだけ接続した ESP32 を選択するようにしてください

書き込み時のログは以下の通りです

esptool.py v3.1
Serial port /dev/cu.usbserial-0001
Connecting........_
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: ec:94:cb:6e:79:d0
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00001000 to 0x00005fff...
Flash will be erased from 0x00010000 to 0x000ecfff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.2 seconds (effective 341.6 kbit/s)...
Hash of data verified.
Compressed 17984 bytes to 12318...
Writing at 0x00001000... (100 %)
Wrote 17984 bytes (12318 compressed) at 0x00001000 in 0.5 seconds (effective 268.9 kbit/s)...
Hash of data verified.
Compressed 903840 bytes to 600114...
Writing at 0x00010000... (2 %)
Writing at 0x0001abc2... (5 %)
Writing at 0x000264bd... (8 %)
Writing at 0x0002c108... (10 %)
Writing at 0x0003199d... (13 %)
Writing at 0x00037107... (16 %)
Writing at 0x0003c589... (18 %)
Writing at 0x00041e50... (21 %)
Writing at 0x000475c0... (24 %)
Writing at 0x0004cb62... (27 %)
Writing at 0x00051fac... (29 %)
Writing at 0x00057404... (32 %)
Writing at 0x0005d6c2... (35 %)
Writing at 0x00062e13... (37 %)
Writing at 0x000688cc... (40 %)
Writing at 0x0006e440... (43 %)
Writing at 0x00073bbb... (45 %)
Writing at 0x0007960a... (48 %)
Writing at 0x0007f3e3... (51 %)
Writing at 0x00084cbd... (54 %)
Writing at 0x0008a4df... (56 %)
Writing at 0x0008fc66... (59 %)
Writing at 0x00095411... (62 %)
Writing at 0x0009b008... (64 %)
Writing at 0x000a02e4... (67 %)
Writing at 0x000a5ab2... (70 %)
Writing at 0x000ab2e0... (72 %)
Writing at 0x000b1079... (75 %)
Writing at 0x000b73ff... (78 %)
Writing at 0x000bd1dc... (81 %)
Writing at 0x000c2a84... (83 %)
Writing at 0x000c8d11... (86 %)
Writing at 0x000d11a2... (89 %)
Writing at 0x000d7d26... (91 %)
Writing at 0x000ddbc9... (94 %)
Writing at 0x000e3887... (97 %)
Writing at 0x000e8ed4... (100 %)
Wrote 903840 bytes (600114 compressed) at 0x00010000 in 10.5 seconds (effective 688.2 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.1 seconds (effective 300.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

動作確認

iOS アプリの LightBlue を使って接続確認します
SimpleBLE.h を使っている場合はアドバタイズ名は「SimpleBLE」になります
PIN コードなどはないのですぐに接続することができると思います

シリアルコンソールを開き速度を「115200bps」にセットします
そして ESP32 上に実装されているブートボタンを押すとアドバタイズ名が変更されるのが確認できると思います

ちなみにサービスは実装されていないのでデバイスを選択しても接続することはできません
サービスの実装方法などもそのうち紹介できればなと思っています

最後に

ESP32 の開発環境の構築とスケッチの書き込み方法を紹介しました

ヘッダファイルが見つからないなどのエラーが出る場合はちゃんと開発ボードの追加ができているか確認しましょう
また選択している開発ボードが間違っていないかも確認しましょう

参考サイト

0 件のコメント:

コメントを投稿