1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| /***************************************************/
/* */
/* Gestion d'un réseau MESH avec des ESP32 */
/* */
/***************************************************/
#include <WiFi.h>
#include <esp_now.h>
#define _LED_ 2
#define _NBRE_MAC_ 2 // Nombre d'adresses MAC
/**************************************/
/* */
/* Déclaration des Constantes */
/* */
/**************************************/
const uint8_t addrMac[_NBRE_MAC_][6] = {
{0xC8,0xC9,0xA3,0xFA,0xB9,0x68}, // 0=Emetteur
{0x24,0x6F,0x28,0x22,0xB2,0xB0} // 1=Recepteur
};
/*************************************/
/* */
/* Déclaration des Variables */
/* */
/*************************************/
int _index; // identification 0:Emetteur 1:Recepteur
bool _flipflop; // Alternance
char _mess[16]; // Message
/*************************************/
/* */
/* Déclaration des Fonctions */
/* */
/*************************************/
int find_Mac(void)
{
unsigned int _val;
uint8_t _mac[6];
bool _tst;
for (short i=0; i<6; i++)
{
_val=(ESP.getEfuseMac() >> (i*8)) & 0x000000FF;
_mac[i] = _val;
}
for (short i=0; i<_NBRE_MAC_; i++)
{
_tst=true;
for (short j=0; j<6; j++)
{ if (addrMac[i][j] != _mac[j]) { _tst=false; } }
if (_tst) { return i; }
}
return -1;
}
void onRecv(const esp_now_recv_info_t *mac, const uint8_t *data, int len)
{
memcpy(_mess, data,len);
_mess[len] = '\0';
if (strcmp(_mess, "ON") == 0) { digitalWrite(_LED_, HIGH); }
if (strcmp(_mess, "OFF") == 0) { digitalWrite(_LED_, LOW); }
}
void Send(void)
{
uint8_t mac[6];
memcpy(mac, addrMac[(1-_index)], 6);
if (_flipflop) { strcpy(_mess, "OFF"); _flipflop=false; }
else { strcpy(_mess, "ON"); _flipflop=true; }
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, mac, 6);
peer.channel = 0;
peer.encrypt = false;
if (!esp_now_is_peer_exist(mac)) esp_now_add_peer(&peer);
if (esp_now_send(mac, (const uint8_t *)_mess, strlen(_mess)+1) == ESP_OK)
{
Serial.print("Envoi de : >");
Serial.print(_mess);
Serial.println("<");
}
}
/*********************/
/* */
/* Démarrage */
/* */
/*********************/
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.write(12); /* Clear Screen */
Serial.println("\e[1;31m+-------------+\e[0m");
Serial.println("\e[1;31m| Réseau MESH |\e[0m");
Serial.println("\e[1;31m+-------------+\e[0m\r\n");
pinMode(_LED_, OUTPUT);
digitalWrite(_LED_, LOW);
_flipflop=true;
_index = find_Mac();
Serial.print("ESP32 : ");
switch (_index)
{
case 0: Serial.println("Emetteur"); break;
case 1: Serial.println("Recepteur"); break;
default: Serial.println("Erreur !"); break;
}
WiFi.mode(WIFI_STA);
esp_now_init();
esp_now_register_recv_cb(onRecv);
}
/****************************/
/* */
/* Boucle Itérative */
/* */
/****************************/
void loop()
{
if (_index == 0) { Send(); }
delay(1000);
} |
Partager