From ae5eaeddea0976cf7e1a23dd383bdd1c3dab10c8 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 13 Sep 2015 09:19:25 -0500 Subject: [PATCH 1/6] mqtt_client updates --- esp-link.vcxproj | 185 +++++++++++++++++++---------------------- esp-link/mqtt_client.c | 156 ++++++++++++++++++++++------------ esp-link/mqtt_client.h | 5 ++ esp-link/status.c | 5 +- user/user_main.c | 59 +------------ 5 files changed, 194 insertions(+), 216 deletions(-) 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 From 5068fa28cb052fff684d811e7bf8631bc9efb5f5 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sat, 19 Sep 2015 14:11:02 -0500 Subject: [PATCH 2/6] Added buildtime wifi config ability Added SDK_DBG parameter to turn off nonsense SDK debug output --- Makefile | 12 ++++++++++++ esp-link/log.c | 3 ++- esp-link/main.c | 16 ++++++++++++---- include/espmissingincludes.h | 18 +++++++++++++++--- serial/serbridge.c | 8 +------- serial/uart.c | 5 +---- 6 files changed, 43 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 1d46090..e0f08e3 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,10 @@ ESPTOOL ?= $(abspath ../esp-open-sdk/esptool/esptool.py) ESPPORT ?= /dev/ttyUSB0 ESPBAUD ?= 460800 +# Build time Wifi Cfg +# STA_SSID ?= +# STA_PASS ?= + # --------------- chipset configuration --------------- # Pick your flash size: "512KB", "1MB", or "4MB" @@ -244,6 +248,14 @@ Q := @ vecho := @echo endif +ifneq ($(strip $(STA_SSID)),) +CFLAGS += -DSTA_SSID="$(STA_SSID)" +endif + +ifneq ($(strip $(STA_PASS)),) +CFLAGS += -DSTA_PASS="$(STA_PASS)" +endif + ifeq ("$(GZIP_COMPRESSION)","yes") CFLAGS += -DGZIP_COMPRESSION endif diff --git a/esp-link/log.c b/esp-link/log.c index ca8e0ad..1a9a985 100644 --- a/esp-link/log.c +++ b/esp-link/log.c @@ -162,8 +162,9 @@ void ICACHE_FLASH_ATTR dumpMem(void *addr, int len) { int off = 0; while (off < len) { os_printf("%p ", a); - for (int i=0; i<16 && off+i 0x20 && *a < 0x3f ? *a : '.'); diff --git a/esp-link/main.c b/esp-link/main.c index 980fc11..ccb596f 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -79,7 +79,19 @@ static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) { } #endif +# define VERS_STR_STR(V) #V +# define VERS_STR(V) VERS_STR_STR(V) +char* esp_link_version = VERS_STR(VERSION); + void user_rf_pre_init(void) { + system_set_os_print(DEBUG_SDK); +#if defined(STA_SSID) && defined(STA_PASS) + struct station_config stconf; + os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32); + os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64); + stconf.bssid_set = 0; + wifi_station_set_config_current(&stconf); +#endif } // address of espfs binary blob @@ -93,10 +105,6 @@ static char *flash_maps[] = { "2MB:1024/1024", "4MB:1024/1024" }; -# define VERS_STR_STR(V) #V -# define VERS_STR(V) VERS_STR_STR(V) -char* esp_link_version = VERS_STR(VERSION); - extern void app_init(void); extern void mqtt_client_init(void); diff --git a/include/espmissingincludes.h b/include/espmissingincludes.h index e5a20a0..82076ab 100644 --- a/include/espmissingincludes.h +++ b/include/espmissingincludes.h @@ -36,9 +36,21 @@ void ets_timer_setfn(ETSTimer *t, ETSTimerFunc *fn, void *parg); void ets_update_cpu_frequency(int freqmhz); -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))); +#ifdef SDK_DBG +#define DEBUG_SDK true +#else +#define DEBUG_SDK false +#endif + +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))); + +#undef os_printf +#define os_printf(format, ...) \ + system_set_os_print(true); \ + os_printf_plus(format, ## __VA_ARGS__); \ + system_set_os_print(DEBUG_SDK); // int os_printf(const char *format, ...) + void pvPortFree(void *ptr); void *pvPortMalloc(size_t xWantedSize); diff --git a/serial/serbridge.c b/serial/serbridge.c index e713693..16e5c65 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -1,12 +1,6 @@ // Copyright 2015 by Thorsten von Eicken, see LICENSE.txt -#include "espmissingincludes.h" -#include "c_types.h" -#include "user_interface.h" -#include "espconn.h" -#include "mem.h" -#include "osapi.h" -#include "gpio.h" +#include "esp8266.h" #include "uart.h" #include "crc16.h" diff --git a/serial/uart.c b/serial/uart.c index 526c078..9b517c4 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -17,10 +17,7 @@ * ---------------------------------------------------------------------------- * Heavily modified and enhanced by Thorsten von Eicken in 2015 */ -#include "espmissingincludes.h" -#include "ets_sys.h" -#include "osapi.h" -#include "user_interface.h" +#include "esp8266.h" #include "uart.h" #ifdef UART_DBG From 90bc0a32dad1e11faafedbfcff33bd7acc3d2baf Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 20 Sep 2015 10:20:35 -0500 Subject: [PATCH 3/6] Changed mqtt statusTopicStr to not pre-pend client_id --- esp-link/mqtt_client.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index 8ada7a9..654d0ec 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -87,10 +87,13 @@ mqtt_client_init() 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); +// 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); +// os_strcpy(statusTopicStr, flashConfig.mqtt_clientid); +// os_strcat(statusTopicStr, "/"); + + statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_status_topic) + 1); + os_strcpy(statusTopicStr, flashConfig.mqtt_status_topic); } char* onlineMsg = " is online"; From 2ab194f83c612b42bd225adfd4b1d14385d03504 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 20 Sep 2015 16:18:26 -0500 Subject: [PATCH 4/6] Added ifdef checks for mqtt enabled Added better handling of compile time config of SSID and PASSWORD Removed default disable of ESP SDK debug log messages --- Makefile | 116 +++++++++++++++++++++-------------------- esp-link.vcxproj | 11 ++++ esp-link/cgi.c | 2 + esp-link/cgimqtt.c | 4 +- esp-link/cgimqtt.h | 5 +- esp-link/main.c | 43 ++++++++++----- esp-link/mqtt_client.c | 3 ++ esp-link/mqtt_client.h | 5 +- esp-link/status.c | 69 +++++++++++++----------- html/mqtt.html | 1 + html/mqtt.js | 76 +++++++++++++++++++++++++++ html/ui.js | 78 --------------------------- include/user_config.h | 1 + 13 files changed, 229 insertions(+), 185 deletions(-) create mode 100644 html/mqtt.js diff --git a/Makefile b/Makefile index c438a3d..3d4c186 100644 --- a/Makefile +++ b/Makefile @@ -33,11 +33,67 @@ ESPBAUD ?= 460800 # STA_SSID ?= # STA_PASS ?= +# hostname or IP address for wifi flashing +ESP_HOSTNAME ?= esp-link + # --------------- chipset configuration --------------- # Pick your flash size: "512KB", "1MB", or "4MB" FLASH_SIZE ?= 4MB +# The pin assignments below are used when the settings in flash are invalid, they +# can be changed via the web interface +# GPIO pin used to reset attached microcontroller, acative low +MCU_RESET_PIN ?= 12 +# GPIO pin used with reset to reprogram MCU (ISP=in-system-programming, unused with AVRs), active low +MCU_ISP_PIN ?= 13 +# GPIO pin used for "connectivity" LED, active low +LED_CONN_PIN ?= 0 +# GPIO pin used for "serial activity" LED, active low +LED_SERIAL_PIN ?= 14 + +# --------------- esp-link config options --------------- + +# If CHANGE_TO_STA is set to "yes" the esp-link module will switch to station mode +# once successfully connected to an access point. Else it will stay in AP+STA mode. + +CHANGE_TO_STA ?= yes + +# --------------- esphttpd config options --------------- + +# If GZIP_COMPRESSION is set to "yes" then the static css, js, and html files will be compressed +# with gzip before added to the espfs image and will be served with gzip Content-Encoding header. +# This could speed up the downloading of these files, but might break compatibility with older +# web browsers not supporting gzip encoding because Accept-Encoding is simply ignored. +# Enable this option if you have large static files to serve (for e.g. JQuery, Twitter bootstrap) +# If you have text based static files with different extensions what you want to serve compressed +# then you will need to add the extension to the following places: +# - Add the extension to this Makefile at the webpages.espfs target to the find command +# - Add the extension to the gzippedFileTypes array in the user/httpd.c file +# +# Adding JPG or PNG files (and any other compressed formats) is not recommended, because GZIP +# compression does not work effectively on compressed files. + +#Static gzipping is disabled by default. +GZIP_COMPRESSION ?= yes + +# If COMPRESS_W_HTMLCOMPRESSOR is set to "yes" then the static css and js files will be compressed with +# htmlcompressor and yui-compressor. This option works only when GZIP_COMPRESSION is set to "yes". +# https://code.google.com/p/htmlcompressor/#For_Non-Java_Projects +# http://yui.github.io/yuicompressor/ +# enabled by default. +COMPRESS_W_HTMLCOMPRESSOR ?= yes +HTML-COMPRESSOR ?= htmlcompressor-1.5.3.jar +YUI-COMPRESSOR ?= yuicompressor-2.4.8.jar + +# Optional Modules +MODULES ?= mqtt rest + +# -------------- End of config options ------------- + +HTML_PATH = $(abspath ./html)/ +WIFI_PATH = $(HTML_PATH)wifi/ + ifeq ("$(FLASH_SIZE)","512KB") # Winbond 25Q40 512KB flash, typ for esp-01 thru esp-11 ESP_SPI_SIZE ?= 0 # 0->512KB (256KB+256KB) @@ -87,20 +143,6 @@ ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_BLANK ?= 0x3FE000 # where to flash blank.bin to erase wireless settings endif -# hostname or IP address for wifi flashing -ESP_HOSTNAME ?= esp-link - -# The pin assignments below are used when the settings in flash are invalid, they -# can be changed via the web interface -# GPIO pin used to reset attached microcontroller, acative low -MCU_RESET_PIN ?= 12 -# GPIO pin used with reset to reprogram MCU (ISP=in-system-programming, unused with AVRs), active low -MCU_ISP_PIN ?= 13 -# GPIO pin used for "connectivity" LED, active low -LED_CONN_PIN ?= 0 -# GPIO pin used for "serial activity" LED, active low -LED_SERIAL_PIN ?= 14 - # --------------- esp-link version --------------- # This queries git to produce a version string like "esp-link v0.9.0 2015-06-01 34bc76" @@ -115,48 +157,6 @@ SHA := $(shell if git diff --quiet HEAD; then git rev-parse --short HEAD | c else echo "development"; fi) VERSION ?=esp-link $(BRANCH) - $(DATE) - $(SHA) -# --------------- esp-link config options --------------- - -# If CHANGE_TO_STA is set to "yes" the esp-link module will switch to station mode -# once successfully connected to an access point. Else it will stay in AP+STA mode. - -CHANGE_TO_STA ?= yes - -# --------------- esphttpd config options --------------- - -# If GZIP_COMPRESSION is set to "yes" then the static css, js, and html files will be compressed -# with gzip before added to the espfs image and will be served with gzip Content-Encoding header. -# This could speed up the downloading of these files, but might break compatibility with older -# web browsers not supporting gzip encoding because Accept-Encoding is simply ignored. -# Enable this option if you have large static files to serve (for e.g. JQuery, Twitter bootstrap) -# If you have text based static files with different extensions what you want to serve compressed -# then you will need to add the extension to the following places: -# - Add the extension to this Makefile at the webpages.espfs target to the find command -# - Add the extension to the gzippedFileTypes array in the user/httpd.c file -# -# Adding JPG or PNG files (and any other compressed formats) is not recommended, because GZIP -# compression does not work effectively on compressed files. - -#Static gzipping is disabled by default. -GZIP_COMPRESSION ?= yes - -# If COMPRESS_W_HTMLCOMPRESSOR is set to "yes" then the static css and js files will be compressed with -# htmlcompressor and yui-compressor. This option works only when GZIP_COMPRESSION is set to "yes". -# https://code.google.com/p/htmlcompressor/#For_Non-Java_Projects -# http://yui.github.io/yuicompressor/ -# enabled by default. -COMPRESS_W_HTMLCOMPRESSOR ?= yes -HTML-COMPRESSOR ?= htmlcompressor-1.5.3.jar -YUI-COMPRESSOR ?= yuicompressor-2.4.8.jar - -HTML_PATH = $(abspath ./html)/ -WIFI_PATH = $(HTML_PATH)wifi/ - -# Optional Modules -MODULES ?= mqtt rest - -# -------------- End of config options ------------- - # Output directors to store intermediate compiled files # relative to the project directory BUILD_BASE = build @@ -376,6 +376,10 @@ ifeq ("$(COMPRESS_W_HTMLCOMPRESSOR)","yes") $(Q) for file in `find html_compressed -type f -name "*.css"`; do \ java -jar tools/$(YUI-COMPRESSOR) $$file -o $$file; \ done +endif +ifeq (,$(findstring mqtt,$(MODULES))) + $(Q) rm -rf html_compressed/mqtt.html + $(Q) rm -rf html_compressed/mqtt.js endif $(Q) for file in `find html_compressed -type f -name "*.htm*"`; do \ cat html_compressed/head- $$file >$${file}-; \ diff --git a/esp-link.vcxproj b/esp-link.vcxproj index ddeacea..57e3a8d 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -85,10 +85,18 @@ + + + + + + + + @@ -97,6 +105,9 @@ + + + {A92F0CAA-F89B-4F78-AD2A-A042429BD87F} MakeFileProj diff --git a/esp-link/cgi.c b/esp-link/cgi.c index 5c0584c..0a52538 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -153,7 +153,9 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) { "{\"menu\": [\"Home\", \"/home.html\", " "\"Wifi\", \"/wifi/wifi.html\"," "\"\xC2\xB5" "C Console\", \"/console.html\", " +#ifdef MQTT "\"REST/MQTT\", \"/mqtt.html\"," +#endif "\"Debug log\", \"/log.html\" ],\n" " \"version\": \"%s\" }", esp_link_version); httpdSend(connData, buff, -1); diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index e0dd92a..877f976 100644 --- a/esp-link/cgimqtt.c +++ b/esp-link/cgimqtt.c @@ -1,6 +1,5 @@ // Copyright 2015 by Thorsten von Eicken, see LICENSE.txt -// // TCP Client settings - +#ifdef MQTT #include #include "cgi.h" #include "config.h" @@ -175,3 +174,4 @@ int ICACHE_FLASH_ATTR cgiMqtt(HttpdConnData *connData) { return HTTPD_CGI_DONE; } } +#endif // MQTT \ No newline at end of file diff --git a/esp-link/cgimqtt.h b/esp-link/cgimqtt.h index 54a6011..ef58f38 100644 --- a/esp-link/cgimqtt.h +++ b/esp-link/cgimqtt.h @@ -1,8 +1,9 @@ +#ifdef MQTT #ifndef CGIMQTT_H #define CGIMQTT_H #include "httpd.h" - int cgiMqtt(HttpdConnData *connData); -#endif +#endif // CGIMQTT_H +#endif // MQTT diff --git a/esp-link/main.c b/esp-link/main.c index ccb596f..66abef9 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -30,8 +30,6 @@ #include "log.h" #include -//#define SHOW_HEAP_USE - /* This is the main url->function dispatching data struct. In short, it's a struct with various URLs plus their handlers. The handlers can @@ -65,7 +63,9 @@ HttpdBuiltInUrl builtInUrls[] = { { "/wifi/special", cgiWiFiSpecial, NULL }, { "/pins", cgiPins, NULL }, { "/tcpclient", cgiTcp, NULL }, +#ifdef MQTT { "/mqtt", cgiMqtt, NULL }, +#endif { "*", cgiEspFsHook, NULL }, //Catch-all cgi function for the filesystem { NULL, NULL, NULL } @@ -83,17 +83,6 @@ static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) { # define VERS_STR(V) VERS_STR_STR(V) char* esp_link_version = VERS_STR(VERSION); -void user_rf_pre_init(void) { - system_set_os_print(DEBUG_SDK); -#if defined(STA_SSID) && defined(STA_PASS) - struct station_config stconf; - os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32); - os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64); - stconf.bssid_set = 0; - wifi_station_set_config_current(&stconf); -#endif -} - // address of espfs binary blob extern uint32_t _binary_espfs_img_start; @@ -108,6 +97,9 @@ static char *flash_maps[] = { extern void app_init(void); extern void mqtt_client_init(void); +void user_rf_pre_init(void) { +} + // Main routine to initialize esp-link. void user_init(void) { // get the flash config so we know how to init things @@ -123,11 +115,33 @@ void user_init(void) { os_delay_us(10000L); os_printf("\n\n** %s\n", esp_link_version); os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*"); + +#if defined(STA_SSID) && defined(STA_PASS) + int x = wifi_get_opmode() & 0x3; + + if (x == 2) { + 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 + os_printf("Wifi pre-config trying to connect to AP %s pw %s\n", (char*)stconf.ssid, (char*)stconf.password); +#endif + wifi_set_opmode(3); + stconf.bssid_set = 0; + wifi_station_set_config(&stconf); + } + } +#endif + // Status LEDs statusInit(); serledInit(); // Wifi wifiInit(); + // init the flash filesystem with the html stuff espFsInit(&_binary_espfs_img_start); //EspFsInitResult res = espFsInit(&_binary_espfs_img_start); @@ -153,8 +167,9 @@ void user_init(void) { fid & 0xff, (fid&0xff00)|((fid>>16)&0xff)); os_printf("** esp-link ready\n"); - +#ifdef MQTT mqtt_client_init(); +#endif app_init(); } diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index 654d0ec..cca4254 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -1,3 +1,4 @@ +#ifdef MQTT #include #include "cgiwifi.h" #include "config.h" @@ -140,3 +141,5 @@ void ICACHE_FLASH_ATTR mqtt_client_on_data(MqttDataCallback dataCb) { data_cb = dataCb; } + +#endif // MQTT diff --git a/esp-link/mqtt_client.h b/esp-link/mqtt_client.h index d0b42fe..75fc8dc 100644 --- a/esp-link/mqtt_client.h +++ b/esp-link/mqtt_client.h @@ -1,6 +1,6 @@ +#ifdef MQTT #ifndef MQTT_CLIENT_H #define MQTT_CLIENT_H - #include "mqtt.h" extern MQTT_Client mqttClient; @@ -11,4 +11,5 @@ void mqtt_client_on_disconnected(MqttCallback disconnectedCb); void mqtt_client_on_published(MqttCallback publishedCb); void mqtt_client_on_data(MqttDataCallback dataCb); -#endif +#endif //MQTT_CLIENT_H +#endif // MQTT diff --git a/esp-link/status.c b/esp-link/status.c index ae57d57..a1ca389 100644 --- a/esp-link/status.c +++ b/esp-link/status.c @@ -4,11 +4,46 @@ #include "config.h" #include "serled.h" #include "cgiwifi.h" + +#ifdef MQTT #include "mqtt.h" #include "mqtt_client.h" - extern MQTT_Client mqttClient; +//===== MQTT Status update + +// Every minute... +#define MQTT_STATUS_INTERVAL (60*1000) + +static ETSTimer mqttStatusTimer; + +int ICACHE_FLASH_ATTR +mqttStatusMsg(char *buf) { + sint8 rssi = wifi_station_get_rssi(); + if (rssi > 0) rssi = 0; // not connected or other error + //os_printf("timer rssi=%d\n", rssi); + + // compose MQTT message + return os_sprintf(buf, + "{\"rssi\":%d, \"heap_free\":%ld}", + rssi, (unsigned long)system_get_free_heap_size()); +} + +// Timer callback to send an RSSI update to a monitoring system +static void ICACHE_FLASH_ATTR mqttStatusCb(void *v) { + if (!flashConfig.mqtt_status_enable || os_strlen(flashConfig.mqtt_status_topic) == 0 || + mqttClient.connState != MQTT_CONNECTED) + return; + + char buf[128]; + mqttStatusMsg(buf); + MQTT_Publish(&mqttClient, statusTopicStr, buf, 1, 0); +} + + + +#endif // MQTT + //===== "CONN" LED status indication static ETSTimer ledTimer; @@ -69,36 +104,6 @@ void ICACHE_FLASH_ATTR statusWifiUpdate(uint8_t state) { os_timer_arm(&ledTimer, 500, 0); } -//===== MQTT Status update - -// Every minute... -#define MQTT_STATUS_INTERVAL (60*1000) - -static ETSTimer mqttStatusTimer; - -int ICACHE_FLASH_ATTR -mqttStatusMsg(char *buf) { - sint8 rssi = wifi_station_get_rssi(); - if (rssi > 0) rssi = 0; // not connected or other error - //os_printf("timer rssi=%d\n", rssi); - - // compose MQTT message - return os_sprintf(buf, - "{\"rssi\":%d, \"heap_free\":%ld}", - rssi, (unsigned long)system_get_free_heap_size()); -} - -// Timer callback to send an RSSI update to a monitoring system -static void ICACHE_FLASH_ATTR mqttStatusCb(void *v) { - if (!flashConfig.mqtt_status_enable || os_strlen(flashConfig.mqtt_status_topic) == 0 || - mqttClient.connState != MQTT_CONNECTED) - return; - - char buf[128]; - mqttStatusMsg(buf); - MQTT_Publish(&mqttClient, statusTopicStr, buf, 1, 0); -} - //===== Init status stuff void ICACHE_FLASH_ATTR statusInit(void) { @@ -114,9 +119,11 @@ void ICACHE_FLASH_ATTR statusInit(void) { os_timer_setfn(&ledTimer, ledTimerCb, NULL); os_timer_arm(&ledTimer, 2000, 0); +#ifdef MQTT os_timer_disarm(&mqttStatusTimer); os_timer_setfn(&mqttStatusTimer, mqttStatusCb, NULL); os_timer_arm(&mqttStatusTimer, MQTT_STATUS_INTERVAL, 1); // recurring timer +#endif // MQTT } diff --git a/html/mqtt.html b/html/mqtt.html index 1afbb95..28d763c 100644 --- a/html/mqtt.html +++ b/html/mqtt.html @@ -90,6 +90,7 @@ +