メモ > 技術 > 開発: M5Stack > プログラム例
■プログラム例
■Hello World
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.print("Hello World");
}
void loop() {
}
■文字の位置とサイズを指定
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.setCursor(40, 60);
M5.Lcd.setTextSize(3);
M5.Lcd.print("Hello World");
}
void loop() {
}
■四角形や円を描く
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.fillRect(120, 140, 80, 30, WHITE);
M5.Lcd.fillCircle(90, 80, 10, WHITE);
M5.Lcd.fillCircle(230, 80, 10, WHITE);
}
void loop() {
}
■ボタンを使用する
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.print("Started.");
}
void loop() {
M5.update();
if (M5.BtnA.wasPressed()) {
M5.Lcd.print("Button A was Pressed.");
} else if (M5.BtnA.pressedFor(1000)) {
M5.Lcd.print("Button A was Pressed for 1000.");
}
if (M5.BtnB.wasPressed()) {
M5.Lcd.print("Button B was Pressed.");
} else if (M5.BtnB.pressedFor(1000)) {
M5.Lcd.print("Button B was Pressed for 1000.");
}
if (M5.BtnC.wasPressed()) {
M5.Lcd.print("Button C was Pressed.");
} else if (M5.BtnC.pressedFor(1000)) {
M5.Lcd.print("Button C was Pressed for 1000.");
}
}
■スピーカーを鳴らす
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.print("Started.");
}
void loop() {
M5.update();
if (M5.BtnA.wasPressed()) {
M5.Speaker.tone(262, 200); // ドの音を鳴らす
} else if (M5.BtnB.wasPressed()) {
M5.Speaker.tone(330, 200); // ミの音を鳴らす
} else if (M5.BtnC.wasPressed()) {
M5.Speaker.tone(392, 200); // ソの音を鳴らす
}
}
■ヘルツ・秒数・音量を指定して音を鳴らす
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.print("Started.");
}
void loop() {
M5.update();
if (M5.BtnA.wasPressed()) {
beep(440, 1000, 2); // 440Hzの音を1秒鳴らす
} else if (M5.BtnB.wasPressed()) {
beep(460, 1000, 2); // 460Hzの音を1秒鳴らす
} else if (M5.BtnC.wasPressed()) {
beep(480, 1000, 2); // 480Hzの音を1秒鳴らす
}
}
// ヘルツ・秒数・音量を指定して音を鳴らす
void beep(int freq, int duration, uint8_t volume) {
int t = 1000000 / freq / 2;
unsigned long start = millis();
while ((millis() - start) < duration) {
dacWrite(SPEAKER_PIN, 0);
delayMicroseconds(t);
dacWrite(SPEAKER_PIN, volume);
delayMicroseconds(t);
}
dacWrite(SPEAKER_PIN, 0);
}
■Wi-Fiに接続する
※M5Stackに使われているESP32は、2.4GHz帯にのみ対応している
接続先を5Ghzにすると、エラーにはならないもののいつまで待っても接続されなかった
#include <M5Stack.h>
#include <WiFi.h>
const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";
void setup(){
M5.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print('.');
}
M5.Lcd.print("\r\nWiFi connected\r\nIP address: ");
M5.Lcd.println(WiFi.localIP());
}
void loop() {
}
■インターネットから時刻を取得する
#include <M5Stack.h>
#include <WiFi.h>
#define JST (3600L * 9)
const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";
void setup(){
M5.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print('.');
}
M5.Lcd.print("\r\nWiFi connected\r\nIP address: ");
M5.Lcd.println(WiFi.localIP());
delay(500);
M5.Lcd.setTextSize(3);
configTime(JST, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp");
}
void loop() {
struct tm tm;
if (getLocalTime(&tm)) {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(60, 80);
M5.Lcd.printf("%d/%2d/%2d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
M5.Lcd.setCursor(80, 140);
M5.Lcd.printf("%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
}
delay(1000);
}
■インターネットからテキストを取得する
#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";
const char* url = "http://example.com/example.php";
void setup(){
M5.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print('.');
}
M5.Lcd.print("\r\nWiFi connected\r\nIP address: ");
M5.Lcd.println(WiFi.localIP());
delay(500);
M5.Lcd.setTextSize(2);
}
void loop() {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String response = http.getString();
M5.Lcd.print("Response=" + response);
} else {
M5.Lcd.print("Error on HTTP request.");
}
http.end();
delay(10 * 1000);
}
■インターネットから画像を取得する
#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";
const char* url = "http://example.com/example.jpg";
uint8_t buff[64 * 1024] = { 0 };
void setup(){
M5.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print('.');
}
M5.Lcd.print("\r\nWiFi connected\r\nIP address: ");
M5.Lcd.println(WiFi.localIP());
delay(500);
}
void loop() {
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
int len = http.getSize();
if (len <= 0) {
M5.Lcd.print("Error on HTTP getSize.");
} else {
WiFiClient * stream = http.getStreamPtr();
uint8_t* p = buff;
int l = len;
while (http.connected() && (l > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int s = ((size > sizeof(buff)) ? sizeof(buff) : size);
int c = stream->readBytes(p, s);
p += c;
Serial.printf("[HTTP] read: %d\n", c);
if (l > 0) {
l -= c;
}
}
}
M5.Lcd.drawJpg(buff, len);
}
} else {
M5.Lcd.print("Error on HTTP request.");
}
http.end();
delay(60 * 1000);
}
■押したボタンに応じて、インターネットから画像を取得する
#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";
const char* targetA = "http://example.com/image1.jpg";
const char* targetB = "http://example.com/image2.jpg";
const char* targetC = "http://example.com/image3.jpg";
const char* url = "";
int count = 0;
int interval = 10 * 60;
uint8_t buff[64 * 1024] = { 0 };
void setup(){
M5.begin();
M5.Lcd.setTextSize(2);
// WiFiに接続
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print('.');
}
M5.Lcd.print("\r\nWiFi connected\r\nIP address: ");
M5.Lcd.println(WiFi.localIP());
delay(500);
// 初期表示するURL
url = targetA;
}
void loop() {
M5.update();
// ボタンに応じて画像を切り替え
if (M5.BtnA.wasPressed()) {
beep(440, 100, 2);
url = targetA;
count = 0;
} else if (M5.BtnB.wasPressed()) {
beep(440, 100, 2);
url = targetB;
count = 0;
} else if (M5.BtnC.wasPressed()) {
beep(440, 100, 2);
url = targetC;
count = 0;
}
// ウェイト処理
if (count-- <= 0) {
count = interval;
// インターネットから画像を取得して表示
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
int len = http.getSize();
if (len <= 0) {
M5.Lcd.print("Error on HTTP getSize.");
} else {
WiFiClient * stream = http.getStreamPtr();
uint8_t* p = buff;
int l = len;
while (http.connected() && (l > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int s = ((size > sizeof(buff)) ? sizeof(buff) : size);
int c = stream->readBytes(p, s);
p += c;
Serial.printf("[HTTP] read: %d\n", c);
if (l > 0) {
l -= c;
}
}
}
M5.Lcd.drawJpg(buff, len);
}
} else {
M5.Lcd.print("Error on HTTP request.");
}
http.end();
}
// 1秒待つ
delay(1000);
}
// ヘルツ・秒数・音量を指定して音を鳴らす
void beep(int freq, int duration, uint8_t volume) {
int t = 1000000 / freq / 2;
unsigned long start = millis();
while ((millis() - start) < duration) {
dacWrite(SPEAKER_PIN, 0);
delayMicroseconds(t);
dacWrite(SPEAKER_PIN, volume);
delayMicroseconds(t);
}
dacWrite(SPEAKER_PIN, 0);
}