diff --git a/src/imu01.ino b/src/imu01.ino new file mode 100644 index 0000000..16ed974 --- /dev/null +++ b/src/imu01.ino @@ -0,0 +1,26 @@ +#include + +void setup() { + auto cfg = M5.config(); + cfg.serial_baudrate = 115200; + M5.begin(cfg); + M5.Display.setRotation(3); //横向き + M5.Display.setFont(&fonts::lgfxJapanGothic_16); + M5.Display.println("IMU Inertial Measurement Unit: 慣性計測装置"); +} + +void loop() { + M5.update(); + auto imu_update = M5.Imu.update(); + if (imu_update) { + m5::IMU_Class::imu_data_t data = M5.Imu.getImuData(); + M5.Display.setCursor(0, 40); // x,y,fonttype + + M5.Display.printf("加速度 傾↑↓:%7.2f ←→:%7.2f 垂直:%7.2f\n", data.accel.x , data.accel.y , data.accel.z ); + Serial.printf("%7.2f , %7.2f , %7.2f \n", data.accel.x , data.accel.y , data.accel.z); //ArduinoIDE シリアルプロッタ用の出力 + + M5.Display.printf("ジャイロ X:%7.2f Y:%7.2f Z:%7.2f \n", data.gyro.x, data.gyro.y, data.gyro.z); + // Serial.printf("%7.2f,%7.2f,%7.2f,", gyroX * M5.IMU.gRes, gyroY * M5.IMU.gRes, gyroZ * M5.IMU.gRes); + } + M5.delay(10); +} \ No newline at end of file diff --git a/src/webclient01.ino b/src/webclient01.ino new file mode 100644 index 0000000..f20611d --- /dev/null +++ b/src/webclient01.ino @@ -0,0 +1,55 @@ +#include +#include +#include // ステータスコードの定義もここにある + +const char* ssid = "ics-ap"; +const char* password = "jikkenics"; +// 天気予報API https://weather.tsukumijima.net/ から、千葉の天気を取得 +const char* weatherapi_url = "https://weather.tsukumijima.net/api/forecast/city/120010"; + +void setup() { + auto cfg = M5.config(); + cfg.serial_baudrate = 115200; + M5.begin(cfg); + M5.Display.setRotation(3); //横向き + M5.Display.setFont(&fonts::lgfxJapanGothic_16); + M5.Display.println("webclient01 ボタンを押すと天気を取得"); + M5.Display.setTextScroll(true); + M5.Display.fillScreen(BLACK); + M5.Display.setTextColor(WHITE,BLACK); + + WiFi.begin(ssid, password); // 接続開始 + while (WiFi.status() != WL_CONNECTED) { // 接続中... + M5.Display.print("."); + M5.delay(500); + } + // 接続完了!! + M5.Display.fillScreen(GREEN); + M5.Display.setCursor(0, 30, 4);//x,y,fonttype + M5.Display.setTextColor(BLACK, GREEN); + M5.Display.print(" Wifi Connected!\n"); + String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス + M5.Display.println(gotip); + M5.Display.println("BtnA to get weather"); + M5.delay(1500); +} + +void loop() { + M5.update(); + if (M5.BtnA.wasReleased()) { + HTTPClient http; // クライアント作成 + http.begin(weatherapi_url); // HTTPでサーバに接続 + int httpCode = http.GET(); // ステータスコードを取得 + if (httpCode > 0) { + Serial.println(httpCode); + M5.Display.setTextColor(GREEN,BLACK); + M5.Display.printf("status code = %d\n ", httpCode); + if (httpCode == HTTP_CODE_OK) { // ステータスコードが「成功」(200) なら + String payload = http.getString(); + Serial.println(payload); + } + } + http.end(); + } + M5.delay(20); +} \ No newline at end of file diff --git a/src/webserver01.ino b/src/webserver01.ino new file mode 100644 index 0000000..edf7dc5 --- /dev/null +++ b/src/webserver01.ino @@ -0,0 +1,100 @@ +#include +#include +#include // ステータスコードの定義もここにある + +const char* ssid = "ics-ap"; +const char* password = "jikkenics"; + +WiFiServer server(80); + +void setup() { + auto cfg = M5.config(); + cfg.serial_baudrate = 115200; + M5.begin(cfg); + M5.Display.setRotation(3); //横向き + M5.Display.setFont(&fonts::lgfxJapanGothic_16); + M5.Display.println("webserver01"); + M5.Display.setTextScroll(true); + + WiFi.begin(ssid, password); // 接続開始 + while (WiFi.status() != WL_CONNECTED) { // 接続中... + M5.Display.print("."); + M5.delay(500); + } + // 接続完了!! + M5.Display.fillScreen(GREEN); + M5.Display.setCursor(0, 30, 4);//x,y,fonttype + M5.Display.setTextColor(BLACK, GREEN); + M5.Display.print(" Wifi Connected!\n"); + String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス + M5.Display.println(gotip); + M5.Display.println("open Web via HTTP"); + + server.begin(); // Webサーバを開始 +} + +void loop() { + M5.update(); + WiFiClient client = server.available(); + if (client) { + String req = "" ; + String tmp = "" , meth = "" ; + while (client.connected()) { // loop while the client's connected + if (client.available()) { // if there's bytes to read from the client, + char c = client.read(); // read a byte, then + req += c; + if (c == '\n') { // if the byte is a newline character + if (tmp.length() == 0) { // end of request, break while loop + break; + } else { //まだ継続 + if (tmp.startsWith("GET ") || tmp.startsWith("POST ") ) { + meth = tmp; + } + tmp = ""; + } + } else if (c != '\r') { // if you got anything else but a carriage return character, + tmp += c; // add it to the end of the currentLine + } + } + } // end of while + + Serial.println(meth); + if ( meth.startsWith("GET /") ) { + client.println("HTTP/1.1 200 OK"); // header (with response code) + client.println("Content-Type:text/plain"); + client.println(""); // HTTPでは、header と body の区切りは改行 + client.println(meth); + client.println("-- request --"); + client.println(req); + } + + if ( meth.startsWith("POST ") ) { + String post = ""; + char buf[257]; + int n; + while ((n = client.available()) > 0) { + if (n < 256) { + client.readBytes(buf, n) ; + buf[n] = 0 ; + } else { + client.readBytes(buf, 256) ; + buf[256] = 0 ; + } + } + post += buf ; + + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type:text/plain"); + client.println(""); // HTTPでは、header と body の区切りは改行 + client.println(meth); + client.println("-- request --"); + client.println(req); + client.println("-- post data --"); + client.println(post); + } + // close the connection: + client.stop(); + Serial.println(" --- Client Disconnected."); + } + M5.delay(20); +} \ No newline at end of file