Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ESP32: ETH + RELAY
#5
maybe you can use for test:
Code:
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "driver/gpio.h"
#include "esp_http_server.h"
#include "esp_mac.h"
#include "esp_eth_phy.h"
#include "esp_eth_phy_lan8720.h"
#include "esp_eth_driver.h"

// Relay GPIOs
#define RELAY1_GPIO GPIO_NUM_15
#define RELAY2_GPIO GPIO_NUM_2

// Ethernet config
#define ETH_ADDR        0
#define ETH_POWER_PIN  -1
#define ETH_MDC_PIN    23
#define ETH_MDIO_PIN   18
#define ETH_TYPE       ETH_PHY_LAN8720
#define ETH_CLK_MODE   ETH_CLOCK_GPIO17_OUT

static const char *TAG = "ethernet_relay";

// ==========================
// Relay Control
// ==========================
static void set_relay(int relay_num, bool on) {
    gpio_num_t gpio = (relay_num == 1) ? RELAY1_GPIO : RELAY2_GPIO;
    gpio_set_level(gpio, on ? 0 : 1);  // Active LOW
}

// ==========================
// HTTP Handlers
// ==========================
esp_err_t relay1_on_handler(httpd_req_t *req) {
    set_relay(1, false);
    httpd_resp_send(req, "Relay 1 ON", HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}

esp_err_t relay1_off_handler(httpd_req_t *req) {
    set_relay(1, true);
    httpd_resp_send(req, "Relay 1 OFF", HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}

esp_err_t relay2_on_handler(httpd_req_t *req) {
    set_relay(2, false);
    httpd_resp_send(req, "Relay 2 ON", HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}

esp_err_t relay2_off_handler(httpd_req_t *req) {
    set_relay(2, true);
    httpd_resp_send(req, "Relay 2 OFF", HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}

esp_err_t root_handler(httpd_req_t *req) {
    const char* resp_str =
        "ESP32 Relay Control (Ethernet):\n"
        "/relay1/on  or /relay1/off\n"
        "/relay2/on  or /relay2/off\n";
    httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}

// ==========================
// HTTP Server
// ==========================
httpd_handle_t start_webserver(void) {
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
    httpd_handle_t server = NULL;

    if (httpd_start(&server, &config) == ESP_OK) {
        httpd_register_uri_handler(server, &(httpd_uri_t){.uri="/", .method=HTTP_GET, .handler=root_handler});
        httpd_register_uri_handler(server, &(httpd_uri_t){.uri="/relay1/on", .method=HTTP_GET, .handler=relay1_on_handler});
        httpd_register_uri_handler(server, &(httpd_uri_t){.uri="/relay1/off", .method=HTTP_GET, .handler=relay1_off_handler});
        httpd_register_uri_handler(server, &(httpd_uri_t){.uri="/relay2/on", .method=HTTP_GET, .handler=relay2_on_handler});
        httpd_register_uri_handler(server, &(httpd_uri_t){.uri="/relay2/off", .method=HTTP_GET, .handler=relay2_off_handler});
    }

    return server;
}

// ==========================
// Ethernet Event Handler
// ==========================
static void eth_event_handler(void *arg, esp_event_base_t event_base,
                              int32_t event_id, void *event_data) {
    switch (event_id) {
        case ETHERNET_EVENT_CONNECTED:
            ESP_LOGI(TAG, "Ethernet Connected");
            break;
        case ETHERNET_EVENT_DISCONNECTED:
            ESP_LOGI(TAG, "Ethernet Disconnected");
            break;
        case ETHERNET_EVENT_START:
            ESP_LOGI(TAG, "Ethernet Started");
            break;
        case ETHERNET_EVENT_STOP:
            ESP_LOGI(TAG, "Ethernet Stopped");
            break;
        default:
            break;
    }
}

static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
                                 int32_t event_id, void *event_data) {
    ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
    ESP_LOGI(TAG, "Got IP Address: " IPSTR, IP2STR(&event->ip_info.ip));
    start_webserver();
}

// ==========================
// Ethernet Initialization
// ==========================
void eth_init(void) {
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
    esp_netif_t *eth_netif = esp_netif_new(&cfg);

    esp_eth_mac_t *mac = esp_eth_mac_new_esp32();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
    phy_config.phy_addr = ETH_ADDR;
    phy_config.reset_gpio_num = ETH_POWER_PIN;
    esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);

    esp_eth_handle_t eth_handle = NULL;
    esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
    ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));
    ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));

    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));

    ESP_ERROR_CHECK(esp_eth_start(eth_handle));
}

// ==========================
// GPIO Initialization
// ==========================
void relay_gpio_init() {
    gpio_config_t io_conf = {
        .pin_bit_mask = (1ULL << RELAY1_GPIO) | (1ULL << RELAY2_GPIO),
        .mode = GPIO_MODE_OUTPUT,
        .pull_up_en = 0,
        .pull_down_en = 0,
        .intr_type = GPIO_INTR_DISABLE,
    };
    gpio_config(&io_conf);

    gpio_set_level(RELAY1_GPIO, 0);
    gpio_set_level(RELAY2_GPIO, 0);
}

// ==========================
// Main
// ==========================
void app_main(void) {
    ESP_ERROR_CHECK(nvs_flash_init());
    relay_gpio_init();
    eth_init();
}
Reply


Messages In This Thread
ESP32: ETH + RELAY - by zital - 07-03-2025, 06:47 AM
RE: ESP32: ETH + RELAY - by admin - 07-03-2025, 07:59 AM
RE: ESP32: ETH + RELAY - by zital - 07-03-2025, 08:38 PM
RE: ESP32: ETH + RELAY - by zital - 07-03-2025, 10:00 PM
RE: ESP32: ETH + RELAY - by admin - 07-04-2025, 01:03 AM
RE: ESP32: ETH + RELAY - by zital - 07-04-2025, 10:16 AM
RE: ESP32: ETH + RELAY - by admin - 07-05-2025, 01:42 AM
RE: ESP32: ETH + RELAY - by zital - 07-05-2025, 06:39 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)