#include "M5StickCPlus.h"
#include <esp_now.h>
#include <WiFi.h>
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 > 0)
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line = 0;
M5.Beep.tone(500, 500);
}
else
{
M5.Beep.tone(250, 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] == 50 && data[1] == 200) // 100,254に深い意味はない。uint8_tは0〜255の数値
{
line++;
if (line = 1)//レ どうし
{
M5.Beep.tone(146, 750);
M5.Lcd.fillScreen(GREEN);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(500);
}
if (line = 2)//レ たっ
{
M5.Beep.tone(146, 1000);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(500);
}
if (line = 3)//シ て
{
M5.Beep.tone(246, 250);
M5.Lcd.fillScreen(RED);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(500);
}
if (line = 4)//ラ け
{
M5.Beep.tone(164, 1000);
M5.Lcd.fillScreen(GREEN);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 5)//シ せ
{
M5.Beep.tone(493, 1000);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 6)//レ な
{
M5.Beep.tone(164, 1000);
M5.Lcd.fillScreen(RED);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 7)//シ い
{
M5.Beep.tone(493, 1000);
M5.Lcd.fillScreen(GREEN);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 8)//ラ ゆ
{
M5.Beep.tone(164, 1000);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 9)//シ め
{
M5.Beep.tone(493, 1000);
M5.Lcd.fillScreen(RED);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(50);
}
if (line = 10)//シ も
{
M5.Beep.tone(493, 250);
M5.Lcd.fillScreen(GREEN);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(500);
}
if (line = 11)//ソ と
{
M5.Beep.tone(391, 250);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 12)//ラ ま
{
M5.Beep.tone(184, 1000);
M5.Lcd.fillScreen(RED);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 13)//シ れ
{
M5.Beep.tone(493, 250);
M5.Lcd.fillScreen(GREEN);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 14)//レ な
{
M5.Beep.tone(587, 1000);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 15)//シ い
{
M5.Beep.tone(493, 1000);
M5.Lcd.fillScreen(RED);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 16)//ラ い
{
M5.Beep.tone(184, 1000);
M5.Lcd.fillScreen(GREEN);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(250);
}
if (line = 17)//シ ま
{
M5.Beep.tone(493, 1000);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 1);
line++;
delay(50);
}
if (line = 18)//シ も
{
M5.Beep.tone(493, 250);
M5.Lcd.fillScreen(RED);
M5.Lcd.setCursor(0, 0, 1);
line=0;
delay(125);
}
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.BtnB.wasReleased())
{
uint8_t data[2] = {50, 200}; // 送信データ 100, 254に深い意味はない。uint8_tは0〜255の数値
esp_err_t result = esp_now_send(peerInfo.peer_addr, data, sizeof(data));
}
{
/* data */
};
delay(20);
}