налажена связь между 2 esp

This commit is contained in:
2026-08-07 01:34:18 +03:00
parent 1edc420b77
commit fb40f3ddef
9 changed files with 197 additions and 47 deletions

View File

@@ -10,17 +10,36 @@ static const uint8_t BROADCAST_MAC[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
bool Radio::begin() {
if (active_) { Log::event("ESP-NOW", "already active"); return true; }
WiFi.mode(WIFI_STA); WiFi.disconnect();
// Keep both devices on a dedicated, fixed STA channel. Stored access-point
// credentials must not reconnect in the background and move ESP-NOW to the
// AP's channel.
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(false);
WiFi.disconnect(false, false);
WiFi.setSleep(false);
const bool txPowerOk = WiFi.setTxPower(WIFI_POWER_8_5dBm);
delay(10);
if (esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
Log::event("ESP-NOW", "Wi-Fi channel setup FAILED"); return false;
}
queue_ = xQueueCreate(8, sizeof(ReceivedPacket));
if (!queue_ || esp_now_init() != ESP_OK) { Log::event("ESP-NOW", "initialization FAILED"); return false; }
if (!queue_) { Log::event("ESP-NOW", "receive queue creation FAILED"); return false; }
if (esp_now_init() != ESP_OK) {
vQueueDelete(queue_); queue_ = nullptr;
Log::event("ESP-NOW", "initialization FAILED"); return false;
}
const esp_err_t rateResult = esp_wifi_config_espnow_rate(WIFI_IF_STA, WIFI_PHY_RATE_1M_L);
instance_ = this;
if (esp_now_register_recv_cb(onReceive) != ESP_OK) { end(); return false; }
active_ = true;
const bool ok = ensurePeer(BROADCAST_MAC);
Log::printf("ESP-NOW", "started channel=%u broadcast-peer=%s", ESPNOW_WIFI_CHANNEL, ok ? "OK" : "FAILED");
uint8_t primaryChannel = 0;
wifi_second_chan_t secondaryChannel = WIFI_SECOND_CHAN_NONE;
esp_wifi_get_channel(&primaryChannel, &secondaryChannel);
Log::printf("ESP-NOW", "started channel=%u expected=%u rate=1M%s tx-power=max%s broadcast-peer=%s",
primaryChannel, ESPNOW_WIFI_CHANNEL, rateResult == ESP_OK ? "" : "-FAILED",
txPowerOk ? "" : "-FAILED", ok ? "OK" : "FAILED");
return ok;
}
@@ -34,13 +53,17 @@ void Radio::end() {
bool Radio::ensurePeer(const uint8_t mac[6]) {
if (esp_now_is_peer_exist(mac)) return true;
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, mac, 6); peer.channel = ESPNOW_WIFI_CHANNEL; peer.encrypt = false;
memcpy(peer.peer_addr, mac, 6);
peer.channel = ESPNOW_WIFI_CHANNEL;
peer.ifidx = WIFI_IF_STA;
peer.encrypt = false;
return esp_now_add_peer(&peer) == ESP_OK;
}
bool Radio::sendBroadcast(ProtocolPacket p) { return sendTo(BROADCAST_MAC, p); }
bool Radio::sendTo(const uint8_t mac[6], ProtocolPacket p) {
maintainChannel();
char peer[20]; macText(mac, peer, sizeof(peer));
if (!active_ || !ensurePeer(mac)) {
Log::printf("ESP-NOW", "TX %s to %s FAILED: inactive/peer", messageName(static_cast<MessageType>(p.type)), peer);
@@ -48,15 +71,32 @@ bool Radio::sendTo(const uint8_t mac[6], ProtocolPacket p) {
}
finalizePacket(p);
const bool ok = esp_now_send(mac, reinterpret_cast<const uint8_t *>(&p), sizeof(p)) == ESP_OK;
Log::printf("ESP-NOW", "TX %s to %s session=%08lX stage=%u seq=%u %s",
messageName(static_cast<MessageType>(p.type)), peer, p.session, p.stage, p.sequence, ok ? "QUEUED" : "FAILED");
const MessageType type = static_cast<MessageType>(p.type);
if (type != MessageType::HEARTBEAT && type != MessageType::HEARTBEAT_ACK)
Log::printf("ESP-NOW", "TX %s to %s session=%08lX stage=%u seq=%u %s",
messageName(type), peer, p.session, p.stage, p.sequence, ok ? "QUEUED" : "FAILED");
return ok;
}
bool Radio::receive(ReceivedPacket &r) {
maintainChannel();
return queue_ && xQueueReceive(queue_, &r, 0) == pdTRUE;
}
void Radio::maintainChannel() {
if (!active_) return;
const uint32_t now = millis();
if (now - lastChannelCheckMs_ < 250) return;
lastChannelCheckMs_ = now;
uint8_t primaryChannel = 0;
wifi_second_chan_t secondaryChannel = WIFI_SECOND_CHAN_NONE;
if (esp_wifi_get_channel(&primaryChannel, &secondaryChannel) != ESP_OK ||
primaryChannel == ESPNOW_WIFI_CHANNEL) return;
const bool restored = esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE) == ESP_OK;
Log::printf("ESP-NOW", "channel drift %u->%u %s", primaryChannel, ESPNOW_WIFI_CHANNEL,
restored ? "RESTORED" : "FAILED");
}
void Radio::flush() { if (queue_) xQueueReset(queue_); }
void Radio::onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int length) {