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/esp-link/mqtt_client.c b/esp-link/mqtt_client.c
index fa0e5fb..2fea118 100644
--- a/esp-link/mqtt_client.c
+++ b/esp-link/mqtt_client.c
@@ -12,10 +12,9 @@ static void ICACHE_FLASH_ATTR
mqttTimerCb(void *arg)
{
if (once++ > 0) return;
- MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, 2,
- flashConfig.mqtt_clientid, flashConfig.mqtt_username, flashConfig.mqtt_password, 60);
- MQTT_Connect(&mqttClient);
- MQTT_Subscribe(&mqttClient, "system/time", 0);
+ if (flashConfig.mqtt_enable)
+ MQTT_Connect(&mqttClient);
+ //MQTT_Subscribe(&mqttClient, "system/time", 0);
}
void ICACHE_FLASH_ATTR
@@ -33,61 +32,9 @@ wifiStateChangeCb(uint8_t status)
void ICACHE_FLASH_ATTR
mqtt_client_init()
{
+ 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);
wifiAddStateChangeCb(wifiStateChangeCb);
}
-
-
-#if 0
-MQTT_Client mqttClient;
-
-void ICACHE_FLASH_ATTR
-mqttConnectedCb(uint32_t *args) {
- MQTT_Client* client = (MQTT_Client*)args;
- MQTT_Publish(client, "announce/all", "Hello World!", 0, 0);
-}
-
-void ICACHE_FLASH_ATTR
-mqttDisconnectedCb(uint32_t *args) {
-// MQTT_Client* client = (MQTT_Client*)args;
- os_printf("MQTT Disconnected\n");
-}
-
-void ICACHE_FLASH_ATTR
-mqttTcpDisconnectedCb(uint32_t *args) {
-// MQTT_Client* client = (MQTT_Client*)args;
- os_printf("MQTT TCP Disconnected\n");
-}
-
-void ICACHE_FLASH_ATTR
-mqttPublishedCb(uint32_t *args) {
-// MQTT_Client* client = (MQTT_Client*)args;
- os_printf("MQTT Published\n");
-}
-
-void ICACHE_FLASH_ATTR
-mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) {
- char *topicBuf = (char*)os_zalloc(topic_len + 1);
- char *dataBuf = (char*)os_zalloc(data_len + 1);
-
-// MQTT_Client* client = (MQTT_Client*)args;
-
- os_memcpy(topicBuf, topic, topic_len);
- topicBuf[topic_len] = 0;
-
- os_memcpy(dataBuf, data, data_len);
- dataBuf[data_len] = 0;
-
- os_printf("Receive topic: %s, data: %s\n", topicBuf, dataBuf);
- os_free(topicBuf);
- os_free(dataBuf);
-}
-
- MQTT_InitConnection(&mqttClient, MQTT_HOST, MQTT_PORT, MQTT_SECURITY);
- MQTT_InitClient(&mqttClient, MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_KEEPALIVE, MQTT_CLSESSION);
- MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);
- MQTT_OnConnected(&mqttClient, mqttConnectedCb);
- MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
- MQTT_OnDisconnected(&mqttClient, mqttTcpDisconnectedCb);
- MQTT_OnPublished(&mqttClient, mqttPublishedCb);
- MQTT_OnData(&mqttClient, mqttDataCb);
-#endif
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 @@
-
-
+
+
+
+
-
-
-
-