ESP_NOW_Serialであそぶ。M5StampS3同士





x 過去ログを見よ!!
x インサーネットフレームの意味が分かる人
x mac addの意味がわかる人
x mac addの宛先と自分がわかる人
x mac addの宛先を書き換えられる人
x インターネット老人会の人
x なぜかUSBシリアルの起動が失敗するので起動画面が出るまでターミナルをオンオフする
x 3.0.7

ここを都合よく書き換える


```c++

//const MacAddress peer_mac({0x48, 0xCA, 0x43, 0x3A, 0x42, 0xEC});
const MacAddress peer_mac({0x48, 0x27, 0xE2, 0xE3, 0xBD, 0x50});


```


![o_coq584.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/509f96f6-16a4-ef85-5111-07011ecd2975.jpeg)


![o_coq585.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/2b2afe49-8faa-05d1-8384-b23ac1948b0c.jpeg)

![o_coq586.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/323e94d2-22b9-8845-c3fa-16581b33c781.jpeg)


```c++


/*
    ESP-NOW Serial Example - Unicast transmission
    Lucas Saavedra Vaz - 2024
    Send data between two ESP32s using the ESP-NOW protocol in one-to-one (unicast) configuration.
    Note that different MAC addresses are used for different interfaces.
    The devices can be in different modes (AP or Station) and still communicate using ESP-NOW.
    The only requirement is that the devices are on the same Wi-Fi channel.
    Set the peer MAC address according to the device that will receive the data.

    Example setup:
    - Device 1: AP mode with MAC address F6:12:FA:42:B6:E8
                Peer MAC address set to the Station MAC address of Device 2 (F4:12:FA:40:64:4C)
    - Device 2: Station mode with MAC address F4:12:FA:40:64:4C
                Peer MAC address set to the AP MAC address of Device 1 (F6:12:FA:42:B6:E8)

    The device running this sketch will also receive and print data from any device that has its MAC address set as the peer MAC address.
    To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR".
*/

#include "ESP32_NOW_Serial.h"
#include "MacAddress.h"
#include "WiFi.h"

#include "esp_wifi.h"

// 0: AP mode, 1: Station mode
#define ESPNOW_WIFI_MODE_STATION 1

// Channel to be used by the ESP-NOW protocol
#define ESPNOW_WIFI_CHANNEL 1

#if ESPNOW_WIFI_MODE_STATION          // ESP-NOW using WiFi Station mode
#define ESPNOW_WIFI_MODE WIFI_STA     // WiFi Mode
#define ESPNOW_WIFI_IF   WIFI_IF_STA  // WiFi Interface
#else                                 // ESP-NOW using WiFi AP mode
#define ESPNOW_WIFI_MODE WIFI_AP      // WiFi Mode
#define ESPNOW_WIFI_IF   WIFI_IF_AP   // WiFi Interface
#endif

// Set the MAC address of the device that will receive the data
// For example: F4:12:FA:40:64:4C
//const MacAddress peer_mac({0xF4, 0x12, 0xFA, 0x40, 0x64, 0x4C});
//const MacAddress peer_mac({0x48, 0xCA, 0x43, 0x3A, 0x42, 0xEC});
const MacAddress peer_mac({0x48, 0x27, 0xE2, 0xE3, 0xBD, 0x50});


ESP_NOW_Serial_Class NowSerial(peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);

char *mac_add_conv(const uint8_t *a){

    //uint8_t a[8] = {0x1f,0x2f,0x3f, 0xf4,0xf5,0xf6,};
    char h[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    static char b[16+1];
    
    b[0]=h[a[0]>>4];
    b[1]=h[a[0]&0x0f];

    b[2]=h[a[1]>>4];
    b[3]=h[a[1]&0x0f];

    b[4]=h[a[2]>>4];
    b[5]=h[a[2]&0x0f];

    b[6]=h[a[3]>>4];
    b[7]=h[a[3]&0x0f];

    b[8]=h[a[4]>>4];
    b[9]=h[a[4]&0x0f];

    b[10]=h[a[5]>>4];
    b[11]=h[a[5]&0x0f];

    b[12]=0;

    //printf("%s\r\n",b);

   return( b );
   
}//mac_add_conv

void setup() {
  Serial.begin(115200);


    for(int i=0;i<(6*3);i++){
      Serial.print(".");
      delay(500); // 再接続待ち
    }
    Serial.println();

    Serial.print("to  MAC Address: " );
    Serial.println( mac_add_conv( peer_mac  ) );

  Serial.print("WiFi Mode: ");
  Serial.println(ESPNOW_WIFI_MODE == WIFI_AP ? "AP" : "Station");
  WiFi.mode(ESPNOW_WIFI_MODE);

  Serial.print("Channel: ");
  Serial.println(ESPNOW_WIFI_CHANNEL);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);

  while (!(WiFi.STA.started() || WiFi.AP.started())) {
    delay(100);
  }

  Serial.print("MAC Address: ");
  Serial.println(ESPNOW_WIFI_MODE == WIFI_AP ? WiFi.softAPmacAddress() : WiFi.macAddress());

  // Start the ESP-NOW communication
  Serial.println("ESP-NOW communication starting...");
  NowSerial.begin(115200);
  Serial.println("You can now send data to the peer device using the Serial Monitor.\n");
}

void loop() {
  while (NowSerial.available()) {
    Serial.write(NowSerial.read());
  }

  while (Serial.available() && NowSerial.availableForWrite()) {
    if (NowSerial.write(Serial.read()) <= 0) {
      Serial.println("Failed to send data");
      break;
    }
  }

  delay(1);
}


```