feat: choose a static IP instead of dhcp
This commit is contained in:
parent
233f3e5d80
commit
235ef588c1
2 changed files with 45 additions and 0 deletions
|
@ -75,4 +75,22 @@ menu "Project Configuration"
|
||||||
default 19
|
default 19
|
||||||
help
|
help
|
||||||
GPIO pin number to be used as GPIO_OUTPUT_IO_1.
|
GPIO pin number to be used as GPIO_OUTPUT_IO_1.
|
||||||
|
|
||||||
|
config STATIC_IP_ADDR
|
||||||
|
string "Static IP address"
|
||||||
|
default "192.168.51.10"
|
||||||
|
help
|
||||||
|
Set static IP address.
|
||||||
|
|
||||||
|
config STATIC_NETMASK_ADDR
|
||||||
|
string "Static netmask address"
|
||||||
|
default "255.255.255.0"
|
||||||
|
help
|
||||||
|
Set static netmask address.
|
||||||
|
|
||||||
|
config STATIC_GW_ADDR
|
||||||
|
string "Static gateway address"
|
||||||
|
default "192.168.51.1"
|
||||||
|
help
|
||||||
|
Set static gateway address.
|
||||||
endmenu
|
endmenu
|
||||||
|
|
27
main/main.c
27
main/main.c
|
@ -14,6 +14,7 @@
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include "esp_wifi_types_generic.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
|
@ -40,6 +41,8 @@
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "esp_eth.h"
|
#include "esp_eth.h"
|
||||||
|
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64)
|
#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64)
|
||||||
|
|
||||||
// GPIO pin definition. Use menu config to choose GPIO
|
// GPIO pin definition. Use menu config to choose GPIO
|
||||||
|
@ -53,6 +56,9 @@
|
||||||
|
|
||||||
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
|
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
|
||||||
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
||||||
|
#define STATIC_IP_ADDR CONFIG_STATIC_IP_ADDR
|
||||||
|
#define STATIC_NETMASK_ADDR CONFIG_STATIC_NETMASK_ADDR
|
||||||
|
#define STATIC_GW_ADDR CONFIG_STATIC_GW_ADDR
|
||||||
|
|
||||||
#if CONFIG_ESP_WPA3_SAE_PWE_HUNT_AND_PECK
|
#if CONFIG_ESP_WPA3_SAE_PWE_HUNT_AND_PECK
|
||||||
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK
|
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK
|
||||||
|
@ -93,11 +99,15 @@ static EventGroupHandle_t s_wifi_event_group;
|
||||||
|
|
||||||
static const char *TAG = "devant";
|
static const char *TAG = "devant";
|
||||||
|
|
||||||
|
void set_static_ip(esp_netif_t *netif);
|
||||||
|
|
||||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void* event_data)
|
int32_t event_id, void* event_data)
|
||||||
{
|
{
|
||||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
|
||||||
|
set_static_ip(arg);
|
||||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
ESP_LOGI(TAG,"Retry connect to the AP");
|
ESP_LOGI(TAG,"Retry connect to the AP");
|
||||||
|
@ -171,6 +181,23 @@ void wifi_init_sta(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_static_ip(esp_netif_t *netif){
|
||||||
|
if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Failed to stop dhcp client");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
esp_netif_ip_info_t ip;
|
||||||
|
memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
|
||||||
|
ip.ip.addr = ipaddr_addr(STATIC_IP_ADDR);
|
||||||
|
ip.netmask.addr = ipaddr_addr(STATIC_NETMASK_ADDR);
|
||||||
|
ip.gw.addr = ipaddr_addr(STATIC_GW_ADDR);
|
||||||
|
if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Failed to set ip info");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ESP_LOGD(TAG, "Success to set static ip: %s, netmask: %s, gw: %s", STATIC_IP_ADDR, STATIC_NETMASK_ADDR, STATIC_GW_ADDR);
|
||||||
|
}
|
||||||
|
|
||||||
static atomic_bool status_one = false;
|
static atomic_bool status_one = false;
|
||||||
static atomic_bool status_two = false;
|
static atomic_bool status_two = false;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue