M5CoreInk

2021年12月26日 (日)

M5CoreInk with Env3_Hat

2021/12/28
使ってみるとバッテリが消耗しやすいので
NTP同期がないと不便と感じたので、
NTP同期を追加した。

2021/12/27+
バッテリ状態の表示を追加した。

2021/12/26
初版

M5CoreInk with Env3_Hat

M5CoreInk with Env3_Hat

概要

platformioベースのArduinoフレームワークで、
M5Stack CoreInk 開発キット(1.5インチ Einkディスプレイ)」に「M5StickC ENV Hat III (SHT30/QMP6988)」を接続して、温度、湿度、気圧、時刻、バッテリ状態を表示する。
ホスト環境は、ubuntu20.04とする。
なお、platformioがインストール済みのものとする。

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

ソース(DISP3_ENV3_HAT)

src/main.ino

//#include <M5Stack.h> #include "M5CoreInk.h" #include "Wire.h" #include "UNIT_ENV.h" #include "esp_adc_cal.h" #define NTP_SYNC #ifdef NTP_SYNC #include <WiFi.h> const char* ssid = "your_ssid"; const char* password = "your_password"; const char* ntpServer = "ntp.jst.mfeed.ad.jp"; #endif #define DISPTIME #define DISPBAT #include "time.h" Ink_Sprite InkPageSprite(&M5.M5Ink); SHT3X sht30; QMP6988 qmp6988; float tmp = 0.0; float hum = 0.0; float pressure = 0.0; char titleStrbuff[64]; char tempStrbuff[64]; char humStrbuff[64]; char pressStrbuff[64]; #ifdef DISPTIME char dateStrbuff[64]; //char weekStrbuff[64]; char timeStrbuff[64]; static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"}; RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; #endif #ifdef DISPBAT char batteryStrbuff[64]; int getBatCapacity() { // 4.02 = 100%, 3.65 = 0% const float maxVoltage = 4.02; const float minVoltage = 3.65; int cap = (int) (100.0 * (getBatVoltage() - minVoltage) / (maxVoltage - minVoltage)); if (cap > 100) { cap = 100; } if (cap < 0) { cap = 0; } return cap; } float getBatVoltage() { analogSetPinAttenuation(35, ADC_11db); esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t*) calloc(1, sizeof(esp_adc_cal_characteristics_t)); esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 3600, adc_chars); uint16_t ADCValue = analogRead(35); uint32_t BatVolmV = esp_adc_cal_raw_to_voltage(ADCValue, adc_chars); float BatVol = float(BatVolmV) * 25.1 / 5.1 / 1000; free(adc_chars); return BatVol; } #endif void ButtonTest(char* str) { InkPageSprite.clear(); InkPageSprite.drawString(35,59,str); InkPageSprite.pushSprite(); // delay(2000); } void ENV_HAT(void) { InkPageSprite.clear(); if (sht30.get() == 0) { tmp = sht30.cTemp; hum = sht30.humidity; pressure = qmp6988.calcPressure() / 100; } else { tmp = 0.0; hum = 0.0; pressure = 0.0; } Serial.printf("Temperature: %2.2f℃ Humidity: %0.2f%% Pressure: %4.2f hPa\r\n", sht30.cTemp, sht30.humidity, qmp6988.calcPressure() / 100); sprintf(titleStrbuff,"ENV III HAT sensing..."); sprintf(tempStrbuff,"temp: %2.1f *C", tmp); sprintf(humStrbuff,"humi: %0.1f %% ", hum); sprintf(pressStrbuff,"press: %0.2f hPa", pressure); InkPageSprite.drawString(10,5,titleStrbuff); InkPageSprite.drawString(10,20,tempStrbuff); InkPageSprite.drawString(10,35,humStrbuff); InkPageSprite.drawString(10,50,pressStrbuff); #ifdef DISPTIME #ifdef M5CI M5.rtc.GetTime(&RTC_TimeStruct); M5.rtc.GetData(&RTC_DateStruct); #endif #ifdef M5STICK_CP M5.Rtc.GetTime(&RTC_TimeStruct); M5.Rtc.GetData(&RTC_DateStruct); #endif #ifdef M5C2 M5.Rtc.GetTime(&RTC_TimeStruct); M5.Rtc.GetDate(&RTC_DateStruct); #endif Serial.printf("Date: %04d-%02d-%02d\r\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date); Serial.printf("Week: %s\r\n", wd[RTC_DateStruct.WeekDay]); Serial.printf("Time: %02d:%02d:%02d\r\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds); sprintf(dateStrbuff,"%04d-%02d-%02d(%s)", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, wd[RTC_DateStruct.WeekDay]); sprintf(timeStrbuff,"%02d:%02d:%02d", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds); InkPageSprite.drawString(10,80,dateStrbuff); InkPageSprite.drawString(10,95,timeStrbuff); #endif #ifdef DISPBAT Serial.printf("BAT:%2.2fV(%02d%%)\r\n",getBatVoltage(),getBatCapacity()); sprintf(batteryStrbuff,"BAT:%2.2fV(%02d%%)",getBatVoltage(),getBatCapacity()); InkPageSprite.drawString(10,185,batteryStrbuff); #endif InkPageSprite.pushSprite(); //delay(15000); } void setup() { /* //Initialize serial and wait for port to open: Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } */ M5.begin(); if( !M5.M5Ink.isInit()) { Serial.printf("Ink Init faild"); } M5.M5Ink.clear(); delay(1000); //creat ink refresh Sprite if( InkPageSprite.creatSprite(0,0,200,200,true) != 0 ) { Serial.printf("Ink Sprite creat faild"); } Wire.begin(25, 26); // CoreInk HAT //Wire.begin(32, 33); // CoreInk EXT. PORT qmp6988.init(); #ifdef NTP_SYNC // connect to WiFi Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" CONNECTED"); // Set ntp time to local configTime(9 * 3600, 0, ntpServer); // Get local time struct tm timeInfo; if (getLocalTime(&timeInfo)) { Serial.print("NTP: "); Serial.println(ntpServer); // Set RTC time RTC_TimeTypeDef TimeStruct; TimeStruct.Hours = timeInfo.tm_hour; TimeStruct.Minutes = timeInfo.tm_min; TimeStruct.Seconds = timeInfo.tm_sec; #ifndef M5CI M5.Rtc.SetTime(&TimeStruct); #endif #ifdef M5CI M5.rtc.SetTime(&TimeStruct); #endif RTC_DateTypeDef DateStruct; DateStruct.WeekDay = timeInfo.tm_wday; DateStruct.Month = timeInfo.tm_mon + 1; DateStruct.Date = timeInfo.tm_mday; DateStruct.Year = timeInfo.tm_year + 1900; #ifdef M5STICK_CP M5.Rtc.SetData(&DateStruct); #endif #ifdef M5C2 M5.Rtc.SetDate(&DateStruct); #endif #ifdef M5CI M5.rtc.SetData(&DateStruct); #endif } // if (getLocalTime(&timeInfo)) { //disconnect WiFi WiFi.disconnect(true); WiFi.mode(WIFI_OFF); #endif } void loop() { /* if( M5.BtnUP.wasPressed()) ButtonTest("Btn UP Pressed"); if( M5.BtnDOWN.wasPressed()) ButtonTest("Btn DOWN Pressed"); if( M5.BtnMID.wasPressed()) ButtonTest("Btn MID Pressed"); if( M5.BtnEXT.wasPressed()) ButtonTest("Btn EXT Pressed"); if( M5.BtnPWR.wasPressed()){ ButtonTest("Btn PWR Pressed"); M5.shutdown(); } */ ENV_HAT(); // control refresh rate with key sensing for (int n = 0; n<30000000; n++) { if( M5.BtnUP.wasPressed()){ ButtonTest("Btn UP Pressed"); break; } if( M5.BtnDOWN.wasPressed()){ ButtonTest("Btn DOWN Pressed"); break; } if( M5.BtnMID.wasPressed()){ ButtonTest("Btn MID Pressed"); break; } if( M5.BtnEXT.wasPressed()){ ButtonTest("Btn EXT Pressed"); break; } if( M5.BtnPWR.wasPressed()){ ButtonTest("Btn PWR Pressed"); M5.shutdown(); } } M5.update(); }

