diff --git a/src/Upload.sh b/src/Upload.sh index 9a6b092..000bf1a 100755 --- a/src/Upload.sh +++ b/src/Upload.sh @@ -1,5 +1,18 @@ #!/bin/bash +### (1) mosi hikisuu ga areba, suguni jikkou +if [ $# -eq 1 ] ; then + file=$1 + echo "compile ${file}" + cp ${file} TestBuild/TestBuild.ino + cd TestBuild + ./compile.sh && ./upload.sh && ./monitor.sh + cd .. + exit +fi + +### (2) hikisuu ga nakereba, list and select + ary=() for i in *.ino ; do diff --git a/src/bts01_master.ino b/src/bts01_master.ino index 62cc8ac..b758796 100644 --- a/src/bts01_master.ino +++ b/src/bts01_master.ino @@ -27,7 +27,8 @@ 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] = {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) { @@ -101,4 +102,4 @@ SerialBT.println("[Power] was Pressed"); } delay(20); -} \ No newline at end of file +} diff --git a/src/espnow02.ino b/src/espnow02.ino new file mode 100644 index 0000000..4b155ca --- /dev/null +++ b/src/espnow02.ino @@ -0,0 +1,108 @@ +#include "M5StickCPlus.h" +#include +#include + +esp_now_peer_info_t peerInfo; +int line = 0; + +void Init_ESPNOW() +{ // ESPNowの初期化 + // 引用: https://101010.fun/iot/esp32-m5stickc-plus-esp-now.html + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + if (esp_now_init() == ESP_OK) + { + M5.Lcd.println("ESP-Now Init Success"); + } + else + { + M5.Lcd.println("ESP-Now Init failed"); + ESP.restart(); + } + // マルチキャスト用Slave登録 + memset(&peerInfo, 0, sizeof(peerInfo)); + for (int i = 0; i < 6; ++i) + { + peerInfo.peer_addr[i] = (uint8_t)0xff; + } + esp_err_t addStatus = esp_now_add_peer(&peerInfo); + if (addStatus == ESP_OK) + { + // Pair success + M5.Lcd.println("Pair success"); + esp_now_register_send_cb(onESPNOWSent); //送信後のコールバック関数を指定する + esp_now_register_recv_cb(onESPNOWReceive); ///受信時のコールバック関数を指定する + } +} +// 引用: https://101010.fun/iot/esp32-m5stickc-plus-esp-now.html +void onESPNOWSent(const uint8_t *mac_addr, esp_now_send_status_t status) +{ + // char macStr[18]; + // snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", + // mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + line++; + if (line > 3) + { + M5.Lcd.fillScreen(YELLOW); + M5.Lcd.setCursor(0, 0, 1); + line = 0; + M5.Beep.tone(2000, 500); + } + else + { + M5.Beep.tone(1000, 500); + } + M5.Lcd.setTextColor(BLACK, GREEN); + + M5.Lcd.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); +} + +// データを受け取った時に実行されるコールバック関数 +void onESPNOWReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) +{ + char macStr[18]; + snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", + mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + // Serial.println(); + // Serial.printf("Last Packet Recv from: %s\n", macStr); + // Serial.printf("Last Packet Recv Data(%d): ", data_len); + if (data[0] == 100 && data[1] == 254) // 100,254に深い意味はない。uint8_tは0〜255の数値 + { + line++; + if (line > 3) + { + M5.Beep.tone(2000, 1000); + M5.Lcd.fillScreen(YELLOW); + M5.Lcd.setCursor(0, 0, 1); + line = 0; + } + M5.Lcd.setTextColor(BLACK, CYAN); + + M5.Lcd.printf(" %s\n", macStr); + M5.Lcd.printf("Data: %d %d\n", data[0], data[1]); + } +} + +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); + + Init_ESPNOW(); +} +void loop() +{ + M5.update(); + M5.Beep.update(); + if (M5.BtnA.wasReleased()) + { + uint8_t data[2] = {100, 254}; // 送信データ  100, 254に深い意味はない。uint8_tは0〜255の数値 + esp_err_t result = esp_now_send(peerInfo.peer_addr, data, sizeof(data)); + } + delay(20); +}