M5Atom

2022年1月 1日 (土)

「M5Stack用回転角ユニット」を動かす

2022/1/1
初版

UNIT_ANGLE

UNIT_ANGLE

概要

UNIT_ANGLE「M5Stack用回転角ユニット」を動かす。
ソースは共通化しており、ターゲットのM5Atom,M5StickCPlus,M5Core2をplatformio.iniの内容で切り替えてビルドする。
直接関係ない話だが、M5Atom用に提供されているUNIT_ANGLEのサンプル・スケッチは、なぜかArduino-IDEではエラーになりコンパイルできなかった。
(なにかしらの環境の問題のようだ)

ソース

src/main.ino

/* ******************************************************************************* * Copyright (c) 2021 by M5Stack * Equipped with Atom-Lite/Matrix sample source code * 配套 Atom-Lite/Matrix 示例源代码 * Visit the website for more information:https://docs.m5stack.com/en/products * 获取更多资料请访问:https://docs.m5stack.com/zh_CN/products * * describe:Angle. 角度计 * date:2021/8/9 ******************************************************************************* Description: Read the Angle of the angometer and convert it to digital display 读取角度计的角度,并转换为数字量显示 */ #ifdef M5ATOM #include <M5Atom.h> #endif #ifdef M5STICK_CP #include <M5StickCPlus.h> #endif #ifdef M5C2 #include <M5Core2.h> #endif #ifdef M5ATOM int sensorPin = 32; // set the input pin for the potentiometer. 设置角度计的输入引脚 #endif #ifdef M5STICK_CP int sensorPin = 33; #endif #ifdef M5C2 int sensorPin = 33; #endif int last_sensorValue = 100; // Stores the value last read by the sensor. 存储传感器上次读取到的值 int cur_sensorValue = 0; // Stores the value currently read by the sensor. 存储传感器当前读取到的值 void setup() { M5.begin(); pinMode(sensorPin, INPUT); dacWrite(25, 0); #ifdef M5STICK_CP M5.Lcd.setRotation(3); M5.Lcd.setCursor(0, 10, 2); M5.Lcd.print("The value of ANGLE: "); #endif #ifdef M5C2 M5.Lcd.setTextSize(2); //Set the font size to 2. 设置字体大小为2 M5.Lcd.print("the value of ANGLE: "); #endif } void loop() { // read the value from the sensor: cur_sensorValue = analogRead(sensorPin); #ifdef M5ATOM if(abs(cur_sensorValue - last_sensorValue) > 10){ //If the difference is more than 10. 如果差值超过10 Serial.print("the value of ANGLE: "); Serial.println(cur_sensorValue); last_sensorValue = cur_sensorValue; } delay(200); #endif #ifdef M5STICK_CP M5.Lcd.setCursor(80, 50, 6); if(abs(cur_sensorValue - last_sensorValue) > 10){//debaunce M5.Lcd.fillRect(100, 50, 100, 40, BLACK); M5.Lcd.print(cur_sensorValue); last_sensorValue = cur_sensorValue; } delay(50); #endif #ifdef M5C2 M5.Lcd.setCursor(0, 25); if(abs(cur_sensorValue - last_sensorValue) > 10){//debaunce M5.Lcd.fillRect(0, 25, 100, 25, BLACK); M5.Lcd.print(cur_sensorValue); last_sensorValue = cur_sensorValue; } delay(50); #endif }

platformio.ini

M5Atom用

[env:M5ATOM] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5ATOM upload_poert = /dev/ttyUSB0 monitor_speed = 115200 lib_deps = m5stack/M5Atom @ ^0.0.7 fastled/FastLED @ ^3.4.0 adafruit/Adafruit NeoPixel @ ^1.10.3 lib_ldf_mode = deep+

M5StickCPlus用

