From 658c4d8b31c18271d64997c895d565d42f951bb5 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 19 Sep 2015 20:57:59 -0700 Subject: [PATCH 1/2] make compile with SDK 1.4.0 --- Makefile | 2 +- include/espmissingincludes.h | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 1d46090..c935555 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/include/espmissingincludes.h b/include/espmissingincludes.h index e5a20a0..70c00bf 100644 --- a/include/espmissingincludes.h +++ b/include/espmissingincludes.h @@ -40,24 +40,20 @@ int os_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4))); int os_printf_plus(const char *format, ...) __attribute__ ((format (printf, 1, 2))); +// memory allocation functions are "different" due to memory debugging functionality +// added in SDK 1.4.0 +void vPortFree(void *ptr, char * file, int line); +void *pvPortMalloc(size_t xWantedSize, char * file, int line); +void *pvPortZalloc(size_t, char * file, int line); +void *vPortMalloc(size_t xWantedSize); void pvPortFree(void *ptr); -void *pvPortMalloc(size_t xWantedSize); -void *pvPortZalloc(size_t); + void uart_div_modify(int no, unsigned int freq); -void vPortFree(void *ptr); -void *vPortMalloc(size_t xWantedSize); uint32 system_get_time(); -//uint8 wifi_get_opmode(void); // defined in SDK 1.0.0 onwards -//int os_random(); // defined in SDK 1.1.0 onwards int rand(void); void ets_bzero(void *s, size_t n); void ets_delay_us(int ms); -// Shortcuts for memory functions -#define os_malloc pvPortMalloc -#define os_free vPortFree -#define os_zalloc pvPortZalloc - // disappeared in SDK 1.1.0: #define os_timer_done ets_timer_done #define os_timer_handler_isr ets_timer_handler_isr @@ -71,4 +67,12 @@ void ets_delay_us(int ms); |( (((FUNC&BIT2)<<2)|(FUNC&0x3))< Date: Sat, 19 Sep 2015 23:31:43 -0700 Subject: [PATCH 2/2] improve mqtt disconnect/reconnect --- esp-link/cgi.h | 12 ++- esp-link/cgimqtt.c | 17 +++- esp-link/config.c | 5 +- esp-link/mqtt_client.c | 67 ++------------ html/mqtt.html | 10 +-- mqtt/mqtt.c | 192 +++++++++++++++++++++++------------------ mqtt/mqtt.h | 6 ++ 7 files changed, 155 insertions(+), 154 deletions(-) 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 @@ - - + + + + - - - -