200 lines
5.5 KiB
C++
200 lines
5.5 KiB
C++
#include <WiFi.h>
|
|
#include "Commander.h"
|
|
|
|
|
|
void wifi_autoconnect(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
WiFi.enableSTA(true);
|
|
WiFi.begin();
|
|
|
|
WiFi.setAutoConnect(true);
|
|
WiFi.setAutoReconnect(true);
|
|
|
|
client->print("connecting to network...");
|
|
while (WiFi.status() != WL_CONNECTED && WiFi.status() != WL_CONNECT_FAILED) {
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
client->print('.');
|
|
}
|
|
client->println();
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
client->print("connected to ");
|
|
client->println(WiFi.SSID());
|
|
} else {
|
|
client->println("connection failed");
|
|
}
|
|
|
|
}
|
|
|
|
void wifi_connect(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
WiFi.enableSTA(true);
|
|
client->println("scanning...");
|
|
int16_t count = WiFi.scanNetworks();
|
|
client->println("available networks:");
|
|
for (int16_t i = 0; i < count; ++i) {
|
|
client->print(i);
|
|
client->print(") ");
|
|
client->print(WiFi.SSID(i));
|
|
client->print("\t signal: ");
|
|
client->print(WiFi.RSSI(i));
|
|
client->print("\t encryption: ");
|
|
switch (WiFi.encryptionType(i)) {
|
|
case WIFI_AUTH_OPEN: client->print("none");
|
|
break;
|
|
case WIFI_AUTH_WEP: client->print("WEP");
|
|
break;
|
|
case WIFI_AUTH_WPA_PSK: client->print("WPA PSK");
|
|
break;
|
|
case WIFI_AUTH_WPA2_PSK: client->print("WPA2 PSK");
|
|
break;
|
|
case WIFI_AUTH_WPA2_ENTERPRISE: client->print("WPA2 ENTERPRISE");
|
|
break;
|
|
case WIFI_AUTH_WPA_WPA2_PSK: client->print("WPA/WPA2 PSK");
|
|
break;
|
|
default: client->print("unknown");
|
|
}
|
|
client->println();
|
|
}
|
|
|
|
int16_t selected_network;
|
|
do {
|
|
client->print("please select which network to connect to: ");
|
|
} while (!client->ReadLine(&selected_network) && selected_network < count);
|
|
|
|
CmdString wifi_name = WiFi.SSID(selected_network);
|
|
|
|
switch (WiFi.encryptionType(selected_network)) {
|
|
|
|
case WIFI_AUTH_OPEN: WiFi.begin(wifi_name.c_str());
|
|
break;
|
|
case WIFI_AUTH_WEP:
|
|
case WIFI_AUTH_WPA_PSK:
|
|
case WIFI_AUTH_WPA2_PSK:
|
|
case WIFI_AUTH_WPA_WPA2_PSK:
|
|
case WIFI_AUTH_WPA2_ENTERPRISE: {
|
|
client->print("wifi password: ");
|
|
CmdString key = client->ReadLine();
|
|
WiFi.begin(wifi_name.c_str(), key.c_str());
|
|
break;
|
|
}
|
|
default: client->println("error: unknown encryption type");
|
|
return;
|
|
}
|
|
|
|
WiFi.setAutoConnect(true);
|
|
WiFi.setAutoReconnect(true);
|
|
|
|
client->print("connecting to network...");
|
|
while (WiFi.status() != WL_CONNECTED && WiFi.status() != WL_CONNECT_FAILED) {
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
client->print('.');
|
|
}
|
|
client->println();
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
client->print("connected to ");
|
|
client->println(WiFi.SSID());
|
|
} else {
|
|
client->println("connection failed");
|
|
}
|
|
}
|
|
|
|
void wifi_disconnect(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
WiFi.disconnect(true);
|
|
client->print("disconnecting...");
|
|
while (WiFi.status() != WL_DISCONNECTED) {
|
|
client->print('.');
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
}
|
|
|
|
client->println();
|
|
client->println("disconnected from wifi network");
|
|
|
|
}
|
|
|
|
void wifi_status(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
if (!WiFi.isConnected()) {
|
|
client->println("wifi is not connected");
|
|
return;
|
|
}
|
|
client->println("wifi is connected");
|
|
|
|
client->print("SSID: ");
|
|
client->println(WiFi.SSID());
|
|
|
|
client->print("RSSI: ");
|
|
client->println(WiFi.RSSI());
|
|
|
|
client->print("IPv4: ");
|
|
client->println(WiFi.localIP());
|
|
|
|
client->print("IPv6: ");
|
|
client->println(WiFi.localIPv6());
|
|
|
|
client->print("MAC: ");
|
|
client->println(WiFi.macAddress());
|
|
}
|
|
|
|
void wifi_ap_enable(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
WiFi.enableAP(true);
|
|
// todo: anything to do here?
|
|
}
|
|
|
|
void wifi_ap_disable(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
WiFi.softAPdisconnect(true);
|
|
}
|
|
|
|
void wifi_ap_setup(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
client->print("SSID for AP: ");
|
|
CmdString ssid = client->ReadLine();
|
|
client->print("password for AP: ");
|
|
CmdString passphrase = client->ReadLine();
|
|
|
|
WiFi.softAP(ssid.c_str(), passphrase.c_str());
|
|
|
|
// todo: WiFi.softAPConfig, WiFi.softAPsetHostname, WiFi.softAPmacAddress
|
|
}
|
|
|
|
void wifi_ap_status(const std::vector<CmdString> ¶ms, CommanderClient *client) {
|
|
if ((WiFi.getMode() & WIFI_MODE_AP) == 0) { // AP is disabled
|
|
client->println("AP is disabled");
|
|
return;
|
|
}
|
|
client->print("SSID: ");
|
|
client->println(WiFi.softAPSSID());
|
|
|
|
client->print("MAC: ");
|
|
client->println(WiFi.softAPmacAddress());
|
|
|
|
client->print("IPv4: ");
|
|
client->println(WiFi.softAPIP());
|
|
|
|
client->print("IPv6: ");
|
|
client->println(WiFi.softAPIPv6());
|
|
|
|
client->print("network ID: ");
|
|
client->println(WiFi.softAPNetworkID());
|
|
|
|
client->print("broadcast IP: ");
|
|
client->println(WiFi.softAPBroadcastIP());
|
|
|
|
client->print("subnet: ");
|
|
client->println(WiFi.softAPSubnetCIDR());
|
|
|
|
client->print("hostname: ");
|
|
client->println(WiFi.softAPgetHostname());
|
|
|
|
client->print("connected clients: ");
|
|
client->println(WiFi.softAPgetStationNum());
|
|
}
|
|
|
|
void registerWifiCommands(Commander &commander) {
|
|
auto wifi_cmd = commander.RegisterCommand("wifi", nullptr);
|
|
wifi_cmd->RegisterCommand("autoconnect", wifi_connect);
|
|
wifi_cmd->RegisterCommand("connect", wifi_connect);
|
|
wifi_cmd->RegisterCommand("disconnect", wifi_disconnect);
|
|
wifi_cmd->RegisterCommand("status", wifi_status);
|
|
|
|
auto wifi_ap_cmd = wifi_cmd->RegisterCommand("ap", nullptr);
|
|
wifi_ap_cmd->RegisterCommand("enable", wifi_ap_enable);
|
|
wifi_ap_cmd->RegisterCommand("disable", wifi_ap_disable);
|
|
wifi_ap_cmd->RegisterCommand("setup", wifi_ap_setup);
|
|
wifi_ap_cmd->RegisterCommand("status", wifi_ap_status);
|
|
} |