diff --git a/Makefile b/Makefile index e0f08e3..c438a3d 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/ # Base directory of the ESP8266 SDK package, absolute # Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 -SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.3.0) +SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.4.0) # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool diff --git a/esp-link/cgi.c b/esp-link/cgi.c index 1c870f5..5c0584c 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -43,7 +43,7 @@ errorResponse(HttpdConnData *connData, int code, char *message) { // look for the HTTP arg 'name' and store it at 'config' with max length 'max_len' (incl // terminating zero), returns -1 on error, 0 if not found, 1 if found and OK -int ICACHE_FLASH_ATTR +int8_t ICACHE_FLASH_ATTR getStringArg(HttpdConnData *connData, char *name, char *config, int max_len) { char buff[128]; int len = httpdFindArg(connData->getArgs, name, buff, sizeof(buff)); @@ -57,7 +57,7 @@ getStringArg(HttpdConnData *connData, char *name, char *config, int max_len) { return 1; } -int ICACHE_FLASH_ATTR +int8_t ICACHE_FLASH_ATTR getBoolArg(HttpdConnData *connData, char *name, bool*config) { char buff[64]; int len = httpdFindArg(connData->getArgs, name, buff, sizeof(buff)); diff --git a/esp-link/cgi.h b/esp-link/cgi.h index aa124f1..66757fe 100644 --- a/esp-link/cgi.h +++ b/esp-link/cgi.h @@ -6,9 +6,17 @@ void jsonHeader(HttpdConnData *connData, int code); void errorResponse(HttpdConnData *connData, int code, char *message); -int getStringArg(HttpdConnData *connData, char *name, char *config, int max_len); -int getBoolArg(HttpdConnData *connData, char *name, bool*config); + +// Get the HTTP query-string param 'name' and store it at 'config' with max length +// 'max_len' (incl terminating zero), returns -1 on error, 0 if not found, 1 if found +int8_t getStringArg(HttpdConnData *connData, char *name, char *config, int max_len); + +// Get the HTTP query-string param 'name' and store it boolean value at 'config', +// supports 1/true and 0/false, returns -1 on error, 0 if not found, 1 if found +int8_t getBoolArg(HttpdConnData *connData, char *name, bool*config); + int cgiMenu(HttpdConnData *connData); + uint8_t UTILS_StrToIP(const char* str, void *ip); #endif diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index 19a950e..e0dd92a 100644 --- a/esp-link/cgimqtt.c +++ b/esp-link/cgimqtt.c @@ -66,7 +66,7 @@ int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // handle MQTT server settings - int mqtt_server = 0; // accumulator for changes/errors + int8_t mqtt_server = 0; // accumulator for changes/errors mqtt_server |= getStringArg(connData, "mqtt-host", flashConfig.mqtt_host, sizeof(flashConfig.mqtt_host)); if (mqtt_server < 0) return HTTPD_CGI_DONE; @@ -85,7 +85,7 @@ int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) { &flashConfig.mqtt_clean_session); if (mqtt_server < 0) return HTTPD_CGI_DONE; - mqtt_server |= getBoolArg(connData, "mqtt-enable", + int8_t mqtt_en_chg = getBoolArg(connData, "mqtt-enable", &flashConfig.mqtt_enable); char buff[16]; @@ -118,6 +118,19 @@ int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) { if (mqtt_server) { #ifdef CGIMQTT_DBG os_printf("MQTT server settings changed, enable=%d\n", flashConfig.mqtt_enable); +#endif + MQTT_Free(&mqttClient); // safe even if not connected + MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, + flashConfig.mqtt_timeout, flashConfig.mqtt_clientid, + flashConfig.mqtt_username, flashConfig.mqtt_password, + flashConfig.mqtt_keepalive); + if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) + MQTT_Connect(&mqttClient); + + // if just enable changed we just need to bounce the client + } else if (mqtt_en_chg > 0) { +#ifdef CGIMQTT_DBG + os_printf("MQTT server enable=%d changed\n", flashConfig.mqtt_enable); #endif if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) MQTT_Reconnect(&mqttClient); diff --git a/esp-link/config.c b/esp-link/config.c index f54651b..cd95e76 100644 --- a/esp-link/config.c +++ b/esp-link/config.c @@ -20,7 +20,7 @@ FlashConfig flashDefault = { "\0", // api_key 0, 0, 0, // slip_enable, mqtt_enable, mqtt_status_enable 2, 1, // mqtt_timeout, mqtt_clean_session - 1833, 600, // mqtt port, mqtt_keepalive + 1883, 60, // mqtt port, mqtt_keepalive "\0", "\0", "\0", "\0", "\0", // mqtt host, client_id, user, password, status-topic }; @@ -115,13 +115,14 @@ bool ICACHE_FLASH_ATTR configRestore(void) { os_memcpy(&flashConfig, &flashDefault, sizeof(FlashConfig)); char chipIdStr[6]; os_sprintf(chipIdStr, "%06x", system_get_chip_id()); - os_memcpy(&flashConfig.mqtt_clientid, chipIdStr, os_strlen(chipIdStr)); #ifdef CHIP_IN_HOSTNAME char hostname[16]; os_strcpy(hostname, "esp-link-"); os_strcat(hostname, chipIdStr); os_memcpy(&flashConfig.hostname, hostname, os_strlen(hostname)); #endif + os_memcpy(&flashConfig.mqtt_clientid, &flashConfig.hostname, os_strlen(flashConfig.hostname)); + os_memcpy(&flashConfig.mqtt_status_topic, &flashConfig.hostname, os_strlen(flashConfig.hostname)); flash_pri = 0; return false; } diff --git a/html/mqtt.html b/html/mqtt.html index 6a627b3..1afbb95 100644 --- a/html/mqtt.html +++ b/html/mqtt.html @@ -40,18 +40,16 @@ - - + + + + - - - -