M5NanoC6、温度を無線で送る。ESP_NOW_Serial_Class



x 過去ログを見よ!!
x インサーネットフレームの意味が分かる人
x mac addの意味がわかる人
x mac addの宛先と自分がわかる人
x mac addの宛先を書き換えられる人
x インターネット老人会の人
x なぜかUSBシリアルの起動が失敗するので起動画面が出るまでターミナルをオンオフする
x 3.0.7
x 温度センサーは、スイッチサイエンスで売っているseeedのGroveのV1.2のやつ

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


```c++

// 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
});
//const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});

```

```c++

// 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});
const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});


```

![o_coq590.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/bd0f02c3-28e0-aa47-8286-8b4f3e7acc16.jpeg)



![o_coq587.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/4741a637-da04-2db5-7769-472669dd25cd.jpeg)


![o_coq588.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/f1184145-60ad-e792-4d1d-d8f60387987d.jpeg)

![o_coq589.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/26c31d35-4c6f-6020-c55f-19a2a53c1fbf.jpeg)



ホスト　NanoC6(温度センサー)


```c++



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

#include "esp_wifi.h"

#include <math.h> //logの計算等で使う

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

#define ESPNOW_WIFI_MODE WIFI_STA     // WiFi Mode
#define ESPNOW_WIFI_IF   WIFI_IF_STA  // WiFi Interface


// 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
});
//const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});


ESP_NOW_Serial_Class NowSerial(peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);

char *mac_add_conv(const uint8_t *a) {

  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;

  return ( b );

}//mac_add_conv

void setup() {

  //WiFiモードとWifiチャンネルの設定
  WiFi.mode(ESPNOW_WIFI_MODE);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);

  //シリアルポートの初期化
  Serial.begin(115200);

  //WiFiの起動待ち
  for (int i = 0; i < (6+3); i++) {
    Serial.print(".");
    delay(500); //接続待ち
  }
  while (!(WiFi.STA.started())) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  //WiFiメッセージの表示
  Serial.print("to  MAC Address: " );
  Serial.println( mac_add_conv( peer_mac  ) );
  Serial.print("WiFi Mode: ");
  Serial.println("Station");
  Serial.print("Channel: ");
  Serial.println(ESPNOW_WIFI_CHANNEL);
  Serial.print("MAC Address: ");
  Serial.println(WiFi.macAddress());

  //ESP_NOW_Serialの起動
  // 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 now_ch(char ch) {

  while ( !(NowSerial.availableForWrite()) ) {
    delay(5);//連続送信防止
    Serial.println(".");
  }

  if (NowSerial.write(ch) <= 0) {
    Serial.println("Failed to send data");
  }

}


//文字列の表示
void now_print(char *str1)
{
  //文字の中身がゼロか
  while (*str1) {

    //一文字出力
    now_ch(*str1 ++);

  }//while

}//now_print


void loop() {

  //↓温度センサーの処理を開始

  static uint32_t msg_count = 0;

  static char data[256+1];

  int B = 4275;               // B value of the thermistor
  int R0 = 100000;            // R0 = 100k

  int pinTempSensor = 1;     // Grove - Temperature Sensor connect to G1

  int a = analogRead(pinTempSensor);

  //float R = 1023.0/a-1.0;
  //R = R0*R;
  float R,v1,a1,o1; 
  v1 = ((float)a) * 0.001; //電圧を求める
  a1 = v1 / 100000.0;           //電流を求める
  o1 = 5.0 / a1;                //全体の抵抗を求める
  R = o1 - 100000.0;    //全体の抵抗から検出抵抗を引く

  float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet

  snprintf(data, sizeof(data), "temperature = %d #%lu",(int)temperature, msg_count++);


  //↑温度センサーの処理が終了

  now_print(data);
  now_print("\n");

  delay(1000);
}




```

スレーブ　StampS3


```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});
const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});


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);
}



```