From 81eebf1cffa5ddcbc74d0df96df11f734842f76c Mon Sep 17 00:00:00 2001 From: AndreyDanyliuk Date: Wed, 6 May 2026 09:34:32 +0200 Subject: [PATCH] Initial commit --- firmware.ino | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ peers.ino | 29 ++++++++++++++++ utils.ino | 36 ++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 firmware.ino create mode 100644 peers.ino create mode 100644 utils.ino diff --git a/firmware.ino b/firmware.ino new file mode 100644 index 0000000..2f37771 --- /dev/null +++ b/firmware.ino @@ -0,0 +1,94 @@ +#include +#include +#include + +void readMacAddress(){ + uint8_t baseMac[6]; + esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac); + if (ret == ESP_OK) { + Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n", + baseMac[0], baseMac[1], baseMac[2], + baseMac[3], baseMac[4], baseMac[5]); + } else { + Serial.println("Failed to read MAC address"); + } +} + + + +esp_now_peer_info_t peerInfo; +// String send_data = ""; + +// Callback when data is sent +void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { + if (status != ESP_NOW_SEND_SUCCESS) { + Serial.println("SENDMSGERR"); + } else { + Serial.println("SENDMSGDONE"); + } +} + +// Callback when data is received +void OnDataRecv(uint8_t * mac, uint8_t *incomingData, int len) { + String message = String((char *)incomingData).substring(0, len); + if (message == "PING") { + AutoSendMessageToPeer("PONG", mac); + } else { + //Serial.printf("INMSG %02x:%02x:%02x:%02x:%02x:%02x", + // mac[0], mac[1], mac[2], + // mac[3], mac[4], mac[5]); + Serial.print("MI"); + for (int i = 0; i < 6; i++) { + if (i > 0) Serial.print(":"); + if (mac[i] < 16) Serial.print("0"); + Serial.print(mac[i], HEX); + } + Serial.print("D"); + Serial.println(message); + } +} + +void setup(){ + Serial.begin(115200); + + WiFi.mode(WIFI_STA); + WiFi.STA.begin(); + + Serial.print("C"); + readMacAddress(); + + if (esp_now_init() != ESP_OK) { + Serial.println("ELC1500"); + return; + } + + esp_now_register_send_cb(esp_now_send_cb_t(OnDataSent)); + esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv)); + +} + +String waitForInput() { + while(Serial.available() == 0) {} + String input = Serial.readString(); + return input; +} + +void loop() { + String input = waitForInput(); + if (input == "C" || input == "C\n") { + Serial.print("C"); + readMacAddress(); + } else if (input.startsWith("S")) { + if (input.startsWith("ST")) { + String receiverMacStr = input.substring(2,19); + uint8_t receiverMac[6]; + + macStringToBytes(receiverMacStr, receiverMac); + if (input.charAt(19) == 'D') { + String msgtxt = input.substring(20); + AutoSendMessageToPeer(msgtxt, receiverMac); + + } + } + } +} \ No newline at end of file diff --git a/peers.ino b/peers.ino new file mode 100644 index 0000000..775ab7a --- /dev/null +++ b/peers.ino @@ -0,0 +1,29 @@ +void addPeer(uint8_t *mac_addr) { + esp_now_peer_info_t peerInfo; + memset(&peerInfo, 0, sizeof(peerInfo)); + memcpy(peerInfo.peer_addr, mac_addr, 6); + peerInfo.channel = 0; + peerInfo.encrypt = false; + + if (esp_now_add_peer(&peerInfo) != ESP_OK) { + Serial.println("ELC1344"); + } +} + +void removePeer(uint8_t *mac_addr) { + if (esp_now_del_peer(mac_addr) != ESP_OK) { + Serial.println("ELC1343"); + } +} + +void SendMessageToPeer(const String payload, uint8_t *castAddr) +{ + esp_err_t result = esp_now_send(castAddr, (uint8_t*)payload.c_str(), payload.length()); + // send_data = payload; +} + +void AutoSendMessageToPeer(const String payload, uint8_t *castAddr) { + addPeer(castAddr); + SendMessageToPeer(payload, castAddr); + removePeer(castAddr); +} \ No newline at end of file diff --git a/utils.ino b/utils.ino new file mode 100644 index 0000000..cbe0e72 --- /dev/null +++ b/utils.ino @@ -0,0 +1,36 @@ +void splitBySpaces(String input, String parts[], int maxSpaces) { + int partIndex = 0; + int startIndex = 0; + + for (int i = 0; i < input.length() && partIndex < maxSpaces; i++) { + if (input.charAt(i) == ' ') { + parts[partIndex++] = input.substring(startIndex, i); + startIndex = i + 1; + } + } + + parts[partIndex] = input.substring(startIndex); + + for (int i = partIndex + 1; i <= maxSpaces; i++) { + parts[i] = ""; + } +} + +int countSpaces(String s) { + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == ' ') { + count++; + } + } + return count; +} + +void macStringToBytes(String macStr, uint8_t macBytes[6]) { + int byteIndex = 0; + + for (int i = 0; i < 6; i++) { + String byteString = macStr.substring(i * 3, i * 3 + 2); // "28", "05", ... + macBytes[i] = strtoul(byteString.c_str(), NULL, 16); // из hex в uint8_t + } +} \ No newline at end of file