fix MQTT start-up

pull/196/head
Thorsten von Eicken 8 years ago
parent ba656e0829
commit 65ba42d5ee
  1. 9
      esp-link/mqtt_client.c
  2. 1
      httpd/httpd.c
  3. 3
      include/user_config.h
  4. 139
      mqtt/mqtt_cmd.c

@ -5,7 +5,7 @@
#include "mqtt.h" #include "mqtt.h"
#ifdef MQTTCLIENT_DBG #ifdef MQTTCLIENT_DBG
#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0)
#else #else
#define DBG(format, ...) do { } while(0) #define DBG(format, ...) do { } while(0)
#endif #endif
@ -66,7 +66,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) {
@ -87,8 +87,9 @@ mqtt_client_init()
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) // Don't connect now, wait for a wifi status change callback
MQTT_Connect(&mqttClient); //if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0)
// MQTT_Connect(&mqttClient);
wifiAddStateChangeCb(wifiStateChangeCb); wifiAddStateChangeCb(wifiStateChangeCb);
} }

@ -17,6 +17,7 @@ Esp8266 http server - core routines
#include <esp8266.h> #include <esp8266.h>
#include "httpd.h" #include "httpd.h"
//#define HTTPD_DBG
#ifdef HTTPD_DBG #ifdef HTTPD_DBG
#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0)
#else #else

@ -22,6 +22,7 @@
#define HTTPD_DBG #define HTTPD_DBG
#define MQTT_DBG #define MQTT_DBG
#define MQTTCMD_DBG #define MQTTCMD_DBG
#define MQTTCLIENT_DBG
#undef PKTBUF_DBG #undef PKTBUF_DBG
#define REST_DBG #define REST_DBG
#define RESTCMD_DBG #define RESTCMD_DBG
@ -32,7 +33,7 @@
#define MDNS_DBG #define MDNS_DBG
#define OPTIBOOT_DBG #define OPTIBOOT_DBG
#undef SYSLOG_DBG #undef SYSLOG_DBG
#undef CGISERVICES_DBG #define CGISERVICES_DBG
// If defined, the default hostname for DHCP will include the chip ID to make it unique // If defined, the default hostname for DHCP will include the chip ID to make it unique
#undef CHIP_IN_HOSTNAME #undef CHIP_IN_HOSTNAME

@ -184,63 +184,6 @@ MQTTCMD_Setup(CmdPacket *cmd) {
if (cmdGetArgc(&req) != 4) return; if (cmdGetArgc(&req) != 4) return;
#if 0
// This section is commented-out because we're using the same client as esp-link is using itself,
// i.e. the one set-up in the Web UI. This code was here when we used a separate client for the
// attached uC, which just makes life more complicated...
if (cmdGetArgc(&req) != 9)
return 0;
// create mqtt client
uint8_t clientLen = sizeof(MQTT_Client);
MQTT_Client* client = (MQTT_Client*)os_zalloc(clientLen);
if (client == NULL) return 0;
os_memset(client, 0, clientLen);
uint16_t len;
uint8_t *client_id, *user_data, *pass_data;
uint32_t keepalive, clean_session;
// get client id
len = cmdArgLen(&req);
if (len > 32) return 0; // safety check
client_id = (uint8_t*)os_zalloc(len + 1);
cmdPopArg(&req, client_id, len);
client_id[len] = 0;
// get username
len = cmdArgLen(&req);
if (len > 32) return 0; // safety check
user_data = (uint8_t*)os_zalloc(len + 1);
cmdPopArg(&req, user_data, len);
user_data[len] = 0;
// get password
len = cmdArgLen(&req);
if (len > 32) return 0; // safety check
pass_data = (uint8_t*)os_zalloc(len + 1);
cmdPopArg(&req, pass_data, len);
pass_data[len] = 0;
// get keepalive
cmdPopArg(&req, (uint8_t*)&keepalive, 4);
// get clean session
cmdPopArg(&req, (uint8_t*)&clean_session, 4);
#ifdef MQTTCMD_DBG
DBG("MQTT: MQTTCMD_Setup clientid=%s, user=%s, pw=%s, keepalive=%ld, clean_session=%ld\n", client_id, user_data, pass_data, keepalive, clean_session);
#endif
// init client
// TODO: why malloc these all here, pass to MQTT_InitClient to be malloc'd again?
MQTT_InitClient(client, (char*)client_id, (char*)user_data, (char*)pass_data, keepalive, clean_session);
os_free(client_id);
os_free(user_data);
os_free(pass_data);
#endif
// create callback // create callback
MqttCmdCb* callback = (MqttCmdCb*)os_zalloc(sizeof(MqttCmdCb)); MqttCmdCb* callback = (MqttCmdCb*)os_zalloc(sizeof(MqttCmdCb));
cmdPopArg(&req, &callback->connectedCb, 4); cmdPopArg(&req, &callback->connectedCb, 4);
@ -263,85 +206,3 @@ MQTTCMD_Setup(CmdPacket *cmd) {
cmdMqttDisconnectedCb(client); cmdMqttDisconnectedCb(client);
} }
} }
#if 0
// This section is commented-out because we're using the same client as esp-link is using itself,
// i.e. the one set-up in the Web UI. This code was here when we used a separate client for the
// attached uC, which just makes life more complicated...
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Connect(CmdPacket *cmd) {
CmdRequest req;
cmdRequest(&req, cmd);
#ifdef MQTT_1_CLIENT
if (mqttClient.connState == MQTT_CONNECTED && mqttClient.cmdConnectedCb) {
mqttClient.cmdConnectedCb((uint32_t*)&mqttClient);
}
else if (mqttClient.connState == MQTT_DISCONNECTED && mqttClient.cmdDisconnectedCb) {
mqttClient.cmdDisconnectedCb((uint32_t*)&mqttClient);
}
return 1;
#else
if (cmdGetArgc(&req) != 4)
return 0;
// get mqtt client
uint32_t client_ptr;
cmdPopArg(&req, (uint8_t*)&client_ptr, 4);
MQTT_Client* client = (MQTT_Client*)client_ptr;
DBG("MQTT: MQTTCMD_Connect client ptr=%p\n", (void*)client_ptr);
uint16_t len;
// get host
if (client->host)
os_free(client->host);
len = cmdArgLen(&req);
if (len > 128) return 0; // safety check
client->host = (char*)os_zalloc(len + 1);
cmdPopArg(&req, client->host, len);
client->host[len] = 0;
// get port
cmdPopArg(&req, (uint8_t*)&client->port, 4);
// get security
cmdPopArg(&req, (uint8_t*)&client->security, 4);
DBG("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n",
client->host,
client->port,
client->security);
MQTT_Connect(client);
return 1;
#endif
}
uint32_t ICACHE_FLASH_ATTR
MQTTCMD_Disconnect(CmdPacket *cmd) {
CmdRequest req;
cmdRequest(&req, cmd);
#ifdef MQTT_1_CLIENT
return 1;
#else
if (cmdGetArgc(&req) != 1)
return 0;
// get mqtt client
uint32_t client_ptr;
cmdPopArg(&req, (uint8_t*)&client_ptr, 4);
MQTT_Client* client = (MQTT_Client*)client_ptr;
DBG("MQTT: MQTTCMD_Disconnect client ptr=%p\n", (void*)client_ptr);
// disconnect
MQTT_Disconnect(client);
return 1;
#endif
}
#endif

Loading…
Cancel
Save