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
pull/49/head
Benjamin Runnels 9 years ago
parent 2b501913f2
commit 2ab194f83c
  1. 116
      Makefile
  2. 11
      esp-link.vcxproj
  3. 2
      esp-link/cgi.c
  4. 4
      esp-link/cgimqtt.c
  5. 5
      esp-link/cgimqtt.h
  6. 43
      esp-link/main.c
  7. 3
      esp-link/mqtt_client.c
  8. 5
      esp-link/mqtt_client.h
  9. 69
      esp-link/status.c
  10. 1
      html/mqtt.html
  11. 76
      html/mqtt.js
  12. 78
      html/ui.js
  13. 1
      include/user_config.h

@ -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}-; \

@ -85,10 +85,18 @@
<None Include="espmake.cmd" />
<None Include="FLASH.md" />
<None Include="html\console.html" />
<None Include="html\console.js" />
<None Include="html\head-" />
<None Include="html\home.html" />
<None Include="html\jl-400x110.png-" />
<None Include="html\log.html" />
<None Include="html\mqtt.html" />
<None Include="html\mqtt.js" />
<None Include="html\pure.css" />
<None Include="html\style.css" />
<None Include="html\ui.js" />
<None Include="html\wifi\wifi.html" />
<None Include="html\wifi\wifi.js" />
<None Include="Makefile" />
<None Include="README.md" />
<None Include="wiflash" />
@ -97,6 +105,9 @@
<Text Include="esp-link.log" />
<Text Include="LICENSE.txt" />
</ItemGroup>
<ItemGroup>
<Image Include="html\favicon.ico" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{A92F0CAA-F89B-4F78-AD2A-A042429BD87F}</ProjectGuid>
<Keyword>MakeFileProj</Keyword>

@ -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);

@ -1,6 +1,5 @@
// Copyright 2015 by Thorsten von Eicken, see LICENSE.txt
// // TCP Client settings
#ifdef MQTT
#include <esp8266.h>
#include "cgi.h"
#include "config.h"
@ -175,3 +174,4 @@ int ICACHE_FLASH_ATTR cgiMqtt(HttpdConnData *connData) {
return HTTPD_CGI_DONE;
}
}
#endif // MQTT

@ -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

@ -30,8 +30,6 @@
#include "log.h"
#include <gpio.h>
//#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();
}

@ -1,3 +1,4 @@
#ifdef MQTT
#include <esp8266.h>
#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

@ -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

@ -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
}

@ -90,6 +90,7 @@
</div>
</div>
<script src="mqtt.js"></script>
<script type="text/javascript">
onLoad(function() {
fetchMqtt();

@ -0,0 +1,76 @@
//===== MQTT cards
function changeMqtt(e) {
e.preventDefault();
var url = "mqtt?1=1";
var i, inputs = document.querySelectorAll('#mqtt-form input');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type != "checkbox")
url += "&" + inputs[i].name + "=" + inputs[i].value;
};
hideWarning();
var cb = $("#mqtt-button");
addClass(cb, 'pure-button-disabled');
ajaxSpin("POST", url, function (resp) {
showNotification("MQTT updated");
removeClass(cb, 'pure-button-disabled');
}, function (s, st) {
showWarning("Error: " + st);
removeClass(cb, 'pure-button-disabled');
window.setTimeout(fetchMqtt, 100);
});
}
function displayMqtt(data) {
Object.keys(data).forEach(function (v) {
el = $("#" + v);
if (el != null) {
if (el.nodeName === "INPUT") el.value = data[v];
else el.innerHTML = data[v];
return;
}
el = document.querySelector('input[name="' + v + '"]');
if (el != null) {
if (el.type == "checkbox") el.checked = data[v] > 0;
else el.value = data[v];
}
});
$("#mqtt-spinner").setAttribute("hidden", "");
$("#mqtt-status-spinner").setAttribute("hidden", "");
$("#mqtt-form").removeAttribute("hidden");
$("#mqtt-status-form").removeAttribute("hidden");
var i, inputs = $("input");
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox")
inputs[i].onclick = function () { console.log(this); setMqtt(this.name, this.checked) };
}
}
function fetchMqtt() {
ajaxJson("GET", "/mqtt", displayMqtt, function () {
window.setTimeout(fetchMqtt, 1000);
});
}
function changeMqttStatus(e) {
e.preventDefault();
var v = document.querySelector('input[name="mqtt-status-topic"]').value;
ajaxSpin("POST", "/mqtt?mqtt-status-topic=" + v, function () {
showNotification("MQTT status settings updated");
}, function (s, st) {
showWarning("Error: " + st);
window.setTimeout(fetchMqtt, 100);
});
}
function setMqtt(name, v) {
ajaxSpin("POST", "/mqtt?" + name + "=" + (v ? 1 : 0), function () {
var n = name.replace("-enable", "");
showNotification(n + " is now " + (v ? "enabled" : "disabled"));
}, function () {
showWarning("Enable/disable failed");
window.setTimeout(fetchMqtt, 100);
});
}

