453 lines
14 KiB
C
453 lines
14 KiB
C
#include "assert.h"
|
|
#include "driver/gpio.h"
|
|
#include "driver/uart.h"
|
|
#include "esp_log.h"
|
|
#include "esp_phy_init.h"
|
|
#include "esp_rom_gpio.h"
|
|
#include "esp_timer.h"
|
|
#include "esp_wifi.h"
|
|
#include "hal/uart_types.h"
|
|
#include "nvs_flash.h"
|
|
#include "portmacro.h"
|
|
#include <esp_event.h>
|
|
#include <esp_now.h>
|
|
#include <esp_wifi.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/queue.h>
|
|
#include <freertos/task.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "main.h"
|
|
|
|
#define MASTER_MODE_PIN GPIO_NUM_23 // Jumper-Erkennungspin
|
|
#define MASTER_UART UART_NUM_2
|
|
#define BROADCAST_INTERVAL_MS 500
|
|
|
|
#define BUF_SIZE (1024)
|
|
#define TXD_PIN (GPIO_NUM_17)
|
|
#define RXD_PIN (GPIO_NUM_16)
|
|
|
|
#define CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout
|
|
#define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen
|
|
|
|
uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
|
0xFF, 0xFF, 0xFF};
|
|
#define IS_BROADCAST_ADDR(addr) \
|
|
(memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
|
|
|
typedef enum {
|
|
BroadCastPage,
|
|
StatusPage,
|
|
PingPage,
|
|
RegisterPage,
|
|
} CommandPages;
|
|
|
|
typedef struct {
|
|
uint32_t uptime;
|
|
uint8_t status;
|
|
} StatusPayload;
|
|
|
|
typedef struct {
|
|
uint32_t timestamp;
|
|
} PingPayload;
|
|
|
|
typedef struct {
|
|
} BroadCastPayload;
|
|
|
|
typedef struct {
|
|
bool familierClient;
|
|
} RegisterPayload;
|
|
|
|
typedef union {
|
|
StatusPayload status_payload;
|
|
PingPayload ping_payload;
|
|
BroadCastPayload broadcast_payload;
|
|
RegisterPayload register_payload;
|
|
} PayloadUnion;
|
|
|
|
typedef struct {
|
|
uint16_t version;
|
|
CommandPages commandPage;
|
|
uint16_t length;
|
|
PayloadUnion payload;
|
|
} BaseMessage;
|
|
|
|
static_assert(sizeof(BaseMessage) <= 255,
|
|
"BaseMessage darf nicht größer als 255 sein");
|
|
|
|
QueueHandle_t messageQueue; // Warteschlange für empfangene Nachrichten
|
|
const char *TAG = "ALOX";
|
|
|
|
typedef struct {
|
|
uint8_t macAddr[ESP_NOW_ETH_ALEN];
|
|
int rssi;
|
|
bool isAvailable;
|
|
TickType_t lastSuccessfullPing;
|
|
} ClientInfo;
|
|
|
|
#define MAX_CLIENTS 19
|
|
ClientInfo clients[MAX_CLIENTS];
|
|
size_t numClients = 0;
|
|
size_t activeClients = 0;
|
|
bool hasMaster = false;
|
|
|
|
// return any inactive client field for new usage
|
|
int getNextFreeClientId() {
|
|
for (int i = 0; i < numClients; i++) {
|
|
if (!clients[i].isAvailable) {
|
|
return i;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void add_peer(uint8_t *macAddr) {
|
|
esp_now_peer_info_t peerInfo = {
|
|
.channel =
|
|
0, // Standardkanal, sollte mit den anderen Geräten übereinstimmen
|
|
.ifidx = ESP_IF_WIFI_STA,
|
|
.encrypt = false, // Keine Verschlüsselung (kann geändert werden)
|
|
};
|
|
memcpy(peerInfo.peer_addr, macAddr, ESP_NOW_ETH_ALEN);
|
|
|
|
esp_err_t result = esp_now_add_peer(&peerInfo);
|
|
if (result == ESP_OK) {
|
|
ESP_LOGI(TAG, "Peer added: %02X:%02X:%02X:%02X:%02X:%02X", macAddr[0],
|
|
macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
|
|
|
if (!IS_BROADCAST_ADDR(macAddr)) {
|
|
|
|
if (numClients >= MAX_CLIENTS) {
|
|
ESP_LOGW(TAG, "Cannot add more clients, maximum reached.");
|
|
return;
|
|
}
|
|
|
|
ClientInfo newClient = {};
|
|
memcpy(newClient.macAddr, macAddr, ESP_NOW_ETH_ALEN);
|
|
newClient.isAvailable = true;
|
|
newClient.lastSuccessfullPing = xTaskGetTickCount();
|
|
clients[getNextFreeClientId()] = newClient;
|
|
ESP_LOGI(TAG, "New client added.");
|
|
}
|
|
} else if (result == ESP_ERR_ESPNOW_EXIST) {
|
|
ESP_LOGW(TAG, "Peer already exists.");
|
|
// Überprüfen, ob der Client bereits existiert
|
|
for (int i = 0; i < numClients; i++) {
|
|
if (memcmp(clients[i].macAddr, macAddr, ESP_NOW_ETH_ALEN) == 0) {
|
|
ESP_LOGI(TAG, "Client found again, welcome back!");
|
|
clients[i].isAvailable = true; // Reaktiviere den Client
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result));
|
|
}
|
|
}
|
|
|
|
// UNSAFE ACCROSS THREADS BUT EZ TO USE
|
|
const char *MACtoString(uint8_t *macAddr) {
|
|
static char output[18]; // 17 Zeichen + 1 für Nullterminierung
|
|
sprintf(output, "%02X:%02X:%02X:%02X:%02X:%02X", macAddr[0], macAddr[1],
|
|
macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
|
return output;
|
|
}
|
|
|
|
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
|
size_t payload_size) {
|
|
BaseMessage message;
|
|
|
|
// Initialisierung der BaseMessage
|
|
message.commandPage = commandPage;
|
|
message.version = 1;
|
|
message.length = (uint16_t)payload_size;
|
|
|
|
// Kopieren des Payloads in die Union
|
|
memset(&message.payload, 0, sizeof(message.payload)); // Sicherheitsmaßnahme
|
|
memcpy(&message.payload, &payload, payload_size);
|
|
|
|
return message;
|
|
}
|
|
|
|
void master_broadcast_task(void *param) {
|
|
while (1) {
|
|
BroadCastPayload payload = {};
|
|
|
|
BaseMessage message = MessageBuilder(
|
|
BroadCastPage, *(PayloadUnion *)&payload, sizeof(payload));
|
|
|
|
ESP_ERROR_CHECK(esp_now_send(broadcast_address, (uint8_t *)&message,
|
|
sizeof(BaseMessage)));
|
|
ESP_LOGI(TAG, "Broadcast Message sent");
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
|
}
|
|
}
|
|
|
|
void master_ping_task(void *param) {
|
|
while (1) {
|
|
for (size_t i = 0; i < MAX_CLIENTS; ++i) {
|
|
if (clients[i].isAvailable) {
|
|
PingPayload payload = {};
|
|
payload.timestamp = esp_timer_get_time();
|
|
BaseMessage message = MessageBuilder(
|
|
PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
|
esp_now_send(clients[i].macAddr, (uint8_t *)&message,
|
|
sizeof(BaseMessage));
|
|
ESP_LOGI(TAG, "SENDING PING!!!!");
|
|
}
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
}
|
|
}
|
|
|
|
void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|
const uint8_t *data, int data_len) {
|
|
ESP_LOGI(TAG, "MASTER GOT MESSAGE");
|
|
ESP_LOGI(TAG, "Message: %.*s", data_len, data);
|
|
|
|
const BaseMessage *message = (const BaseMessage *)data;
|
|
switch (message->commandPage) {
|
|
case StatusPage:
|
|
ESP_LOGI(TAG, "GOT STATUS MESSAGE");
|
|
break;
|
|
case PingPage:
|
|
ESP_LOGI(TAG, "GOT PING MESSAGE");
|
|
uint32_t currentTime = esp_timer_get_time();
|
|
uint32_t diff = currentTime - message->payload.ping_payload.timestamp;
|
|
|
|
ESP_LOGI(TAG, "Start: %lu, End: %lu, Diff: %lu, Ping: %lu",
|
|
message->payload.ping_payload.timestamp, currentTime, diff,
|
|
diff / 1000); // ping in ms
|
|
|
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
// Überprüfen, ob der Client existiert und die MAC-Adresse übereinstimmt
|
|
if (clients[i].isAvailable &&
|
|
memcmp(clients[i].macAddr, esp_now_info->src_addr,
|
|
ESP_NOW_ETH_ALEN) == 0) {
|
|
clients[i].lastSuccessfullPing = xTaskGetTickCount();
|
|
ESP_LOGI(TAG, "Updated client %d last ping time to %lu", i,
|
|
clients[i].lastSuccessfullPing);
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
case BroadCastPage:
|
|
ESP_LOGI(TAG, "MASTER SHOULD NOT GET BROADCAST MESSAGE, is there another "
|
|
"master calling?");
|
|
break;
|
|
case RegisterPage:
|
|
ESP_LOGI(TAG, "WILL REGISTER DEVICE");
|
|
esp_now_peer_info_t checkPeerInfo;
|
|
esp_err_t checkPeer =
|
|
esp_now_get_peer(esp_now_info->src_addr, &checkPeerInfo);
|
|
switch (checkPeer) {
|
|
case (ESP_OK):
|
|
ESP_LOGI(TAG, "CLIENT BEKANNT");
|
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
// client in liste wiederfinden
|
|
if (!clients[i].isAvailable &&
|
|
memcmp(clients[i].macAddr, esp_now_info->src_addr,
|
|
ESP_NOW_ETH_ALEN) == 0) {
|
|
clients[i].isAvailable = true;
|
|
clients[i].lastSuccessfullPing = xTaskGetTickCount();
|
|
ESP_LOGI(TAG, "Updated client %d last ping time to %lu", i,
|
|
clients[i].lastSuccessfullPing);
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
case (ESP_ERR_ESPNOW_NOT_INIT):
|
|
ESP_LOGI(TAG, "Not initalised");
|
|
break;
|
|
case (ESP_ERR_ESPNOW_ARG):
|
|
ESP_LOGI(TAG, "ESP ERR ESPNOW_ARG");
|
|
break;
|
|
case (ESP_ERR_ESPNOW_NOT_FOUND):
|
|
ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN");
|
|
add_peer(esp_now_info->src_addr);
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "Unknown Message %i", checkPeer);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|
const uint8_t *data, int data_len) {
|
|
ESP_LOGI(TAG, "SLAVE GOT MESSAGE");
|
|
ESP_LOGI(TAG, "Received message from: %02X:%02X:%02X:%02X:%02X:%02X",
|
|
esp_now_info->src_addr[0], esp_now_info->src_addr[1],
|
|
esp_now_info->src_addr[2], esp_now_info->src_addr[3],
|
|
esp_now_info->src_addr[4], esp_now_info->src_addr[5]);
|
|
ESP_LOGI(TAG, "Message: %.*s", data_len, data);
|
|
|
|
BaseMessage replyMessage = {};
|
|
|
|
const BaseMessage *message = (const BaseMessage *)data;
|
|
switch (message->commandPage) {
|
|
case StatusPage:
|
|
ESP_LOGI(TAG, "GOT STATUS MESSAGE");
|
|
break;
|
|
case PingPage:
|
|
ESP_LOGI(TAG, "GOT PING MESSAGE");
|
|
replyMessage = MessageBuilder(PingPage, *(PayloadUnion *)&message->payload,
|
|
sizeof(message->payload));
|
|
ESP_ERROR_CHECK(esp_now_send(
|
|
esp_now_info->src_addr, (uint8_t *)&replyMessage, sizeof(BaseMessage)));
|
|
break;
|
|
case BroadCastPage:
|
|
ESP_LOGI(TAG, "GOT BROADCAST MESSAGE");
|
|
if (!hasMaster) {
|
|
if (IS_BROADCAST_ADDR(esp_now_info->des_addr)) {
|
|
ESP_LOGI(TAG,
|
|
"GOT BROADCAST MESSAGE ATTEMPTING TO REGISTER TO MASTER!");
|
|
add_peer(esp_now_info->src_addr);
|
|
replyMessage =
|
|
MessageBuilder(RegisterPage, *(PayloadUnion *)&message->payload,
|
|
sizeof(message->payload));
|
|
ESP_ERROR_CHECK(esp_now_send(esp_now_info->src_addr,
|
|
(uint8_t *)&replyMessage,
|
|
sizeof(BaseMessage)));
|
|
hasMaster = true;
|
|
}
|
|
}
|
|
break;
|
|
case RegisterPage:
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "GOT UNKONW MESSAGE");
|
|
break;
|
|
}
|
|
}
|
|
|
|
void client_data_sending_task(void *param) {
|
|
while (1) {
|
|
const char *dataToSend = "DATA:42";
|
|
ESP_LOGI(TAG, "SEND DATA");
|
|
esp_now_send(NULL, (uint8_t *)dataToSend,
|
|
strlen(dataToSend)); // Sende Daten an Master
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
|
}
|
|
}
|
|
|
|
void client_monitor_task(void *pvParameters) {
|
|
TickType_t timeout_ticks =
|
|
pdMS_TO_TICKS(CLIENT_TIMEOUT_MS); // Timeout in Ticks
|
|
TickType_t interval_ticks =
|
|
pdMS_TO_TICKS(CHECK_INTERVAL_MS); // Prüfintervall in Ticks
|
|
|
|
while (1) {
|
|
TickType_t now = xTaskGetTickCount(); // Aktuelle Zeit in Ticks
|
|
|
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
if (clients[i].isAvailable) {
|
|
TickType_t time_diff = now - clients[i].lastSuccessfullPing;
|
|
|
|
// Prüfen, ob der Client als "nicht verfügbar" markiert werden soll
|
|
if (time_diff > timeout_ticks) {
|
|
clients[i].isAvailable = false;
|
|
ESP_LOGW(
|
|
TAG,
|
|
"Client %d (MAC: %02X:%02X:%02X:%02X:%02X:%02X) is unavailable",
|
|
i, clients[i].macAddr[0], clients[i].macAddr[1],
|
|
clients[i].macAddr[2], clients[i].macAddr[3],
|
|
clients[i].macAddr[4], clients[i].macAddr[5]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Task für das Prüfintervall anhalten
|
|
vTaskDelay(interval_ticks);
|
|
}
|
|
}
|
|
|
|
void uart_read_task(void *param) {
|
|
uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
|
|
int len = 0;
|
|
while (1) {
|
|
len = 0;
|
|
len =
|
|
uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS));
|
|
if (len > 0) {
|
|
data[len] = '\0';
|
|
ESP_LOGI(TAG, "GOT UART DATA %s", data);
|
|
uart_write_bytes(MASTER_UART, data, len);
|
|
}
|
|
}
|
|
}
|
|
|
|
void init_uart() {
|
|
uart_config_t uart_config = {.baud_rate = 115200,
|
|
.data_bits = UART_DATA_8_BITS,
|
|
.parity = UART_PARITY_DISABLE,
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
|
|
|
|
uart_driver_install(MASTER_UART, BUF_SIZE * 2, 0, 0, NULL, 0);
|
|
uart_param_config(MASTER_UART, &uart_config);
|
|
uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
|
UART_PIN_NO_CHANGE);
|
|
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
|
}
|
|
|
|
void app_main(void) {
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
|
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
// GPIO-Pin für Moduserkennung
|
|
gpio_reset_pin(MASTER_MODE_PIN);
|
|
gpio_set_direction(MASTER_MODE_PIN, GPIO_MODE_INPUT);
|
|
bool isMaster = (gpio_get_level(MASTER_MODE_PIN) == 0);
|
|
|
|
// ESP-NOW Initialisieren
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
wifi_config_t wifi_config = {
|
|
.sta =
|
|
{
|
|
.channel = 1, // Kanal 1, stelle sicher, dass alle Geräte
|
|
// denselben Kanal verwenden
|
|
},
|
|
};
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
ESP_ERROR_CHECK(esp_now_init());
|
|
|
|
if (isMaster) {
|
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(master_receive_callback));
|
|
} else {
|
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback));
|
|
}
|
|
|
|
// Nachrichtenschlange initialisieren
|
|
messageQueue = xQueueCreate(10, sizeof(char *));
|
|
|
|
// Tasks starten basierend auf Master/Client
|
|
if (isMaster) {
|
|
ESP_LOGI(TAG, "Started in Mastermode");
|
|
add_peer(broadcast_address);
|
|
xTaskCreate(master_broadcast_task, "MasterBroadcast", 4096, NULL, 1, NULL);
|
|
xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
|
xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1, NULL);
|
|
init_uart();
|
|
} else {
|
|
ESP_LOGI(TAG, "Started in Slavemode");
|
|
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
|
NULL);
|
|
}
|
|
}
|