Inital Commit
Working: - Detection if Master or Slave over Pin - Master Broadcasts message to Slaves - Slave connect to Master and get Pinged
This commit is contained in:
commit
3e7c842597
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
build/
|
||||
.cache
|
||||
6
CMakeLists.txt
Normal file
6
CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
# ESP-IDF-Projekt einbinden
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(espAlox)
|
||||
|
||||
24
Makefile
Normal file
24
Makefile
Normal file
@ -0,0 +1,24 @@
|
||||
all:
|
||||
echo "no default rule"
|
||||
|
||||
export:
|
||||
source ~/esp/esp-idf/export.fish && fish
|
||||
|
||||
buildIdf:
|
||||
idf.py build
|
||||
|
||||
flash0:
|
||||
sudo chmod o+rw /dev/ttyUSB0
|
||||
idf.py flash -p /dev/ttyUSB0
|
||||
|
||||
flash1:
|
||||
sudo chmod o+rw /dev/ttyUSB1
|
||||
idf.py flash -p /dev/ttyUSB1
|
||||
|
||||
monitor0:
|
||||
sudo chmod o+rw /dev/ttyUSB0
|
||||
idf.py monitor -p /dev/ttyUSB0
|
||||
|
||||
monitor1:
|
||||
sudo chmod o+rw /dev/ttyUSB0
|
||||
idf.py monitor -p /dev/ttyUSB1
|
||||
3
main/CMakeLists.txt
Normal file
3
main/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
295
main/main.c
Normal file
295
main/main.c
Normal file
@ -0,0 +1,295 @@
|
||||
#include "assert.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_phy_init.h"
|
||||
#include "esp_rom_gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "nvs_flash.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 <string.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#define MASTER_MODE_PIN GPIO_NUM_23 // Jumper-Erkennungspin
|
||||
#define BROADCAST_INTERVAL_MS 500
|
||||
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,
|
||||
} CommandPages;
|
||||
|
||||
typedef struct {
|
||||
uint32_t uptime;
|
||||
uint8_t status;
|
||||
} StatusPayload;
|
||||
|
||||
typedef struct {
|
||||
uint32_t timestamp;
|
||||
} PingPayload;
|
||||
|
||||
typedef struct {
|
||||
} BroadCastPayload;
|
||||
|
||||
typedef union {
|
||||
StatusPayload status_payload;
|
||||
PingPayload ping_payload;
|
||||
BroadCastPayload broadcast_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;
|
||||
} ClientInfo;
|
||||
|
||||
ClientInfo clients[19];
|
||||
size_t numClients = 0;
|
||||
bool hasMaster = false;
|
||||
|
||||
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]);
|
||||
ClientInfo newClient = {};
|
||||
memcpy(newClient.macAddr, macAddr, ESP_NOW_ETH_ALEN);
|
||||
newClient.isAvailable = true;
|
||||
clients[numClients++] = newClient;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
ESP_LOGI(TAG, "SENDING PING TO CLIENTS");
|
||||
for (size_t i = 0; i < numClients; ++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
|
||||
break;
|
||||
case BroadCastPage:
|
||||
ESP_LOGI(TAG, "WILL REGISTER NEW 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");
|
||||
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);
|
||||
uint8_t data[] = "REGISTER ME DADDY";
|
||||
esp_now_send(esp_now_info->src_addr, data, strlen((const char *)data));
|
||||
hasMaster = true;
|
||||
}
|
||||
}
|
||||
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 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_SlavePin);
|
||||
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);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Started in Slavemode");
|
||||
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
6
main/main.h
Normal file
6
main/main.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#define Master_SlavePin 23
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user