@ -355,81 +355,3 @@ function setPins(v, name) {
window.setTimeout(fetchPins, 100);
});
}
//===== MQTT cards
function changeMqtt(e) {
e.preventDefault();
var url = "mqtt?1=1";
var i, inputs = document.querySelectorAll('#mqtt-form input');
for (i=0; i<inputs.length; i++) {
if (inputs[i].type != "checkbox")
url += "&" + inputs[i].name + "=" + inputs[i].value;
};
hideWarning();
var cb = $("#mqtt-button");
addClass(cb, 'pure-button-disabled');
ajaxSpin("POST", url, function(resp) {
showNotification("MQTT updated");
removeClass(cb, 'pure-button-disabled');
}, function(s, st) {
showWarning("Error: "+st);
removeClass(cb, 'pure-button-disabled');
window.setTimeout(fetchMqtt, 100);
});
}
function displayMqtt(data) {
Object.keys(data).forEach(function(v) {
el = $("#" + v);
if (el != null) {
if (el.nodeName === "INPUT") el.value = data[v];
else el.innerHTML = data[v];
return;
}
el = document.querySelector('input[name="' + v + '"]');
if (el != null) {
if (el.type == "checkbox") el.checked = data[v] > 0;
else el.value = data[v];
}
});
$("#mqtt-spinner").setAttribute("hidden", "");
$("#mqtt-status-spinner").setAttribute("hidden", "");
$("#mqtt-form").removeAttribute("hidden");
$("#mqtt-status-form").removeAttribute("hidden");
var i, inputs = $("input");
for (i=0; i<inputs.length; i++) {
if (inputs[i].type == "checkbox")
inputs[i].onclick = function() { console.log(this); setMqtt(this.name, this.checked) };
}
}
function fetchMqtt() {
ajaxJson("GET", "/mqtt", displayMqtt, function() {
window.setTimeout(fetchMqtt, 1000);
});
}
function changeMqttStatus(e) {
e.preventDefault();
var v = document.querySelector('input[name="mqtt-status-topic"]').value;
ajaxSpin("POST", "/mqtt?mqtt-status-topic=" + v, function() {
showNotification("MQTT status settings updated");
}, function(s, st) {
showWarning("Error: "+st);
window.setTimeout(fetchMqtt, 100);
});
}
function setMqtt(name, v) {
ajaxSpin("POST", "/mqtt?" + name + "=" + (v ? 1 : 0), function() {
var n = name.replace("-enable", "");
showNotification(n + " is now " + (v ? "enabled" : "disabled"));
}, function() {
showWarning("Enable/disable failed");
window.setTimeout(fetchMqtt, 100);
});
}

@ -5,6 +5,7 @@
#include <_mingw.h>
#endif
#undef SHOW_HEAP_USE
#define DEBUGIP
#define CMD_DBG

Loading…
Cancel
Save