diff --git a/esp-link.vcxproj b/esp-link.vcxproj
index 5682d27..ddeacea 100644
--- a/esp-link.vcxproj
+++ b/esp-link.vcxproj
@@ -10,66 +10,20 @@
ARM
-
- {A92F0CAA-F89B-4F78-AD2A-A042429BD87F}
- MakeFileProj
-
-
-
- Makefile
-
-
-
-
-
-
-
-
-
-
- __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__
- .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.3.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include
-
-
-
-
-
-
- bin
- build
-
-
- espmake all wiflash
- espmake clean all
- espmake clean
-
-
- espmake clean all wiflash
- espmake clean all
- espmake clean
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
+
@@ -78,44 +32,31 @@
-
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
+
+
+
+
+
+
+
@@ -127,34 +68,78 @@
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {A92F0CAA-F89B-4F78-AD2A-A042429BD87F}
+ MakeFileProj
+
+
+
+ Makefile
+
+
+
+
+
+
+
+
+
+
+ __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__
+ .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.3.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include
+
+
+
+
+
+
+ bin
+ build
+
+
+ espmake all wiflash
+ espmake clean all
+ espmake clean
+
+
+ espmake clean all wiflash
+ espmake clean all
+ espmake clean
+
+
+
+
+
+
diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c
index fa0e5fb..8ada7a9 100644
--- a/esp-link/mqtt_client.c
+++ b/esp-link/mqtt_client.c
@@ -3,91 +3,137 @@
#include "config.h"
#include "mqtt.h"
-MQTT_Client mqttClient;
-
-static ETSTimer mqttTimer;
-
-static int once = 0;
-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);
-}
-
-void ICACHE_FLASH_ATTR
-wifiStateChangeCb(uint8_t status)
-{
- if (status == wifiGotIP) {
- os_timer_disarm(&mqttTimer);
- os_timer_setfn(&mqttTimer, mqttTimerCb, NULL);
- os_timer_arm(&mqttTimer, 200, 0);
- }
-}
-
-
-// initialize the custom stuff that goes beyond esp-link
-void ICACHE_FLASH_ATTR
-mqtt_client_init()
-{
- wifiAddStateChangeCb(wifiStateChangeCb);
-}
-
+#ifdef MQTTCLIENT_DBG
+#define DBG_MQTTCLIENT(format, ...) os_printf(format, ## __VA_ARGS__)
+#else
+#define DBG_MQTTCLIENT(format, ...) do { } while(0)
+#endif
-#if 0
MQTT_Client mqttClient;
+char* statusTopicStr;
+static char* onlineMsgStr;
+
+static MqttCallback connected_cb;
+static MqttCallback disconnected_cb;
+static MqttCallback published_cb;
+static MqttDataCallback data_cb;
void ICACHE_FLASH_ATTR
mqttConnectedCb(uint32_t *args) {
MQTT_Client* client = (MQTT_Client*)args;
- MQTT_Publish(client, "announce/all", "Hello World!", 0, 0);
+ DBG_MQTTCLIENT("MQTT Client: Connected\n");
+ MQTT_Subscribe(client, "system/time", 0);
+ MQTT_Publish(client, "announce/all", onlineMsgStr, 0, 0);
+ if (connected_cb)
+ connected_cb(args);
}
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");
+ DBG_MQTTCLIENT("MQTT Client: Disconnected\n");
+ if (disconnected_cb)
+ disconnected_cb(args);
}
void ICACHE_FLASH_ATTR
mqttPublishedCb(uint32_t *args) {
// MQTT_Client* client = (MQTT_Client*)args;
- os_printf("MQTT Published\n");
+ DBG_MQTTCLIENT("MQTT Client: Published\n");
+ if (published_cb)
+ published_cb(args);
}
void ICACHE_FLASH_ATTR
mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) {
+ // MQTT_Client* client = (MQTT_Client*)args;
+
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);
+ DBG_MQTTCLIENT("MQTT Client: Received topic: %s, data: %s\n", topicBuf, dataBuf);
os_free(topicBuf);
os_free(dataBuf);
+
+ if (data_cb)
+ data_cb(args, topic, topic_len, data, data_len);
}
- 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
+void ICACHE_FLASH_ATTR
+wifiStateChangeCb(uint8_t status)
+{
+ if (flashConfig.mqtt_enable) {
+ if (status == wifiGotIP && mqttClient.connState != TCP_CONNECTING) {
+ MQTT_Connect(&mqttClient);
+ }
+ else if (status == wifiIsDisconnected && mqttClient.connState == TCP_CONNECTING) {
+ MQTT_Disconnect(&mqttClient);
+ }
+ }
+}
+
+void ICACHE_FLASH_ATTR
+mqtt_client_init()
+{
+ if (flashConfig.mqtt_enable) {
+ 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_status_enable) {
+ statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(flashConfig.mqtt_status_topic) + 2);
+ os_strcpy(statusTopicStr, flashConfig.mqtt_clientid);
+ os_strcat(statusTopicStr, "/");
+ os_strcat(statusTopicStr, flashConfig.mqtt_status_topic);
+ }
+
+ char* onlineMsg = " is online";
+ onlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(onlineMsg) + 1);
+ os_strcpy(onlineMsgStr, flashConfig.mqtt_clientid);
+ os_strcat(onlineMsgStr, onlineMsg);
+
+ char* offlineMsg = " is offline";
+ char* offlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(offlineMsg) + 1);
+ os_strcpy(offlineMsgStr, flashConfig.mqtt_clientid);
+ os_strcat(offlineMsgStr, offlineMsg);
+
+ char* lwt = "/lwt";
+ char *lwtMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(lwt) + 1);
+ os_strcpy(lwtMsgStr, flashConfig.mqtt_clientid);
+ os_strcat(lwtMsgStr, lwt);
+ MQTT_InitLWT(&mqttClient, lwtMsgStr, offlineMsg, 0, 0);
+
+ MQTT_OnConnected(&mqttClient, mqttConnectedCb);
+ MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
+ MQTT_OnPublished(&mqttClient, mqttPublishedCb);
+ MQTT_OnData(&mqttClient, mqttDataCb);
+ }
+
+ wifiAddStateChangeCb(wifiStateChangeCb);
+}
+
+void ICACHE_FLASH_ATTR
+mqtt_client_on_connected(MqttCallback connectedCb) {
+ connected_cb = connectedCb;
+}
+
+void ICACHE_FLASH_ATTR
+mqtt_client_on_disconnected(MqttCallback disconnectedCb) {
+ disconnected_cb = disconnectedCb;
+}
+
+void ICACHE_FLASH_ATTR
+mqtt_client_on_published(MqttCallback publishedCb) {
+ published_cb = publishedCb;
+}
+
+void ICACHE_FLASH_ATTR
+mqtt_client_on_data(MqttDataCallback dataCb) {
+ data_cb = dataCb;
+}
diff --git a/esp-link/mqtt_client.h b/esp-link/mqtt_client.h
index 9ae07aa..d0b42fe 100644
--- a/esp-link/mqtt_client.h
+++ b/esp-link/mqtt_client.h
@@ -4,6 +4,11 @@
#include "mqtt.h"
extern MQTT_Client mqttClient;
+extern char* statusTopicStr;
void mqtt_client_init();
+void mqtt_client_on_connected(MqttCallback connectedCb);
+void mqtt_client_on_disconnected(MqttCallback disconnectedCb);
+void mqtt_client_on_published(MqttCallback publishedCb);
+void mqtt_client_on_data(MqttDataCallback dataCb);
#endif
diff --git a/esp-link/status.c b/esp-link/status.c
index ded4ea4..ae57d57 100644
--- a/esp-link/status.c
+++ b/esp-link/status.c
@@ -5,6 +5,7 @@
#include "serled.h"
#include "cgiwifi.h"
#include "mqtt.h"
+#include "mqtt_client.h"
extern MQTT_Client mqttClient;
@@ -95,9 +96,7 @@ static void ICACHE_FLASH_ATTR mqttStatusCb(void *v) {
char buf[128];
mqttStatusMsg(buf);
- MQTT_Publish(&mqttClient, flashConfig.mqtt_status_topic, buf, 1, 0);
-
- //espconn_disconnect(mqttClient.pCon);
+ MQTT_Publish(&mqttClient, statusTopicStr, buf, 1, 0);
}
//===== Init status stuff
diff --git a/user/user_main.c b/user/user_main.c
index 72608c6..5a4331d 100644
--- a/user/user_main.c
+++ b/user/user_main.c
@@ -1,64 +1,7 @@
#include
-#include "cgiwifi.h"
-#include "mqtt.h"
+#include
// initialize the custom stuff that goes beyond esp-link
void app_init() {
}
-
-#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