1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

無線受信を0.3秒ためて出力(M5NanoC6)(ESP_NOW_Serial_Class)

Posted at

無線受信を0.3秒ためて出力(M5NanoC6)(ESP_NOW_Serial_Class)

目的
前後するが画面表示のちらつき防止の為に
ある程度、文字を貯めて出力する

無線で画面表示を参照の事



//ESP_NOW_Serial_SSD1306_M5NanoC6_1


//インクルド
#include "ESP32_NOW_Serial.h"
#include "MacAddress.h"
#include "WiFi.h"
#include "esp_wifi.h"


//定義
// 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

//ここは、相手先のMACアドレスを記述する。
// Set the MAC address of the device that will receive the data
// For example: XX:XX:XX:XX:XX:XX
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);


//MACを16進に変換
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];

  for(int i=0;i<6;i++){
    b[i*2+0] = h[a[i] >> 4];
    b[i*2+1] = h[a[i] & 0x0f];
  }
  b[12] = 0;

  return ( b );
  
}//mac_add_conv


//1文字の送信
void now_ch(char ch) {

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

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

}//now_ch


//文字列の送信
void now_print(char *str1)
{
  //文字の中身がゼロか
  while (*str1) {now_ch(*str1 ++); /*一文字出力*/}
}//now_print


//初期化
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");

}//setup


//メインループ
void loop() {

  static char data[4096+1];
  int str_num = 0;

  str_num = 0;data[0]=0;
  while (NowSerial.available()) {
    //Serial.print("[");
    data[str_num] = NowSerial.read();
    str_num++;
    if( str_num > 4095 ){str_num = 4095;}
    data[str_num] = 0;
    //Serial.print("]");
  }
  //Serial.print("(");
  //Serial.print("(");
  //Serial.print(str_num);
  //Serial.print(")");

  if( str_num != 0 ){
    //Serial.print("-");
    Serial.print(data);
  }
  
  //Serial.print(")");

  delay(300);

}//loop


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?