・時刻をNTP同期する機能はないので、時刻を合わせる場合、「NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2)」のスケッチを利用する。 (最新版ではNTP同期を追加したので、これは不要となった)
・デバッグ用に温度、湿度、気圧、時刻、バッテリ状態がシリアル出力される。
・表示の電子インクの制限でフレッシュレートが15秒くらいになっているので、時刻の秒の表示はズレがある。
・課題:delay部分をループに代えて、そのなかでボタンをセンスするようにしたがボタン押下の反応は改善しなかった。他のやり方があるのかもしれない。
(ボタンをセンスするのは、今後の機能拡張のため)

platformio.ini

以下のplatformio.iniを使用する:

[env:M5CoreInk] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5CI upload_poert = /dev/ttyUSB0 monitor_speed = 115200 lib_deps = m5stack/M5-CoreInk @ ^0.0.2 knolleary/PubSubClient @ ^2.8 m5stack/UNIT_ENV @ ^0.0.2 lib_ldf_mode = deep+

参考情報

応用例:
ENV2_HAT_M5StickC_Plus
M5Paper Clock

M5CoreInk関連:
M5 CoreInkとEnvHat,TVOC/eCO2 ガスセンサユニット(SGP30)で環境モニタを作る
M5Stack CoreInk発売

バッテリ関連:
M5Stack CoreInk、M5Paperのバッテリーについて
CoreInkのバッテリーレベルを取得する

