From 2ab194f83c612b42bd225adfd4b1d14385d03504 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 20 Sep 2015 16:18:26 -0500 Subject: [PATCH] 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 @@ +