PlatformIOでESP8266を開発する

Library Manager


Arduino-IDEにもLibrary Managerが有りますが、Platformioにも(メチャクチャ強力な)Library Managerが有ります。
Arduino-IDEのLibrary Managerは、PlatformioのLibrary Managerをベースにして作られています。

こちらのページでライブラリを検索することができますが、 pioコマンドでも検索することができます。
コマンドの使い方はこちらの ページのDemoビデを見れば大体分かります。

最初にshow または searchオプションを使ってライブラリを探します。
例えばキーワードにOneWireを指定した場合、以下の様に表示されます。
$ pio lib show OneWire
Looking for OneWire library in registry
Conflict: More than one library has been found by request {"requirements": null, "name": "OneWire"}:
OneWire
=======
#ID: 1
Control 1-Wire protocol (DS18S20, DS18B20, DS2408 and etc)

Keywords: onewire, 1-wire, bus, sensor, temperature, ibutton
Compatible frameworks: Arduino
Compatible platforms:
Authors: Paul Stoffregen, Jim Studt, Tom Pollard, Derek Yerger, Josh Larios, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Ken Butcher, Mark Tillotson, Bertrik Sikken, Scott Roberts

OneWire
=======
#ID: 2604
OneWire Library lets you access 1-wire devices made by Maxim/Dallas, such as temperature sensors

Keywords: 1-wire, ds18b20, one, one wire, onewire
Compatible frameworks: mbed
Compatible platforms: Atmel SAM, Freescale Kinetis, Maxim Integrated MAX32, Nordic nRF51, Nordic nRF52, NXP LPC, Silicon Labs EFM32, ST STM32, Teensy, WIZNet W7500
Authors: Ivan Kravets



searchオプションを使うと、以下の様に表示されます。
$ pio lib search OneWire
Found 21 libraries:

OneWire
=======
#ID: 1
Control 1-Wire protocol (DS18S20, DS18B20, DS2408 and etc)

Keywords: onewire, 1-wire, bus, sensor, temperature, ibutton
Compatible frameworks: Arduino
Compatible platforms:
Authors: Paul Stoffregen, Jim Studt, Tom Pollard, Derek Yerger, Josh Larios, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Ken Butcher, Mark Tillotson, Bertrik Sikken, Scott Roberts

DallasTemperature
=================
#ID: 54
Arduino Library for Dallas Temperature ICs (DS18B20, DS18S20, DS1822, DS1820)

Keywords: onewire, 1-wire, bus, sensor, temperature
Compatible frameworks: Arduino
Compatible platforms: Atmel AVR, Atmel SAM, Espressif 32, Espressif 8266, Intel ARC32, Microchip PIC32, Nordic nRF51, Nordic nRF52, ST STM32, Teensy, TI MSP430
Authors: Miles Burton, Tim Newsome, Guil Barros, Rob Tillaart

showとsearchで何が違うのか、深く追及していません。



ライブラリの詳細はshowオプションで確認することができます。
ライブラリ#1の詳細を見てみました。
サンプルコードの有無などを確認することができます。
$ pio lib show 1
OneWire
=======
#ID: 1
Control 1-Wire protocol (DS18S20, DS18B20, DS2408 and etc)

Version: 2.3.4, released Thu Mar 15 15:24:59 2018
Manifest: https://raw.githubusercontent.com/PaulStoffregen/OneWire/master/library.json
Homepage: https://www.pjrc.com/teensy/td_libs_OneWire.html
Repository: https://github.com/PaulStoffregen/OneWire

Authors
-------
Paul Stoffregen <paul@pjrc.com> http://www.pjrc.com (maintainer)
Tom Pollard <pollard@alum.mit.edu>
Derek Yerger
Jim Studt
Jason Dangel <dangel.jason@gmail.com>
Robin James
Ken Butcher
Guillermo Lovato
Scott Roberts
Bertrik Sikken
Mark Tillotson
Glenn Trewitt
Josh Larios

Keywords
--------
onewire
1-wire
bus
sensor
temperature
ibutton

Compatible frameworks
---------------------
Arduino

Headers
-------
OneWire.h
OneWire_direct_gpio.h
OneWire_direct_regtype.h

Examples
--------
http://dl.platformio.org/libraries/examples/0/1/DS18x20_Temperature.pde
http://dl.platformio.org/libraries/examples/0/1/DS2408_Switch.pde
http://dl.platformio.org/libraries/examples/0/1/DS250x_PROM.pde

Versions
--------
2.3.2, released Tue Sep 27 08:35:29 2016
2.3.4, released Thu Mar 15 15:24:59 2018

Unique Downloads
----------------
Today: 0
Week: 332
Month: 1506



ライブラリのインストールは以下のいずれかで行います。

・プロジェクト固有のライブラリ(project dependent library)としてインストールする
プロジェクトディレクトリ(platformio.iniが有るディレクトリ)に移動し以下のコマンドを実行します。
他のプロジェクトでは使うことができません。
platformio lib install <id>

・全てのプロジェクトで使えるライブラリ(global storage)としてインストールする
以下のコマンドを実行します。ディレクトリはどこでも構いません。
全てのプロジェクトで使るようになります。
platformio lib --global install <id>

今回はglobal storageとして1番のライブラリをインストールしてみます。
$ pio lib --global install 1
Library Storage: /home/pi/.platformio/lib
LibraryManager: Installing id=1
Downloading...
Unpacking  [####################################]  100%
OneWire @ 2.3.4 has been successfully installed!

既にインストールされているライブラリを指定すると、以下の様にalready installedと表示されます。
$ pio lib --global install 1
Library Storage: /home/pi/.platformio/lib
LibraryManager: Installing id=1
OneWire @ 2.3.4 is already installed

ライブラリに複数のバージョンが有るときは、以下のコマンドでインストールバージョンを指定します。

pio lib [--global] install ライブラリ番号 --version "バージョン番号"

ライブラリのアンインストールは以下のコマンドで行います。
$ pio lib -g uninstall 1
Library Storage: /home/pi/.platformio/lib
Uninstalling OneWire @ 2.3.4:   [OK]

インストールされているライブラリの一覧は以下のコマンドで見ることができます。
$ pio lib -g list



このライブラリには以下のサンプルコードが公開されています。
Examples
--------
http://dl.platformio.org/libraries/examples/0/1/DS18x20_Temperature.pde
http://dl.platformio.org/libraries/examples/0/1/DS2408_Switch.pde
http://dl.platformio.org/libraries/examples/0/1/DS250x_PROM.pde

そこで、公開されているサンプルコードをそのまま使ってみます。
新しいプロジェクトを作成し、wgetでサンプルコードを取り込みます。
$ mkdir ds18x20

$ cd ds18x20/

$ pio init -b nodemcuv2

$ wget -O src/test.cpp "http://dl.platformio.org/libraries/examples/0/1/DS18x20_Temperature.pde"
--2019-02-09 10:59:18--  http://dl.platformio.org/libraries/examples/0/1/DS18x20_Temperature.pde
Resolving dl.platformio.org (dl.platformio.org)... 193.222.52.25
Connecting to dl.platformio.org (dl.platformio.org)|193.222.52.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3040 (3.0K) [application/octet-stream]
Saving to: ‘src/test.cpp’

src/test.cpp        100%[===================>]   2.97K  --.-KB/s    in 0s

2019-02-09 10:59:18 (92.9 MB/s) - ‘src/test.cpp’ saved [3040/3040]

$ pio run

srcディレクトリにtest.cppがダウンロードされるので、pio run 一発でコンパイルが通ります。

続く...