HAT関連:
M5StickC ENV Hat III (SHT30/QMP6988)
M5StickC ENV II Hat(SHT30/BMP280/BMM150搭載)

platformio関連:
arduinoフレームワーク用platformio.ini集

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

M5Stackファミリ関連:
M5Paper Arduino Install
M5CoreInk Arduino Install
M5StickCPlus 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編)

以上

続きを読む "M5CoreInk with Env3_Hat"

| | コメント (0)

2021年12月25日 (土)

NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2)

2021/12/26
サポートボードとしてM5Core2を追加した。

2021/12/25
初版

NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2)

NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2)

概要

M5StickC_Plus, M5CoreInk, M5Core2は、RTC内蔵されており電源オフでも時刻を保存している。ここでは、NTPに時刻設定するスケッチを紹介する。
M5StickCのRTCをNTPサーバーからセットする」のスケッチを参考にして、M5StickC_Plus, M5CoreInk,M5Core2用に修正する。ソースは共通になっており、platformio.iniの内容で切り替えてビルドする。

ソース

以下のようにソースを修正する:
src/main.ino

//#define M5CI //#define M5C2 //#define M5STICK_CP #ifdef M5STICK_CP //#include <M5StickC.h> #include <M5StickCPlus.h> #endif #ifdef M5C2 #include <M5Core2.h> #endif #ifdef M5CI #include "M5CoreInk.h" #endif #include <WiFi.h> #include "time.h" #ifdef M5CI Ink_Sprite InkPageSprite(&M5.M5Ink); char titleStrbuff[64]; char ntpStrbuff[64]; char dateStrbuff[64]; char weekStrbuff[64]; char timeStrbuff[64]; #endif const char* ssid = "your_ssid"; const char* password = "your_password"; const char* ntpServer = "ntp.jst.mfeed.ad.jp"; RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; void setup() { #ifdef M5CI M5.begin(); if( !M5.M5Ink.isInit()) { Serial.printf("Ink Init faild"); } M5.M5Ink.clear(); delay(1000); //creat ink refresh Sprite if( InkPageSprite.creatSprite(0,0,200,200,true) != 0 ) { Serial.printf("Ink Sprite creat faild"); } #endif #ifndef M5CI // put your setup code here, to run once: M5.begin(); M5.Lcd.setRotation(3); M5.Lcd.fillScreen(BLACK); M5.Lcd.setTextSize(1); M5.Lcd.setCursor(40, 0, 2); M5.Lcd.println("RTC NTP TEST"); #endif // connect to WiFi Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" CONNECTED"); // Set ntp time to local configTime(9 * 3600, 0, ntpServer); // Get local time struct tm timeInfo; if (getLocalTime(&timeInfo)) { #ifndef M5CI M5.Lcd.print(" NTP : "); M5.Lcd.println(ntpServer); #endif // Set RTC time RTC_TimeTypeDef TimeStruct; TimeStruct.Hours = timeInfo.tm_hour; TimeStruct.Minutes = timeInfo.tm_min; TimeStruct.Seconds = timeInfo.tm_sec; #ifndef M5CI M5.Rtc.SetTime(&TimeStruct); #endif #ifdef M5CI M5.rtc.SetTime(&TimeStruct); #endif RTC_DateTypeDef DateStruct; DateStruct.WeekDay = timeInfo.tm_wday; DateStruct.Month = timeInfo.tm_mon + 1; DateStruct.Date = timeInfo.tm_mday; DateStruct.Year = timeInfo.tm_year + 1900; #ifdef M5STICK_CP M5.Rtc.SetData(&DateStruct); #endif #ifdef M5C2 M5.Rtc.SetDate(&DateStruct); #endif #ifdef M5CI M5.rtc.SetData(&DateStruct); #endif } //disconnect WiFi WiFi.disconnect(true); WiFi.mode(WIFI_OFF); } void loop() { static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"}; #ifndef M5CI // put your main code here, to run repeatedly: M5.Rtc.GetTime(&RTC_TimeStruct); #ifdef M5STICK_CP M5.Rtc.GetData(&RTC_DateStruct); #endif #ifdef M5C2 M5.Rtc.GetDate(&RTC_DateStruct); #endif M5.Lcd.setCursor(0, 30); M5.Lcd.printf(" Date: %04d-%02d-%02d\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date); M5.Lcd.printf(" Week: %s\n", wd[RTC_DateStruct.WeekDay]); M5.Lcd.printf(" Time: %02d : %02d : %02d\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds); #endif #ifdef M5CI // put your main code here, to run repeatedly: M5.rtc.GetTime(&RTC_TimeStruct); M5.rtc.GetData(&RTC_DateStruct); sprintf(titleStrbuff,"RTC NTP TEST"); sprintf(ntpStrbuff,"NTP:%s", ntpServer); sprintf(dateStrbuff,"Date: %04d-%02d-%02d", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date); sprintf(weekStrbuff,"Week: %s", wd[RTC_DateStruct.WeekDay]); sprintf(timeStrbuff,"Time: %02d : %02d : %02d", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds); InkPageSprite.clear(); InkPageSprite.drawString(5,5,titleStrbuff); InkPageSprite.drawString(5,20,ntpStrbuff); InkPageSprite.drawString(5,35,dateStrbuff); InkPageSprite.drawString(5,50,weekStrbuff); InkPageSprite.drawString(5,65,timeStrbuff); InkPageSprite.pushSprite(); #endif delay(500); }

