8000 Added static IP fragment · programmer131/esp32-snippets@e00f07a · GitHub
[go: up one dir, main page]

Skip to content

Commit e00f07a

Browse files
author
kolban
committed
Added static IP fragment
1 parent 67641ca commit e00f07a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* A common task is to connect to an access point and hence be a
3+
* WiFi station. By default, when we connect to the access point we
4+
* then perform DHCP client protocols to obtain an IP address. This
5+
* takes a few seconds (in my experience). If we are in the middle of
6+
* edit, compile, run, test - repeat cycles ... every few seconds of
7+
* "pause" can add up to our workflow. This fragment illustrates the use
8+
* of a static IP address. We continue to connect to the access point but
9+
* now instead of using DHCP to be allocated an address, we tell the
10+
* access point the address we want to use. Obviously it can't already
11+
* be in use and it has to be in the range of valid addresses. If these
12+
* are satisfied, we shave off a few seconds of pause time and work as we
13+
* immediately have our IP address.
14+
*/
15+
#include <lwip/sockets.h>
16+
17+
// The IP address that we want our device to have.
18+
#define DEVICE_IP "192.168.5.2"
19+
20+
// The Gateway address where we wish to send packets.
21+
// This will commonly be our access point.
22+
#define DEVICE_GW "192.168.5.1"
23+
24+
// The netmask specification.
25+
#define DEVICE_NETMASK "255.255.255.0"
26+
27+
// The identity of the access point to which we wish to connect.
28+
#define AP_TARGET_SSID "RASPI3"
29+
30+
// The password we need to supply to the access point for authorization.
31+
#define AP_TARGET_PASSWORD "password"
32+
33+
esp_err_t event_handler(void *ctx, system_event_t *event)
34+
{
35+
return ESP_OK;
36+
}
37+
38+
39+
// Code fragment here ...
40+
nvs_flash_init();
41+
tcpip_adapter_init();
42+
43+
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); // Don't run a DHCP client
44+
tcpip_adapter_ip_info_t ipInfo;
45+
46+
inet_pton(AF_INET, DEVICE_IP, &ipInfo.ip);
47+
inet_pton(AF_INET, DEVICE_GW, &ipInfo.gw);
48+
inet_pton(AF_INET, DEVICE_NETMASK, &ipInfo.netmask);
49+
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
50+
51+
ESP_ERROR_CHECK(esp_event_loop_init(wifiEventHandler, NULL));
52+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
53+
ESP_ERROR_CHECK(esp_wifi_init(&cfg) );
54+
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
55+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
56+
wifi_config_t sta_config = {
57+
.sta = {
58+
.ssid = AP_TARGET_SSID,
59+
.password = AP_TARGET_PASSWORD,
60+
.bssid_set = 0
61+
}
62+
};
63+
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));
64+
ESP_ERROR_CHECK(esp_wifi_start());
65+
ESP_ERROR_CHECK(esp_wifi_connect());

0 commit comments

Comments
 (0)
0