misc mqtt tweaks

pull/65/head v2.0.rc1
Thorsten von Eicken 9 years ago
parent de7207ac1d
commit e7311d54f3
  1. 4
      Makefile
  2. 7
      esp-link/cgimqtt.c
  3. 3
      esp-link/cgiwifi.c
  4. 29
      esp-link/main.c
  5. 43
      esp-link/mqtt_client.c
  6. 2
      esp-link/status.c

@ -31,8 +31,8 @@ ESPPORT ?= /dev/ttyUSB0
ESPBAUD ?= 460800 ESPBAUD ?= 460800
# The Wifi station configuration can be hard-coded here, which makes esp-link come up in STA+AP # The Wifi station configuration can be hard-coded here, which makes esp-link come up in STA+AP
# mode trying to connect to the specified AP. It will only switch to STA-only a few seconds after # mode trying to connect to the specified AP *only* if the flash wireless settings are empty!
# actually connecting the AP. # This happens on a full serial flash and avoids having to hunt for the AP...
# STA_SSID ?= # STA_SSID ?=
# STA_PASS ?= # STA_PASS ?=

@ -119,12 +119,7 @@ int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) {
os_printf("MQTT server settings changed, enable=%d\n", flashConfig.mqtt_enable); os_printf("MQTT server settings changed, enable=%d\n", flashConfig.mqtt_enable);
#endif #endif
MQTT_Free(&mqttClient); // safe even if not connected MQTT_Free(&mqttClient); // safe even if not connected
MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, mqtt_client_init();
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 // if just enable changed we just need to bounce the client
} else if (mqtt_en_chg > 0) { } else if (mqtt_en_chg > 0) {

@ -110,6 +110,7 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) {
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
wifiAddStateChangeCb(WifiStateChangeCb cb) { wifiAddStateChangeCb(WifiStateChangeCb cb) {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
if (wifi_state_change_cb[i] == cb) return;
if (wifi_state_change_cb[i] == NULL) { if (wifi_state_change_cb[i] == NULL) {
wifi_state_change_cb[i] = cb; wifi_state_change_cb[i] = cb;
return; return;
@ -599,7 +600,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) {
len += os_sprintf(buff+len, "\"x\":0}\n"); len += os_sprintf(buff+len, "\"x\":0}\n");
#ifdef CGIWIFI_DBG #ifdef CGIWIFI_DBG
os_printf(" -> %s\n", buff); //os_printf(" -> %s\n", buff);
#endif #endif
httpdSend(connData, buff, len); httpdSend(connData, buff, len);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;

@ -52,7 +52,7 @@ HttpdBuiltInUrl builtInUrls[] = {
{ "/console/baud", ajaxConsoleBaud, NULL }, { "/console/baud", ajaxConsoleBaud, NULL },
{ "/console/text", ajaxConsole, NULL }, { "/console/text", ajaxConsole, NULL },
//Enable the line below to protect the WiFi configuration with an username/password combo. //Enable the line below to protect the WiFi configuration with an username/password combo.
// {"/wifi/*", authBasic, myPassFn}, // {"/wifi/*", authBasic, myPassFn},
{ "/wifi", cgiRedirect, "/wifi/wifi.html" }, { "/wifi", cgiRedirect, "/wifi/wifi.html" },
{ "/wifi/", cgiRedirect, "/wifi/wifi.html" }, { "/wifi/", cgiRedirect, "/wifi/wifi.html" },
{ "/wifi/info", cgiWifiInfo, NULL }, { "/wifi/info", cgiWifiInfo, NULL },
@ -119,18 +119,25 @@ void user_init(void) {
os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*"); os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*");
#if defined(STA_SSID) && defined(STA_PASS) #if defined(STA_SSID) && defined(STA_PASS)
struct station_config stconf; int x = wifi_get_opmode() & 0x3;
wifi_station_get_config(&stconf); if (x == 2) {
// we only force the STA settings when a full flash of the module has been made, which
os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32); // resets the wifi settings not to have anything configured
os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64); struct station_config stconf;
wifi_station_get_config(&stconf);
if (os_strlen((char*)stconf.ssid) == 0 && os_strlen((char*)stconf.password) == 0) {
os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32);
os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64);
#ifdef CGIWIFI_DBG #ifdef CGIWIFI_DBG
os_printf("Wifi pre-config trying to connect to AP %s pw %s\n", os_printf("Wifi pre-config trying to connect to AP %s pw %s\n",
(char*)stconf.ssid, (char*)stconf.password); (char*)stconf.ssid, (char*)stconf.password);
#endif #endif
wifi_set_opmode(3); // sta+ap, will switch to sta-only 15 secs after connecting wifi_set_opmode(3); // sta+ap, will switch to sta-only 15 secs after connecting
stconf.bssid_set = 0; stconf.bssid_set = 0;
wifi_station_set_config(&stconf); wifi_station_set_config(&stconf);
}
}
#endif #endif
// Status LEDs // Status LEDs

@ -10,9 +10,12 @@
#define DBG_MQTTCLIENT(format, ...) do { } while(0) #define DBG_MQTTCLIENT(format, ...) do { } while(0)
#endif #endif
MQTT_Client mqttClient; MQTT_Client mqttClient; // main mqtt client used by esp-link
#ifdef BRUNNELS
char* statusTopicStr; char* statusTopicStr;
static char* onlineMsgStr; static char* onlineMsgStr;
#endif
static MqttCallback connected_cb; static MqttCallback connected_cb;
static MqttCallback disconnected_cb; static MqttCallback disconnected_cb;
@ -21,10 +24,12 @@ static MqttDataCallback data_cb;
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
mqttConnectedCb(uint32_t *args) { mqttConnectedCb(uint32_t *args) {
MQTT_Client* client = (MQTT_Client*)args;
DBG_MQTTCLIENT("MQTT Client: Connected\n"); DBG_MQTTCLIENT("MQTT Client: Connected\n");
MQTT_Subscribe(client, "system/time", 0); //MQTT_Client* client = (MQTT_Client*)args;
//MQTT_Subscribe(client, "system/time", 0); // handy for testing
#ifdef BRUNNELS
MQTT_Publish(client, "announce/all", onlineMsgStr, 0, 0); MQTT_Publish(client, "announce/all", onlineMsgStr, 0, 0);
#endif
if (connected_cb) if (connected_cb)
connected_cb(args); connected_cb(args);
} }
@ -49,6 +54,7 @@ void ICACHE_FLASH_ATTR
mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) { mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) {
// MQTT_Client* client = (MQTT_Client*)args; // MQTT_Client* client = (MQTT_Client*)args;
#ifdef MQTTCLIENT_DBG
char *topicBuf = (char*)os_zalloc(topic_len + 1); char *topicBuf = (char*)os_zalloc(topic_len + 1);
char *dataBuf = (char*)os_zalloc(data_len + 1); char *dataBuf = (char*)os_zalloc(data_len + 1);
@ -58,9 +64,10 @@ mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *da
os_memcpy(dataBuf, data, data_len); os_memcpy(dataBuf, data, data_len);
dataBuf[data_len] = 0; dataBuf[data_len] = 0;
DBG_MQTTCLIENT("MQTT Client: Received topic: %s, data: %s\n", topicBuf, dataBuf); os_printf("MQTT Client: Received topic: %s, data: %s\n", topicBuf, dataBuf);
os_free(topicBuf); os_free(topicBuf);
os_free(dataBuf); os_free(dataBuf);
#endif
if (data_cb) if (data_cb)
data_cb(args, topic, topic_len, data, data_len); data_cb(args, topic, topic_len, data, data_len);
@ -70,7 +77,7 @@ void ICACHE_FLASH_ATTR
wifiStateChangeCb(uint8_t status) wifiStateChangeCb(uint8_t status)
{ {
if (flashConfig.mqtt_enable) { if (flashConfig.mqtt_enable) {
if (status == wifiGotIP && mqttClient.connState != TCP_CONNECTING) { if (status == wifiGotIP && mqttClient.connState != TCP_CONNECTING) {
MQTT_Connect(&mqttClient); MQTT_Connect(&mqttClient);
} }
else if (status == wifiIsDisconnected && mqttClient.connState == TCP_CONNECTING) { else if (status == wifiIsDisconnected && mqttClient.connState == TCP_CONNECTING) {
@ -82,21 +89,16 @@ wifiStateChangeCb(uint8_t status)
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
mqtt_client_init() mqtt_client_init()
{ {
if (flashConfig.mqtt_enable) { MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, flashConfig.mqtt_timeout,
MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, flashConfig.mqtt_timeout, flashConfig.mqtt_clientid, flashConfig.mqtt_username, flashConfig.mqtt_password,
flashConfig.mqtt_clientid, flashConfig.mqtt_username, flashConfig.mqtt_password, flashConfig.mqtt_keepalive);
flashConfig.mqtt_keepalive);
if (flashConfig.mqtt_status_enable) {
// removed client_id concat for now until a better solution is devised // removed client_id concat for now until a better solution is devised
// statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(flashConfig.mqtt_status_topic) + 2); // statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(flashConfig.mqtt_status_topic) + 2);
// os_strcpy(statusTopicStr, flashConfig.mqtt_clientid); // os_strcpy(statusTopicStr, flashConfig.mqtt_clientid);
// os_strcat(statusTopicStr, "/"); // os_strcat(statusTopicStr, "/");
statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_status_topic) + 1); #ifdef BRUNNELS
os_strcpy(statusTopicStr, flashConfig.mqtt_status_topic);
}
char* onlineMsg = " is online"; char* onlineMsg = " is online";
onlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(onlineMsg) + 1); onlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(onlineMsg) + 1);
os_strcpy(onlineMsgStr, flashConfig.mqtt_clientid); os_strcpy(onlineMsgStr, flashConfig.mqtt_clientid);
@ -112,12 +114,15 @@ mqtt_client_init()
os_strcpy(lwtMsgStr, flashConfig.mqtt_clientid); os_strcpy(lwtMsgStr, flashConfig.mqtt_clientid);
os_strcat(lwtMsgStr, lwt); os_strcat(lwtMsgStr, lwt);
MQTT_InitLWT(&mqttClient, lwtMsgStr, offlineMsg, 0, 0); MQTT_InitLWT(&mqttClient, lwtMsgStr, offlineMsg, 0, 0);
#endif
MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnConnected(&mqttClient, mqttConnectedCb);
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb);
MQTT_OnData(&mqttClient, mqttDataCb); MQTT_OnData(&mqttClient, mqttDataCb);
}
if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0)
MQTT_Connect(&mqttClient);
wifiAddStateChangeCb(wifiStateChangeCb); wifiAddStateChangeCb(wifiStateChangeCb);
} }

@ -37,7 +37,7 @@ static void ICACHE_FLASH_ATTR mqttStatusCb(void *v) {
char buf[128]; char buf[128];
mqttStatusMsg(buf); mqttStatusMsg(buf);
MQTT_Publish(&mqttClient, statusTopicStr, buf, 1, 0); MQTT_Publish(&mqttClient, flashConfig.mqtt_status_topic, buf, 1, 0);
} }

Loading…
Cancel
Save