以下は、自分の環境に合わせて修正すること:

const char* ssid = "your_ssid"; const char* password = "your_password"; const char* ntpServer = "ntp.jst.mfeed.ad.jp";

platformio.ini

M5StickC_Plus, M5CoreInk. M5Core2用は以下のようになり、ビルドのターゲットに合わせて内容を変更してビルドする。

M5StickC_Plus用:
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 adafruit/Adafruit BMP280 Library @ ^2.5.0 adafruit/Adafruit Unified Sensor @ ^1.1.4

M5CoreInk用:
platformio.ini

[env:M5CoreInk] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5CI upload_poert = /dev/ttyUSB0 monitor_speed = 115200 lib_deps = m5stack/M5-CoreInk @ ^0.0.2 knolleary/PubSubClient @ ^2.8 m5stack/UNIT_ENV @ ^0.0.2 lib_ldf_mode = deep+

M5Core2用:
platformio.ini

[env:m5core2] platform = espressif32 board = m5stack-core2 framework = arduino upload_speed = 2000000 monitor_speed = 115200 board_build.partitions = default_16MB.csv build_flags = -DM5C2 -DCORE_DEBUG_LEVEL=4 -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue lib_deps = https://github.com/m5stack/M5Core2.git https://github.com/FastLED/FastLED ;https://github.com/m5stack/M5Core2/blob/master/examples/core2_for_aws/ArduinoECCX08.zip lovyan03/LovyanGFX adafruit/Adafruit BMP280 Library @ ^2.5.0 adafruit/Adafruit Unified Sensor @ ^1.1.4 m5stack/UNIT_ENV @ ^0.0.2 lib_ldf_mode = deep+

なお、ファイルとして、default_16MB.csvが必要になる。
以下の手順でダウンロードすること:

wget https://github.com/espressif/arduino-esp32/blob/master/tools/partitions/default_16MB.csv

動作

ビルドして起動するとNTP同期した時刻が表示される。
一回動かせば時刻が設定されるので、他のスケッチを書き込んでも、設定された時刻が保存される。

参考情報

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ファミリ関連:
M5Core2 Arduino Install
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本入)

以上

続きを読む "NTPによる時刻設定(M5StickC_Plus, M5CoreInk, M5Core2)"

| | コメント (0)

2021年12月18日 (土)

M5CoreInk Arduino Install

2021/12/18++
初版

M5CoreInk Arduino Install

M5CoreInk Arduino Install

概要

platformioベースのArduinoフレームワークで、
M5Stack CoreInk 開発キット(1.5インチ Einkディスプレイ)」を動かす。
ホスト環境は、ubuntu20.04とする。
なお、platformioがインストール済みのものとする。

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

工場出荷時にArduinoスケッチであるFactoryTestが書き込まれており、 まずは、それをビルド&書き込むことにする。

電源操作:
・電源をオンする場合、電源ボタン(右下側面の物理ボタン)を押す。
・デバイスをシャットダウンする必要がある場合、USB給電をしない状態で背面のリセットボタンを押す。 (シャットダウンすると、電子インクなので画面が消えずに表示が残るが薄くなるようだ)

FactoryTestの書き込み方法

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

# platformio環境に入る source pio_env/bin/activate # ソースのダウンロード git clone https://github.com/m5stack/M5-CoreInk.git cd M5-CoreInk cd examples/FactoryTest mkdir src cp *.* src/ # 次にあるplatformio.iniの内容のファイルを作成する。 gedit platformio.ini # ソースのパッチ gedit src/FactoryTest

以下のパッチをソースの先頭付近に置く。
(使用しているライブラリにtypoがあるので、それを修正する)

// lib typo fix #define GetDate GetData #define SetDate SetData

続き:

# ビルド環境のクリーンアップ pio run -t clean # M5PaperとPCをUSBで接続する # ビルド&書き込む pio run -t upload

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

なお、動作として、通常は時刻が表示されているが
LまたはRボタンを押すと「WIFI SCAN」モードになり
SSIDが表示される。

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

platformio.ini

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

[env:M5CoreInk] platform = espressif32 board = m5stick-c framework = arduino upload_poert = /dev/ttyUSB0 monitor_speed = 115200 lib_deps = m5stack/M5-CoreInk @ ^0.0.2 knolleary/PubSubClient @ ^2.8 lib_ldf_mode = deep+

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

sketch#1(RTC_BM8563)

別のサンプルとして内蔵RTCのスケッチを挙げる:

以下を実行する:

# platformio環境に入る source pio_env/bin/activate # ソース指定 cd M5-CoreInk/examples/RTC_BM8563 mkdir src cp *.ino src/ gedit platformio.ini #上と同じ内容のものを作成する。 gedit src/RTC_BM8563.ino

以下のパッチをソースの先頭に置く。

#include "M5CoreInk.h" // lib typo fix #define GetDate GetData #define SetDate SetData

続き:

# ビルド環境のクリーンアップ pio run -t clean # M5PaperとPCをUSBで接続する # ビルド&書き込む pio run -t upload

実行するとRTCの時刻が表示される。

sketch#2(MQTT)

cd M5-CoreInk/examples/Advanced/MQTT mkdir src cp *.ino gedit platformio.ini # 上と同じplatformio.iniを使用する。 gedit src/MQTT.ino

以下のところを自分の環境に合わせて変更する:

const char* ssid = "Explore-F"; const char* password = "xingchentansuo123";

続き:

pio run -t clean pio run -t upload

ここまでで、ビルド&書き込みが完了する。

実行画面(E-Ink):

MQTT Success connecting wifi Please see serial

シリアル出力(例):

... Publish message: hello world #63 Message arrived [M5Stack] hello world #63 Publish message: hello world #64 Message arrived [M5Stack] hello world #64 Publish message: hello world #65 Message arrived [M5Stack] hello world #65 Publish message: hello world #66 Message arrived [M5Stack] hello world #66 Publish message: hello world #67 Message arrived [M5Stack] hello world #67 Publish message: hello world #68 Message arrived [M5Stack] hello world #68 ...

sketch#3(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の内容でターゲットを切り替えることができる。

