Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)
2020/12/27+
初版
board3 REST API v2
board3 REST API v2
概要
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)
本記事は「
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(REST-API編)」
の改版にあたり、wio-terminalのファームウェア・バージョンアップに対応している。
wio-terminalのファームウェアのバージョンアップで以下の不具合も解消している。
Wio-Terminalの場合、REST-APIは、起動後の1回しか動作しなかった。(原因不明)
wio-terminalのファームウェアのアップデート方法については以下を参照のこと:
wio-terminalのファームウェア・アップデートについて(v2)(linux版)
デモ・スケッチ
src/REST-API_test.ino
// select board
////#define M5ATOM
////#define WIO_TERMINAL
////#define ESP8266
////#define ESP32
//------------------
// REST-API server for Wio-Terminal/ESP8266/ESP32
/*
* Forked from the following code:
*
* Simple hello world Json REST response
* by Mischianti Renzo <https://www.mischianti.org>
*
* https://www.mischianti.org/
*
*/
#ifdef M5ATOM
#include "M5Atom.h"
#define ESP32
#endif
#ifdef WIO_TERMINAL
//#include <AtWiFi.h>
#include <rpcWiFi.h>
#include <WebServer.h>
#include <WiFiUdp.h>
#include <ArduinoMDNS.h>
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#endif
const char* ssid = "<your-ssid>";
const char* password = "<your-passwd>";
#ifdef WIO_TERMINAL
WiFiUDP udp;
MDNS mdns(udp);
WebServer server(80);
#endif
#ifdef ESP8266
ESP8266WebServer server(80);
#endif
#ifdef ESP32
WebServer server(80);
#endif
// Serving Hello world
void getHelloWord() {
server.send(200, "text/json", "{\"name\": \"Hello world\"}\r\n");
}
void ledOff() {
// do something for LED off
server.send(200, "text/json", "{\"led\": \"OFF\"}\r\n");
}
void ledOn() {
// do something for LED on
server.send(200, "text/json", "{\"led\": \"ON\"}\r\n");
}
// Define routing
void restServerRouting() {
server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"),
F("Welcome to the REST Web Server"));
});
server.on(F("/helloWorld"), HTTP_GET, getHelloWord);
server.on(F("/api/v1/led=0"), HTTP_GET, ledOff);
server.on(F("/api/v1/led=1"), HTTP_GET, ledOn);
}
// Manage not found URL
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void) {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Activate mDNS this is used to be able to connect to the server
// with local DNS hostmane esp8266.local
#ifdef WIO_TERMINAL
mdns.begin(WiFi.localIP(), "wiot");
Serial.println("MDNS responder started(wiot)");
#endif
#ifdef ESP8266
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started(esp8266)");
}
#endif
#ifdef ESP32
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started(esp32)");
}
#endif
// Set server routing
restServerRouting();
// Set not found response
server.onNotFound(handleNotFound);
// Start server
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
#ifdef WIO_TERMINAL
mdns.run(); // This actually runs the mDNS module. YOU HAVE TO CALL THIS PERIODICALLY
#endif
#ifdef ESP8266
MDNS.update();
#endif
#ifdef ESP32
//MDNS.update(); // no need to update on ESP32
#endif
server.handleClient();
}
wio-terminalのファームウェアのバージョン・アップにともない
以下のようにヘッダーが変更になっている:
//#include <AtWiFi.h>
#include <rpcWiFi.h>
以下の#defineでボードを切り換えているがボードの種類を設定すると、 システムで設定されている定義が有効になるので特にソースを変更する必要はない。
#define WIO_TERMINAL
#define ESP8266
#define ESP32
#define M5ATOM
以下については、自分の環境に合わせて変更すること:
const char ssid[] = "yours_ssid";
const char pass[] = "yours_passwd";
書き込み後に「picocom /dev/ttyACM0 -b115200」または「picocom /dev/ttyUSB0 -b115200」で通信ソフトを起動すると以下のような出力が表示される:
$ picocom /dev/ttyACM0 -b115200
# ESP8266の場合
...
Connected to xxxxxxxx
IP address: 192.168.0.5
MDNS responder started(esp8266)
HTTP server started
# M5Atom/ESP32の場合
..
Connected to xxxxxxxx
IP address: 192.168.0.15
MDNS responder started(esp32)
HTTP server started
# Wio-Terminalの場合
..
Connected to xxxxxxxx
IP address: 192.168.0.15
MDNS responder started(wiot)
HTTP server started
curlによるコマンド実行例:
# ESP8266の場合
$ curl 'http://esp8266.local:80/helloWorld'
{"name": "Hello world"}
$ curl 'http://esp8266.local:80/api/v1/led=1'
{"led": "ON"}
$ curl 'http://esp8266.local:80/api/v1/led=0'
{"led": "OFF"}
# ESP32の場合
$ curl 'http://esp32.local:80/helloWorld'
{"name": "Hello world"}
$ curl 'http://esp32.local:80/api/v1/led=0'
{"led": "OFF"}
$ curl 'http://esp32.local:80/api/v1/led=1'
{"led": "ON"}
# wio-terminalの場合
$ curl 'http://wiot.local:80/helloWorld'
{"name": "Hello world"}
$ curl 'http://wiot.local:80/api/v1/led=0'
{"led": "OFF"}
$ curl 'http://wiot.local:80/api/v1/led=1'
{"led": "ON"}
mDNSによる名前解決には時間がかかることがあるので
その場合、IPアドレスでアクセスすること。
また、M5Atomの場合、プログラム起動後、リセットボタンを押す必要があるようだ。
platformio.ini
platformio.iniは、ボードに合わせて以下を使用する:
wio-terminalの場合:
; 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:seeed_wio_terminal]
platform = atmelsam
board = seeed_wio_terminal
framework = arduino
build_flags = -DWIO_TERMINAL
upload_protocol = sam-ba
monitor_speed = 115200
lib_ldf_mode = deep+
lib_deps =
https://github.com/Seeed-Studio/Seeed_Arduino_mbedtls/archive/dev.zip
https://github.com/Seeed-Studio/Seeed_Arduino_rpcUnified/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_rpcBLE/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_rpcWiFi/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_FreeRTOS/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_FS/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_SFUD/archive/master.zip
#
https://github.com/Seeed-Studio/Seeed_Arduino_LCD/archive/master.zip
#
# mDNS lib "ArduinoMDNS"
2848
M5Atomの場合:
; 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:esp32dev]
platform = espressif32
#board = esp32dev
board = m5stick-c
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep+
lib_deps =
# use M5Atom lib
3113
# use "FastLED"
126
ESP32の場合:
; 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:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
build_flags = -DESP32
monitor_speed = 115200
lib_ldf_mode = deep+
ESP8266の場合:
; 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:huzzah]
platform = espressif8266
#board = huzzah
board = esp_wroom_02
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep+
wio-terminalのファームウェアを切り替えたときの注意
古いファームウェアのライブラリを動かした後は、古いライブラリがキャッシュで残っていると ビルド・エラーになるので以下を実行すること:
cd project_directory
rm -r .pio/libdeps/seeed_wio_terminal/*
rm -r .pio/build/seeed_wio_terminal/*
参考情報
以上
| 固定リンク
« Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編) | トップページ | Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編) »
「PlatformIO」カテゴリの記事
- NuEVI/NuRADのビルド(2022.08.18)
- Wio_ExtFlashLoad(WriteSampleMenu.ino)スケッチをplatformioでビルドする(2022.02.03)
- uncannyeyesスケッチをplatformioでビルドする(2022.01.31)
- LovyanGFX-Display ライブラリを使用したスケッチをplatformioでビルドする(2022.01.30)
- Wio-Terminal/M5Core2のWiFiAnallyzer(2022.01.24)
「Arduino」カテゴリの記事
- platformioのために新しいユーザーを設定する(2022.02.06)
- MAKER_PI_RP2040でI2Cを使う(Arduino編)(2022.01.09)
- M5Stamp-PICO Arduino Install(2021.12.12)
- M5Stamp-C3 Arduino Install(2021.12.12)
- Arduino-CLIのインストール(2021.05.19)
「REST-API」カテゴリの記事
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)(2020.12.27)
- Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチでWorld Time APIを使う(WorldTimeAPI編)(2020.08.03)
- Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(HTTP-ACCESS編)(2020.08.03)
- Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(REST-API2編)(2020.07.25)
- Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(REST-API編)(2020.07.24)
「ESP-WROOM-02」カテゴリの記事
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)(2020.12.28)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)(2020.12.27)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)(2020.12.27)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)(2020.12.26)
- XIAOにWiFiモジュールを接続する(STARWARS)(2020.08.15)
「M5Atom」カテゴリの記事
- 「M5Stack用回転角ユニット」を動かす(2022.01.01)
- M5Atomを開発ツールPlatformIOで使う(v2)(M5Atom/Arduino版)(2021.01.10)
- python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(v2,microbit-v2対応)(linux版)(2020.12.31)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)(2020.12.28)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)(2020.12.27)
「ESP32」カテゴリの記事
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)(2020.12.28)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)(2020.12.27)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)(2020.12.27)
- Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)(2020.12.26)
- M5Atomを開発ツールPlatformIOで使う(Windows10版)(2020.10.09)
この記事へのコメントは終了しました。
コメント