Initial commit

This commit is contained in:
2026-05-06 09:34:32 +02:00
commit 81eebf1cff
3 changed files with 159 additions and 0 deletions

94
firmware.ino Normal file
View File

@@ -0,0 +1,94 @@
#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
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);
}
}
}
}

29
peers.ino Normal file
View File

@@ -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);
}

36
utils.ino Normal file
View File

@@ -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
}
}