実行時のシリアル出力(/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

sketch#4(RTC_NTP)

# platformio環境に入る source pio_env/bin/activate mkdir RTC_NTP gedit platformio.ini # 上と同じplatformio.iniを使用する mkdir src cd src wget https://github.com/kagurazakarasen/M5Stack/blob/master/M5CoreInk/RTC_NTP/RTC_NTP.ino gedit src/RTC_NTP.ino

このスケッチの場合、Arduino-IDEでサポートされているStringがplatformioにないので、 以下のように修正する:

修正前: const String SSID = "... const String WF_KEY = "... → 修正後: #include <string.h> const char SSID[] = "YourSSID"; const char WF_KEY[] = "YourPasswd"; // lib typo fix #define GetDate GetData #define SetDate SetData --------- 修正前: // Connect to an access point if(SSID.length() == 0 ){ → 修正後: // Connect to an access point //if(SSID.length() == 0 ){ if(strlen(SSID) == 0 ){

続き:

pio run -t clean pio run -t upload

ここまでで、ビルド&書き込みが完了する。

仕様としては、NTPで同期した時刻が表示される。

sketch#5(M5Stack CoreInk NTP Clock)

説明:
M5Stack CoreInk NTP Clock

以下の手順でソースをダウンロードする:

git clone https://github.com/hpsaturn/m5coreink-ntp-clock.git cd m5coreink-ntp-clock

もともとplatformioのプロジェクトだが platformio.iniを以下のものに差し替える:
platformio.ini

; 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 [wifi] ; ssid = *** ; password = *** ssid = ${sysenv.PIO_WIFI_SSID} password = ${sysenv.PIO_WIFI_PASSWORD} [env:M5CoreInk] platform = espressif32 board = m5stick-c framework = arduino upload_poert = /dev/ttyUSB0 monitor_speed = 115200 build_flags = '-DWIFI_SSID="${wifi.ssid}"' '-DWIFI_PASS="${wifi.password}"' lib_deps = m5stack/M5-CoreInk @ ^0.0.2 adafruit/Adafruit Unified Sensor @ 1.1.4 adafruit/Adafruit BMP280 Library @ ^2.1.0

ライブラリのバグ修正のため、
今までのスケッチと同様に
以下のパッチをsrc/main.cppをかける:

// lib typo fix #define GetDate GetData #define SetDate SetData

NTPを日本の設定にするために以下を修正する:

修正前: const char* NTP_SERVER = "ch.pool.ntp.org"; const char* TZ_INFO = "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"; // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php) → 修正後: const char* NTP_SERVER = "ntp.nict.jp"; const char* TZ_INFO = "JST-9";

WiFiの設定をexportを使用しているので ビルド時に以下を実行する:

export PIO_WIFI_SSID="YourSSID" export PIO_WIFI_PASSWORD="YourPassword" pio run -t upload

なお、上のようにexportで設定するのが面倒であれば、
platformio.iniに以下のように設定を置くことができる:
# 文字列の両端の「"」が不要のことに注目
platformio.iniの一部:

[wifi] ssid = ssss password = pppp ;ssid = ${sysenv.PIO_WIFI_SSID} ;password = ${sysenv.PIO_WIFI_PASSWORD} ...

実行時:
USBシリアル接続して、Middleボタンを押しながら resetボタンを押すと 以下のようなメッセージが表示され WiFiが接続できたことが確認できる。 この時、ビープが鳴ればNTPが正常動作したことになる。

initializing.....OK 09:19:48 [WiFi] connecting to SSID..... connected! [NTP] sync.......now 1639787623 [NTP] in: 2021/12/18 09:33:43 [NTP] Sat 18-12-21 09:33:43 [NTP] 18/12/21-9:33:43 Day of Week 6 [BATT] Voltage 4.29 Switch Mode to INK_PARTIAL_MODE

また、USBケーブルを外してバッテリ動作の場合、
Middleボタンを押しながら電源オンにすると
ビープが鳴りNTPが正常動作したことを表す。
Middleボタンを無しで電源オンした場合、
NTP同期無しで起動する。

注意

ライブラリがバージョンアップして修正完了したら 以下のパッチは外すこと:

// lib typo fix #define GetDate GetData #define SetDate SetData

参考情報

M5CoreInk関連:
M5 CoreInkとEnvHat,TVOC/eCO2 ガスセンサユニット(SGP30)で環境モニタを作る
M5Stack CoreInk発売
M5StickC ENV Hat III (SHT30/QMP6988)
M5StickC ENV II Hat(SHT30/BMP280/BMM150搭載)

platformio関連:
arduinoフレームワーク用platformio.ini集

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

M5Stackファミリ関連:
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本入)

以上

続きを読む "M5CoreInk Arduino Install"

| | コメント (0)