« M5Core2 Arduino Install | トップページ | NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2) »

2021年12月23日 (木)

M5StickCPlus Arduino Install

2021/12/22.
初版

M5StickCPlus Arduino Install

M5StickCPlus Arduino Install

概要

platformioベースのArduinoフレームワークを使って
M5StickC Plus」を動かす。
ホスト環境は、ubuntu20.04とする。
なお、platformioがインストール済みのものとする。

linuxのplatformioのインストールについては以下を参照のこと。
arduinoフレームワーク用platformio.ini集

電源操作:
・電源オン:電源ボタン(左側面)を2秒間押す
・電源オフ:電源ボタン(左側面)を6秒間押す

M5SrickCPlus_FactoryTestの書き込み方法

まず、FactoryTestの書き込み方法について説明する。
以下を実行する:

cd pio # platformio環境に入る source pio_env/bin/activate mkdir M5SCP cd M5SCP mkdir FactoryTest cd FactoryTest # ソースのダウンロード git clone https://github.com/m5stack/M5StickC-Plus.git cp -r M5StickC-Plus/examples/FactoryTest src gedit platformio.ini # 次節の内容のplatformio.iniを作成する # ビルド環境のクリーンアップ pio -t clean # M5CoreとPCをUSBで接続する # ビルド&書き込む pio -t upload

以上で書き込みが完了する。

なお、シリアルデバイスは通常「/dev/ttyUSB0」になる。

platformio.ini

M5StickCPlusのスケッチのビルドには
必要なライブラリの追加はあり得るが
基本的には以下のplatformio.iniを使用する。
platformio.ini

[env:M5StickCPlus] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5STICK_CP monitor_speed = 115200 lib_ldf_mode = deep+ lib_deps = m5stack/M5StickCPlus @ ^0.0.5 tinyu-zhao/FFT @ ^0.0.1 m5stack/UNIT_ENV @ ^0.0.2

新たなプロジェクトをビルドしたい場合、 FactoryTestのディレクトリを、 まるごとコピーして、ディレクトリ名と src配下のスケッチを変更すれば良い。

sketch#1(ENVIII_HAT)

別のサンプルとして「M5StickC ENV Hat III (SHT30/QMP6988)」のHATをM5StickCPlusに接続して温度、湿度、気圧をLCDに表示するスケッチを動かす。

src/ENVIII_HAT.ino

/* ******************************************************************************* * Copyright (c) 2021 by M5Stack * Equipped with M5Core sample source code * 配套 M5Core 示例源代码 * Visit the website for more information:https://docs.m5stack.com/en/core/gray * 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray * * describe:ENVIII_SHT30_QMP6988. 环境传感器 * date:2021/8/17 ******************************************************************************* Please connect to Port A(22、21),Read temperature, humidity and atmospheric pressure and display them on the display screen 请连接端口A(22、21),读取温度、湿度和大气压强并在显示屏上显示 */ //#include <M5Stack.h> #include <M5StickCPlus.h> #include "UNIT_ENV.h" SHT3X sht30; QMP6988 qmp6988; float tmp = 0.0; float hum = 0.0; float pressure = 0.0; void setup() { M5.begin(); Wire.begin(0,26); M5.Lcd.setRotation(3); M5.Lcd.fillScreen(BLACK); M5.lcd.setTextSize(2); M5.Lcd.setCursor(0, 0, 1); Wire.begin(0,26); // HAT ////M5.Power.begin(); //Init power 初始化电源模块 qmp6988.init(); ////M5.lcd.println(F("ENV Unit III test")); M5.lcd.println(F("ENV3 test")); } void loop() { pressure = qmp6988.calcPressure(); if(sht30.get()==0){ //Obtain the data of shT30. 获取sht30的数据 tmp = sht30.cTemp; //Store the temperature obtained from shT30. 将sht30获取到的温度存储 hum = sht30.humidity; //Store the humidity obtained from the SHT30. 将sht30获取到的湿度存储 }else{ tmp=0,hum=0; } M5.lcd.fillRect(0,20,100,60,BLACK); //Fill the screen with black (to clear the screen). 将屏幕填充黑色(用来清屏) M5.lcd.setCursor(0,20); /////M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", tmp, hum, pressure); M5.Lcd.printf("Temp:%2.1f\r\nHumi:%2.1f%%\r\n%4.1f hPa\r\n", tmp, hum, pressure/100); delay(2000); }