[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 adafruit/Adafruit NeoPixel @ ^1.10.3

M5Core2用

[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 adafruit/Adafruit NeoPixel @ ^1.10.3 lib_ldf_mode = deep+

動作

実態はポテンショメーターなのでアナログで読み込んだ値がLCDに表示される。
(M5Atomではシリアルに出力される)

M5Atomの出力例(シリアル出力):

$ bt -L 1 No port specified, using ttyUSB0 (last registered). Use -l to list ports. Trying port ttyUSB0... Connected to ttyUSB0 at 115200 bps. Escape character is 'Ctrl-]'. Use escape followed by '?' for help. the value of ANGLE: 2739 the value of ANGLE: 2704 the value of ANGLE: 2717 <省略> the value of ANGLE: 706 the value of ANGLE: 1059 the value of ANGLE: 2411 the value of ANGLE: 4095 the value of ANGLE: 4049 the value of ANGLE: 4095 ...

参考情報

terminal関連:
Bootterm – a developer-friendly serial terminal program

M5Atom関連:
M5Atomの細かい仕様
Grove端子(HY2.0-4P、PH2.0-4P)について調べてみた

M5StickC/Plus関連:
M5StickCのIOについて調べてみた
Arduino(M5StickC)でefont Unicodeフォント表示 完結編

M5Core2関連:
M5Stack Core2を使ってみました。

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編)

以上

続きを読む "「M5Stack用回転角ユニット」を動かす"

| | コメント (0)

2021年1月10日 (日)

M5Atomを開発ツールPlatformIOで使う(v2)(M5Atom/Arduino版)

2021/1/9

PlatformIO M5ATOM v2

PlatformIO M5ATOM v2

概要

以下のM5Atomを開発ツールPlatformIOで使う(v2)(M5Atom/Arduino版)
本記事は「 M5Atomを開発ツールPlatformIOで使う(M5Atom/Arduino版) 」を見直して第2版としたものである。
(ホストPCとしてはubuntuを想定している)

ATOM Lite
ATOM Matrix

Peripherals Pin Map

Lite:

Func GPIO
RGB Led(Neo) G27
Btn G39
IR G12

Matrix:

Func GPIO
Neo G27
Btn G39
IR G12
CLK(MPU6886) G21
SDA(MPU6886) GP25

Grove Interface

GND 5V G26 G32
GND 5V SDA SCL

PlatformIOのインストール

python3 -m venv pio_env source pio_env/bin/activate pip3 install platformio インストール後も、本ツールを使用する場合 同じディレクトリで以下を実行する: source pio_env/bin/activate # 「source」は、「.」でも良い

準備

以下を実行して、udevのrulesを登録する:

curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules sudo udevadm control --reload-rules sudo usermod -a -G dialout $USER sudo usermod -a -G plugdev $USER

過去に設定してあったとしても、rulesが更新されている場合があるので、再設定したほうが良い。

テスト用プロジェクト sample を作成/実行する

# プロジェクト sample のディレクトリを作成する mkdir sample cd sample # 以下を実行して必要なファイルを作成する pio init --board m5stick-c # platformをupdateする pio platform update nano 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 [env:esp32dev] platform = espressif32 board = m5stick-c framework = arduino monitor_speed = 115200 lib_ldf_mode = deep+ lib_deps = m5stack/M5Atom @ ^0.0.1 fastled/FastLED @ ^3.4.0 jorgen-vikinggod/FastLED GFX Library @ ^0.1.0
# テスト用のbutton.inoを作成する nano src/button.ino 以下のように編集する:
/**************************************************************** * * This Example is used to test button * * Arduino tools Setting * -board : M5StickC * -Upload Speed: 115200 / 750000 / 1500000 * ****************************************************************/ #include "M5Atom.h" uint8_t DisBuff[2 + 5 * 5 * 3]; void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata) { DisBuff[0] = 0x05; DisBuff[1] = 0x05; for (int i = 0; i < 25; i++) { DisBuff[2 + i * 3 + 0] = Rdata; DisBuff[2 + i * 3 + 1] = Gdata; DisBuff[2 + i * 3 + 2] = Bdata; } } void setup() { M5.begin(true, false, true); delay(10); setBuff(0xff, 0x00, 0x00); M5.dis.displaybuff(DisBuff); } uint8_t FSM = 0; void loop() { if (M5.Btn.wasPressed()) { switch (FSM) { case 0: setBuff(0x40, 0x00, 0x00); break; case 1: setBuff(0x00, 0x40, 0x00); break; case 2: setBuff(0x00, 0x00, 0x40); break; case 3: setBuff(0x20, 0x20, 0x20); break; default: break; } M5.dis.displaybuff(DisBuff); FSM++; if (FSM >= 4) { FSM = 0; } } delay(50); M5.update(); }

続き:

# build pio run # ボードをホストPCに接続する # build&upload(flash) pio run -t upload # buildしないで書き込む場合は以下を実行する: pio run -t nobuild -t upload -v # -v は、詳細を表示するオプション # 以上で、基本的な操作としては完了となる

書き込み後、ボタンを押す度に、LEDの色が変化する。 (本スケッチは、Matrix/Lite兼用になっていて、Matrixの場合、5x5のLEDが同じ色で光る。Liteの場合、一つのLEDが光る)

これ以降、別のプログラムを動かすときは sampleのディレクトリをまるごと コピーして別のプロジェクトのディレクトリを作り そこにプログラム(.ino)を置く。

例:

cp sample m5a_proj01 cd m5a_proj01 ...

MPU6886を利用するスケッチ#1(Matrix限定)

src/mpu6886_2.ino

#include "M5Atom.h" #define SIG_MAX (4096) uint8_t DisBuff[2 + 5 * 5 * 3]; int16_t adX,adY,adZ; void setBuffP(uint8_t posData, uint8_t Rdata, uint8_t Gdata, uint8_t Bdata) { DisBuff[2 + posData * 3 + 0] = Rdata; DisBuff[2 + posData * 3 + 1] = Gdata; DisBuff[2 + posData * 3 + 2] = Bdata; } void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata) { for (uint8_t i = 0; i < 25; i++) setBuffP(i, Rdata, Gdata, Bdata); } void shftBuff() { for (uint8_t i = 24; i > 0; i--) { for (uint8_t j = 0; j < 3; j++) DisBuff[2 + i * 3 + j] = DisBuff[2 + (i-1) * 3 + j]; } } void setup() { DisBuff[0] = 0x05; DisBuff[1] = 0x05; M5.begin(false, true, true); delay(10); M5.IMU.Init(); setBuff(0x20, 0x20, 0x20); M5.dis.displaybuff(DisBuff); // Serial.begin(115200); } void loop() { M5.update(); if (M5.Btn.read()==0) { M5.IMU.getAccelAdc(&adX, &adY, &adZ); int r = min(max((int)map(adX,-SIG_MAX,SIG_MAX,0,255),0),255); int g = min(max((int)map(adY,-SIG_MAX,SIG_MAX,0,255),0),255); int b = min(max((int)map(adZ,-SIG_MAX,SIG_MAX,0,255),0),255); shftBuff(); setBuffP(0,r,g,b); M5.dis.displaybuff(DisBuff); // Serial.printf("x,y,z: %d,%d,%d\r\n", adX, adY, adZ); } delay(20); }

書き込み実行し、M5Atomを傾けるとそれに応じて5x5のLEDの色が変化する。

MPU6886を利用するスケッチ#2(Matrix限定)

src/mpu6886_3.ino

#include "M5Atom.h" float accX = 0, accY = 0, accZ = 0; float gyroX = 0, gyroY = 0, gyroZ = 0; float temp = 0; bool IMU6886Flag = false; void setup() { M5.begin(true, false, true); if (M5.IMU.Init() != 0) IMU6886Flag = false; else IMU6886Flag = true; } void loop() { if (IMU6886Flag == true) { M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ); M5.IMU.getAccelData(&accX, &accY, &accZ); M5.IMU.getTempData(&temp); Serial.printf("%.2f,%.2f,%.2f o/s \r\n", gyroX, gyroY, gyroZ); Serial.printf("%.2f,%.2f,%.2f mg\r\n", accX * 1000, accY * 1000, accZ * 1000); Serial.printf("Temperature : %.2f C \r\n", temp); } delay(500); M5.update(); }

スケッチ#3(Matrix限定)

src/Test_GFX.ino

#include "M5Atom.h" #include <FastLED.h> #include <FastLED_GFX.h> // for M5Atom Matrix #define LED_PIN 27 #define COLOR_ORDER GRB #define CHIPSET WS2811 #define BRIGHTNESS 64 #define CANVAS_WIDTH 5 #define CANVAS_HEIGHT 5 #define NUM_LEDS (CANVAS_WIDTH * CANVAS_HEIGHT) GFXcanvas canvas(CANVAS_WIDTH, CANVAS_HEIGHT); void setup() { FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(canvas.getBuffer(), NUM_LEDS).setCorrection(TypicalSMD5050); FastLED.setBrightness(BRIGHTNESS); } void loop() { EVERY_N_SECONDS(5) { canvas.fillScreen(CRGB::Red); canvas.drawLine(0, 0, CANVAS_WIDTH-1, CANVAS_HEIGHT-1, CRGB::White); canvas.drawCircle(2, 2, 2, CRGB(255,255,0)); FastLED.show(); } }

Adafruit_GFXのライブラリをLED_Matrix(5x5)に適用したデモ・スケッチであり、直線と円を描画している。

スケッチ#4(Matrix限定)

src/Test_DispBuff.ino

#include "M5Atom.h" #include "hello_img.c" int ofs = 0; void setup() { M5.begin(true, false, true); } void loop() { M5.dis.displaybuff((uint8_t *) image_hello, ofs, 0); ofs--; ofs = (ofs == -1) ? image_hello[0] - 1 : ofs; delay(100); }

src/hello_img.c

const unsigned char image_hello[977]= { /* width 065 */ 0x41, /* height 005 */ 0x05, /* Line 000 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, // /* Line 001 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, // /* Line 002 */ 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, // /* Line 003 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, // /* Line 004 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, // };

M5.dis.displaybuffのデモ・スケッチでメッセージをLED_Matrixに表示する。

WiFi対応スケッチ

ESP32のWiFi対応スケッチと互換性があるので、以下をソースの先頭に入れると動作するようだ。

#ifdef M5ATOM #include "M5Atom.h" #define ESP32 #endif

したがって、以下のスケッチの先頭に上のコードを入れるとM5Atomでも動作するようになる:
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチでWorld Time APIを使う(WorldTimeAPI編)
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(HTTP-ACCESS編)
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(NTP-CLIENT編)
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(REST-API2編)
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(OSC編)
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(MQTT編)

参考情報

Atom pixel tool
wget https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/software/AtomPixTool.exe
windowsのプログラムだがlinuxのwineでも動作するようだ。
マトリックスのデザインを作成でき、それを保存すると、そのデータのC言語ソースができあがる

Display API Document:
https://github.com/m5stack/M5Atom

サンプル・スケッチ:
git clone https://github.com/m5stack/M5Atom.git
git clone https://github.com/hajimef/m5atom-matrix-samples.git
M5AtomをPlatformIOで動かす-ライブラリインストールから加速度取得まで

PlatformIO Core (CLI)

以上

続きを読む "M5Atomを開発ツールPlatformIOで使う(v2)(M5Atom/Arduino版)"

| | コメント (0)

2020年12月31日 (木)

python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(v2,microbit-v2対応)(linux版)

2020/12/31

Mu-editor micro:bit v2

Mu-editor micro:bit v2

概要

python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(v2,microbit-v2対応)(linux版)

この記事は「 python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(linux版) 」の第2版にあたり、micro:bit-v2の対応方法についてなどを追加している。
(ホストはubuntu20.04を想定している)

インストール方法

以下を実行する:

# 以下のライブラリをインストールする sudo apt -y install git python3-dev python3-setuptools python3-numpy python3-opengl \ libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \ libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev \ libtiff5-dev libx11-6 libx11-dev fluid-soundfont-gm timgm6mb-soundfont \ xfonts-base xfonts-100dpi xfonts-75dpi xfonts-cyrillic fontconfig fonts-freefont-ttf libfreetype-dev # mu-editorのインストール pip3 install mu-editor # ショートカットを作成するためshortcutをインストールする pip installl shortcut # mu-editorのショートカットを作る shortcut mu-editor # この時点で、デスクトップにmu-editorのショートカット(アイコン)が作られる # そのショートカットをクリックするとmu-editorが起動する

micro:bit-v2の対応方法

mu-editor自身はmicro:bit-v2対応が未完で、そのままでは使用できない。 そのため以下の手順で対応する:

(1)以下のurlへchromeブラウザーでアクセスしてmu-editorのweb版を起動する。 https://python.microbit.org/v/2 (2)micro:bit-v2をホストに接続する (3)起動しているmu-editorでデバイスを接続して、そこにあるスクリプトを書き込む (4)以上で、micro:bit-v2に対応したmicropythonのファームウェアが書き込まれる

その後、通常のmu-editorを起動してスクリプトを書き込むことができる。

micro:bit-v2の最新のmicropythonを使う

以下の手順で最新のmu-editorを使用すると最新版のmicropythonが書き込める。

mkdir latest_mu cd latest_mu git clone https://github.com/bbcmicrobit/PythonEditor cd PythonEditor git submodule update --init --recursive # 以下のコマンドでmu-editor(のサーバー)を起動することができる。。 ./bin/show Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... # http://localhost:8000/editor.htmlをchromeブラウザーでアクセスするとweb版と同じeditorが起動する (Web-USBの機能を使う関係上,chromeである必要がある) # 起動しているmu-editorでデバイスを接続して、そこにあるスクリプトを書き込む 以上で、micro:bit-v2に対応したmicropythonの最新版が書き込まれる

以上が完了すれば、通常のmu-editorを使用しても最新版のmicropythonが使える。

micro:bitのmicropythonのバージョン

REPLでバージョンを確認すると以下のようになっていた:

旧版 MP現行版(v1.5)

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822 Type "help()" for more information. >>> >> import gc >>> gc.collect() >>> gc.mem_free() 9136 >>>

v2 MP現行版(webからの書き込み)

MicroPython v1.13 on 2020-11-24; micro:bit v2.0.0-beta.1 with nRF52833 Type "help()" for more information. >>> >>> import gc >>> gc.collect() >>> gc.mem_free() 61344 >>>

v2 MP最新版(2020/12/31現在)

MicroPython v1.13 on 2020-12-21; micro:bit v2.0.0-beta.3 with nRF52833 Type "help()" for more information. >>> >>> import gc >>> gc.collect() >>> gc.mem_free() 63616 >>>

micro:bi-v2になってRAMが増えていることからフリーメモリが増えていることが分かる。

micro:bit-v2で追加された機能を使ったスクリプト例

music_button.py

from microbit import * import music pin0.set_touch_mode(pin0.CAPACITIVE) #pin0.set_touch_mode(pin0.RESISTIVE) pin1.set_touch_mode(pin1.CAPACITIVE) #pin1.set_touch_mode(pin1.RESISTIVE) pin2.set_touch_mode(pin2.CAPACITIVE) #pin2.set_touch_mode(pin2.RESISTIVE) while True: if pin_logo.is_touched(): music.play(music.BLUES) if pin0.is_touched(): music.play(music.NYAN) if pin1.is_touched(): music.play(music.ODE) if pin2.is_touched(): music.play(music.BLUES) if button_a.was_pressed(): music.play(music.NYAN) if button_b.was_pressed(): for x in range(2): music.play(["C4:4", "D4", "E4", "C4"]) for x in range(2): music.play(["E4:4", "F4", "G4:8"])

mic_test.py

from microbit import * while True: if microphone.current_event() == SoundEvent.LOUD: display.show(Image.SQUARE) elif microphone.current_event() == SoundEvent.QUIET: display.show(Image.SQUARE_SMALL)

speak_happy.py

from microbit import * import speech set_volume(100) speech.say("are you happy?")

send_smile.py

from microbit import * import radio radio.config(group=2) radio.on() while True: message = radio.receive() if message: display.show(Image.HAPPY) if button_a.is_pressed(): display.clear() radio.send('smile')

このスクリプトは新機能を使っていないがbluetoothの使用例として載せた。

ハードウェアの相違点

micro:bitのバージョンの違いは以下のようになる:

v1.5 Nordic Semiconductor nRF51822 256kB Flash, 16kB RAM v2.0 Nordic Semiconductor nRF52833 512kB Flash, 128kB RAM

詳細は以下を参照のこと:
micro:bit のバージョンアップについて

参照情報

Using Mu with micro:bit V2
How to install Mu with Python packaging on Windows, OSX and Linux

https://microbit-micropython.readthedocs.io/en/v2-docs/tutorials/hello.html

https://microbit-micropython.readthedocs.io/en/latest/tutorials/introduction.html

https://microbit-micropython.readthedocs.io/en/v2-docs/tutorials/radio.html

https://qiita.com/sat0ken/items/13bd03378c28b98a794e
micro:bit v2 で遊ぶ

https://qiita.com/inachi/items/a591707ab6bbcb1a467c
micro:bit v2 用 MicroPython の beta-3 が出たので試してみた

公式の技術情報
micro:bit developer community and technical resources
Working together on the latest BBC micro:bit
Guidance on using the latest micro:bit revision

以上

続きを読む "python3/micro:bit-micropython/CircuitPython用エディタ(mu-editor)をインストールする(v2,microbit-v2対応)(linux版)"

| | コメント (0)

2020年12月28日 (月)

Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)

2020/12/28:
初版

board3 Ascii STARWARS v2

board3 Ascii STARWARS v2

概要

Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)
本記事は「 Wio-TerminalでWiFiで使う(その2: STARTWARS,WEB_ACCESS) 」 のStarwarsのスケッチを複数ボード対応とwio-terminalのファームウェア・バージョンアップに対応したものである。

wio-terminalのファームウェアのバージョンアップで以下の不具合も解消している。
(描画速度も大幅な改善が見られる)

一部、以下のようなATコマンドのレスポンス・ヘッダーが表示されることがある。 wifi関係のライブラリにバグがあるようだ。 +IPD,4,988,94.142.241.111,23:__|_____|___________|__|____________|_||____ +IPD,4,1460,94.142.241.111,23: +IPD,4,1460,94.142.241.111,23: |(I/ /][][\| {}/ {} \[][][]

wio-terminalのファームウェアのアップデート方法については以下を参照のこと:
wio-terminalのファームウェア・アップデートについて(v2)(linux版)

デモ・スケッチ

src/wifi_starwars.ino

// select board ////#define WIO_TERMINAL ////#define ESP8266 ////#define ESP32 ////#define M5ATOM //------------------ #ifdef M5ATOM #include "M5Atom.h" #define ESP32 #endif #ifdef WIO_TERMINAL //#include <AtWiFi.h> #include <rpcWiFi.h> #endif #ifdef ESP8266 #include <ESP8266WiFi.h> #endif #ifdef ESP32 #include <WiFi.h> #endif const char* ssid = "your_ssid"; const char* passwd = "yours_passwd"; // Use WiFiClient class to create TCP connections WiFiClient client; void setup() { Serial.begin(115200); while(!Serial); // Wait for Serial to be ready delay(1000); // Set WiFi to station mode and disconnect from an AP if it was previously connected WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(2000); WiFi.begin(ssid, passwd); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Connecting to WiFi.."); } Serial.println("Connected to the WiFi network"); Serial.print("IP Address: "); Serial.println (WiFi.localIP()); // prints out the device's IP address // set url for telnet const char* host = "towel.blinkenlights.nl"; const uint16_t port = 23; Serial.print("Connecting to "); Serial.println(host); while (!client.connect(host, port)) { Serial.println("Connection failed."); Serial.println("Waiting 5 seconds before retrying..."); delay(5000); } } void loop() { while (!client.available()) { delay(1); //delay 1 msec } // no buffering for ASCII Art Animation while (client.available()) { char c = client.read(); Serial.write(c); } /*********** if (client.available() > 0) { String line = client.readString(); // Read from the server response Serial.print(line); } ************/ }

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 passwd[] = "yours_passwd";

書き込み後に「picocom /dev/ttyACM0 -b115200」または「picocom /dev/ttyUSB0 -b115200」で通信ソフトを起動すると以下のような出力が表示される:
(M5Atomの場合、実行時にリセット・ボタンを押す必要があるようだ)

$ picocom /dev/ttyACM0 -b115200 #以下のようなAsciiArtのアニメが実行される: ........... @@@@@ @@@@@ ........... .......... @ @ @ @ .......... ........ @@@ @ @ .......... ....... @@ @ @ ......... ...... @@@@@@@ @@@@@ th ........ ..... ----------------------- ....... .... C E N T U R Y ....... ... ----------------------- ..... .. @@@@@ @@@@@ @ @ @@@@@ ... == @ @ @ @ @ == __||__ @ @@@@ @ @ __||__ | | @ @ @ @ @ | | _________|______|_____ @ @@@@@ @ @ @ _____|______|_________

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

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/*

参考情報

PlatformIO Core (CLI)

以上

続きを読む "Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)"

| | コメント (0)

2020年12月27日 (日)

Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)

2020/12/27:
初版

board3 MQTT v2

board3 MQTT v2

概要

Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)

本記事は「 Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(MQTT編) 」 の改版にあたり、wio-terminalのファームウェア・バージョンアップに対応している。

wio-terminalのファームウェアのアップデート方法については以下を参照のこと:
wio-terminalのファームウェア・アップデートについて(v2)(linux版)

デモ・スケッチ

src/MQTT_test.ino

// select board ////#define WIO_TERMINAL ////#define ESP8266 ////#define ESP32 ////#define M5ATOM //------------------ // // MQTT program for Wio-Terminal/ESP8266/ESP32 // // Forked from the following code: //-------------------------------------------- // This example uses an Adafruit Huzzah ESP8266 // to connect to shiftr.io. // // You can check on your device after a successful // connection here: https://shiftr.io/try. // // by Joël Gähwiler // https://github.com/256dpi/arduino-mqtt //-------------------------------------------- #ifdef M5ATOM #include "M5Atom.h" #define ESP32 #endif #ifdef WIO_TERMINAL //#include <AtWiFi.h> #include <rpcWiFi.h> #endif #ifdef ESP8266 #include <ESP8266WiFi.h> #endif #ifdef ESP32 #include <WiFi.h> #endif #include <MQTT.h> const char ssid[] = "yours_ssid"; const char pass[] = "yours_passwd"; WiFiClient net; MQTTClient client; unsigned long lastMillis = 0; void connect() { Serial.print("checking wifi..."); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1000); } Serial.print("\nconnecting..."); while (!client.connect("arduino", "try", "try")) { // for "broker.shiftr.io" //while (!client.connect("arduino", "", "")) { // no username. no passwd (for "mqtt.eclipse.org") Serial.print("."); delay(1000); } Serial.println("\nconnected!"); client.subscribe("topic0"); // client.unsubscribe("/hello"); } void messageReceived(String &topic, String &payload) { Serial.println("incoming: " + topic + " - " + payload); } void setup() { Serial.begin(115200); WiFi.begin(ssid, pass); // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. // You need to set the IP address directly. // uncomment one of them to select MQTT server client.begin("broker.shiftr.io", net); //OK //client.begin("mqtt.eclipse.org", net); //OK //client.begin("test.mosquitto.org", net); //NG? client.onMessage(messageReceived); connect(); } void loop() { char s[100]; // buf for sprintf // dumy sensor values float pascals = 101312; float altm = 42; //m float tempC = 28.7; // deg C client.loop(); delay(10); // <- fixes some issues with WiFi stability if (!client.connected()) { connect(); } // publish a message roughly every second. if (millis() - lastMillis > 1000) { lastMillis = millis(); // publish sensor values sprintf(s,"{ \"press\" : %.2f, \"altm\" : %.1f, \"tempC\" : %.1f}", pascals/100, altm, tempC); client.publish("topic0", &s[0]); } delay(1000); }

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";

以下は利用するMQTTサーバー(broker)に合わせてコメントアウトする:

<省略> while (!client.connect("arduino", "try", "try")) { // for "broker.shiftr.io" //while (!client.connect("arduino", "", "")) { // no username. no passwd (for "mqtt.eclipse.org") <省略> <省略> client.begin("broker.shiftr.io", net); //OK //client.begin("mqtt.eclipse.org", net); //OK <省略>

書き込み後に「picocom /dev/ttyACM0 -b115200」または「picocom /dev/ttyUSB0 -b115200」で通信ソフトを起動すると以下のような出力が表示される:

$ picocom /dev/ttyACM0 -b115200 Terminal ready # 実際のセンサーを接続していないので一定のダミーデータを受信する incoming: topic0 - { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} incoming: topic0 - { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} incoming: topic0 - { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} incoming: topic0 - { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} incoming: topic0 - { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} incoming: topic0 - { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} ... <省略>

対向する送受信プログラム

以下をブラウザ(chromeのみ)で動かす:

MQTT_AT_webTestZero_02.html

<html> <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> <body> <script> //var mqtt_url = 'ws://test.mosquitto.org:8081' //var mqtt_url = 'ws://mqtt.eclipse.org/mqtt:443' var mqtt_url = 'wss://try:try@broker.shiftr.io' document.body.innerHTML = ''; var consout = 'MQTT over WebSockets Test'+'('+mqtt_url+')<br>' document.body.innerHTML = consout; //var mqtt = require('mqtt'); var client = mqtt.connect(mqtt_url); // subscribe Topic //client.subscribe('hello'); client.subscribe('topic0'); //client.subscribe('topic0/sample/hello'); //client.subscribe('topic0/sample/#'); // wildcard topic //client.subscribe('topic0/#'); //client.subscribe('#'); client.on('message', function(topic, payload) { console.log([topic, payload].join(': ')); consout += [topic, payload].join(': ')+'<br>' document.body.innerHTML = consout // disconnect //client.end(); }); // publish messages client.publish('topic0', 'hello world of MQTT! #1'); client.publish('topic0', 'hello world of MQTT! #2'); client.publish('topic0', 'hello world of MQTT! #3'); client.publish('topic0', 'hello world of MQTT! #4'); client.publish('topic0', 'hello world of MQTT! #5'); </script> </body> </html>

以下の部分は使用しているbrokerによって変更すること:
(brokerが止まっていることがあるようなので、そのときはbrokerを変えてみる)

//var mqtt_url = 'ws://test.mosquitto.org:8081' //var mqtt_url = 'ws://mqtt.eclipse.org/mqtt:443' var mqtt_url = 'wss://try:try@broker.shiftr.io'

デモプログラムを起動後、上の「MQTT_AT_webTestZero_02.html」をプラウザーで起動すると
以下のweb画面が表示される(例):

MQTT over WebSockets Test(wss://try:try@broker.shiftr.io) topic0: hello world of MQTT! #1 topic0: hello world of MQTT! #2 topic0: hello world of MQTT! #3 topic0: hello world of MQTT! #4 topic0: hello world of MQTT! #5 topic0: { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} topic0: { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} topic0: { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} topic0: { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} topic0: { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} topic0: { "press" : 1013.12, "altm" : 42.0, "tempC" : 28.7} ...

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 # MQTT lib 617

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 # MQTT lib 617

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 monitor_speed = 115200 lib_ldf_mode = deep+ lib_deps = # MQTT lib 617

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+ lib_deps = # MQTT lib 617

wio-terminalのファームウェアを切り替えたときの注意

古いファームウェアのライブラリを動かした後は、古いライブラリがキャッシュで残っていると ビルド・エラーになるので以下を実行すること:

cd project_directory rm -r .pio/libdeps/seeed_wio_terminal/* rm -r .pio/build/seeed_wio_terminal/*

参考情報

https://docs.shiftr.io/interfaces/mqtt/

The following ports are available on the broker.shiftr.io host: MQTT MQTTS MQTTWS MQTTWSS 1883 8883 80 443

MQTT test server @mosquitto.org

https://test.mosquitto.org/ The server listens on the following ports: 1883 : MQTT, unencrypted 8883 : MQTT, encrypted 8884 : MQTT, encrypted, client certificate required 8080 : MQTT over WebSockets, unencrypted 8081 : MQTT over WebSockets, encrypted websocketで使用するurlは以下になる: 'ws://test.mosquitto.org:8081'

MQTT test server @eclipse.org

http://mqtt.eclipse.org/ This is a public test MQTT broker service. It currently listens on the following ports: 1883 : MQTT over unencrypted TCP 8883 : MQTT over encrypted TCP 80 : MQTT over unencrypted WebSockets (note: URL must be /mqtt ) 443 : MQTT over encrypted WebSockets (note: URL must be /mqtt ) websocketで使用するurlは以下になる: 'ws://mqtt.eclipse.org/mqtt:443'

10 Free Public & Private MQTT Brokers(For Testing & Production)

PlatformIO Core (CLI)

ESP-WROOM-02ボードでMQTTとMPL3115A2(気圧・高度・気温センサ)を動かす(Arduino版)
本記事のスケッチは、この記事のスケッチとほとんど同じであるので参考にできる。

以上

続きを読む "Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)"

| | コメント (0)

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/*

参考情報

PlatformIO Core (CLI)

以上

続きを読む "Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)"

| | コメント (0)

2020年12月26日 (土)

Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)

2020/12/26+
初版

board3 OSC v2

board3 OSC v2

概要

Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)
本記事は「Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(OSC編)」の改版にあたる。wio-terminalのWiFi/BLEファームウェアがバージョンアップしたので、それの対応方法を記載した。これにより、wio-terminalの古いファームウェアで原因不明で動作しなかったOSC受信が動作するようになった。

wio-terminalのファームウェアのアップデート方法については以下を参照のこと:
wio-terminalのファームウェア・アップデートについて(v2)(linux版)

OSCcodecライブラリ

OSC_codecライブラリとしてプロジェクトのsrcディレクトリに以下の2つのファイルを置く:

src/OSCmsgCodec.h

// OSC message Codec Header // 2013/10/28 #ifndef _OSCMSGCODEC_H #define _OSCMSGCODEC_H #include <string.h> int encOSCmsg(char *packet , union OSCarg *msg); // makes packet from OSC message and returns packet size void decOSCmsg(char *packet , union OSCarg *msg); // makes OSC message from packet union OSCarg { // char*, int and float are assumed four bytes char *address; char *typeTag; long int i; // int32 for Arduino(16bits) float f; char *s; struct { long int len; // is "int i" char *p; } blob; char m[4]; // for MIDI char _b[4]; // endian conversion temp variable }; #endif // _OSCMSGCODEC_H

src/OSCmsgCodec.cpp

/* <---------------------------------------------------------------------------------- OSC message Codec(encoder/decoder) version: 1.3 (2014/ 8/31) encoder bufix(enoceder returns byte length) version: 1.2 (2014/ 8/30) decoder bufix version: 1.1 (2013/11/20) support BIG_ENDIAN by #define version: 1.0 (2013/11/10) Copyright (C) 2011,2013,2014 S.Komatsu released under the MIT License: http://mbed.org/license/mit please refer to: http://opensoundcontrol.org/introduction-osc for OSC(Open Sound Control) The followings are supported: Features: Packet Parsing (Client) Packet Construction (Server) Bundle NOT Support Timetag NOT Support Type Support: i: int32 b: blob s: string f: float32 m: MIDI message(port id, status byte, data1, data2) // I don't know the detail Change Log: Bug(string length is not correct in encoding) Fix on 2013/11/10 (v0.9 -> v1.0) >---------------------------------------------------------------------------------- */ #include "OSCmsgCodec.h" //#define BIG_ENDIAN int lenAlign4B(int len) { if ((len % 4) == 0) {return len; } else {return len+4-(len % 4);} } int encOSCmsg(char *packet , union OSCarg *msg){ // *** important notice *** // output buffer must be cleared before call this char *p, *s, *d, *typeTag; char c; p=packet; d=p; s=msg[0].address; // address for(int i=0; i<strlen(msg[0].address); i++) *d++ = *s++; *d=0; // terminator // p += 4*((strlen(msg[0].address)+1)/4+1); p += lenAlign4B(strlen(msg[0].address)+1); // s=msg[1].typeTag; d=p; for(int i=0; i<strlen(msg[1].typeTag); i++) *d++ = *s++; *d=0; // terminator // p += 4*((strlen(msg[1].s)+1)/4+1); p += lenAlign4B(strlen(msg[1].typeTag)+1); // typeTag=msg[1].s+1; // skip ',' for(int n=0; n<strlen(typeTag); n++){ c = typeTag[n]; if (('s'==c)) { s=msg[n+2].s; d=p; for(int i=0; i<strlen(msg[n+2].s); i++) *d++ = *s++; *d=0; // terminater // p += 4*((strlen(msg[n+2].s)+1)/4+1); p += lenAlign4B(strlen(msg[n+2].s)+1); } else if (('i'==c)||('f'==c)) { #ifdef BIG_ENDIAN // no change endian (big to big) p[0]=msg[n+2]._b[0]; p[1]=msg[n+2]._b[1]; p[2]=msg[n+2]._b[2]; p[3]=msg[n+2]._b[3]; #else // change endian (little to big) p[0]=msg[n+2]._b[3]; p[1]=msg[n+2]._b[2]; p[2]=msg[n+2]._b[1]; p[3]=msg[n+2]._b[0]; #endif p +=4; } else if ('b'==c) { // put length of blog #ifdef BIG_ENDIAN // no change endian (big to big) p[0]=msg[n+2]._b[0]; p[1]=msg[n+2]._b[1]; p[2]=msg[n+2]._b[2]; p[3]=msg[n+2]._b[3]; #else // change endian (little to big) p[0]=msg[n+2]._b[3]; p[1]=msg[n+2]._b[2]; p[2]=msg[n+2]._b[1]; p[3]=msg[n+2]._b[0]; #endif p +=4; // get ponter of blog (copy to msg[n].blog.p) s=msg[n+2].blob.p; d=p; for(int i=0; i<msg[n+2].blob.len; i++) *d++ = *s++; p += 4*(msg[n+2].blob.len/4+1); } else if ('m'==c) { // get midi data (copy to msg[n].m[]) p[0]=msg[n+2].m[0]; p[1]=msg[n+2].m[1]; p[2]=msg[n+2].m[2]; p[3]=msg[n+2].m[3]; p +=4; } else { //printf("*** Not Supported TypeTag:%s ****\n",typeTag); } }; //return (p-packet); // return packet size // bugfix 2014/8/31 return sizeof(char)*(p-packet); // return byte length }; void decOSCmsg(char *packet , union OSCarg *msg){ // Caution: the returned result points to packet as blobs or strings (not newly allocatd) char *p, *typeTag; char c; int n; msg[0].address = packet; // address msg[1].typeTag = packet+4*((strlen(msg[0].s)+1)/4+1);//typeTag typeTag=msg[1].s+1; // skip ',' // bugfix 2014/8/30 if (strlen(typeTag)%2 == 0) p= msg[1].s+4*((strlen(msg[1].s)+1)/4); else p= msg[1].s+4*((strlen(msg[1].s)+1)/4+1); for(n=0; n<strlen(typeTag); n++){ c = typeTag[n]; if (('s'==c)) { msg[n+2].s=p; //p += 4*((strlen(msg[n+2].s)+1)/4+1); p += lenAlign4B(strlen(msg[n+2].s)+1); } else if (('i'==c)||('f'==c)) { #ifdef BIG_ENDIAN // no change endian (big to big) msg[n+2]._b[0]=p[0]; msg[n+2]._b[1]=p[1]; msg[n+2]._b[2]=p[2]; msg[n+2]._b[3]=p[3]; #else // change endian (big to little) msg[n+2]._b[3]=p[0]; msg[n+2]._b[2]=p[1]; msg[n+2]._b[1]=p[2]; msg[n+2]._b[0]=p[3]; #endif p +=4; } else if ('b'==c) { // get lenth of blog (copy to msg[n].blog.len) #ifdef BIG_ENDIAN // no change endian (big to big) msg[n+2]._b[0]=p[0]; msg[n+2]._b[1]=p[1]; msg[n+2]._b[2]=p[2]; msg[n+2]._b[3]=p[3]; #else // change endian (big to little) msg[n+2]._b[3]=p[0]; msg[n+2]._b[2]=p[1]; msg[n+2]._b[1]=p[2]; msg[n+2]._b[0]=p[3]; #endif p +=4; // get ponter of blog (copy to msg[n].blog.p) msg[n+2].blob.p=p; //p += 4*(msg[n+2].blob.len/4+1); p += lenAlign4B(msg[n+2].blob.len+1); } else if ('m'==c) { // get midi data (copy to msg[n].m[]) msg[n+2].m[0]=p[0]; msg[n+2].m[1]=p[1]; msg[n+2].m[2]=p[2]; msg[n+2].m[3]=p[3]; p +=4; } else { //printf("*** Not Supported TypeTag:%s ****\n",typeTag); } }; };

デモ・スケッチ

src/main.ino

// select board ////#define WIO_TERMINAL ////#define ESP8266 ////#define ESP32 ////#define M5ATOM /* OSC sender/receiver Forked for Wio-Terminal/ESP8266(ESP-WROOM-02)/ESP32 from: This sketch sends random data over UDP on a ESP32 device */ #ifdef M5ATOM #include "M5Atom.h" #define ESP32 #endif #ifdef WIO_TERMINAL //#include <AtWiFi.h> #include <rpcWiFi.h> #endif #ifdef ESP8266 #include <ESP8266WiFi.h> #endif #ifdef ESP32 #include <WiFi.h> #endif #include <WiFiUdp.h> // WiFi network name and password: const char* ssid = "your-ssid"; const char* passwd = "your-password"; //IP address to send UDP data to: const char* udpAddress = "192.168.0.18"; // target IP const int udpInPort = 8000; // incomming port const int udpOutPort = 9000; // outgoing port //<<<<<<<<<<<<<<<<<<<<< // OSC related #include "OSCmsgCodec.h" #define PACKET_SIZE 512 #define RECBUF_SIZE 256 union OSCarg msg[10], outmsg[10]; char buff[PACKET_SIZE],packet[PACKET_SIZE]; //>>>>>>>>>>>>>>>>>>>> char recbuf[128]; // buffer for incoming packets //The udp library class WiFiUDP udp; void setup() { Serial.begin(115200); Serial.println("OSC test program start..."); Serial.println(""); // delete old config WiFi.disconnect(true); Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, passwd); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" connected"); udp.begin(udpInPort); Serial.printf("Now listening at IP %s, UDP port %d\r\n", WiFi.localIP().toString().c_str(), udpInPort); } void loop() { int packetSize = udp.parsePacket(); if (packetSize>0) { Serial.printf("Received %d bytes from %s, port %d\r\n", packetSize, udp.remoteIP().toString().c_str(), udpInPort); int len = udp.read(recbuf, 128); if (len > 0) { decOSCmsg(recbuf, msg); Serial.printf("incomming OSC msg:\r\n%s %s", msg[0].address, msg[1].typeTag); for(int m=0; m < (strlen(msg[1].typeTag)-1); m++) { if (msg[1].typeTag[m+1]=='f') Serial.printf(" %f",msg[m+2].f); if (msg[1].typeTag[m+1]=='i') Serial.printf(" %d",msg[m+2].i); }; Serial.printf("\r\n"); Serial.printf("-------------------\r\n"); } } else { // test send // fader // send OSC message with random values (to touchOSC) // make OSC message for sending outmsg[0].address="/1/fader5"; outmsg[1].typeTag=",f"; outmsg[2].f= rand()%1000/1000.0; memset(packet,0,sizeof(packet)); // clear send buff for OSC msg int plen=encOSCmsg(packet,outmsg); // send it // Send a packet udp.beginPacket(udpAddress, udpOutPort); udp.write((const uint8_t*)packet, plen); udp.endPacket(); // //Serial.print("."); // indicate sending packet //Wait delay(10); // seems 'must' (for platformio) } }

wio-terminalのファームウェアのバージョン・アップにともない 以下のようにヘッダーが変更になっている:

//#include <AtWiFi.h> #include <rpcWiFi.h>

以下の#defineでボードを切り換えているがボードの種類を設定すると、 システムで設定されている定義が有効になるので特にソースを変更する必要はない。

#define WIO_TERMINAL #define ESP8266 #define ESP32 #define M5ATOM

以下については、自分の環境に合わせて変更すること:

// WiFi network name and password: const char* ssid = "your_ssid"; const char* passwd = "your_passwd"; //IP address to send UDP data to: const char* udpAddress = "192.168.0.18"; // target IP const int udpInPort = 8000; // incomming port const int udpOutPort = 9000; // outgoing port

udpAddressは、対向するOSC受信デバイス(touchOSCなど)のIPを設定する。

書き込み後に「picocom /dev/ttyACM0 -b115200」または「picocom /dev/ttyUSB0 -b115200」で通信ソフトを起動すると以下のような出力が表示される:

$ picocom /dev/ttyACM0 -b115200 ........... connected Now listening at IP 192.168.0.11, UDP port 8000

上の例ではTouchOSCから192.168.0.11:8000へOSCパッケットに送る(ようにTouchOSCを設定する)。

または、動作としては、touchOSCのsimpleレイアウトの画面で、最上行の受信アイコンがOSCを受信すると「赤」になり、fader5のfaderが送られてきたOSCの内容に従ってランダムに変化する。

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 #

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

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

wio-terminalのファームウェア切り替えたときの注意

古いファームウェアのライブラリを動かした後は、古いライブラリがキャッシュで残っていると ビルド・エラーになるので以下を実行すること:

cd project_directory rm -r .pio/libdeps/seeed_wio_terminal/* rm -r .pio/build/seeed_wio_terminal/*

参考情報

TouchOSC - Modular touch control surface for OSC & MIDI
TouchOSC - iPhone
TouchOSC - Android

OSCmsgCode lib:
https://os.mbed.com/users/xshige/code/Sparkfun_CC3000_WiFi_OSCtranceiver//raw-file/e62251d890c1/OSCmsgCodec.h
https://os.mbed.com/users/xshige/code/Sparkfun_CC3000_WiFi_OSCtranceiver//raw-file/e62251d890c1/OSCmsgCodec.cpp
https://os.mbed.com/users/xshige/code/Sparkfun_CC3000_WiFi_OSCtranceiver//file/e62251d890c1/main.cpp/

Platform/Wio Terminal Network/Wi-Fi - example sketches

Wio Terminalをはじめよう(ピン配置、データシート、回路図など)

PlatformIO Core (CLI)

old version:
Wio-TerminalでWiFiで使う(その4:OSC)

以上

続きを読む "Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)"

| | コメント (0)

2020年10月17日 (土)

XIAO/M5Atom/Wio-TerminlでCardKB(I2C)を使用する(Arduino版)

2020/10/17
初版

PlatformIO XAIO M5ATOM WIOT CardKB(I2C)

PlatformIO XAIO M5ATOM WIOT CardKB(I2C)

概要

XIAO/M5Atom/Wio-Terminlで以下のCardKB(I2C)を使用する(Arduino版)。
3つのボードのplatformがArduinoなのでソースを共通化して動かしてみる。

CardKB(M5Stack用カード型キーボードユニット)

開発ツールplatformioのインストールについては以下を参照のこと:
開発ツールPlatformIOをcliで使う(Seeeduino-XIAO版)
M5Atomを開発ツールPlatformIOで使う(M5Atom/Arduino版)
M5Atomを開発ツールPlatformIOで使う(Windows10版)
開発ツールPlatformIOをcli(comand line interface)で使う(v2:Seeeduino-Wio-Terminal/Arduino版)

接続

本モジュールはI2Cタイプなので(I2C用の)Grove-Conectorに接続する。
(XIAOは専用のshield経由で接続する)

platformio.ini

XIAO用:

; 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_xiao] platform = atmelsam board = seeed_xiao framework = arduino build_flags = -DXIAO upload_protocol = sam-ba monitor_speed = 115200 lib_ldf_mode = deep+

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:m5stick-c] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5ATOM monitor_speed = 115200 lib_deps = # use M5Atom lib 3113 # use "FastLED" 126 lib_ldf_mode = deep+

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+

デモ・スケッチ

以下のようなファイルを作成する:
src/main.ino

#ifdef M5ATOM #include "M5Atom.h" #endif //#include <Arduino.h> #include <Wire.h> #define CARDKB_ADDR 0x5F void setup(void) { Serial.begin(115200); #ifdef M5ATOM M5.begin(); // Wire.begin() must be after M5.begin() Wire.begin(26, 32); // Grove Connector of Atom Matrix (I2C GPIO Pin: G26,G32) #endif #ifdef XIAO Wire.begin(); #endif #ifdef WIO_TERMINAL Wire.begin(); #endif } void loop() { Wire.requestFrom(CARDKB_ADDR, 1); while(Wire.available()) { char c = Wire.read(); // receive a byte as characterif if (c != 0) { Serial.print(c); Serial.print("("); Serial.print(c, HEX); Serial.println(")"); } } // delay(10); }

ビルド・実行

以下の手順でビルド・実行する:
(platformio.iniは、ボードごとに切り替える)

run pio -t upload

スケッチが起動した後、
通信ソフト(「picocom /dev/ttyACM0 -b115200」または「picocom /dev/ttyUSB0 -b115200」)を起動する。
CardKBのキーを押すと、対応した文字が通信ソフトの画面に表示される。

CardKBの操作の詳細は以下を参照のこと:
CardKBドキュメント

表示例

a(61) s(73) d(64) f(66) g(67) h(68) j(6A) k(6B) l(6C) A(41) S(53) D(44) F(46) G(47) H(48) J(4A) K(4B) L(4C) ;(3B) :(3A) `(60) +(2B) -(2D) _(5F) =(3D) ?(3F)

参考情報

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

XIAO/M5Atom/Wio-Terminalでneopixelsを制御する(Arduino版)

XIAO/M5Atomで気圧センサー(HP206C)を動かす(XIAO/Arduino版、M5Atom/Arduino版)
XIAO/M5AtomでLCD240x240(SPI)を制御する((XIAO/Arduino版、M5Atom/Arduino版)

M5AtomでOLED128x128(SPI)を制御する(Arduino版)
XIAOでOLED128x128(SPI)を制御する(Arduino版)
XIAOでLCD160x80(SPI)を制御する(Arduino版)

以上

続きを読む "XIAO/M5Atom/Wio-TerminlでCardKB(I2C)を使用する(Arduino版)"

| | コメント (0)

2020年10月16日 (金)

XIAO/M5Atom/Wio-Terminalでneopixelsを制御する(Arduino版)

2020/10/16
初版

PlatformIO XAIO M5ATOM WIOT neopixels

PlatformIO XAIO M5ATOM WIOT neopixels

概要

XIAO/M5Atom/Wio-Terminalでneopixelsを制御する(Arduino版)。
XIAO/M5Atom/Wio-TerminalはplatformがArduinoなので、それを活かして3つのボードでソースを共通化して動かしてみる。

開発ツールplatformioのインストールについては以下を参照のこと:
開発ツールPlatformIOをcliで使う(Seeeduino-XIAO版)
M5Atomを開発ツールPlatformIOで使う(M5Atom/Arduino版)
M5Atomを開発ツールPlatformIOで使う(Windows10版)
開発ツールPlatformIOをcli(comand line interface)で使う(v2:Seeeduino-Wio-Terminal/Arduino版)

接続

neopixelsはGrove_Connector(GPIO用)に接続する。
(1)XIAO
XIAOのShield経由でGrove_Connectorに接続する。
(2)M5Atom
M5AtomのGrove_Connectorに接続する。
(3)Wio-Terminal
LCD画面に向かって、下部の右のGrove_Connectorに接続する。

platformio.ini

XIAO用:

; 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_xiao] platform = atmelsam board = seeed_xiao framework = arduino build_flags = -DXIAO upload_protocol = sam-ba monitor_speed = 115200 lib_deps = # Accept new functionality in a backwards compatible manner and patches adafruit/Adafruit NeoPixel @ ^1.6.0 lib_ldf_mode = deep+

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:m5stick-c] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5ATOM monitor_speed = 115200 lib_deps = # use M5Atom lib 3113 # use "FastLED" 126 # Accept new functionality in a backwards compatible manner and patches adafruit/Adafruit NeoPixel @ ^1.6.0 lib_ldf_mode = deep+

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 lib_deps = adafruit/Adafruit Zero DMA Library@^1.0.8 https://github.com/Seeed-Studio/Seeed_Arduino_SFUD/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_USBDISP/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_LCD/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_FS/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_atWiFi/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_FreeRTOS/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_atUnified/archive/master.zip https://github.com/Seeed-Studio/esp-at-lib/archive/develop.zip https://github.com/Seeed-Studio/Seeed_Arduino_mbedtls/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_atWiFiClientSecure/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_atHTTPClient/archive/master.zip https://github.com/Seeed-Studio/Seeed_Arduino_atWebServer/archive/master.zip # # Accept new functionality in a backwards compatible manner and patches adafruit/Adafruit NeoPixel @ ^1.6.0 lib_ldf_mode = deep+

Wio-Terminalにおいて「adafruit/Adafruit NeoPixel @ ^1.6.0」ライブラリ以外のライブラリは標準的なものとして登録してあるが、 実際には、今回のスケッチで使用していない。
ビルド時に毎回コンパイルされるようなので、ビルド時間短縮のために、使っていないライブラリの登録は削除してもかまわない。

デモ・スケッチ

該当スケッチをダウンロードする:

cd src wget https://raw.githubusercontent.com/adafruit/Adafruit_NeoPixel/master/examples/simple_new_operator/simple_new_operator.ino

以下のように変更して共通化する:
src/simple_new_operator.ino

#ifdef M5ATOM #include "M5Atom.h" #endif // NeoPixel Ring simple sketch (c) 2013 Shae Erisson // Released under the GPLv3 license to match the rest of the // Adafruit NeoPixel library // This sketch shows use of the "new" operator with Adafruit_NeoPixel. // It's helpful if you don't know NeoPixel settings at compile time or // just want to store this settings in EEPROM or a file on an SD card. #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> // Required for 16 MHz Adafruit Trinket #endif // Which pin on the Arduino is connected to the NeoPixels? #ifdef XIAO int pin = 2; // XIAO Sheild Connector #endif #ifdef M5ATOM int pin = 26; // Grove Connector of M5Atom #endif #ifdef WIO_TERMINAL int pin = 1; // Grove Connector(lower right) of wio-terminal #endif //int pin = 6; // On Trinket or Gemma, suggest changing this to 1 // How many NeoPixels are attached to the Arduino? int numPixels = 16; // Popular NeoPixel ring size // NeoPixel color format & data rate. See the strandtest example for // information on possible values. int pixelFormat = NEO_GRB + NEO_KHZ800; // Rather than declaring the whole NeoPixel object here, we just create // a pointer for one, which we'll then allocate later... Adafruit_NeoPixel *pixels; #define DELAYVAL 100 // Time (in milliseconds) to pause between pixels //#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels void setup() { // These lines are specifically to support the Adafruit Trinket 5V 16 MHz. // Any other board, you can remove this part (but no harm leaving it): #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif // END of Trinket-specific code. // Right about here is where we could read 'pin', 'numPixels' and/or // 'pixelFormat' from EEPROM or a file on SD or whatever. This is a simple // example and doesn't do that -- those variables are just set to fixed // values at the top of this code -- but this is where it would happen. // Then create a new NeoPixel object dynamically with these values: pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat); // Going forward from here, code works almost identically to any other // NeoPixel example, but instead of the dot operator on function calls // (e.g. pixels.begin()), we instead use pointer indirection (->) like so: pixels->begin(); // INITIALIZE NeoPixel strip object (REQUIRED) // You'll see more of this in the loop() function below. } void loop() { pixels->clear(); // Set all pixel colors to 'off' delay(500); // The first NeoPixel in a strand is #0, second is 1, all the way up // to the count of pixels minus one. for(int i=0; i<numPixels; i++) { // For each pixel... // pixels->Color() takes RGB values, from 0,0,0 up to 255,255,255 // Here we're using a moderately bright green color: pixels->setPixelColor(i, pixels->Color(0, 150, 0)); pixels->show(); // Send the updated pixel colors to the hardware. delay(DELAYVAL); // Pause before next pass through loop } }

ビルド・実行

以下の手順でビルド・実行する:
(platformio.iniは、ボードごとに切り替える)

run pio -t upload

スケッチが起動すると、neopixelsが緑色に光って伸び縮みする。

参考情報

neopixel library:
https://github.com/adafruit/Adafruit_NeoPixel

M5Stack用NeoPixel互換 LEDテープ 20 cm
HEX RGB LED Board
NeoPixel Ring - 12連フルカラーシリアルLED

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

XIAO/M5Atomで気圧センサー(HP206C)を動かす(XIAO/Arduino版、M5Atom/Arduino版)
XIAO/M5AtomでLCD240x240(SPI)を制御する((XIAO/Arduino版、M5Atom/Arduino版)

M5AtomでOLED128x128(SPI)を制御する(Arduino版)
XIAOでOLED128x128(SPI)を制御する(Arduino版)
XIAOでLCD160x80(SPI)を制御する(Arduino版)

以上

続きを読む "XIAO/M5Atom/Wio-Terminalでneopixelsを制御する(Arduino版)"

| | コメント (0)

2020年10月15日 (木)

XIAO/M5AtomでOLED128x128(I2C)を制御する((XIAO/Arduino版、M5Atom/Arduino版)

2020/10/16
wio-terminal用のplatformio.iniを追加した。

2020/10/15
初版

PlatformIO XAIO M5ATOM OLED128x128(I2C)

PlatformIO XAIO M5ATOM OLED128x128(I2C)

概要

XIAO/M5Atomで以下のOLED128x128(I2C)を制御する((XIAO/Arduino版、M5Atom/Arduino版)。
platformがArduinoなのでソースを共通化して動かしてみる。

・https://wiki.seeedstudio.com/Grove-OLED_Display_1.12inch/
・https://github.com/SeeedDocument/Seeed-WiKi/blob/master/docs/Grove-OLED_Display_1.12inch.md
本ボードは、ボードのバージョンによって、解像度が96x96または128x128であり、それぞれでコントローラも異なる。
入手したボードは、v1.1で解像度は128x128でコントローラはSH1107Gであった。(Vdd:3.3V/5V両用対応)

開発ツールplatformioのインストールについては以下を参照のこと:
開発ツールPlatformIOをcliで使う(Seeeduino-XIAO版)
M5Atomを開発ツールPlatformIOで使う(M5Atom/Arduino版)
M5Atomを開発ツールPlatformIOで使う(Windows10版)

接続

本モジュールはI2Cタイプなので(I2C用の)Grove-Conectorに接続する。
(XIAOは専用のshield経由で接続する)

platformio.ini

XIAO用:

; 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_xiao] platform = atmelsam board = seeed_xiao framework = arduino build_flags = -DXIAO upload_protocol = sam-ba monitor_speed = 115200 lib_deps = # Accept new functionality in a backwards compatible manner and patches olikraus/U8g2 @ ^2.28.7 lib_ldf_mode = deep+

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:m5stick-c] platform = espressif32 board = m5stick-c framework = arduino build_flags = -DM5ATOM monitor_speed = 115200 lib_deps = # use M5Atom lib 3113 # use "FastLED" 126 # Accept new functionality in a backwards compatible manner and patches olikraus/U8g2 @ ^2.28.7 lib_ldf_mode = deep+

デモ・スケッチ

以下のようなファイルを作成する:
src/helloworld.ino

#ifdef M5ATOM #include "M5Atom.h" #endif #include <Arduino.h> #include <U8g2lib.h> #include <U8x8lib.h> #include <Wire.h> #ifdef M5ATOM // Grove Connector of Atom Matrix (I2C GPIO Pin: G26,G32) #define SCL 32 #define SDA 26 #endif U8G2_SH1107_SEEED_128X128_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); U8X8_SH1107_SEEED_128X128_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); void setup(void) { /* NO NEED in this sketch #ifdef M5ATOM M5.begin(); // Wire.begin() must be after M5.begin() Wire.begin(26, 32); // Grove Connector of Atom Matrix (I2C GPIO Pin: G26,G32) #endif */ u8g2.begin(); u8g2.enableUTF8Print(); u8x8.begin() ; } void loop(void) { // using u8g2 lib u8g2.firstPage(); do { u8g2.setFont(u8g2_font_ncenB10_tr); u8g2.drawStr(0,12,"Hello World! #0"); u8g2.drawStr(0,24,"Hello World! #1"); u8g2.drawStr(0,36,"Hello World! #2"); u8g2.drawStr(0,48,"Hello World! #3"); u8g2.drawStr(0,60,"Hello World! #4"); u8g2.drawStr(0,72,"Hello World! #5"); u8g2.drawStr(0,84,"Hello World! #6"); u8g2.drawStr(0,96,"Hello World! #7"); u8g2.drawStr(0,108,"Hello World! #8"); u8g2.drawStr(0,120,"Hello World! #9"); u8g2.drawStr(0,132,"Hello World! #10"); } while ( u8g2.nextPage() ); delay(1000); //---------------- // kanji display u8g2.setFont(u8g2_font_b10_t_japanese1); u8g2.setFontDirection(0); u8g2.firstPage(); do { u8g2.setCursor(0, 15); u8g2.print("Hello World!"); u8g2.setCursor(0, 40); u8g2.print("こんにちは世界"); u8g2.setCursor(0, 50); u8g2.print("今日は良い天気/晴天です。"); u8g2.setCursor(0, 60); u8g2.print("明日は台風で強風でしょう。"); u8g2.setCursor(0, 70); u8g2.print("佐藤、田中、鈴木、青木、小松、増野、茅野、松岡"); } while ( u8g2.nextPage() ); delay(1000); // using u8x8 lib u8x8.setFont(u8x8_font_artossans8_r) ; u8x8.clear(); u8x8.println("Text Test") ; u8x8.println("XIAO/M5Atom\n") ; u8x8.print("OLED") ; u8x8.print("128x128(i2c)\n"); u8x8.println(millis()); u8x8.println(123.45); delay(1000); //-------------------- }

ビルド・実行

以下の手順でビルド・実行する:
(platformio.iniは、ボードごとに切り替える)

run pio -t upload

スケッチが起動すると、LCDに文字が表示される。
u8g2ライブラリ、u8x8ライブラリを交互に使用して文字を表示する。
日本語も表示するが、すべての漢字を持っているわけでないようだ。
(このデモでは人名が一部表示されない)

追加(wio-terminal用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 lib_deps = # Accept new functionality in a backwards compatible manner and patches olikraus/U8g2 @ ^2.28.7 upload_protocol = sam-ba monitor_speed = 115200 lib_ldf_mode = deep+

参考情報

U8g2 Library wiki: U8g2 is a monochrome graphics library
u8g2 reference

Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載

XIAO/M5Atomで気圧センサー(HP206C)を動かす(XIAO/Arduino版、M5Atom/Arduino版)
XIAO/M5AtomでLCD240x240(SPI)を制御する((XIAO/Arduino版、M5Atom/Arduino版)

M5AtomでOLED128x128(SPI)を制御する(Arduino版)
XIAOでOLED128x128(SPI)を制御する(Arduino版)
XIAOでLCD160x80(SPI)を制御する(Arduino版)

以上

続きを読む "XIAO/M5AtomでOLED128x128(I2C)を制御する((XIAO/Arduino版、M5Atom/Arduino版)"

| | コメント (0)