revised esp32<->esp32 broadcast comm.

This commit is contained in:
Dooho Yi 2022-04-26 01:47:29 +09:00
parent 85ed754955
commit 7db9b9a374
4 changed files with 104 additions and 81 deletions

@ -1 +1 @@
Subproject commit e5e7497cc468cb1814e490a721c6d32384561b88 Subproject commit 5d289fd8151796d5c1861b02360d3a8c8971fea0

View file

@ -16,8 +16,9 @@
//============<identities>============ //============<identities>============
// //
#define MY_GROUP_ID (10000) #define MY_GROUP_ID (10000)
#define MY_ID (MY_GROUP_ID + 1) #define MY_ID (MY_GROUP_ID + 2)
#define MY_SIGN ("AUDIOOOO") #define MY_SIGN ("AUDIOOOO")
#define ADDRESSBOOK_TITLE ("broadcast only")
// //
//============</identities>============ //============</identities>============
@ -35,15 +36,14 @@
// + then, network would be flooded by infinite duplicating msg., // + then, network would be flooded by infinite duplicating msg.,
// unless they stop reacting to 'known' req. for some seconds. (e.g. 3 seconds) // unless they stop reacting to 'known' req. for some seconds. (e.g. 3 seconds)
// //
// 'USE_ALTERNATIVE_ADDRESSES' // 'ADDRESSBOOK_TITLE'
// --> peer list limited max. 20. // --> peer list limited max. 20.
// so, we have alternative address book that covers after 20th. // so, we might use different address books for each node to cover a network of more than 20 nodes.
// //
//==========</list-of-configurations>========== //==========</list-of-configurations>==========
// //
#define DISABLE_AP #define DISABLE_AP
#define REPLICATE_NOTE_REQ #define REPLICATE_NOTE_REQ
#define USE_ALTERNATIVE_ADDRESSES
//============<audioooo-param>============ //============<audioooo-param>============
#define GAIN_MAX 1.0 // if 1.0 is too loud, give max. limit here. #define GAIN_MAX 1.0 // if 1.0 is too loud, give max. limit here.
@ -256,7 +256,29 @@ void repeat() {
memcpy(frm + 1, (uint8_t *) &note_now, sizeof(Note)); memcpy(frm + 1, (uint8_t *) &note_now, sizeof(Note));
frm[frm_size - 1] = ']'; frm[frm_size - 1] = ']';
// //
esp_now_send(NULL, frm, frm_size); // to all peers in the list. // strange but following didn't work as expected. (instead, i have to send one-by-one.)
// esp_now_send(NULL, frm, frm_size); // to all peers in the list.
// so, forget about peer list -> just pick a broadcast peer to be sent.
uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_send(broadcastmac, frm, frm_size);
// (DEBUG) fetch full peer list
esp_now_peer_num_t num;
esp_now_get_peer_num(&num);
esp_now_peer_info_t peer_info;
bool first = true;
for (uint8_t ii = 0; ii < num.total_num; ii++) {
esp_now_fetch_peer(first, &peer_info);
if (first) first = false;
MONITORING_SERIAL.printf("# peer: %02X:%02X:%02X:%02X:%02X:%02X\n",
peer_info.peer_addr[0],
peer_info.peer_addr[1],
peer_info.peer_addr[2],
peer_info.peer_addr[3],
peer_info.peer_addr[4],
peer_info.peer_addr[5]);
}
// //
MONITORING_SERIAL.print("repeat! ==> "); MONITORING_SERIAL.print("repeat! ==> ");
MONITORING_SERIAL.println(note_now.to_string()); MONITORING_SERIAL.println(note_now.to_string());
@ -298,21 +320,6 @@ void hello() {
if (hello_delay < 100) hello_delay = 100; if (hello_delay < 100) hello_delay = 100;
hello_task.restartDelayed(hello_delay); hello_task.restartDelayed(hello_delay);
} }
// //TEST
// Note n = {
// 10001, // int32_t id;
// 1, // float pitch;
// 127, // float velocity;
// 1, // float onoff;
// 0, // float x1;
// 0, // float x2;
// 0, // float x3;
// 0, // float x4;
// 5000 // float ps;
// };
// note_now = n;
// repeat_task.restart();
} }
Task hello_task(0, TASK_ONCE, &hello, &runner, false); Task hello_task(0, TASK_ONCE, &hello, &runner, false);
@ -372,7 +379,7 @@ void onNoteHandler(Note & n) {
void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len) { void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len) {
// //
//MONITORING_SERIAL.write(incomingData, len); // MONITORING_SERIAL.write(incomingData, len);
// //
#if defined(HAVE_CLIENT) #if defined(HAVE_CLIENT)
@ -402,7 +409,6 @@ void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len
} }
} }
//
MONITORING_SERIAL.println(note.to_string()); MONITORING_SERIAL.println(note.to_string());
#if defined(REPLICATE_NOTE_REQ) #if defined(REPLICATE_NOTE_REQ)
@ -417,7 +423,11 @@ void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len
// on 'sent' // on 'sent'
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sendStatus) { void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sendStatus) {
if (sendStatus != 0) MONITORING_SERIAL.println("Delivery failed!"); // well, i think this cb should be called once for EVERY single TX attempts,
// but in reality, it doesn't get called that often.
// i think this is sorta bug. but have no clear clue.
MONITORING_SERIAL.printf("* delivery attempt! ~~~>>> %02X:%02X:%02X:%02X:%02X:%02X\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
if (sendStatus != 0) MONITORING_SERIAL.printf("* ==>> FAILED :(\n\n");
} }
// SD TEST // SD TEST
@ -505,6 +515,7 @@ void setup() {
Serial.println("-"); Serial.println("-");
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\"");
Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL)); Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL));
Serial.println("- my peer book ==> \"" + String(ADDRESSBOOK_TITLE) + "\"");
#if defined(HAVE_CLIENT) #if defined(HAVE_CLIENT)
Serial.println("- ======== 'HAVE_CLIENT' ========"); Serial.println("- ======== 'HAVE_CLIENT' ========");
#endif #endif
@ -543,20 +554,16 @@ void setup() {
// uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// //
// // // //
// esp_now_peer_info_t peerInfo; // esp_now_peer_info_t peerInfo = {};
// memcpy(peerInfo.peer_addr, broadcastmac, 6); // memcpy(peerInfo.peer_addr, broadcastmac, 6);
// peerInfo.channel = 0; // peerInfo.channel = 0;
// peerInfo.encrypt = false; // peerInfo.encrypt = false;
// esp_now_add_peer(&peerInfo); // esp_now_add_peer(&peerInfo);
#if defined(USE_ALTERNATIVE_ADDRESSES) AddressBook * book = lib.getBookByTitle(ADDRESSBOOK_TITLE);
AddressBook * book = lib.getBookByTitle("audioooo alt");
#else
AddressBook * book = lib.getBookByTitle("audioooo");
#endif
for (int idx = 0; idx < book->list.size(); idx++) { for (int idx = 0; idx < book->list.size(); idx++) {
Serial.println("- ! (esp_now_add_peer) ==> add a '" + book->list[idx].name + "'."); Serial.println("- ! (esp_now_add_peer) ==> add a '" + book->list[idx].name + "'.");
esp_now_peer_info_t peerInfo; esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, book->list[idx].mac, 6); memcpy(peerInfo.peer_addr, book->list[idx].mac, 6);
peerInfo.channel = 0; peerInfo.channel = 0;
peerInfo.encrypt = false; peerInfo.encrypt = false;

@ -1 +1 @@
Subproject commit e5e7497cc468cb1814e490a721c6d32384561b88 Subproject commit 5d289fd8151796d5c1861b02360d3a8c8971fea0

View file

@ -15,9 +15,10 @@
//============<identities>============ //============<identities>============
// //
#define MY_GROUP_ID (70000) #define MY_GROUP_ID (70000)
#define MY_ID (MY_GROUP_ID + 1) #define MY_ID (MY_GROUP_ID + 1)
#define MY_SIGN ("Q") #define MY_SIGN ("Q")
#define ADDRESSBOOK_TITLE ("broadcast only")
// //
//============</identities>============ //============</identities>============
@ -38,15 +39,14 @@
// 'GEN_NOTE_REQ' // 'GEN_NOTE_REQ'
// --> this will generate 'note' msg. // --> this will generate 'note' msg.
// //
// 'USE_ALTERNATIVE_ADDRESSES' // 'ADDRESSBOOK_TITLE'
// --> peer list limited max. 20. // --> peer list limited max. 20.
// so, we have alternative address book that covers after 20th. // so, we might use different address books for each node to cover a network of more than 20 nodes.
// //
//==========</list-of-configurations>========== //==========</list-of-configurations>==========
// //
#define DISABLE_AP #define DISABLE_AP
#define GEN_NOTE_REQ #define GEN_NOTE_REQ
// #define USE_ALTERNATIVE_ADDRESSES
//============<parameters>============ //============<parameters>============
// //
@ -234,7 +234,7 @@ void screen() {
// create a NOTE req. and send it out. // create a NOTE req. and send it out.
Note note_composed = { Note note_composed = {
10001, // int32_t id; 10000, // int32_t id;
song_request, // float pitch; song_request, // float pitch;
127, // float velocity; 127, // float velocity;
1, // float onoff; 1, // float onoff;
@ -244,6 +244,9 @@ void screen() {
0, // float x4; 0, // float x4;
0 // float ps; 0 // float ps;
}; };
MONITORING_SERIAL.print("# posting a req.# ==> ");
MONITORING_SERIAL.println(note_composed.to_string());
// //
uint8_t frm_size = sizeof(Note) + 2; uint8_t frm_size = sizeof(Note) + 2;
uint8_t frm[frm_size]; uint8_t frm[frm_size];
@ -251,11 +254,29 @@ void screen() {
memcpy(frm + 1, (uint8_t *) &note_composed, sizeof(Note)); memcpy(frm + 1, (uint8_t *) &note_composed, sizeof(Note));
frm[frm_size - 1] = ']'; frm[frm_size - 1] = ']';
// //
esp_now_send(NULL, frm, frm_size); // to all peers in the list. // strange but following didn't work as expected. (instead, i have to send one-by-one.)
// // esp_now_send(NULL, frm, frm_size); // to all peers in the list.
MONITORING_SERIAL.print("# posting a req.# ==> ");
MONITORING_SERIAL.println(note_composed.to_string()); // so, forget about peer list -> just pick a broadcast peer to be sent.
// uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_send(broadcastmac, frm, frm_size);
// (DEBUG) fetch full peer list
esp_now_peer_num_t num;
esp_now_get_peer_num(&num);
esp_now_peer_info_t peer_info;
bool first = true;
for (uint8_t ii = 0; ii < num.total_num; ii++) {
esp_now_fetch_peer(first, &peer_info);
if (first) first = false;
MONITORING_SERIAL.printf("# peer: %02X:%02X:%02X:%02X:%02X:%02X\n",
peer_info.peer_addr[0],
peer_info.peer_addr[1],
peer_info.peer_addr[2],
peer_info.peer_addr[3],
peer_info.peer_addr[4],
peer_info.peer_addr[5]);
}
//+ play start for myself //+ play start for myself
sample_now = song_request; sample_now = song_request;
@ -428,7 +449,29 @@ void repeat() {
memcpy(frm + 1, (uint8_t *) &note_now, sizeof(Note)); memcpy(frm + 1, (uint8_t *) &note_now, sizeof(Note));
frm[frm_size - 1] = ']'; frm[frm_size - 1] = ']';
// //
esp_now_send(NULL, frm, frm_size); // to all peers in the list. // strange but following didn't work as expected. (instead, i have to send one-by-one.)
// esp_now_send(NULL, frm, frm_size); // to all peers in the list.
// so, forget about peer list -> just pick a broadcast peer to be sent.
uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_send(broadcastmac, frm, frm_size);
// (DEBUG) fetch full peer list
esp_now_peer_num_t num;
esp_now_get_peer_num(&num);
esp_now_peer_info_t peer_info;
bool first = true;
for (uint8_t ii = 0; ii < num.total_num; ii++) {
esp_now_fetch_peer(first, &peer_info);
if (first) first = false;
MONITORING_SERIAL.printf("# peer: %02X:%02X:%02X:%02X:%02X:%02X\n",
peer_info.peer_addr[0],
peer_info.peer_addr[1],
peer_info.peer_addr[2],
peer_info.peer_addr[3],
peer_info.peer_addr[4],
peer_info.peer_addr[5]);
}
// //
MONITORING_SERIAL.print("repeat! ==> "); MONITORING_SERIAL.print("repeat! ==> ");
MONITORING_SERIAL.println(note_now.to_string()); MONITORING_SERIAL.println(note_now.to_string());
@ -470,33 +513,6 @@ void hello() {
if (hello_delay < 100) hello_delay = 100; if (hello_delay < 100) hello_delay = 100;
hello_task.restartDelayed(hello_delay); hello_task.restartDelayed(hello_delay);
} }
// //TEST
// {
// Note note_composed = {
// 10001, // int32_t id;
// 1, // float pitch;
// 127, // float velocity;
// 1, // float onoff;
// 0, // float x1;
// 0, // float x2;
// 0, // float x3;
// 0, // float x4;
// 5000 // float ps;
// };
// //
// uint8_t frm_size = sizeof(Note) + 2;
// uint8_t frm[frm_size];
// frm[0] = '[';
// memcpy(frm + 1, (uint8_t *) &note_composed, sizeof(Note));
// frm[frm_size - 1] = ']';
// //
// esp_now_send(NULL, frm, frm_size); // to all peers in the list.
// //
// MONITORING_SERIAL.print("# posting a req.# ==> ");
// MONITORING_SERIAL.println(note_composed.to_string());
// //
// }
} }
Task hello_task(0, TASK_ONCE, &hello, &runner, false); Task hello_task(0, TASK_ONCE, &hello, &runner, false);
@ -593,13 +609,16 @@ void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len
new_note_time = millis(); new_note_time = millis();
} }
#endif #endif
} }
} }
// on 'sent' // on 'sent'
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sendStatus) { void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sendStatus) {
if (sendStatus != 0) MONITORING_SERIAL.println("Delivery failed!"); // well, i think this cb should be called once for EVERY single TX attempts,
// but in reality, it doesn't get called that often.
// i think this is sorta bug. but have no clear clue.
MONITORING_SERIAL.printf("* delivery attempt! ~~~>>> %02X:%02X:%02X:%02X:%02X:%02X\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
if (sendStatus != 0) MONITORING_SERIAL.printf("* ==>> FAILED :(\n\n");
} }
// SD TEST // SD TEST
@ -695,6 +714,7 @@ void setup() {
Serial.println("-"); Serial.println("-");
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\"");
Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL)); Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL));
Serial.println("- my peer book ==> \"" + String(ADDRESSBOOK_TITLE) + "\"");
#if defined(HAVE_CLIENT) #if defined(HAVE_CLIENT)
Serial.println("- ======== 'HAVE_CLIENT' ========"); Serial.println("- ======== 'HAVE_CLIENT' ========");
#endif #endif
@ -733,20 +753,16 @@ void setup() {
// uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// //
// // // //
// esp_now_peer_info_t peerInfo; // esp_now_peer_info_t peerInfo = {};
// memcpy(peerInfo.peer_addr, broadcastmac, 6); // memcpy(peerInfo.peer_addr, broadcastmac, 6);
// peerInfo.channel = 0; // peerInfo.channel = 0;
// peerInfo.encrypt = false; // peerInfo.encrypt = false;
// esp_now_add_peer(&peerInfo); // esp_now_add_peer(&peerInfo);
#if defined(USE_ALTERNATIVE_ADDRESSES) AddressBook * book = lib.getBookByTitle(ADDRESSBOOK_TITLE);
AddressBook * book = lib.getBookByTitle("audioooo alt");
#else
AddressBook * book = lib.getBookByTitle("audioooo");
#endif
for (int idx = 0; idx < book->list.size(); idx++) { for (int idx = 0; idx < book->list.size(); idx++) {
Serial.println("- ! (esp_now_add_peer) ==> add a '" + book->list[idx].name + "'."); Serial.println("- ! (esp_now_add_peer) ==> add a '" + book->list[idx].name + "'.");
esp_now_peer_info_t peerInfo; esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, book->list[idx].mac, 6); memcpy(peerInfo.peer_addr, book->list[idx].mac, 6);
peerInfo.channel = 0; peerInfo.channel = 0;
peerInfo.encrypt = false; peerInfo.encrypt = false;