|
| 1 | +#include <sdkconfig.h> |
| 2 | +#include <string> |
| 3 | + |
| 4 | +#include "cli.hpp" |
| 5 | +#include "ping.hpp" |
| 6 | +#include "wifi_sta.hpp" |
| 7 | +#include "wifi_sta_menu.hpp" |
| 8 | + |
| 9 | +extern "C" void app_main(void) { |
| 10 | + espp::Logger logger({.tag = "ping_example", .level = espp::Logger::Verbosity::INFO}); |
| 11 | + logger.info("Starting Ping example..."); |
| 12 | + |
| 13 | +#if CONFIG_ESP32_WIFI_NVS_ENABLED |
| 14 | + // Initialize NVS |
| 15 | + esp_err_t ret = nvs_flash_init(); |
| 16 | + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 17 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 18 | + ret = nvs_flash_init(); |
| 19 | + } |
| 20 | + ESP_ERROR_CHECK(ret); |
| 21 | +#endif |
| 22 | + |
| 23 | + { |
| 24 | + //! [ping_cli_example] |
| 25 | + // Simple WiFi STA bring-up assumed configured via menu |
| 26 | + espp::WifiSta wifi_sta({.ssid = "", |
| 27 | + .password = "", |
| 28 | + .num_connect_retries = 5, |
| 29 | + .on_connected = nullptr, |
| 30 | + .on_disconnected = nullptr, |
| 31 | + .on_got_ip = |
| 32 | + [&](ip_event_got_ip_t *eventdata) { |
| 33 | + logger.info("got IP: {}.{}.{}.{}", |
| 34 | + IP2STR(&eventdata->ip_info.ip)); |
| 35 | + }, |
| 36 | + .log_level = espp::Logger::Verbosity::INFO}); |
| 37 | + // create the wifi menu to allow connecting to APs |
| 38 | + espp::WifiStaMenu wifi_menu(wifi_sta); |
| 39 | + |
| 40 | + // create the ping instance and menu |
| 41 | + espp::Ping ping({.session = |
| 42 | + { |
| 43 | + .target_host = "1.1.1.1", |
| 44 | + .task_stack_size = 8192, |
| 45 | + }, |
| 46 | + .callbacks = { |
| 47 | + .on_session_start = [&]() { logger.info("Ping session started"); }, |
| 48 | + .on_reply = |
| 49 | + [&](uint32_t seq, uint32_t ttl, uint32_t time_ms, uint32_t bytes) { |
| 50 | + logger.info("Reply: seq={} ttl={} time={}ms bytes={}", seq, ttl, |
| 51 | + time_ms, bytes); |
| 52 | + }, |
| 53 | + .on_timeout = [&]() { logger.warn("Request timed out"); }, |
| 54 | + .on_end = |
| 55 | + [&](const espp::Ping::Stats &stats) { |
| 56 | + logger.info("Ping session ended: {}", stats); |
| 57 | + }, |
| 58 | + }, |
| 59 | + .log_level = espp::Logger::Verbosity::DEBUG}); |
| 60 | + espp::Ping::Menu ping_menu(ping); |
| 61 | + |
| 62 | + auto root = std::make_unique<cli::Menu>("root", "root menu"); |
| 63 | + root->Insert(wifi_menu.get()); |
| 64 | + root->Insert(ping_menu.get()); |
| 65 | + |
| 66 | + cli::Cli cli(std::move(root)); |
| 67 | + cli::SetColor(); |
| 68 | + cli.ExitAction([](auto &out) { out << "Goodbye and thanks for all the fish.\n"; }); |
| 69 | + |
| 70 | + espp::Cli input(cli); |
| 71 | + input.SetInputHistorySize(10); |
| 72 | + input.Start(); |
| 73 | + //! [ping_cli_example] |
| 74 | + } |
| 75 | + |
| 76 | + // run the simple example after the CLI so that the user can set the |
| 77 | + // SSID/password, which will allow the second wifi_sta to auto-connect, since |
| 78 | + // it will use the stored config in NVS |
| 79 | + { |
| 80 | + //! [ping_simple_example] |
| 81 | + std::atomic<bool> got_ip{false}; |
| 82 | + // Simple WiFi STA bring-up assumed configured via menu |
| 83 | + espp::WifiSta wifi_sta({.ssid = "", |
| 84 | + .password = "", |
| 85 | + .num_connect_retries = 5, |
| 86 | + .on_connected = nullptr, |
| 87 | + .on_disconnected = nullptr, |
| 88 | + .on_got_ip = |
| 89 | + [&](ip_event_got_ip_t *eventdata) { |
| 90 | + logger.info("got IP: {}.{}.{}.{}", |
| 91 | + IP2STR(&eventdata->ip_info.ip)); |
| 92 | + got_ip = true; |
| 93 | + }, |
| 94 | + .log_level = espp::Logger::Verbosity::INFO}); |
| 95 | + // create the ping instance |
| 96 | + espp::Ping ping({.session = |
| 97 | + { |
| 98 | + .target_host = "google.com", |
| 99 | + }, |
| 100 | + .callbacks = { |
| 101 | + .on_session_start = [&]() { logger.info("Ping session started"); }, |
| 102 | + .on_reply = |
| 103 | + [&](uint32_t seq, uint32_t ttl, uint32_t time_ms, uint32_t bytes) { |
| 104 | + logger.info("Reply: seq={} ttl={} time={}ms bytes={}", seq, ttl, |
| 105 | + time_ms, bytes); |
| 106 | + }, |
| 107 | + .on_timeout = [&]() { logger.warn("Request timed out"); }, |
| 108 | + .on_end = |
| 109 | + [&](const espp::Ping::Stats &stats) { |
| 110 | + logger.info("Ping session ended: {}", stats); |
| 111 | + }, |
| 112 | + }, |
| 113 | + .log_level = espp::Logger::Verbosity::DEBUG}); |
| 114 | + // wait for wifi connection |
| 115 | + while (!got_ip) { |
| 116 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 117 | + } |
| 118 | + // run the ping session (synchronously) |
| 119 | + std::error_code ec; |
| 120 | + if (!ping.run(ec)) { |
| 121 | + logger.error("Ping failed to start: {}", ec.message()); |
| 122 | + } |
| 123 | + //! [ping_simple_example] |
| 124 | + } |
| 125 | +} |
0 commit comments