|
@@ -11,7 +11,7 @@
|
|
#include <esp_sntp.h>
|
|
#include <esp_sntp.h>
|
|
#include <esp_wifi.h>
|
|
#include <esp_wifi.h>
|
|
|
|
|
|
-static String ssid, password, hostname, dnsserver;
|
|
|
|
|
|
+static const char *ssid, *password, *hostname, *dnsserver;
|
|
static TimerHandle_t sta_failure_timer;
|
|
static TimerHandle_t sta_failure_timer;
|
|
|
|
|
|
static bool sta_timeout_enabled;
|
|
static bool sta_timeout_enabled;
|
|
@@ -102,7 +102,7 @@ static void sntp_server_show(void)
|
|
printf("[SNTP] Time server: %s\n", sntp_server);
|
|
printf("[SNTP] Time server: %s\n", sntp_server);
|
|
setenv_cond("status.net.sntp.server", sntp_server);
|
|
setenv_cond("status.net.sntp.server", sntp_server);
|
|
} else {
|
|
} else {
|
|
- unsetenv("status.net.sntp.server");
|
|
|
|
|
|
+ setenv_cond("status.net.sntp.server", NULL);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -137,7 +137,7 @@ static void start_services(void)
|
|
if (invalid_ip(dns_ip) || getenv_bool("ip4.dhcp.nodns")) {
|
|
if (invalid_ip(dns_ip) || getenv_bool("ip4.dhcp.nodns")) {
|
|
/* Static DNS server configuration */
|
|
/* Static DNS server configuration */
|
|
ip_addr_t addr;
|
|
ip_addr_t addr;
|
|
- if (dnsserver != "" && inet_aton(dnsserver.c_str(), &addr)) {
|
|
|
|
|
|
+ if (dnsserver && inet_aton(dnsserver, &addr)) {
|
|
if (memcmp(dns_ip, &addr, sizeof addr))
|
|
if (memcmp(dns_ip, &addr, sizeof addr))
|
|
dns_setserver(0, &addr);
|
|
dns_setserver(0, &addr);
|
|
}
|
|
}
|
|
@@ -314,7 +314,7 @@ static void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
|
|
sta_timeout_disable();
|
|
sta_timeout_disable();
|
|
if (!ap_clients)
|
|
if (!ap_clients)
|
|
WiFi.enableAP(false);
|
|
WiFi.enableAP(false);
|
|
- } else if (ssid != "") {
|
|
|
|
|
|
+ } else if (ssid) {
|
|
sta_timeout_enable();
|
|
sta_timeout_enable();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -341,7 +341,7 @@ static void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
|
|
setenv_ip("status.net.eth.ip4.gw", WiFi.gatewayIP());
|
|
setenv_ip("status.net.eth.ip4.gw", WiFi.gatewayIP());
|
|
}
|
|
}
|
|
|
|
|
|
- if (ssid == "") {
|
|
|
|
|
|
+ if (!ssid) {
|
|
// No network configured
|
|
// No network configured
|
|
led_set(LED_GREEN, connected & CON_AP ? LED_FLASH_SLOW : LED_OFF);
|
|
led_set(LED_GREEN, connected & CON_AP ? LED_FLASH_SLOW : LED_OFF);
|
|
} else {
|
|
} else {
|
|
@@ -368,7 +368,7 @@ static void setenv_mac(const char *var, const uint8_t mac[6])
|
|
|
|
|
|
snprintf(mac_str, sizeof mac_str, "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
snprintf(mac_str, sizeof mac_str, "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
- setenv(var, mac_str, 1);
|
|
|
|
|
|
+ setenv_cond(var, mac_str);
|
|
}
|
|
}
|
|
|
|
|
|
static void wifi_config_ap(void)
|
|
static void wifi_config_ap(void)
|
|
@@ -377,10 +377,10 @@ static void wifi_config_ap(void)
|
|
IPAddress AP_IP = IPAddress(192,168,0,1);
|
|
IPAddress AP_IP = IPAddress(192,168,0,1);
|
|
IPAddress AP_Netmask = IPAddress(255,255,255,0);
|
|
IPAddress AP_Netmask = IPAddress(255,255,255,0);
|
|
IPAddress AP_Gateway = IPAddress(0,0,0,0); // No gateway
|
|
IPAddress AP_Gateway = IPAddress(0,0,0,0); // No gateway
|
|
- unsigned int channel = time(NULL) % 11; // Pseudo-random
|
|
|
|
|
|
+ unsigned int channel = (time(NULL) % 11) + 1; // Pseudo-random
|
|
uint8_t mac[6];
|
|
uint8_t mac[6];
|
|
char mac_str[6*3];
|
|
char mac_str[6*3];
|
|
- char ap_ssid[64];
|
|
|
|
|
|
+ static char ap_ssid[64];
|
|
|
|
|
|
WiFi.softAPmacAddress(mac);
|
|
WiFi.softAPmacAddress(mac);
|
|
setenv_mac("status.net.ap.mac", mac);
|
|
setenv_mac("status.net.ap.mac", mac);
|
|
@@ -388,64 +388,92 @@ static void wifi_config_ap(void)
|
|
snprintf(ap_ssid, sizeof ap_ssid, "MAX80_%02X%02X", mac[4], mac[5]);
|
|
snprintf(ap_ssid, sizeof ap_ssid, "MAX80_%02X%02X", mac[4], mac[5]);
|
|
|
|
|
|
printf("[WIFI] AP SSID %s IP %s netmask %s channel %u\n",
|
|
printf("[WIFI] AP SSID %s IP %s netmask %s channel %u\n",
|
|
- ap_ssid, AP_IP.toString(), AP_Netmask.toString(), channel+1);
|
|
|
|
|
|
+ ap_ssid, AP_IP.toString(), AP_Netmask.toString(), channel);
|
|
setenv_cond("status.net.ap.ssid", ap_ssid);
|
|
setenv_cond("status.net.ap.ssid", ap_ssid);
|
|
setenv_ip("status.net.ap.ip4", AP_IP);
|
|
setenv_ip("status.net.ap.ip4", AP_IP);
|
|
setenv_ip("status.net.ap.ip4.mask", AP_Netmask);
|
|
setenv_ip("status.net.ap.ip4.mask", AP_Netmask);
|
|
setenv_ul("status.net.ap.clients", 0);
|
|
setenv_ul("status.net.ap.clients", 0);
|
|
|
|
|
|
- WiFi.softAP(ap_ssid, NULL, channel+1, 0, 4, true);
|
|
|
|
|
|
+ printf("WiFi.softAP\n");
|
|
|
|
+ WiFi.softAP(ap_ssid, NULL, channel, 0, 4, true);
|
|
|
|
+ printf("WiFi.softAPConfig\n");
|
|
WiFi.softAPConfig(AP_IP, AP_Gateway, AP_Netmask);
|
|
WiFi.softAPConfig(AP_IP, AP_Gateway, AP_Netmask);
|
|
|
|
+ printf("WiFi.softAPsetHostname\n");
|
|
WiFi.softAPsetHostname(ap_ssid);
|
|
WiFi.softAPsetHostname(ap_ssid);
|
|
|
|
|
|
// Conservative setting: 20 MHz (single channel) only; this is for
|
|
// Conservative setting: 20 MHz (single channel) only; this is for
|
|
// reliability, not performance.
|
|
// reliability, not performance.
|
|
|
|
+ printf("esp_wifi_set_bandwidth\n");
|
|
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_AP, WIFI_BW_HT20);
|
|
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_AP, WIFI_BW_HT20);
|
|
|
|
|
|
// Enable unconditionally if no SSID
|
|
// Enable unconditionally if no SSID
|
|
- WiFi.enableAP(ssid == "");
|
|
|
|
|
|
+ printf("WiFi.enableAP\n");
|
|
|
|
+ WiFi.enableAP(!ssid);
|
|
}
|
|
}
|
|
|
|
|
|
static void wifi_config_sta(void)
|
|
static void wifi_config_sta(void)
|
|
{
|
|
{
|
|
uint8_t mac[6];
|
|
uint8_t mac[6];
|
|
|
|
+
|
|
|
|
+ printf("WiFi.macAddress\n");
|
|
WiFi.macAddress(mac);
|
|
WiFi.macAddress(mac);
|
|
|
|
+ printf("setenv_mac\n");
|
|
setenv_mac("status.net.sta.mac", mac);
|
|
setenv_mac("status.net.sta.mac", mac);
|
|
|
|
|
|
- setenv_cond("status.net.sta.ssid", ssid.c_str());
|
|
|
|
- if (ssid == "") {
|
|
|
|
|
|
+ printf("setenv ssid\n");
|
|
|
|
+ setenv_cond("status.net.sta.ssid", ssid);
|
|
|
|
+ if (!ssid) {
|
|
WiFi.enableSTA(false);
|
|
WiFi.enableSTA(false);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ printf("xTimerCreate\n");
|
|
sta_failure_timer = xTimerCreate("wifi_sta", configTICK_RATE_HZ*30,
|
|
sta_failure_timer = xTimerCreate("wifi_sta", configTICK_RATE_HZ*30,
|
|
pdFALSE, NULL,
|
|
pdFALSE, NULL,
|
|
(TimerCallbackFunction_t)sta_timeout);
|
|
(TimerCallbackFunction_t)sta_timeout);
|
|
|
|
+ printf("sta_timeout_enable\n");
|
|
sta_timeout_enable();
|
|
sta_timeout_enable();
|
|
|
|
|
|
- WiFi.begin(ssid.c_str(), password.c_str());
|
|
|
|
|
|
+ printf("WiFi.begin(%s,%s)\n", ssid, password);
|
|
|
|
+ WiFi.begin(ssid, password);
|
|
|
|
+ printf("WiFi.setAutoConnect\n");
|
|
WiFi.setAutoConnect(true);
|
|
WiFi.setAutoConnect(true);
|
|
|
|
+ printf("WiFi.setAutoReconnect\n");
|
|
WiFi.setAutoReconnect(true);
|
|
WiFi.setAutoReconnect(true);
|
|
|
|
+ printf("WiFi.enableSTA\n");
|
|
WiFi.enableSTA(true);
|
|
WiFi.enableSTA(true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static const char *getenv_notempty(const char *env)
|
|
|
|
+{
|
|
|
|
+ const char *str = getenv(env);
|
|
|
|
+ if (str && !*str)
|
|
|
|
+ str = NULL;
|
|
|
|
+ return str;
|
|
|
|
+}
|
|
|
|
+
|
|
static void wifi_config(void)
|
|
static void wifi_config(void)
|
|
{
|
|
{
|
|
- ssid = getenv("wifi.ssid");
|
|
|
|
- password = getenv("wifi.psk");
|
|
|
|
- hostname = getenv("hostname");
|
|
|
|
- dnsserver = getenv("ip4.dns");
|
|
|
|
|
|
+ ssid = getenv_notempty("wifi.ssid");
|
|
|
|
+ password = getenv_notempty("wifi.psk");
|
|
|
|
+ hostname = getenv_notempty("hostname");
|
|
|
|
+ dnsserver = getenv_notempty("ip4.dns");
|
|
|
|
|
|
force_conn_update = true;
|
|
force_conn_update = true;
|
|
|
|
|
|
|
|
+ printf("WiFi.persistent\n");
|
|
WiFi.persistent(false);
|
|
WiFi.persistent(false);
|
|
|
|
+ printf("WiFi.setSleep\n");
|
|
WiFi.setSleep(false);
|
|
WiFi.setSleep(false);
|
|
|
|
|
|
- if (hostname != "")
|
|
|
|
|
|
+ if (hostname)
|
|
WiFi.hostname(hostname);
|
|
WiFi.hostname(hostname);
|
|
|
|
|
|
- wifi_config_sta();
|
|
|
|
|
|
+ printf("wifi_config_ap\n");
|
|
wifi_config_ap();
|
|
wifi_config_ap();
|
|
|
|
+ printf("wifi_config_sta\n");
|
|
|
|
+ wifi_config_sta();
|
|
|
|
+ printf("wifi_config done\n");
|
|
}
|
|
}
|
|
|
|
|
|
void SetupWiFi() {
|
|
void SetupWiFi() {
|