いろいろ注意
-
過去ログを見よeeee
-
リセット強化,電源強化済み(3.3v,5v,リセットに0.1ufを並列に追加!!!
-
USB CDC On Boot: を Enabled
-
Uplode Mode : を Internal USB
-
AE-ESP32-S2-MINIが送信側-2bootボタンを押す
-
AE-ESP32-S2-MINIが受信側-RGB-LEDが光る
-
相手(受信側)の状態が不明なので、何度か信号を送ってあげると同期する(仕様です!!!
-
ESPNOW(ブロードキャスト)
-
構造体
struct {
int R; //red
int G; //green
int B; //blue
} RGBs[3];
結果
プログラム
マスター AE-ESP32-S2-MINI
//ESP32_NOW_m_3rgb_led_36_1_ae_esp32_s2_mini_1
//インクルド (ESP-NOW)
#include <Arduino.h>
#include "esp_now_m_36byte_begin.h"
//初期化
void setup() {
//ESP-NOW開始->
//ESP-NOWの初期化関数(バッファサイズ12バイト)
esp_now_m_36byte_begin();
//<-ESP-NOW終了
pinMode(1, INPUT_PULLUP); //gpio init blue
pinMode(2, INPUT_PULLUP); //gpio init red
} //setup
//メインループ
void loop() {
int a = digitalRead(1); //ボタンの読み込み
int b = digitalRead(2); //ボタンの読み込み
if (a == 0) { //青ボタン
RGBs[0].R = 0;
RGBs[0].G = 0;
RGBs[0].B = 0;
RGBs[1].R = 0;
RGBs[1].G = 0;
RGBs[1].B = 0;
RGBs[2].R = 0;
RGBs[2].G = 064;
RGBs[2].B = 0;
} //end-if blue
if (b == 0) { //赤ボタン
RGBs[0].R = 64;
RGBs[0].G = 0;
RGBs[0].B = 0;
RGBs[1].R = 0;
RGBs[1].G = 0;
RGBs[1].B = 0;
RGBs[2].R = 0;
RGBs[2].G = 0;
RGBs[2].B = 0;
} //end-if red
if (a == 0 && b == 0) { //青ボタンと赤ボタン
RGBs[0].R = 0;
RGBs[0].G = 0;
RGBs[0].B = 0;
RGBs[1].R = 64;
RGBs[1].G = 64;
RGBs[1].B = 0;
RGBs[2].R = 0;
RGBs[2].G = 0;
RGBs[2].B = 0;
} //end-if red
if (a == 0 || b == 0) { //青ボタンか赤ボタン
//ESP-NOW開始->
memcpy(&txb, &RGBs, BLEN); //データの転送
//ESP-NOWのパケット(フレーム)を送信する
//int型x2,8バイトを1個だけ送る
broadcast_peer.send_message(txb, BLEN);
//<-ESP-NOW終了
delay(200); // 1 / 5 秒待つ
//ESP-NOW開始->
RGBs[0].R = 0;
RGBs[0].G = 0;
RGBs[0].B = 0;
RGBs[1].R = 0;
RGBs[1].G = 0;
RGBs[1].B = 0;
RGBs[2].R = 0;
RGBs[2].G = 0;
RGBs[2].B = 0;
memcpy(&txb, &RGBs, BLEN); //データの転送
//ESP-NOWのパケット(フレーム)を送信する
//int型x2,8バイトを1個だけ送る
broadcast_peer.send_message(txb, BLEN);
//<-ESP-NOW終了
} //end-if レコード
delay(3); //ダミー
} //loop
esp_now_m_36byte_begin.h
//インクルド (ESP-NOW)
#include <Arduino.h>
#include <ESP32_NOW.h>
#include <WiFi.h>
//#include <esp_mac.h> // For the MAC2STR and MACSTR macros
//定義
//送信バッファー int(32bit)x3x3
//グローバルで定義しているのは、速度低下を防ぐため
//パケットの構造体 項目の追加、削除は、ここをいじる
struct {
int R; //red
int G; //green
int B; //blue
} RGBs[3];
uint8_t txb[sizeof(RGBs)] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#define BLEN sizeof(txb)
/* Definitions */
//WiFiチャンネルの指定
#define ESPNOW_WIFI_CHANNEL 1
//クラス
/* Classes */
// Creating a new class that inherits from the ESP_NOW_Peer class is required.
class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
public:
// Constructor of the class using the broadcast address
ESP_NOW_Broadcast_Peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk)
: ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}
// Destructor of the class
~ESP_NOW_Broadcast_Peer() {
remove();
}
// Function to properly initialize the ESP-NOW and register the broadcast peer
bool begin() {
if (!ESP_NOW.begin() || !add()) {
log_e("Failed to initialize ESP-NOW or register the broadcast peer");
return false;
}
return true;
}
// Function to send a message to all devices within the network
bool send_message(const uint8_t *data, size_t len) {
if (!send(data, len)) {
log_e("Failed to broadcast message");
return false;
}
return true;
}
};
/* Global Variables */
//ブロードキャスト オブジェクトの定義
// Create a broadcast peer object
ESP_NOW_Broadcast_Peer broadcast_peer(ESP_NOW.BROADCAST_ADDR, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
//const MacAddress peer_mac({0x48, 0x27, 0xe2, 0xe3, 0xbd, 0x50});
//ESP_NOW_Broadcast_Peer broadcast_peer( peer_mac , ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
/* Main */
//ESP-NOWの初期化関数(バッファサイズ36バイト)
void esp_now_m_36byte_begin() {
// ESP-NOW 関連 ->
//WiFiの初期化
// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
delay(3000); //決め打ちなので、おかしかったら調整してね!
//ブロードキャストの初期化
// Register the broadcast peer
if (!broadcast_peer.begin()) {
delay(5000); //瞬リセット防止用
ESP.restart(); //リセット
} //endif
//テストメッセージを送る 相手側のバッファクリアする為
//先に何か送らないと相手側の接続制御が入らず、なぜか先頭パケットロストする?
broadcast_peer.send_message(txb, BLEN);delay(5);
broadcast_peer.send_message(txb, BLEN);delay(5);
broadcast_peer.send_message(txb, BLEN);delay(5);
// <- ESP-NOW 関連
} //esp_now_m_36byte_begin
スレーブ
スレーブ AE-ESP32-S2-MINI
//ESP32_NOW_s_3rgb_led_36_1_ae_esp32_s2_mini_1
//1対1に改造する 対応
//ブロードキャスト
//インクルド
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include "esp_now_s_36byte_begin.h"
//定義
#define NUM_LEDS 3
#define OUT_GPIO_NUM 2 //ESP32S2
Adafruit_NeoPixel strip(NUM_LEDS, OUT_GPIO_NUM, NEO_GRB + NEO_KHZ800);
//debug
/*
struct {
int R; //red
int G; //green
int B; //blue
} RGBs[3];
*/
//初期化
void setup() {
//ESP-NOW開始->
esp_now_s_36byte_begin();
//<-ESP-NOW終了
//ネオピクセルの設定
strip.begin();
strip.show();
RGBs[0].R = 0;
RGBs[0].G = 0;
RGBs[0].B = 0;
RGBs[1].R = 0;
RGBs[1].G = 0;
RGBs[1].B = 0;
RGBs[2].R = 0;
RGBs[2].G = 0;
RGBs[2].B = 0;
} //setup
//メインループ
void loop() {
/*
//表示 debug
Serial.print(SWs.sw1);
Serial.println();
*/
strip.setPixelColor(0, strip.Color(RGBs[0].R, RGBs[0].G, RGBs[0].B));
strip.setPixelColor(1, strip.Color(RGBs[1].R, RGBs[1].G, RGBs[1].B));
strip.setPixelColor(2, strip.Color(RGBs[2].R, RGBs[2].G, RGBs[2].B));
strip.show(); //再表示
delay(300); //ダミー 0.3秒待つ
} //loop
esp_now_s_36byte_begin.h
//インクルド
#include <Arduino.h>
#include <string.h> //memcpy
#include <ESP32_NOW.h>
#include <WiFi.h>
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
#include <vector>
#include <MacAddress.h>
//定義
//パケットの構造体 項目の追加、削除は、ここをいじる
struct {
int R; //red
int G; //green
int B; //blue
} RGBs[3];
#define BLEN sizeof(RGBs)
/* Definitions */
//WiFiの2.4Ghzのチャンネル
#define ESPNOW_WIFI_CHANNEL 1
//プロトタイプ宣言
int acceleration_num();
void acceleration_write(unsigned char *a);
void acceleration_read(unsigned char *a);
//クラス
/* Classes */
// Creating a new class that inherits from the ESP_NOW_Peer class is required.
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
public:
// Constructor of the class
ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk)
: ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}
// Destructor of the class
~ESP_NOW_Peer_Class() {}
// Function to register the master peer
bool add_peer() {
if (!add()) {
log_e("Failed to register the broadcast peer");
return false;
}
return true;
}
// Function to print the received messages from the master
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
//読み込みデータが36バイトの時
if (len == BLEN) {
//読み込みデータをセンサーの値に合成する
memcpy(&RGBs, data, BLEN); //センサーの値をセット
} //endif
}
};
/* Global Variables */
// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;
/* Callbacks */
// Callback called when an unknown peer sends a message
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
//自分に送られてきた (自分のMACアドレス)
unsigned char a_add[6] = { 0x48, 0x27, 0xe2, 0xe3, 0xbd, 0x50 };
if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {
//if (memcmp(info->des_addr, a_add, 6) == 0) {
//新しいピアを設定する
ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
masters.push_back(new_master);
if (!masters.back().add_peer()) {
return;
}
} else {
// The slave will only receive broadcast messages
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
log_v("Igorning the message");
}
}
//ESP-NOWの初期化関数(バッファサイズ12バイト)
void esp_now_s_36byte_begin() {
//WiFiの初期化
// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
/*
//シリアルの初期化 debug
Serial.begin(9600);
Serial.println("");
//シリアルの待ちが0.5*9
//delay(3000);//決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
for (int i = 0; i < (9 + 6); i++) {
delay(500); //接続待ち
Serial.print(".");
} //for
Serial.println("");
*/
delay(3000); //決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
//ブロードキャストの初期化
// Initialize the ESP-NOW protocol
if (!ESP_NOW.begin()) {
delay(5000); //瞬リセット防止用
ESP.restart(); //リセット
} //endif
//ESP-NOWのコールバック(割り込み)
// Register the new peer callback
ESP_NOW.onNewPeer(register_new_master, NULL);
} //esp_now_s_36byte_begin