platformio.iniは上のものが、そのまま使用できる。
(必要なライブラリは登録済み)

sketch#2(b3NTP)

b3NTP.ino

// select board ////#define WIO_TERMINAL ////#define ESP8266 ////#define ESP32 ////#define M5ATOM //------------------ // NTP Client for Wio-Terminal/ESP8266/ESP32 #ifdef M5ATOM #include "M5Atom.h" #define ESP32 #endif #ifdef WIO_TERMAL //#include <AtWiFi.h> #include <rpcWiFi.h> #endif #ifdef ESP8266 #include <ESP8266WiFi.h> #endif #ifdef ESP32 #include <WiFi.h> #endif #include <time.h> #define WIFI_SSID "your_wifi_ssid" #define WIFI_PASSWORD "your_wifi_password" void setup() { Serial.begin(115200); delay(100); Serial.print("\r\n\nReset:\r\n"); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while(WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(500); } Serial.println(); Serial.printf("Connected, IP address: "); Serial.println(WiFi.localIP()); configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); // 2.7.0以降, esp32コンパチ } void loop() { time_t t; struct tm *tm; static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"}; t = time(NULL); tm = localtime(&t); /***** Serial.printf("ESP8266/Arduino ver%s : %04d/%02d/%02d(%s) %02d:%02d:%02d\n", __STR(ARDUINO_ESP8266_GIT_DESC), tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec); ****/ Serial.printf("Arduino NTP: %04d/%02d/%02d(%s) %02d:%02d:%02d\r\n", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec); delay(1000); }

以下はwifi環境に応じて変更すること:

#define WIFI_SSID "your_wifi_ssid" #define WIFI_PASSWORD "your_wifi_password"

本ソースは、WIO_TERMINAL、ESP8266、ESP32、M5ATOMで共通になっており、platformio.iniの内容でターゲットを切り替えることができる。

platformio.iniは、上のものが、そのまま使用できる。

実行時のシリアル出力(/dev/ttyUSB0):

........ Connected, IP address: 192.168.1.15 Arduino NTP: 1970/01/01(Thr) 09:00:04 Arduino NTP: 1970/01/01(Thr) 09:00:05 Arduino NTP: 1970/01/01(Thr) 09:00:06 Arduino NTP: 1970/01/01(Thr) 09:00:07 Arduino NTP: 1970/01/01(Thr) 09:00:08 Arduino NTP: 2021/12/11(Sat) 16:55:59 ← ここでNTPによって時刻が同期している Arduino NTP: 2021/12/11(Sat) 16:56:00 Arduino NTP: 2021/12/11(Sat) 16:56:01

参考情報

M5StickC関連:
M5StickC非公式日本語リファレンス
M5StickCとM5StickC Plusの違い
M5StickCのトラブルシュート 動かないときに試すこと
【WiFi温度計】M5StackとM5Stick Cでコードを共通化する

platformio関連:
arduinoフレームワーク用platformio.ini集
Building Core2 FactoryDemo in PlatformIO
VSCodeとPlatformIOでM5Stack Core2開発
M5Stack Core2とVSCode + PlatformIOとでM5Stackプログラミングを始めてみた。

Arduino-IDE関連:
Arduino IDE environment - M5Paper
Arduino IDEのインストールと設定 (Windows, Mac, Linux対応)

M5Stackファミリ関連:
M5Paper Arduino Install
M5CoreInk Arduino Install
M5Stamp-PICO Arduino Install
M5Stamp-C3 Arduino Install
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(NTP-CLIENT編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)

その他:
スルーホール用テストワイヤ TT-200 (10本入)

以上

|

« M5Core2 Arduino Install | トップページ | NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2) »

PlatformIO」カテゴリの記事

M5StickCPlus」カテゴリの記事

コメント

この記事へのコメントは終了しました。