-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathwifi_example.cpp
More file actions
66 lines (53 loc) · 1.82 KB
/
wifi_example.cpp
File metadata and controls
66 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <chrono>
#include <vector>
#if CONFIG_ESP32_WIFI_NVS_ENABLED
#include "nvs_flash.h"
#endif
#include "sdkconfig.h"
#include "task.hpp"
#include "wifi_ap.hpp"
#include "wifi_sta.hpp"
using namespace std::chrono_literals;
extern "C" void app_main(void) {
fmt::print("Starting WiFi example!\n");
size_t num_seconds_to_run = 2;
#if CONFIG_ESP32_WIFI_NVS_ENABLED
// Initialize NVS
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);
#endif
{
fmt::print("Starting WiFi STA example...\n");
//! [wifi sta example]
espp::WifiSta wifi_sta({.ssid = CONFIG_ESP_WIFI_SSID,
.password = CONFIG_ESP_WIFI_PASSWORD,
.num_connect_retries = CONFIG_ESP_MAXIMUM_RETRY,
.on_connected = nullptr,
.on_disconnected = nullptr,
.on_got_ip = [](ip_event_got_ip_t *eventdata) {
fmt::print("got IP: {}.{}.{}.{}\n", IP2STR(&eventdata->ip_info.ip));
}});
while (!wifi_sta.is_connected()) {
std::this_thread::sleep_for(100ms);
}
//! [wifi sta example]
std::this_thread::sleep_for(num_seconds_to_run * 1s);
fmt::print("WiFi STA example complete!\n");
}
{
fmt::print("Starting WiFi AP example...\n");
//! [wifi ap example]
espp::WifiAp wifi_ap({.ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD});
//! [wifi ap example]
std::this_thread::sleep_for(num_seconds_to_run * 1s);
fmt::print("WiFi AP example complete!\n");
}
fmt::print("WiFi example complete!\n");
while (true) {
std::this_thread::sleep_for(1s);
}
}