diff --git a/SampleSrc/accgyro.ino b/SampleSrc/accgyro.ino index 4963b47..3654c27 100644 --- a/SampleSrc/accgyro.ino +++ b/SampleSrc/accgyro.ino @@ -6,10 +6,9 @@ float ax, ay, az; // 加速度データ void setup() { - M5.begin(); + M5.begin(115200); M5.Lcd.setRotation(3); M5.IMU.Init(); - Serial.begin(115200); Serial.println(""); } void loop() { @@ -26,3 +25,4 @@ delay(50); } + diff --git a/SampleSrc/analogread.ino b/SampleSrc/analogread.ino index d7b029e..e85f840 100644 --- a/SampleSrc/analogread.ino +++ b/SampleSrc/analogread.ino @@ -10,7 +10,7 @@ // 注意点: https://lang-ship.com/reference/unofficial/M5StickC/Peripherals/ADC/ void setup() { - M5.begin(); + M5.begin(115200); pinMode(PIN, ANALOG); // PINのモード設定 @@ -23,3 +23,5 @@ delay(1000); } + + diff --git a/SampleSrc/bts01_master.ino b/SampleSrc/bts01_master.ino index b758796..2770367 100644 --- a/SampleSrc/bts01_master.ino +++ b/SampleSrc/bts01_master.ino @@ -1,105 +1,105 @@ -#include -#include - -BluetoothSerial SerialBT; - -bool willConnect = true; -bool connected; - -const char* dname = "BT_Serial_00"; // Slave device name - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.fillScreen(BLUE); - - M5.Lcd.setTextColor(BLACK, CYAN); - M5.Lcd.setCursor(0, 0, 1); - M5.Lcd.setTextSize(2); - - Serial.begin(115200); - SerialBT.begin("BT_Serial_01", true); - Serial.println("The device started in master mode, make sure remote BT device is on!"); - - // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs - // to resolve name to address first, but it allows to connect to different devices with the same name. - // Set CoreDebugLevel to Info to view devices bluetooth address and device names - if (false) { - connected = SerialBT.connect(dname); - } else { - // uint8_t address[6] = {0x24, 0xA1, 0x60, 0x47, 0x84, 0x4E}; // MACアドレスが 24:A1:60:47:84:4E のとき - uint8_t address[6] = {0x94, 0xB9, 0x7e, 0xad, 0x53, 0x92}; - connected = SerialBT.connect(address); - } - if (connected) { - Serial.println("Connected Succesfully!"); - M5.Beep.tone(2000, 1000); - M5.Lcd.fillScreen(CYAN); - } - // disconnect() may take upto 10 secs max - // if (SerialBT.disconnect()) { - // connected = false; - // Serial.println("Disconnected Succesfully!"); - // } - // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address). - //SerialBT.connect(); -} - -void connectOrDisconnect(bool willCon) { - if (willCon && connected) return; - if (willCon) { - if (connected) { - Serial.println("Connected Succesfully!"); - M5.Beep.tone(2000, 1000); - M5.Lcd.fillScreen(CYAN); - } else { - while (!SerialBT.connected(10000)) { - Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); - M5.Lcd.fillScreen(PURPLE); - } - } - } else { - if ( SerialBT.disconnect() ) { - connected = false; - Serial.println("Disconnected Succesfully!"); - M5.Lcd.fillScreen(BLUE); - } - } -} -int line = 0; -void loop() { - connectOrDisconnect( willConnect ); - - char c; - if (Serial.available()) { - SerialBT.write(c = Serial.read()); - Serial.printf("read:%c\n", c); - } - if (SerialBT.available()) { - Serial.write(c = SerialBT.read()); - M5.Lcd.print(c); - if (c == 0xa) line++; - if (line > 7) { - M5.Lcd.fillScreen(CYAN); M5.Lcd.setCursor(0, 0, 1); - line = 0; - } - } - - M5.update(); M5.Beep.update(); - if (M5.BtnA.wasReleasefor(1000) ) { - SerialBT.println("[A] was Pressed longer than 1s"); - } else if (M5.BtnA.wasReleased()) { - SerialBT.println("[A] was Pressed"); - } else if (M5.BtnB.wasReleasefor(1000) ) { - SerialBT.println("[B] was Pressed longer than 1s"); - if (SerialBT.disconnect()) { - connected = false; - M5.Lcd.fillScreen(PURPLE); - } - } else if (M5.BtnB.wasReleased()) { - SerialBT.println("[B] was Pressed"); - } else if (M5.Axp.GetBtnPress() == 2) { - SerialBT.println("[Power] was Pressed"); - } - delay(20); -} +#include +#include + +BluetoothSerial SerialBT; + +bool willConnect = true; +bool connected; + +const char* dname = "BT_Serial_00"; // Slave device name + +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + M5.Lcd.fillScreen(BLUE); + + M5.Lcd.setTextColor(BLACK, CYAN); + M5.Lcd.setCursor(0, 0, 1); + M5.Lcd.setTextSize(2); + + SerialBT.begin("BT_Serial_01", true); + Serial.println("The device started in master mode, make sure remote BT device is on!"); + + // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs + // to resolve name to address first, but it allows to connect to different devices with the same name. + // Set CoreDebugLevel to Info to view devices bluetooth address and device names + if (false) { + connected = SerialBT.connect(dname); + } else { + // uint8_t address[6] = {0x24, 0xA1, 0x60, 0x47, 0x84, 0x4E}; // MACアドレスが 24:A1:60:47:84:4E のとき + uint8_t address[6] = {0x94, 0xB9, 0x7e, 0xad, 0x53, 0x92}; + connected = SerialBT.connect(address); + } + if (connected) { + Serial.println("Connected Succesfully!"); + M5.Beep.tone(2000, 1000); + M5.Lcd.fillScreen(CYAN); + } + // disconnect() may take upto 10 secs max + // if (SerialBT.disconnect()) { + // connected = false; + // Serial.println("Disconnected Succesfully!"); + // } + // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address). + //SerialBT.connect(); +} + +void connectOrDisconnect(bool willCon) { + if (willCon && connected) return; + if (willCon) { + if (connected) { + Serial.println("Connected Succesfully!"); + M5.Beep.tone(2000, 1000); + M5.Lcd.fillScreen(CYAN); + } else { + while (!SerialBT.connected(10000)) { + Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); + M5.Lcd.fillScreen(PURPLE); + } + } + } else { + if ( SerialBT.disconnect() ) { + connected = false; + Serial.println("Disconnected Succesfully!"); + M5.Lcd.fillScreen(BLUE); + } + } +} +int line = 0; +void loop() { + connectOrDisconnect( willConnect ); + + char c; + if (Serial.available()) { + SerialBT.write(c = Serial.read()); + Serial.printf("read:%c\n", c); + } + if (SerialBT.available()) { + Serial.write(c = SerialBT.read()); + M5.Lcd.print(c); + if (c == 0xa) line++; + if (line > 7) { + M5.Lcd.fillScreen(CYAN); M5.Lcd.setCursor(0, 0, 1); + line = 0; + } + } + + M5.update(); M5.Beep.update(); + if (M5.BtnA.wasReleasefor(1000) ) { + SerialBT.println("[A] was Pressed longer than 1s"); + } else if (M5.BtnA.wasReleased()) { + SerialBT.println("[A] was Pressed"); + } else if (M5.BtnB.wasReleasefor(1000) ) { + SerialBT.println("[B] was Pressed longer than 1s"); + if (SerialBT.disconnect()) { + connected = false; + M5.Lcd.fillScreen(PURPLE); + } + } else if (M5.BtnB.wasReleased()) { + SerialBT.println("[B] was Pressed"); + } else if (M5.Axp.GetBtnPress() == 2) { + SerialBT.println("[Power] was Pressed"); + } + delay(20); +} + diff --git a/SampleSrc/bts01_slave.ino b/SampleSrc/bts01_slave.ino index 01e38a8..1b8d18b 100644 --- a/SampleSrc/bts01_slave.ino +++ b/SampleSrc/bts01_slave.ino @@ -1,61 +1,61 @@ -#include -#include "BluetoothSerial.h" - -BluetoothSerial SerialBT; - -const char* dname = "BT_Serial_00"; // Slave device name - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.fillScreen(YELLOW); - M5.Lcd.setTextColor(BLACK, YELLOW); - M5.Lcd.setCursor(0, 0, 1); - M5.Lcd.setTextSize(2); - - Serial.begin(115200); - SerialBT.begin(dname); //Bluetooth device name - Serial.println("The device started, now you can pair it with bluetooth!"); - - uint8_t macBT[6]; - esp_read_mac(macBT, ESP_MAC_BT); - char mac[20]; - sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]); - Serial.println(mac); - M5.Lcd.println(mac); - -} - -int line = 0; -void loop() { - char c; - if (Serial.available()) { - SerialBT.write(c = Serial.read()); - Serial.printf("read:%c\n", c); - } - if (SerialBT.available()) { - Serial.write(c = SerialBT.read()); - SerialBT.write(c + 0); - M5.Lcd.print(c); - if (c == 0xa) line++; - if (line > 7) { - M5.Lcd.fillScreen(YELLOW); M5.Lcd.setCursor(0, 0, 1); - line = 0; - } - } - - M5.update(); - if (M5.BtnA.wasReleasefor(1000) ) { - SerialBT.println("[A] was Pressed longer than 1s"); - } else if (M5.BtnA.wasReleased()) { - SerialBT.println("[A] was Pressed"); - } else if (M5.BtnB.wasReleasefor(1000) ) { - SerialBT.println("[B] was Pressed longer than 1s"); - } else if (M5.BtnB.wasReleased()) { - SerialBT.println("[B] was Pressed"); - } else if (M5.Axp.GetBtnPress() == 2) { - SerialBT.println("[Power] was Pressed"); - } - - delay(20); -} \ No newline at end of file +#include +#include "BluetoothSerial.h" + +BluetoothSerial SerialBT; + +const char* dname = "BT_Serial_00"; // Slave device name + +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + M5.Lcd.fillScreen(YELLOW); + M5.Lcd.setTextColor(BLACK, YELLOW); + M5.Lcd.setCursor(0, 0, 1); + M5.Lcd.setTextSize(2); + + SerialBT.begin(dname); //Bluetooth device name + Serial.println("The device started, now you can pair it with bluetooth!"); + + uint8_t macBT[6]; + esp_read_mac(macBT, ESP_MAC_BT); + char mac[20]; + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]); + Serial.println(mac); + M5.Lcd.println(mac); + +} + +int line = 0; +void loop() { + char c; + if (Serial.available()) { + SerialBT.write(c = Serial.read()); + Serial.printf("read:%c\n", c); + } + if (SerialBT.available()) { + Serial.write(c = SerialBT.read()); + SerialBT.write(c + 0); + M5.Lcd.print(c); + if (c == 0xa) line++; + if (line > 7) { + M5.Lcd.fillScreen(YELLOW); M5.Lcd.setCursor(0, 0, 1); + line = 0; + } + } + + M5.update(); + if (M5.BtnA.wasReleasefor(1000) ) { + SerialBT.println("[A] was Pressed longer than 1s"); + } else if (M5.BtnA.wasReleased()) { + SerialBT.println("[A] was Pressed"); + } else if (M5.BtnB.wasReleasefor(1000) ) { + SerialBT.println("[B] was Pressed longer than 1s"); + } else if (M5.BtnB.wasReleased()) { + SerialBT.println("[B] was Pressed"); + } else if (M5.Axp.GetBtnPress() == 2) { + SerialBT.println("[Power] was Pressed"); + } + + delay(20); +} + diff --git a/SampleSrc/button01.ino b/SampleSrc/button01.ino index 5d1c552..99d3a87 100644 --- a/SampleSrc/button01.ino +++ b/SampleSrc/button01.ino @@ -1,7 +1,7 @@ #include void setup() { - M5.begin(); + M5.begin(115200); M5.Lcd.setRotation(3); } @@ -22,3 +22,4 @@ delay(10); } + diff --git a/SampleSrc/cds01.ino b/SampleSrc/cds01.ino index 539570c..8cfede4 100644 --- a/SampleSrc/cds01.ino +++ b/SampleSrc/cds01.ino @@ -1,19 +1,20 @@ -int PIN = 26; - -// +--- 15kΩ抵抗 --+-- CdSセル --+ -// | | | -// GND G26 3V3 -// https://www.storange.jp/2012/03/arduinocds.html - -// 注意点: https://lang-ship.com/reference/unofficial/M5StickC/Peripherals/ADC/ - -void setup() { - Serial.begin(115200); - pinMode(PIN, ANALOG); // PINのモード設定 - // https://lang-ship.com/blog/work/m5stickc-io/ -} - -void loop() { - Serial.printf("%04d\n", analogRead(PIN) ); // 暗 0 〜 4095 明 - delay(1000); -} \ No newline at end of file +int PIN = 26; + +// +--- 15kΩ抵抗 --+-- CdSセル --+ +// | | | +// GND G26 3V3 +// https://www.storange.jp/2012/03/arduinocds.html + +// 注意点: https://lang-ship.com/reference/unofficial/M5StickC/Peripherals/ADC/ + +void setup() { + Serial.begin(115200); //M5.beginを使わずにシリアルを使う場合 + pinMode(PIN, ANALOG); // PINのモード設定 + // https://lang-ship.com/blog/work/m5stickc-io/ +} + +void loop() { + Serial.printf("%04d\n", analogRead(PIN) ); // 暗 0 〜 4095 明 + delay(1000); +} + diff --git a/SampleSrc/do_nothing.ino b/SampleSrc/do_nothing.ino new file mode 100644 index 0000000..95c2b6e --- /dev/null +++ b/SampleSrc/do_nothing.ino @@ -0,0 +1,9 @@ +void setup() { + // put your setup code here, to run once: + +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/SampleSrc/env2.ino b/SampleSrc/env2.ino index b009203..b3d182e 100644 --- a/SampleSrc/env2.ino +++ b/SampleSrc/env2.ino @@ -1,44 +1,45 @@ -/* - Please install the < Adafruit BMP280 Library > (https://github.com/adafruit/Adafruit_BMP280_Library) - < Adafruit SHT31 Library > (https://github.com/adafruit/Adafruit_SHT31) - from the library manager before use. - This code will display the temperature, humidity and air pressure information on the screen - https://lang-ship.com/blog/work/m5stack-env-ii-unit-u001-b/ - */ -#include -#include -#include -Adafruit_SHT31 sht3x = Adafruit_SHT31(&Wire); -Adafruit_BMP280 bme = Adafruit_BMP280(&Wire); -float tmp = 0.0; -float hum = 0.0; -float pressure = 0.0; -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.setTextSize(3); - Wire.begin(32, 33); - Serial.println(F("ENV Unit(SHT30 and BMP280) test...")); - while (!bme.begin(0x76)) { - Serial.println("Could not find a valid BMP280 sensor, check wiring!"); - M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); - } - while (!sht3x.begin(0x44)) { - Serial.println("Could not find a valid SHT3X sensor, check wiring!"); - M5.Lcd.println("Could not find a valid SHT3X sensor, check wiring!"); - } - M5.Lcd.fillScreen(BLACK); - M5.Lcd.println("ENV Unit test..."); -} -void loop() { - pressure = bme.readPressure(); - tmp = sht3x.readTemperature(); - hum = sht3x.readHumidity(); - Serial.printf("Temperature: %2.2f*C Humidity: %0.2f%% Pressure: %0.2fhPa\r\n", tmp, hum, pressure / 100); - M5.Lcd.setCursor(0, 25); - M5.Lcd.setTextColor(WHITE, BLACK); - M5.Lcd.printf("Temp:%2.2f*C\n", tmp); - M5.Lcd.printf("Humi:%2.2f%%\n", hum); - M5.Lcd.printf("Prs :%2.0fhPa\n", pressure / 100); - delay(1000); -} \ No newline at end of file +/* +Please install the < Adafruit BMP280 Library > (https://github.com/adafruit/Adafruit_BMP280_Library) +< Adafruit SHT31 Library > (https://github.com/adafruit/Adafruit_SHT31) +from the library manager before use. +This code will display the temperature, humidity and air pressure information on the screen +https://lang-ship.com/blog/work/m5stack-env-ii-unit-u001-b/ +*/ +#include +#include +#include +Adafruit_SHT31 sht3x = Adafruit_SHT31(&Wire); +Adafruit_BMP280 bme = Adafruit_BMP280(&Wire); +float tmp = 0.0; +float hum = 0.0; +float pressure = 0.0; +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + M5.Lcd.setTextSize(3); + Wire.begin(32, 33); + Serial.println(F("ENV Unit(SHT30 and BMP280) test...")); + while (!bme.begin(0x76)) { + Serial.println("Could not find a valid BMP280 sensor, check wiring!"); + M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); + } + while (!sht3x.begin(0x44)) { + Serial.println("Could not find a valid SHT3X sensor, check wiring!"); + M5.Lcd.println("Could not find a valid SHT3X sensor, check wiring!"); + } + M5.Lcd.fillScreen(BLACK); + M5.Lcd.println("ENV Unit test..."); +} +void loop() { + pressure = bme.readPressure(); + tmp = sht3x.readTemperature(); + hum = sht3x.readHumidity(); + Serial.printf("Temperature: %2.2f*C Humidity: %0.2f%% Pressure: %0.2fhPa\r\n", tmp, hum, pressure / 100); + M5.Lcd.setCursor(0, 25); + M5.Lcd.setTextColor(WHITE, BLACK); + M5.Lcd.printf("Temp:%2.2f*C\n", tmp); + M5.Lcd.printf("Humi:%2.2f%%\n", hum); + M5.Lcd.printf("Prs :%2.0fhPa\n", pressure / 100); + delay(1000); +} + diff --git a/SampleSrc/espnow01.ino b/SampleSrc/espnow01.ino index 850c749..f151ce3 100644 --- a/SampleSrc/espnow01.ino +++ b/SampleSrc/espnow01.ino @@ -49,7 +49,7 @@ } void setup() { - M5.begin(); + M5.begin(115200); M5.Lcd.fillScreen(BLACK); M5.Lcd.setRotation(3); M5.Lcd.print("ESP-NOW Test\n"); @@ -124,3 +124,4 @@ delay(1); } + diff --git a/SampleSrc/espnow02.ino b/SampleSrc/espnow02.ino index f2f9147..72cc0c4 100644 --- a/SampleSrc/espnow02.ino +++ b/SampleSrc/espnow02.ino @@ -86,7 +86,7 @@ void setup() { - M5.begin(); + M5.begin(115200); M5.Lcd.setRotation(3); M5.Lcd.fillScreen(YELLOW); @@ -107,3 +107,4 @@ } delay(20); } + diff --git a/SampleSrc/espnow03.ino b/SampleSrc/espnow03.ino index 62f392c..09db83b 100644 --- a/SampleSrc/espnow03.ino +++ b/SampleSrc/espnow03.ino @@ -60,7 +60,7 @@ void setup() { - M5.begin(); + M5.begin(115200); M5.Lcd.setRotation(3); M5.Lcd.fillScreen(YELLOW); @@ -109,3 +109,4 @@ // M5.Lcd.printf("%s\n", ?????[??] ); // Print string stored at Line86 } } + diff --git a/SampleSrc/httpclient01.ino b/SampleSrc/httpclient01.ino index bef05d4..3fcdb90 100644 --- a/SampleSrc/httpclient01.ino +++ b/SampleSrc/httpclient01.ino @@ -1,33 +1,58 @@ -#include -#include // ステータスコードの定義もここにある - -const char* ssid = "miura2g"; -const char* password = "jikken2022"; - -// 天気予報API https://weather.tsukumijima.net/ から、千葉の天気を取得 -const char* weatherapi_url = "http://weather.tsukumijima.net/api/forecast/city/120010"; - -void setup() { - Serial.begin(115200); - WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { // 接続中... - delay(50); - Serial.print("."); - } - delay(1000); - - HTTPClient http; // クライアント作成 - http.begin(weatherapi_url); // HTTPでサーバに接続 - int httpCode = http.GET(); // ステータスコードを取得 - if (httpCode > 0) { - Serial.println(httpCode); - if (httpCode == HTTP_CODE_OK) { // ステータスコードが「成功」(200) なら - String payload = http.getString(); - Serial.println(payload); - } - } - http.end(); -} - -void loop() { -} \ No newline at end of file +#include +#include +#include // ステータスコードの定義もここにある + +const char* ssid = "miura2g"; +const char* password = "jikken2022"; + + +void setup() { + M5.begin(115200); + pinMode(10, OUTPUT); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { // 接続中... + delay(150); + Serial.print("."); + } + Serial.println("Connected."); + delay(2000); + get_weather(); // シリアルモニタに天気予報を書き出す +} + +int led=0; +void loop() { + M5.update(); // 各ボタンの状態を(読み取って)更新する:ボタンを判定するときは必須。 + + if (M5.BtnA.wasReleased()) { + get_weather(); // シリアルモニタに天気予報を書き出す + } + delay(150); + + led++; + if (led%2==1){ + digitalWrite(10, LOW ); // LED ON (LOW=0) + } else { + digitalWrite(10, HIGH ); // LED OFF (HIGH=1) + } +} + +void get_weather(){ + // 天気予報API https://weather.tsukumijima.net/ から、千葉の天気を取得 + const char* weatherapi_url = "http://weather.tsukumijima.net/api/forecast/city/120010"; + + HTTPClient http; // クライアント作成 + http.begin(weatherapi_url); // HTTPでサーバに接続 + int httpCode = http.GET(); // ステータスコードを取得 + if (httpCode > 0) { + Serial.println(httpCode); + if (httpCode == HTTP_CODE_OK) { // ステータスコードが「成功」(200) なら + String payload = http.getString(); + Serial.println(payload); + } + } + http.end(); + Serial.println("[A]ボタンを押すと、再度取得します。"); +} + + + diff --git a/SampleSrc/irsend02.ino b/SampleSrc/irsend02.ino index 27ad9be..caa17ad 100644 --- a/SampleSrc/irsend02.ino +++ b/SampleSrc/irsend02.ino @@ -1,81 +1,81 @@ -/* IRremoteESP8266: IRsendDemo - demonstrates sending IR codes with IRsend. - - Version 1.1 January, 2019 - Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009, - Copyright 2009 Ken Shirriff, http://arcfn.com - - An IR LED circuit *MUST* be connected to the ESP8266 on a pin - as specified by kIrLed below. - - TL;DR: The IR LED needs to be driven by a transistor for a good result. - - Suggested circuit: - https://github.com/crankyoldgit/IRremoteESP8266/wiki#ir-sending - - Common mistakes & tips: - * * Don't just connect the IR LED directly to the pin, it won't - have enough current to drive the IR LED effectively. - * * Make sure you have the IR LED polarity correct. - See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity - * * Typical digital camera/phones can be used to see if the IR LED is flashed. - Replace the IR LED with a normal LED if you don't have a digital camera - when debugging. - * * Avoid using the following pins unless you really know what you are doing: - * * Pin 0/D3: Can interfere with the boot/program mode & support circuits. - * * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere. - * * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere. - * * ESP-01 modules are tricky. We suggest you use a module with more GPIOs - for your first time. e.g. ESP-12 etc. -*/ -#include -#include -#include - -const uint16_t kIrLed = 9; // 赤外LEDが接続されたピン番号 - -IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message. - -// Example of data captured by IRrecvDumpV2.ino -uint16_t photoshot[99] = {3488, 1740, 438, 424, 438, 1302, 440, 422, 438, 424, 440, 422, 440, 422, - 440, 422, 438, 424, 438, 424, 438, 424, 438, 424, 438, 426, 438, 424, - 438, 1302, 440, 422, 442, 422, 440, 424, 438, 424, 438, 424, - 442, 420, 438, 1304, 438, 1304, 440, 1302, 438, 424, 438, 424, 438, 424, - 438, 424, 438, 1302, 440, 424, 440, 1302, 440, 422, 438, 424, 440, 422, - 440, 1302, 438, 1304, 438, 424, 438, 424, 438, 1304, 438, 1302, 438, 424, - 438, 424, 462, 1278, 464, 1278, 464, 1278, 462, 1280, 460, 1280, 464, 398, - 464, 398, 438 - }; // PANASONIC 40040E14667C - -uint16_t recording[99] = {3490, 1738, 440, 424, 438, 1304, 438, 424, 438, 424, 438, 424, 438, 424, - 438, 424, 438, 424, 438, 424, 438, 424, 438, 424, 438, 424, 438, 424, - 438, 1302, 440, 424, 442, 422, 438, 424, 438, 424, 438, 424, 438, 424, - 438, 1302, 442, 1302, 438, 1302, 440, 424, 440, 424, 438, 424, 438, 424, - 438, 1302, 442, 420, 442, 1302, 438, 424, 438, 422, 442, 422, 438, 424, - 440, 1302, 438, 1302, 438, 424, 438, 424, 438, 1302, 442, 1300, 440, 424, - 440, 422, 440, 1302, 440, 424, 462, 1278, 464, 400, 464, 398, 464, 1278, 464 - }; // PANASONIC 40040E143329 - -void setup() { - M5.begin(); - Serial.begin(115200); - M5.Lcd.fillScreen(BLUE); - irsend.begin(); -} - -void loop() { - // Serial.println("NEC"); // irsend.sendNEC(0x00FFE01FUL); - // Serial.println("Sony"); // irsend.sendSony(0xa90, 12, 2); // 12 bits & 2 repeats - M5.update(); - if (M5.BtnA.wasReleased()) { - irsend.sendRaw(photoshot, 99, 38); // Send a raw data capture at 38kHz. - M5.Lcd.fillScreen(YELLOW); - } else if (M5.BtnB.wasReleased()) { - irsend.sendRaw(recording, 99, 38); // Send a raw data capture at 38kHz. - M5.Lcd.fillScreen(RED); - } - delay(150); - M5.Lcd.fillScreen(BLUE); - // Serial.println("a Samsung A/C state from IRrecvDumpV2"); - // irsend.sendSamsungAC(samsungState); - // delay(2000); -} \ No newline at end of file +/* IRremoteESP8266: IRsendDemo - demonstrates sending IR codes with IRsend. + + Version 1.1 January, 2019 + Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009, + Copyright 2009 Ken Shirriff, http://arcfn.com + + An IR LED circuit *MUST* be connected to the ESP8266 on a pin + as specified by kIrLed below. + + TL;DR: The IR LED needs to be driven by a transistor for a good result. + + Suggested circuit: + https://github.com/crankyoldgit/IRremoteESP8266/wiki#ir-sending + + Common mistakes & tips: + * * Don't just connect the IR LED directly to the pin, it won't + have enough current to drive the IR LED effectively. + * * Make sure you have the IR LED polarity correct. + See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity + * * Typical digital camera/phones can be used to see if the IR LED is flashed. + Replace the IR LED with a normal LED if you don't have a digital camera + when debugging. + * * Avoid using the following pins unless you really know what you are doing: + * * Pin 0/D3: Can interfere with the boot/program mode & support circuits. + * * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere. + * * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere. + * * ESP-01 modules are tricky. We suggest you use a module with more GPIOs + for your first time. e.g. ESP-12 etc. +*/ +#include +#include +#include + +const uint16_t kIrLed = 9; // 赤外LEDが接続されたピン番号 + +IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message. + +// Example of data captured by IRrecvDumpV2.ino +uint16_t photoshot[99] = {3488, 1740, 438, 424, 438, 1302, 440, 422, 438, 424, 440, 422, 440, 422, + 440, 422, 438, 424, 438, 424, 438, 424, 438, 424, 438, 426, 438, 424, + 438, 1302, 440, 422, 442, 422, 440, 424, 438, 424, 438, 424, + 442, 420, 438, 1304, 438, 1304, 440, 1302, 438, 424, 438, 424, 438, 424, + 438, 424, 438, 1302, 440, 424, 440, 1302, 440, 422, 438, 424, 440, 422, + 440, 1302, 438, 1304, 438, 424, 438, 424, 438, 1304, 438, 1302, 438, 424, + 438, 424, 462, 1278, 464, 1278, 464, 1278, 462, 1280, 460, 1280, 464, 398, + 464, 398, 438 + }; // PANASONIC 40040E14667C + +uint16_t recording[99] = {3490, 1738, 440, 424, 438, 1304, 438, 424, 438, 424, 438, 424, 438, 424, + 438, 424, 438, 424, 438, 424, 438, 424, 438, 424, 438, 424, 438, 424, + 438, 1302, 440, 424, 442, 422, 438, 424, 438, 424, 438, 424, 438, 424, + 438, 1302, 442, 1302, 438, 1302, 440, 424, 440, 424, 438, 424, 438, 424, + 438, 1302, 442, 420, 442, 1302, 438, 424, 438, 422, 442, 422, 438, 424, + 440, 1302, 438, 1302, 438, 424, 438, 424, 438, 1302, 442, 1300, 440, 424, + 440, 422, 440, 1302, 440, 424, 462, 1278, 464, 400, 464, 398, 464, 1278, 464 + }; // PANASONIC 40040E143329 + +void setup() { + M5.begin(115200); + M5.Lcd.fillScreen(BLUE); + irsend.begin(); +} + +void loop() { + // Serial.println("NEC"); // irsend.sendNEC(0x00FFE01FUL); + // Serial.println("Sony"); // irsend.sendSony(0xa90, 12, 2); // 12 bits & 2 repeats + M5.update(); + if (M5.BtnA.wasReleased()) { + irsend.sendRaw(photoshot, 99, 38); // Send a raw data capture at 38kHz. + M5.Lcd.fillScreen(YELLOW); + } else if (M5.BtnB.wasReleased()) { + irsend.sendRaw(recording, 99, 38); // Send a raw data capture at 38kHz. + M5.Lcd.fillScreen(RED); + } + delay(150); + M5.Lcd.fillScreen(BLUE); + // Serial.println("a Samsung A/C state from IRrecvDumpV2"); + // irsend.sendSamsungAC(samsungState); + // delay(2000); +} + diff --git a/SampleSrc/lcd01.ino b/SampleSrc/lcd01.ino index 123ecda..7d04037 100644 --- a/SampleSrc/lcd01.ino +++ b/SampleSrc/lcd01.ino @@ -1,35 +1,36 @@ -#include - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.fillRect(0, 0, 240, 135, RED); - //M5StickCPlusの画面サイズは、240 x 135 - - M5.Lcd.fillRect(10, 10, 220, 115, ORANGE); - M5.Lcd.fillRect(20, 20, 200, 95, YELLOW); - M5.Lcd.fillRect(30, 30, 180, 75, GREENYELLOW); - M5.Lcd.fillRect(40, 40, 160, 55, CYAN); - M5.Lcd.fillRect(50, 50, 140, 35, BLUE); - M5.Lcd.fillRect(60, 60, 120, 15, MAGENTA); - - M5.Lcd.setTextFont(1); // 8pixel ASCII font - M5.Lcd.setTextSize(1); // Magnify (x1-7) - M5.Lcd.setTextColor(WHITE); - M5.Lcd.setCursor(10,1); M5.Lcd.print("RED"); - M5.Lcd.setTextColor(BLACK); - M5.Lcd.setCursor(20,11); M5.Lcd.print("ORANGE"); - M5.Lcd.setCursor(30,21); M5.Lcd.print("YELLOW"); - M5.Lcd.setCursor(40,31); M5.Lcd.print("GREENYELLOW"); - M5.Lcd.setCursor(50,41); M5.Lcd.print("CYAN"); - M5.Lcd.setTextColor(WHITE); - M5.Lcd.setCursor(60,51); M5.Lcd.print("BLUE"); - M5.Lcd.setCursor(70,60,2); M5.Lcd.print("MAGENTA (2)"); - // setCursorの第3引数は、TextFont番号 - - M5.Lcd.setTextColor(BLACK); - M5.Lcd.setCursor(30,90,4); M5.Lcd.print("26pixel ASCII (4)"); -} - -void loop() { -} \ No newline at end of file +#include + +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + M5.Lcd.fillRect(0, 0, 240, 135, RED); + //M5StickCPlusの画面サイズは、240 x 135 + + M5.Lcd.fillRect(10, 10, 220, 115, ORANGE); + M5.Lcd.fillRect(20, 20, 200, 95, YELLOW); + M5.Lcd.fillRect(30, 30, 180, 75, GREENYELLOW); + M5.Lcd.fillRect(40, 40, 160, 55, CYAN); + M5.Lcd.fillRect(50, 50, 140, 35, BLUE); + M5.Lcd.fillRect(60, 60, 120, 15, MAGENTA); + + M5.Lcd.setTextFont(1); // 8pixel ASCII font + M5.Lcd.setTextSize(1); // Magnify (x1-7) + M5.Lcd.setTextColor(WHITE); + M5.Lcd.setCursor(10,1); M5.Lcd.print("RED"); + M5.Lcd.setTextColor(BLACK); + M5.Lcd.setCursor(20,11); M5.Lcd.print("ORANGE"); + M5.Lcd.setCursor(30,21); M5.Lcd.print("YELLOW"); + M5.Lcd.setCursor(40,31); M5.Lcd.print("GREENYELLOW"); + M5.Lcd.setCursor(50,41); M5.Lcd.print("CYAN"); + M5.Lcd.setTextColor(WHITE); + M5.Lcd.setCursor(60,51); M5.Lcd.print("BLUE"); + M5.Lcd.setCursor(70,60,2); M5.Lcd.print("MAGENTA (2)"); + // setCursorの第3引数は、TextFont番号 + + M5.Lcd.setTextColor(BLACK); + M5.Lcd.setCursor(30,90,4); M5.Lcd.print("26pixel ASCII (4)"); +} + +void loop() { +} + diff --git a/SampleSrc/mqtt01pub.ino b/SampleSrc/mqtt01pub.ino index aee91ff..7d7054c 100644 --- a/SampleSrc/mqtt01pub.ino +++ b/SampleSrc/mqtt01pub.ino @@ -1,70 +1,71 @@ -// MQTT Publisher example -//#include -#include -// ライブラリで PubSubClient をいれておく ★★★ 重要 ★★★ -#include - -const char* ssid = "miura2g"; -const char* password = "jikken2022"; - -const char* server = "192.168.11.11"; // "mqtt.istlab.info"; -const int port = 1883; // 注:学内ネットワークはポート制限あり。 -const char* pubTopic = "office/temp"; //"ex1/groupXX/sensor";のようにして、他の班とかぶらないようにすること。 -const char* mquser = "ex1"; -//const char* mqpass = "***PASSWORD***"; // 講義システム(三浦担当講義用)を参照すること。 -const char* mqpass = ""; // 講義システム(三浦担当講義用)を参照すること。 -WiFiClient wifiClient; -char* clientid = "m5stickc01_00000001"; //デバイス個別に設定すればなんでもよい -PubSubClient mqttClient(wifiClient); // MQTT Client - -void setup() { - // M5.begin(); - Serial.begin(115200); - WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { // 接続中... - Serial.print("."); - delay(200); - } - Serial.println( WiFi.localIP().toString() ); //取得したIPアドレス - // 参考:WiFiデバイスのMACアドレスを取得し、clientid として用いる - // (18行目のclientidの定義を変更し、char clientid[20]とするのがのぞましい) - // uint8_t mac[6]; - // esp_read_mac(mac, ESP_MAC_WIFI_STA); - // sprintf(clientid, "%02X:%02X:%02X:%02X:%02X:%02X", - // mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); - mqttClient.setServer(server, port); - - if (!mqttClient.connected()) { - Serial.println("Try (re)connecting..."); - reconnect(); - } -} - -void loop() { - // シリアルコンソールから書き込みがあれば, publishする - byte mbuf[100]; int pos = 0; - while (Serial.available()) { // ノンブロッキング - mbuf[pos] = Serial.read(); - pos++; - } - if (pos > 0) { - mbuf[pos-1] = 0; //改行をNULLに置き換える - Serial.println((char*)mbuf); - // ブローカにデータを送信する。最後の false を true にすると、retained になる。 - bool ret = mqttClient.publish(pubTopic, mbuf, pos-1, false); - if (!ret){ - Serial.println("publish failed."); - } - } - delay(10); -} - -void reconnect() { - while (!mqttClient.connected()) { - if (mqttClient.connect(clientid, mquser, mqpass)) { - Serial.println("Connected to MQTT Broker."); - } else { - Serial.printf("Connect Failed. state=%d", mqttClient.state()); - } - } -} +// MQTT Publisher example +//#include +#include +// ライブラリで PubSubClient をいれておく ★★★ 重要 ★★★ +#include + +const char* ssid = "miura2g"; +const char* password = "jikken2022"; + +const char* server = "192.168.11.11"; // "mqtt.istlab.info"; +const int port = 1883; // 注:学内ネットワークはポート制限あり。 +const char* pubTopic = "office/temp"; //"ex1/groupXX/sensor";のようにして、他の班とかぶらないようにすること。 +const char* mquser = "ex1"; +//const char* mqpass = "***PASSWORD***"; // 講義システム(三浦担当講義用)を参照すること。 +const char* mqpass = ""; // 講義システム(三浦担当講義用)を参照すること。 +WiFiClient wifiClient; +char* clientid = "m5stickc01_00000001"; //デバイス個別に設定すればなんでもよい +PubSubClient mqttClient(wifiClient); // MQTT Client + +void setup() { + // M5.begin(); + Serial.begin(115200); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { // 接続中... + Serial.print("."); + delay(200); + } + Serial.println( WiFi.localIP().toString() ); //取得したIPアドレス + // 参考:WiFiデバイスのMACアドレスを取得し、clientid として用いる + // (18行目のclientidの定義を変更し、char clientid[20]とするのがのぞましい) + // uint8_t mac[6]; + // esp_read_mac(mac, ESP_MAC_WIFI_STA); + // sprintf(clientid, "%02X:%02X:%02X:%02X:%02X:%02X", + // mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + mqttClient.setServer(server, port); + + if (!mqttClient.connected()) { + Serial.println("Try (re)connecting..."); + reconnect(); + } +} + +void loop() { + // シリアルコンソールから書き込みがあれば, publishする + byte mbuf[100]; int pos = 0; + while (Serial.available()) { // ノンブロッキング + mbuf[pos] = Serial.read(); + pos++; + } + if (pos > 0) { + mbuf[pos-1] = 0; //改行をNULLに置き換える + Serial.println((char*)mbuf); + // ブローカにデータを送信する。最後の false を true にすると、retained になる。 + bool ret = mqttClient.publish(pubTopic, mbuf, pos-1, false); + if (!ret){ + Serial.println("publish failed."); + } + } + delay(10); +} + +void reconnect() { + while (!mqttClient.connected()) { + if (mqttClient.connect(clientid, mquser, mqpass)) { + Serial.println("Connected to MQTT Broker."); + } else { + Serial.printf("Connect Failed. state=%d", mqttClient.state()); + } + } +} + diff --git a/SampleSrc/mqtt01sub.ino b/SampleSrc/mqtt01sub.ino index 6852c61..5504f02 100644 --- a/SampleSrc/mqtt01sub.ino +++ b/SampleSrc/mqtt01sub.ino @@ -1,82 +1,83 @@ -// MQTT Subscriber example -// #include -#include -// ライブラリで PubSubClient をいれておく ★★★ 重要 ★★★ -#include -// ライブラリで ArduinoJson v6.xをいれておく ★★★ 重要 ★★★ -// (注意:Arduino_Json v0.1.0 は別物) -#include - -const char* ssid = "miura2g"; -const char* password = "jikken2022"; - -const char* server = "192.168.11.11"; // "mqtt.istlab.info"; -const int port = 1883; // 注:学内ネットワークはポート制限あり。 -const char* pubTopic = "office/temp"; // 例: ex1/groupXX/sensor -const char* mquser = "ex1"; -const char* mqpass = "***PASSWORD***"; -WiFiClient wifiClient; -char* clientid = "m5stickc01_00000001"; //デバイス個別に設定すればなんでもよい -PubSubClient mqttClient(wifiClient); // MQTT Client - -void setup() { -// M5.begin(); - Serial.begin(115200); - WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { // 接続中... - Serial.print("."); - delay(200); - } - Serial.println( WiFi.localIP().toString() ); //取得したIPアドレス - // 参考:WiFiデバイスのMACアドレスを取得し、clientid として用いる - // (18行目のclientidの定義を変更し、char clientid[20]とするのがのぞましい) - // uint8_t mac[6]; - // esp_read_mac(mac, ESP_MAC_WIFI_STA); - // sprintf(clientid, "%02X:%02X:%02X:%02X:%02X:%02X", - // mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); - mqttClient.setServer(server, port); - mqttClient.setCallback(callback_on_subscribe); - - if (!mqttClient.connected()) { - Serial.println("Try (re)connecting..."); - reconnect(); - } - mqttClient.subscribe(pubTopic); - -} - -void loop() { - - mqttClient.loop(); // データがpublishされたら、callback_on_subscribe が呼ばれる - - delay(100); -} - -void reconnect() { - while (!mqttClient.connected()) { - if (mqttClient.connect(clientid, mquser, mqpass)) { - Serial.println("Connected to MQTT Broker."); - } else { - Serial.printf("Connect Failed. state=%d", mqttClient.state()); - } - } -} - -// データがPublishされたら、ここが実行される。(39行目でコールバック関数を設定しているため) -void callback_on_subscribe(char* topic, byte* payload, unsigned int len) { - char buf[100]; - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.print("] "); - for (int i = 0; i < len ; i++) { - buf[i] = (char)payload[i]; - } - buf[len] = 0; - Serial.println(buf); - return; - // 参考:JSON Parsing example - // StaticJsonDocument<200> sjdoc; - // deserializeJson(sjdoc, buf); - // int intval = sjdoc["intval"]; - // const char* str = sjdoc["string"]; -} \ No newline at end of file +// MQTT Subscriber example +// #include +#include +// ライブラリで PubSubClient をいれておく ★★★ 重要 ★★★ +#include +// ライブラリで ArduinoJson v6.xをいれておく ★★★ 重要 ★★★ +// (注意:Arduino_Json v0.1.0 は別物) +#include + +const char* ssid = "miura2g"; +const char* password = "jikken2022"; + +const char* server = "192.168.11.11"; // "mqtt.istlab.info"; +const int port = 1883; // 注:学内ネットワークはポート制限あり。 +const char* pubTopic = "office/temp"; // 例: ex1/groupXX/sensor +const char* mquser = "ex1"; +const char* mqpass = "***PASSWORD***"; +WiFiClient wifiClient; +char* clientid = "m5stickc01_00000001"; //デバイス個別に設定すればなんでもよい +PubSubClient mqttClient(wifiClient); // MQTT Client + +void setup() { +// M5.begin(); + Serial.begin(115200); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { // 接続中... + Serial.print("."); + delay(200); + } + Serial.println( WiFi.localIP().toString() ); //取得したIPアドレス + // 参考:WiFiデバイスのMACアドレスを取得し、clientid として用いる + // (18行目のclientidの定義を変更し、char clientid[20]とするのがのぞましい) + // uint8_t mac[6]; + // esp_read_mac(mac, ESP_MAC_WIFI_STA); + // sprintf(clientid, "%02X:%02X:%02X:%02X:%02X:%02X", + // mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + mqttClient.setServer(server, port); + mqttClient.setCallback(callback_on_subscribe); + + if (!mqttClient.connected()) { + Serial.println("Try (re)connecting..."); + reconnect(); + } + mqttClient.subscribe(pubTopic); + +} + +void loop() { + + mqttClient.loop(); // データがpublishされたら、callback_on_subscribe が呼ばれる + + delay(100); +} + +void reconnect() { + while (!mqttClient.connected()) { + if (mqttClient.connect(clientid, mquser, mqpass)) { + Serial.println("Connected to MQTT Broker."); + } else { + Serial.printf("Connect Failed. state=%d", mqttClient.state()); + } + } +} + +// データがPublishされたら、ここが実行される。(39行目でコールバック関数を設定しているため) +void callback_on_subscribe(char* topic, byte* payload, unsigned int len) { + char buf[100]; + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i = 0; i < len ; i++) { + buf[i] = (char)payload[i]; + } + buf[len] = 0; + Serial.println(buf); + return; + // 参考:JSON Parsing example + // StaticJsonDocument<200> sjdoc; + // deserializeJson(sjdoc, buf); + // int intval = sjdoc["intval"]; + // const char* str = sjdoc["string"]; +} + diff --git a/SampleSrc/ntp01.ino b/SampleSrc/ntp01.ino index f8c8868..ccda88a 100644 --- a/SampleSrc/ntp01.ino +++ b/SampleSrc/ntp01.ino @@ -1,31 +1,33 @@ -#include - -const char* ssid = "miura2g"; -const char* password = "jikken2022"; - -void setup() { - Serial.begin(115200); - WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { // 接続中... - Serial.print("."); - } -// char* ntpserver = "ntp.nict.jp"; // 学内のNetworkを利用するときは、10.64.7.184 にする - char* ntpserver = "192.168.11.11"; // 学内のNetworkを利用するときは、10.64.7.184 にする - configTime(9 * 3600, 0, ntpserver);//GMTとの時差(秒) が9*3600, サマータイムで進める時間(秒)が0 -} - -void loop() { - struct tm localTime; - char buf[30]; - getLocalTime(&localTime); - sprintf(buf, ">> %04d/%02d/%02d %02d:%02d:%02d", - localTime.tm_year + 1900, - localTime.tm_mon + 1, - localTime.tm_mday, - localTime.tm_hour, - localTime.tm_min, - localTime.tm_sec - ); - Serial.println(buf); - delay(1000); -} \ No newline at end of file +#include + +const char* ssid = "miura2g"; +const char* password = "jikken2022"; + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { // 接続中... + Serial.print("."); + delay(150); + } + char* ntpserver = "ntp.nict.jp"; // 学内のNetworkを利用するときは、10.64.7.184 にする + // char* ntpserver = "192.168.11.11"; // 学内のNetworkを利用するときは、10.64.7.184 にする + configTime(9 * 3600, 0, ntpserver);//GMTとの時差(秒) が9*3600, サマータイムで進める時間(秒)が0 +} + +void loop() { + struct tm localTime; + char buf[30]; + getLocalTime(&localTime); + sprintf(buf, ">> %04d/%02d/%02d %02d:%02d:%02d", + localTime.tm_year + 1900, + localTime.tm_mon + 1, + localTime.tm_mday, + localTime.tm_hour, + localTime.tm_min, + localTime.tm_sec + ); + Serial.println(buf); + delay(1000); +} + diff --git a/SampleSrc/pref01.ino b/SampleSrc/pref01.ino index a86d7cc..3805932 100644 --- a/SampleSrc/pref01.ino +++ b/SampleSrc/pref01.ino @@ -1,55 +1,56 @@ -#include -#include - -Preferences pref; - -int count = 0; -void setup() { - M5.begin(); - M5.Lcd.setRotation(0); //縦型 - M5.Lcd.fillScreen(CYAN); - M5.Lcd.setTextSize(2); - loadCount(&count); - M5.Beep.tone(2000, 500); - M5.Lcd.printf("count = %d\n", count); -} - -void loop() { - M5.Beep.update(); - M5.update(); - if (M5.BtnA.wasReleasefor(1000) ) { // Aボタン長押し - count = 0; - saveCount(&count); - M5.Beep.tone(1200, 300); - M5.Lcd.printf("count = %d\n", count); - } else if (M5.BtnA.wasReleased()) { // Aボタン押し - count++ ; - saveCount(&count); - M5.Beep.tone(2000, 300); - M5.Lcd.printf("count = %d\n", count); - } else if (M5.BtnB.wasReleased()) { // Bボタン押し - reboot(); - } else if (M5.Axp.GetBtnPress() == 2) { // 電源ボタン押し - poweroff(); - } - delay(50); -} - -void loadCount(int *c) { - pref.begin("mydata", false); - *c = pref.getInt("count"); - pref.end(); -} -void saveCount(int *c) { - pref.begin("mydata", false); - pref.putInt("count", *c); - pref.end(); -} - -void poweroff() { - M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80);//PowerOff -} - -void reboot() { - ESP.restart(); -} \ No newline at end of file +#include +#include + +Preferences pref; + +int count = 0; +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(0); //縦型 + M5.Lcd.fillScreen(CYAN); + M5.Lcd.setTextSize(2); + loadCount(&count); + M5.Beep.tone(2000, 500); + M5.Lcd.printf("count = %d\n", count); +} + +void loop() { + M5.Beep.update(); + M5.update(); + if (M5.BtnA.wasReleasefor(1000) ) { // Aボタン長押し + count = 0; + saveCount(&count); + M5.Beep.tone(1200, 300); + M5.Lcd.printf("count = %d\n", count); + } else if (M5.BtnA.wasReleased()) { // Aボタン押し + count++ ; + saveCount(&count); + M5.Beep.tone(2000, 300); + M5.Lcd.printf("count = %d\n", count); + } else if (M5.BtnB.wasReleased()) { // Bボタン押し + reboot(); + } else if (M5.Axp.GetBtnPress() == 2) { // 電源ボタン押し + poweroff(); + } + delay(50); +} + +void loadCount(int *c) { + pref.begin("mydata", false); + *c = pref.getInt("count"); + pref.end(); +} +void saveCount(int *c) { + pref.begin("mydata", false); + pref.putInt("count", *c); + pref.end(); +} + +void poweroff() { + M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80);//PowerOff +} + +void reboot() { + ESP.restart(); +} + diff --git a/SampleSrc/rtc01.ino b/SampleSrc/rtc01.ino index b3b6d92..53b3b18 100644 --- a/SampleSrc/rtc01.ino +++ b/SampleSrc/rtc01.ino @@ -1,64 +1,65 @@ -#include -#include -// #include "time.h" - -#define USE_NTP 1 // NTPからRTCに時刻設定するなら1 - -void setRTCfromLT(struct tm lt) { - RTC_DateTypeDef DateStruct; - DateStruct.Year = lt.tm_year + 1900; - DateStruct.Month = lt.tm_mon + 1; - DateStruct.Date = lt.tm_mday; - DateStruct.WeekDay = lt.tm_wday; - M5.Rtc.SetData(&DateStruct); - - RTC_TimeTypeDef TimeStruct; - TimeStruct.Hours = lt.tm_hour; - TimeStruct.Minutes = lt.tm_min; - TimeStruct.Seconds = lt.tm_sec+1; - M5.Rtc.SetTime(&TimeStruct); -} - -void getRTC(char* buf) { - RTC_DateTypeDef DateStruct; - RTC_TimeTypeDef TimeStruct; - M5.Rtc.GetData(&DateStruct); - M5.Rtc.GetTime(&TimeStruct); - sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", - DateStruct.Year, DateStruct.Month, DateStruct.Date, - TimeStruct.Hours, TimeStruct.Minutes, TimeStruct.Seconds - ); -} - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - Serial.begin(115200); - - if (USE_NTP) { - const char* ssid = "miura2g"; - const char* password = "jikken2022"; - const char* ntpserver = "192.168.11.11"; // or ntp.nict.jp"; // or 10.64.7.184 for CIT-ap1x - WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { // 接続中... - Serial.print("."); - } - configTime(9 * 3600, 0, ntpserver); - - struct tm localTime; - while (localTime.tm_year < 80) { - getLocalTime(&localTime); delay(50); - } - setRTCfromLT(localTime); - } -} - -void loop() { - char buf[30]; - getRTC(buf); // bufに、日時文字列を書き込む - Serial.println(buf); - M5.Lcd.fillScreen(BLUE); - M5.Lcd.setCursor(0, 50, 4); - M5.Lcd.println(buf); - delay(1000); -} \ No newline at end of file +#include +#include +// #include "time.h" + +#define USE_NTP 1 // NTPからRTCに時刻設定するなら1 + +void setRTCfromLT(struct tm lt) { + RTC_DateTypeDef DateStruct; + DateStruct.Year = lt.tm_year + 1900; + DateStruct.Month = lt.tm_mon + 1; + DateStruct.Date = lt.tm_mday; + DateStruct.WeekDay = lt.tm_wday; + M5.Rtc.SetData(&DateStruct); + + RTC_TimeTypeDef TimeStruct; + TimeStruct.Hours = lt.tm_hour; + TimeStruct.Minutes = lt.tm_min; + TimeStruct.Seconds = lt.tm_sec+1; + M5.Rtc.SetTime(&TimeStruct); +} + +void getRTC(char* buf) { + RTC_DateTypeDef DateStruct; + RTC_TimeTypeDef TimeStruct; + M5.Rtc.GetData(&DateStruct); + M5.Rtc.GetTime(&TimeStruct); + sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", + DateStruct.Year, DateStruct.Month, DateStruct.Date, + TimeStruct.Hours, TimeStruct.Minutes, TimeStruct.Seconds + ); +} + +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + + if (USE_NTP) { + const char* ssid = "miura2g"; + const char* password = "jikken2022"; + const char* ntpserver = "192.168.11.11"; // or ntp.nict.jp"; // or 10.64.7.184 for CIT-ap1x + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { // 接続中... + Serial.print("."); + delay(250); + } + configTime(9 * 3600, 0, ntpserver); + + struct tm localTime; + while (localTime.tm_year < 80) { + getLocalTime(&localTime); delay(50); + } + setRTCfromLT(localTime); + } +} + +void loop() { + char buf[30]; + getRTC(buf); // bufに、日時文字列を書き込む + Serial.println(buf); + M5.Lcd.fillScreen(BLUE); + M5.Lcd.setCursor(0, 50, 4); + M5.Lcd.println(buf); + delay(1000); +} + diff --git a/SampleSrc/serial03.ino b/SampleSrc/serial03.ino index a7300d1..b358a72 100644 --- a/SampleSrc/serial03.ino +++ b/SampleSrc/serial03.ino @@ -18,13 +18,13 @@ void setup() { - M5.begin(); // M5StickCPlusの初期化処理 + M5.begin(115200); // M5StickCPlusの初期化処理 M5.Lcd.setRotation(3); // Aボタンが左側になる向きで画面を使用するよう設定 M5.Lcd.fillScreen(BLACK); // 画面を黒く塗りつぶす M5.Lcd.setCursor(0, 0, 2); // 左から0,上から0ピクセルの位置にフォントサイズ2の文字を出力するよう設定 M5.Lcd.println("Serial Input Test"); M5.Lcd.println("--------------------------------------"); - Serial.begin(115200); // シリアル通信を開始する +// Serial.begin(115200); // シリアル通信を開始する } @@ -60,3 +60,4 @@ digitalWrite(PIN, 1); // HIGH = 1, LOW = 0 vTaskDelete(NULL); // 自タスクを削除する } + diff --git a/SampleSrc/sketch_feb25a.ino b/SampleSrc/sketch_feb25a.ino deleted file mode 100644 index 95c2b6e..0000000 --- a/SampleSrc/sketch_feb25a.ino +++ /dev/null @@ -1,9 +0,0 @@ -void setup() { - // put your setup code here, to run once: - -} - -void loop() { - // put your main code here, to run repeatedly: - -} diff --git a/SampleSrc/sound01.ino b/SampleSrc/sound01.ino index f468876..9bed73c 100644 --- a/SampleSrc/sound01.ino +++ b/SampleSrc/sound01.ino @@ -1,34 +1,35 @@ -#include - -int f[28] = { 262, 294, 330, 349, 392, 440, 494 }; // 配列のサイズは4オクターブ分 -void setup() { - Serial.begin(115200); - M5.begin(); - M5.Lcd.setRotation(3); - for (int i = 0; i < 7; i++) { - f[i + 7] = f[i] * 2; - f[i + 14] = f[i] * 4; - f[i + 21] = f[i] * 8; - } - // setBeep( Hz, ms) でビープ音を設定して、beep()で鳴らす。 - M5.Beep.setBeep(523, 500); M5.Beep.beep(); delay(500); - // tone( Hz, ms ) で鳴らす方法 - M5.Beep.tone(1046, 500); delay(500); - // msを省略し、tone( Hz ) で鳴らす(結局、delayが必要になるので) - M5.Beep.tone(2092); delay(500); - // ここはmsを指定するとよい (loopのM5.Beep.updateが停めてくれるので、delayを省略できる) - M5.Beep.tone(2092 * 2, 1000); -} - -void loop() { - M5.Beep.update(); //tone()やsetBeep()で指定した時間を過ぎていたら音を止める。 - // M5.Beep.mute() とすると、すぐに停止する。 - - while (Serial.available()) { - char c = Serial.read(); - if (c < 'c' || 'z' < c) continue; // c〜zの文字以外なら、以降のwhile内処理をスキップ - M5.Beep.tone( f[c - 'c'] , 1000); // 1秒鳴らす - Serial.println( f[c - 'c'] ); - } - delay(50); -} +#include + +int f[28] = { 262, 294, 330, 349, 392, 440, 494 }; // 配列のサイズは4オクターブ分 +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + for (int i = 0; i < 7; i++) { + f[i + 7] = f[i] * 2; + f[i + 14] = f[i] * 4; + f[i + 21] = f[i] * 8; + } + // setBeep( Hz, ms) でビープ音を設定して、beep()で鳴らす。 + M5.Beep.setBeep(523, 500); M5.Beep.beep(); delay(500); + // tone( Hz, ms ) で鳴らす方法 + M5.Beep.tone(1046, 500); delay(500); + // msを省略し、tone( Hz ) で鳴らす(結局、delayが必要になるので) + M5.Beep.tone(2092); delay(500); + // ここはmsを指定するとよい (loopのM5.Beep.updateが停めてくれるので、delayを省略できる) + M5.Beep.tone(2092 * 2, 1000); + Serial.println("Input [c] -- [z] character."); +} + +void loop() { + M5.Beep.update(); //tone()やsetBeep()で指定した時間を過ぎていたら音を止める。 + // M5.Beep.mute() とすると、すぐに停止する。 + + while (Serial.available()) { + char c = Serial.read(); + if (c < 'c' || 'z' < c) continue; // c〜zの文字以外なら、以降のwhile内処理をスキップ + M5.Beep.tone( f[c - 'c'] , 1000); // 1秒鳴らす + Serial.printf( "%d Hz\n", f[c - 'c'] ); + } + delay(50); +} + diff --git a/SampleSrc/speakerhat01.ino b/SampleSrc/speakerhat01.ino index a369b31..b37cb07 100644 --- a/SampleSrc/speakerhat01.ino +++ b/SampleSrc/speakerhat01.ino @@ -1,55 +1,56 @@ -#include - -const int servo_pin = 26; -uint8_t ledChannel = 0; -uint8_t resolution = 8; -uint32_t sampling_rate = 16000; //16kの場合。8kの場合は、8000 - -/* - 別ファイル(ファイル名拡張子は.ino)に、音声データを以下の形式で貼っておく。 - 【音1】 - const uint8_t coin05_8k_raw[] PROGMEM = { 0x80, ... }; - const uint32_t coin05_8k_raw_len = 7218; // ←配列のサイズ - 【音2】 - const uint8_t coin05_16k_raw[] PROGMEM = { 0x80, ... }; - const uint32_t coin05_16k_raw_len = 14437; // ←配列のサイズ - PROGMEM と書くと、フラッシュメモリに配置する - - そのうえで、以下のextern文を、別タブの変数名と合わせて書く。 - 変数名を一致させることが重要です。 -*/ -// 【音1】 -extern const uint8_t coin05_8k_raw[]; -extern const uint32_t coin05_8k_raw_len; -// 【音2】 -extern const uint8_t coin05_16k_raw[]; -extern const uint32_t coin05_16k_raw_len; - -void setup() { - // put your setup code here, to run once: - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.setCursor(0, 30, 4); - M5.Lcd.println(" speaker hat test"); - ledcSetup(ledChannel, sampling_rate * 32, resolution); - ledcAttachPin(servo_pin, ledChannel); - ledcWrite(ledChannel, 0); -} -void playMusic(const uint8_t* music_data, uint32_t len, uint16_t sample_rate) { - uint32_t delay_interval = ((uint32_t)1000000 / sample_rate); - for (int i = 0; i < len; i++) { - ledcWrite(ledChannel, music_data[i]); - delayMicroseconds(delay_interval); - } - ledcWriteTone(ledChannel, 0); -} -void loop() { - M5.update(); - if (M5.BtnA.wasReleased()) { - playMusic(coin05_16k_raw, coin05_16k_raw_len, sampling_rate + 800); //なぜか音が低いので +800した - } else if (M5.BtnB.wasReleased()) { - playMusic(coin05_8k_raw, coin05_8k_raw_len, sampling_rate / 2); // 8kなので、2で割った - } - ledcWriteTone(ledChannel, 0); - delay(100); -} \ No newline at end of file +#include + +const int servo_pin = 26; +uint8_t ledChannel = 0; +uint8_t resolution = 8; +uint32_t sampling_rate = 16000; //16kの場合。8kの場合は、8000 + +/* + 別ファイル(ファイル名拡張子は.ino)に、音声データを以下の形式で貼っておく。 + 【音1】 + const uint8_t coin05_8k_raw[] PROGMEM = { 0x80, ... }; + const uint32_t coin05_8k_raw_len = 7218; // ←配列のサイズ + 【音2】 + const uint8_t coin05_16k_raw[] PROGMEM = { 0x80, ... }; + const uint32_t coin05_16k_raw_len = 14437; // ←配列のサイズ + PROGMEM と書くと、フラッシュメモリに配置する + + そのうえで、以下のextern文を、別タブの変数名と合わせて書く。 + 変数名を一致させることが重要です。 +*/ +// 【音1】 +extern const uint8_t coin05_8k_raw[]; +extern const uint32_t coin05_8k_raw_len; +// 【音2】 +extern const uint8_t coin05_16k_raw[]; +extern const uint32_t coin05_16k_raw_len; + +void setup() { + // put your setup code here, to run once: + M5.begin(115200); + M5.Lcd.setRotation(3); + M5.Lcd.setCursor(0, 30, 4); + M5.Lcd.println(" speaker hat test"); + ledcSetup(ledChannel, sampling_rate * 32, resolution); + ledcAttachPin(servo_pin, ledChannel); + ledcWrite(ledChannel, 0); +} +void playMusic(const uint8_t* music_data, uint32_t len, uint16_t sample_rate) { + uint32_t delay_interval = ((uint32_t)1000000 / sample_rate); + for (int i = 0; i < len; i++) { + ledcWrite(ledChannel, music_data[i]); + delayMicroseconds(delay_interval); + } + ledcWriteTone(ledChannel, 0); +} +void loop() { + M5.update(); + if (M5.BtnA.wasReleased()) { + playMusic(coin05_16k_raw, coin05_16k_raw_len, sampling_rate + 800); //なぜか音が低いので +800した + } else if (M5.BtnB.wasReleased()) { + playMusic(coin05_8k_raw, coin05_8k_raw_len, sampling_rate / 2); // 8kなので、2で割った + } + ledcWriteTone(ledChannel, 0); + delay(100); +} + diff --git a/SampleSrc/sprite01.ino b/SampleSrc/sprite01.ino index bcc0fb8..8c34185 100644 --- a/SampleSrc/sprite01.ino +++ b/SampleSrc/sprite01.ino @@ -1,39 +1,40 @@ -#include - -int USE_SPRITE = 0 ; //スプライトを使用するとき1 - -TFT_eSprite spu = TFT_eSprite(&M5.Lcd); // Sprite object - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - - spu.setColorDepth(8); - spu.createSprite(240, 135); - - randomSeed(analogRead(0)); -} - -void loop() { - int r = random(100000); - if ( USE_SPRITE ) { - spu.fillSprite( CYAN ); - spu.setCursor(30, 34, 4); spu.setTextSize(2); - spu.setTextColor( WHITE, BLUE ); - spu.printf(" %d \n Sprite ", r ); - spu.pushSprite(0, 0); - } else { - M5.Lcd.fillScreen( CYAN ); - M5.Lcd.setCursor(30, 34, 4); M5.Lcd.setTextSize(2); - M5.Lcd.setTextColor( WHITE, BLUE ); - M5.Lcd.printf(" %d \n LCD ", r); - } - - M5.update(); - if (M5.BtnA.wasReleased()) { - USE_SPRITE = 1 - USE_SPRITE ; // スプライト使用の切り替え(1なら0、0なら1) - M5.Beep.tone(1000 + 1000 * USE_SPRITE, 500); - } - M5.Beep.update(); - delay(100); -} \ No newline at end of file +#include + +int USE_SPRITE = 0 ; //スプライトを使用するとき1 + +TFT_eSprite spu = TFT_eSprite(&M5.Lcd); // Sprite object + +void setup() { + M5.begin(115200); + M5.Lcd.setRotation(3); + + spu.setColorDepth(8); + spu.createSprite(240, 135); + + randomSeed(analogRead(0)); +} + +void loop() { + int r = random(100000); + if ( USE_SPRITE ) { + spu.fillSprite( CYAN ); + spu.setCursor(30, 34, 4); spu.setTextSize(2); + spu.setTextColor( WHITE, BLUE ); + spu.printf(" %d \n Sprite ", r ); + spu.pushSprite(0, 0); + } else { + M5.Lcd.fillScreen( CYAN ); + M5.Lcd.setCursor(30, 34, 4); M5.Lcd.setTextSize(2); + M5.Lcd.setTextColor( WHITE, BLUE ); + M5.Lcd.printf(" %d \n LCD ", r); + } + + M5.update(); + if (M5.BtnA.wasReleased()) { + USE_SPRITE = 1 - USE_SPRITE ; // スプライト使用の切り替え(1なら0、0なら1) + M5.Beep.tone(1000 + 1000 * USE_SPRITE, 500); + } + M5.Beep.update(); + delay(100); +} + diff --git a/SampleSrc/str01.ino b/SampleSrc/str01.ino index 9e3d177..381ad56 100644 --- a/SampleSrc/str01.ino +++ b/SampleSrc/str01.ino @@ -1,8 +1,7 @@ #include void setup() { - Serial.begin(115200); - M5.begin(); + M5.begin(115200); M5.Lcd.setRotation(3); Serial.println("整数 または 6文字の16進数(RRGGBB)を入力してください。"); M5.Lcd.fillScreen( getColorUInt16( 255 , 255 , 200 ) ); @@ -72,3 +71,4 @@ } + diff --git a/SampleSrc/task01.ino b/SampleSrc/task01.ino index 0a4aec8..4262fce 100644 --- a/SampleSrc/task01.ino +++ b/SampleSrc/task01.ino @@ -1,70 +1,71 @@ -#include - -int interval_msec[] = { 333, 1000, 2000 }; // Led, Lcd, Beep -TaskHandle_t tH[3]; - -void ledTask(void *pvParam) { - /** setup をここに書く **/ - portTickType lastTime; - int PIN = 10; - pinMode(PIN, OUTPUT); // PINのモード設定 - int highOrLow = 0; - for (;;) { - /** loop をここに書く **/ - lastTime = xTaskGetTickCount(); - vTaskDelayUntil(&lastTime, interval_msec[0] ); // 第2引数に、実行間隔ミリ秒を指定 - digitalWrite(PIN, highOrLow); // HIGH = 1, LOW = 0 - highOrLow = 1 - highOrLow; // HIGH <=> LOW を切り替える - } -} - -void lcdTask(void *pvParam) { - /** setup をここに書く **/ - portTickType lastTime; - M5.Lcd.setRotation(0); - M5.Lcd.fillScreen(GREEN); - M5.Lcd.setTextColor(WHITE, OLIVE); - M5.Lcd.setTextSize(2); - M5.Lcd.setCursor(0, 0); - int count = 0; - for (;;) { - /** loop をここに書く **/ - M5.Lcd.printf("count=%d\n", count); - count++; - lastTime = xTaskGetTickCount(); - vTaskDelayUntil(&lastTime, interval_msec[1] ); // 第2引数に、実行間隔ミリ秒を指定 - if (count % 10 == 0) { - M5.Lcd.fillScreen(GREEN); - M5.Lcd.setCursor(0, 0); - } - } -} - -void beepTask(void *pvParam) { - /** setup をここに書く **/ - portTickType lastTime; - int f[8] = { 262, 294, 330, 349, 392, 440, 494, 524 }; - int note = 0; - for (;;) { - /** loop をここに書く **/ - lastTime = xTaskGetTickCount(); // ここでの時刻を変数に保存 - M5.Beep.tone( f[note] ); - delay(500); //0.5秒鳴らす - M5.Beep.mute(); - note = (note+1)%8; - vTaskDelayUntil(&lastTime, interval_msec[2] ); - // 途中の処理やdelayは含まず、「保存」時刻の2秒後まで待つ。 - } -} - -void setup() { - M5.begin(); - - xTaskCreatePinnedToCore(ledTask , "LedT", 4096, NULL, 1, &tH[0], 1/*<= CoreNo.*/ ); - xTaskCreatePinnedToCore(lcdTask , "LcdT", 4096, NULL, 1, &tH[1], 1/*<= CoreNo.*/ ); - xTaskCreatePinnedToCore(beepTask,"BeepT", 4096, NULL, 1, &tH[2], 0/*<= CoreNo.*/ ); -} - -void loop() { - delay(10); -} \ No newline at end of file +#include + +int interval_msec[] = { 333, 1000, 2000 }; // Led, Lcd, Beep +TaskHandle_t tH[3]; + +void ledTask(void *pvParam) { + /** setup をここに書く **/ + portTickType lastTime; + int PIN = 10; + pinMode(PIN, OUTPUT); // PINのモード設定 + int highOrLow = 0; + for (;;) { + /** loop をここに書く **/ + lastTime = xTaskGetTickCount(); + vTaskDelayUntil(&lastTime, interval_msec[0] ); // 第2引数に、実行間隔ミリ秒を指定 + digitalWrite(PIN, highOrLow); // HIGH = 1, LOW = 0 + highOrLow = 1 - highOrLow; // HIGH <=> LOW を切り替える + } +} + +void lcdTask(void *pvParam) { + /** setup をここに書く **/ + portTickType lastTime; + M5.Lcd.setRotation(0); + M5.Lcd.fillScreen(GREEN); + M5.Lcd.setTextColor(WHITE, OLIVE); + M5.Lcd.setTextSize(2); + M5.Lcd.setCursor(0, 0); + int count = 0; + for (;;) { + /** loop をここに書く **/ + M5.Lcd.printf("count=%d\n", count); + count++; + lastTime = xTaskGetTickCount(); + vTaskDelayUntil(&lastTime, interval_msec[1] ); // 第2引数に、実行間隔ミリ秒を指定 + if (count % 10 == 0) { + M5.Lcd.fillScreen(GREEN); + M5.Lcd.setCursor(0, 0); + } + } +} + +void beepTask(void *pvParam) { + /** setup をここに書く **/ + portTickType lastTime; + int f[8] = { 262, 294, 330, 349, 392, 440, 494, 524 }; + int note = 0; + for (;;) { + /** loop をここに書く **/ + lastTime = xTaskGetTickCount(); // ここでの時刻を変数に保存 + M5.Beep.tone( f[note] ); + delay(500); //0.5秒鳴らす + M5.Beep.mute(); + note = (note+1)%8; + vTaskDelayUntil(&lastTime, interval_msec[2] ); + // 途中の処理やdelayは含まず、「保存」時刻の2秒後まで待つ。 + } +} + +void setup() { + M5.begin(115200); + + xTaskCreatePinnedToCore(ledTask , "LedT", 4096, NULL, 1, &tH[0], 1/*<= CoreNo.*/ ); + xTaskCreatePinnedToCore(lcdTask , "LcdT", 4096, NULL, 1, &tH[1], 1/*<= CoreNo.*/ ); + xTaskCreatePinnedToCore(beepTask,"BeepT", 4096, NULL, 1, &tH[2], 0/*<= CoreNo.*/ ); +} + +void loop() { + delay(10); +} + diff --git a/SampleSrc/telnet01.ino b/SampleSrc/telnet01.ino index 572ca6e..3b896bc 100644 --- a/SampleSrc/telnet01.ino +++ b/SampleSrc/telnet01.ino @@ -1,130 +1,131 @@ -// オリジナルのWifiTelnetToSerial を、改変しました。 -/* - WiFiTelnetToSerial - Example Transparent UART to Telnet Server for ESP32 - - Copyright (c) 2017 Hristo Gochkov. All rights reserved. - This file is part of the ESP32 WiFi library for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ -#include -#include - -WiFiMulti wifiMulti; - -//how many clients should be able to telnet to this ESP32 -#define MAX_SRV_CLIENTS 3 -const char* ssid = "miura2g"; -const char* password = "jikken2022"; - -WiFiServer server(23); -WiFiClient serverClients[MAX_SRV_CLIENTS]; - -void setup() { - Serial.begin(115200); - Serial.println("\nConnecting"); - - wifiMulti.addAP(ssid, password); - // wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); - // wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); - - Serial.println("Connecting Wifi "); - for (int loops = 10; loops > 0; loops--) { - if (wifiMulti.run() == WL_CONNECTED) { - Serial.println(""); - Serial.print("WiFi connected "); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); - break; - } - else { - Serial.println(loops); - delay(1000); - } - } - if (wifiMulti.run() != WL_CONNECTED) { - Serial.println("WiFi connect failed"); - delay(1000); - ESP.restart(); - } - - //start UART and the server - // Serial2.begin(9600); - server.begin(); - server.setNoDelay(true); - - Serial.print("Ready! Use 'telnet "); - Serial.print(WiFi.localIP()); - Serial.println(" 23' to connect"); -} - -void loop() { - uint8_t i; - if (wifiMulti.run() == WL_CONNECTED) { - //check if there are any new clients - if (server.hasClient()) { - for (i = 0; i < MAX_SRV_CLIENTS; i++) { - //find free/disconnected spot - if (!serverClients[i] || !serverClients[i].connected()) { - if (serverClients[i]) serverClients[i].stop(); - serverClients[i] = server.available(); - if (!serverClients[i]) Serial.println("available broken"); - Serial.print("New client: "); - Serial.print(i); Serial.print(' '); - Serial.println(serverClients[i].remoteIP()); - break; - } - } - if (i >= MAX_SRV_CLIENTS) { - //no free/disconnected spot so reject - server.available().stop(); - } - } - //check clients for data - for (i = 0; i < MAX_SRV_CLIENTS; i++) { - if (serverClients[i] && serverClients[i].connected()) { - if (serverClients[i].available()) { - //get data from the telnet client and push it to the UART - while (serverClients[i].available()) Serial.write(serverClients[i].read()); - } - } - else { - if (serverClients[i]) { - serverClients[i].stop(); - } - } - } - //check ==UART== => Serial for data - if (Serial.available()) { - size_t len = Serial.available(); - uint8_t sbuf[len]; - Serial.readBytes(sbuf, len); - //push UART data to all connected telnet clients - for (i = 0; i < MAX_SRV_CLIENTS; i++) { - if (serverClients[i] && serverClients[i].connected()) { - serverClients[i].write(sbuf, len); - delay(1); - } - } - } - } - else { - Serial.println("WiFi not connected!"); - for (i = 0; i < MAX_SRV_CLIENTS; i++) { - if (serverClients[i]) serverClients[i].stop(); - } - delay(1000); - } -} \ No newline at end of file +// オリジナルのWifiTelnetToSerial を、改変しました。 +/* +WiFiTelnetToSerial - Example Transparent UART to Telnet Server for ESP32 + +Copyright (c) 2017 Hristo Gochkov. All rights reserved. +This file is part of the ESP32 WiFi library for Arduino environment. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include +#include + +WiFiMulti wifiMulti; + +//how many clients should be able to telnet to this ESP32 +#define MAX_SRV_CLIENTS 3 +const char* ssid = "miura2g"; +const char* password = "jikken2022"; + +WiFiServer server(23); +WiFiClient serverClients[MAX_SRV_CLIENTS]; + +void setup() { + Serial.begin(115200); + Serial.println("\nConnecting"); + + wifiMulti.addAP(ssid, password); + // wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); + // wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); + + Serial.println("Connecting Wifi "); + for (int loops = 10; loops > 0; loops--) { + if (wifiMulti.run() == WL_CONNECTED) { + Serial.println(""); + Serial.print("WiFi connected "); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + break; + } + else { + Serial.println(loops); + delay(1000); + } + } + if (wifiMulti.run() != WL_CONNECTED) { + Serial.println("WiFi connect failed"); + delay(1000); + ESP.restart(); + } + + //start UART and the server + // Serial2.begin(9600); + server.begin(); + server.setNoDelay(true); + + Serial.print("Ready! Use 'telnet "); + Serial.print(WiFi.localIP()); + Serial.println(" 23' to connect"); +} + +void loop() { + uint8_t i; + if (wifiMulti.run() == WL_CONNECTED) { + //check if there are any new clients + if (server.hasClient()) { + for (i = 0; i < MAX_SRV_CLIENTS; i++) { + //find free/disconnected spot + if (!serverClients[i] || !serverClients[i].connected()) { + if (serverClients[i]) serverClients[i].stop(); + serverClients[i] = server.available(); + if (!serverClients[i]) Serial.println("available broken"); + Serial.print("New client: "); + Serial.print(i); Serial.print(' '); + Serial.println(serverClients[i].remoteIP()); + break; + } + } + if (i >= MAX_SRV_CLIENTS) { + //no free/disconnected spot so reject + server.available().stop(); + } + } + //check clients for data + for (i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + if (serverClients[i].available()) { + //get data from the telnet client and push it to the UART + while (serverClients[i].available()) Serial.write(serverClients[i].read()); + } + } + else { + if (serverClients[i]) { + serverClients[i].stop(); + } + } + } + //check ==UART== => Serial for data + if (Serial.available()) { + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes(sbuf, len); + //push UART data to all connected telnet clients + for (i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + serverClients[i].write(sbuf, len); + delay(1); + } + } + } + } + else { + Serial.println("WiFi not connected!"); + for (i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i]) serverClients[i].stop(); + } + delay(1000); + } +} + diff --git a/SampleSrc/wifi01.ino b/SampleSrc/wifi01.ino index 3c6ff6f..efd2ef8 100644 --- a/SampleSrc/wifi01.ino +++ b/SampleSrc/wifi01.ino @@ -1,32 +1,33 @@ -#include -#include - -const char* ssid = "miura2g"; -const char* password = "jikken2022"; - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.fillScreen(ORANGE); - M5.Lcd.setCursor(10, 50, 4); - - WiFi.begin(ssid, password); // 接続開始 - while (WiFi.status() != WL_CONNECTED) { // 接続中... - M5.Beep.tone(2000); delay(200); - M5.Beep.mute(); delay(300); - M5.Lcd.print("."); - } - // 接続完了!! - M5.Beep.tone(4000); - M5.Lcd.fillScreen(GREEN); - M5.Lcd.setCursor(0, 40, 4); - M5.Lcd.setTextColor(BLACK, GREEN); - M5.Lcd.print(" Wifi Connected!\n "); - String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス - M5.Lcd.println(gotip); - delay(1500); - M5.Beep.mute(); -} - -void loop() { -} \ No newline at end of file +#include +#include + +const char* ssid = "miura2g"; +const char* password = "jikken2022"; + +void setup() { + M5.begin(); + M5.Lcd.setRotation(3); + M5.Lcd.fillScreen(ORANGE); + M5.Lcd.setCursor(10, 50, 4); + + WiFi.begin(ssid, password); // 接続開始 + while (WiFi.status() != WL_CONNECTED) { // 接続中... + M5.Beep.tone(2000); delay(200); + M5.Beep.mute(); delay(300); + M5.Lcd.print("."); + } + // 接続完了!! + M5.Beep.tone(4000); + M5.Lcd.fillScreen(GREEN); + M5.Lcd.setCursor(0, 40, 4); + M5.Lcd.setTextColor(BLACK, GREEN); + M5.Lcd.print(" Wifi Connected!\n "); + String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス + M5.Lcd.println(gotip); + delay(1500); + M5.Beep.mute(); +} + +void loop() { +} + diff --git a/SampleSrc/wifimac.ino b/SampleSrc/wifimac.ino index 3a668f3..e32b0cd 100644 --- a/SampleSrc/wifimac.ino +++ b/SampleSrc/wifimac.ino @@ -6,41 +6,42 @@ void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.fillScreen(ORANGE); - M5.Lcd.setCursor(10, 50, 4); - - WiFi.begin(ssid, password); // 接続開始 - while (WiFi.status() != WL_CONNECTED) - { // 接続中... - M5.Beep.tone(2000); - delay(200); - M5.Beep.mute(); - delay(300); - M5.Lcd.print("."); - } - // 接続完了!! - M5.Beep.tone(4000); - M5.Lcd.fillScreen(GREEN); - M5.Lcd.setCursor(0, 30, 4); - M5.Lcd.setTextColor(BLACK, GREEN); - M5.Lcd.print(" Wifi Connected!\n "); - String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス - M5.Lcd.println(gotip); - - // MACアドレスを表示 - uint8_t mac[6]; - char macchar[100]; - esp_read_mac(mac, ESP_MAC_WIFI_STA); - sprintf(macchar, " %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - String macstr = macchar; - M5.Lcd.println(macstr); - - delay(1500); + M5.begin(115200); + M5.Lcd.setRotation(3); + M5.Lcd.fillScreen(ORANGE); + M5.Lcd.setCursor(10, 50, 4); + + WiFi.begin(ssid, password); // 接続開始 + while (WiFi.status() != WL_CONNECTED) + { // 接続中... + M5.Beep.tone(2000); + delay(200); M5.Beep.mute(); + delay(300); + M5.Lcd.print("."); + } + // 接続完了!! + M5.Beep.tone(4000); + M5.Lcd.fillScreen(GREEN); + M5.Lcd.setCursor(0, 30, 4); + M5.Lcd.setTextColor(BLACK, GREEN); + M5.Lcd.print(" Wifi Connected!\n "); + String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス + M5.Lcd.println(gotip); + + // MACアドレスを表示 + uint8_t mac[6]; + char macchar[100]; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + sprintf(macchar, " %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + String macstr = macchar; + M5.Lcd.println(macstr); + + delay(1500); + M5.Beep.mute(); } void loop() { -} \ No newline at end of file +} +