fix-up MQTT page

pull/49/head
Thorsten von Eicken 9 years ago
parent 6158d8f957
commit b2d7d2a579
  1. 30
      esp-link/cgimqtt.c
  2. 8
      esp-link/mqtt_client.c
  3. 2
      esp-link/status.c
  4. 1
      esp-link/status.h
  5. 24
      html/home.html
  6. 4
      html/mqtt.html
  7. 38
      html/ui.js

@ -4,18 +4,39 @@
#include <esp8266.h> #include <esp8266.h>
#include "cgi.h" #include "cgi.h"
#include "config.h" #include "config.h"
#include "status.h"
#include "mqtt_client.h"
#include "cgimqtt.h" #include "cgimqtt.h"
static char *mqtt_states[] = {
"disconnected", "reconnecting", "connecting", "connected",
};
// Cgi to return MQTT settings // Cgi to return MQTT settings
int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) {
char buff[2048]; char buff[1024];
int len; int len;
if (connData->conn==NULL) return HTTPD_CGI_DONE; if (connData->conn==NULL) return HTTPD_CGI_DONE;
// get the current status topic for display
char status_buf1[128], *sb1=status_buf1;
char status_buf2[128], *sb2=status_buf2;
mqttStatusMsg(status_buf1);
// quote all " for the json, sigh...
for (int i=0; i<127 && *sb1; i++) {
if (*sb1 == '"') {
*sb2++ = '\\';
i++;
}
*sb2++ = *sb1++;
}
*sb2 = 0;
len = os_sprintf(buff, "{ " len = os_sprintf(buff, "{ "
"\"slip-enable\":%d, " "\"slip-enable\":%d, "
"\"mqtt-enable\":%d, " "\"mqtt-enable\":%d, "
"\"mqtt-state\":\"%s\", "
"\"mqtt-status-enable\":%d, " "\"mqtt-status-enable\":%d, "
"\"mqtt-port\":%d, " "\"mqtt-port\":%d, "
"\"mqtt-host\":\"%s\", " "\"mqtt-host\":\"%s\", "
@ -23,11 +44,12 @@ int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) {
"\"mqtt-username\":\"%s\", " "\"mqtt-username\":\"%s\", "
"\"mqtt-password\":\"%s\", " "\"mqtt-password\":\"%s\", "
"\"mqtt-status-topic\":\"%s\", " "\"mqtt-status-topic\":\"%s\", "
"\"mqtt-state\":\"%s\" }", "\"mqtt-status-value\":\"%s\" }",
flashConfig.slip_enable, flashConfig.mqtt_enable, flashConfig.mqtt_status_enable, flashConfig.slip_enable, flashConfig.mqtt_enable,
mqtt_states[mqttClient.connState], flashConfig.mqtt_status_enable,
flashConfig.mqtt_port, flashConfig.mqtt_hostname, flashConfig.mqtt_client, flashConfig.mqtt_port, flashConfig.mqtt_hostname, flashConfig.mqtt_client,
flashConfig.mqtt_username, flashConfig.mqtt_password, flashConfig.mqtt_username, flashConfig.mqtt_password,
flashConfig.mqtt_status_topic, "connected"); flashConfig.mqtt_status_topic, status_buf2);
jsonHeader(connData, 200); jsonHeader(connData, 200);
httpdSend(connData, buff, len); httpdSend(connData, buff, len);

@ -8,7 +8,9 @@ MQTT_Client mqttClient;
static ETSTimer mqttTimer; static ETSTimer mqttTimer;
static int once = 0; static int once = 0;
static void ICACHE_FLASH_ATTR mqttTimerCb(void *arg) { static void ICACHE_FLASH_ATTR
mqttTimerCb(void *arg)
{
if (once++ > 0) return; if (once++ > 0) return;
MQTT_Init(&mqttClient, flashConfig.mqtt_hostname, flashConfig.mqtt_port, 0, 2, MQTT_Init(&mqttClient, flashConfig.mqtt_hostname, flashConfig.mqtt_port, 0, 2,
flashConfig.mqtt_client, flashConfig.mqtt_username, flashConfig.mqtt_password, 60); flashConfig.mqtt_client, flashConfig.mqtt_username, flashConfig.mqtt_password, 60);
@ -28,7 +30,9 @@ wifiStateChangeCb(uint8_t status)
// initialize the custom stuff that goes beyond esp-link // initialize the custom stuff that goes beyond esp-link
void mqtt_client_init() { void ICACHE_FLASH_ATTR
mqtt_client_init()
{
wifiAddStateChangeCb(wifiStateChangeCb); wifiAddStateChangeCb(wifiStateChangeCb);
} }

@ -75,7 +75,7 @@ void ICACHE_FLASH_ATTR statusWifiUpdate(uint8_t state) {
static ETSTimer mqttStatusTimer; static ETSTimer mqttStatusTimer;
static int ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR
mqttStatusMsg(char *buf) { mqttStatusMsg(char *buf) {
sint8 rssi = wifi_station_get_rssi(); sint8 rssi = wifi_station_get_rssi();
if (rssi > 0) rssi = 0; // not connected or other error if (rssi > 0) rssi = 0; // not connected or other error

@ -1,6 +1,7 @@
#ifndef STATUS_H #ifndef STATUS_H
#define STATUS_H #define STATUS_H
int mqttStatusMsg(char *buf);
void statusWifiUpdate(uint8_t state); void statusWifiUpdate(uint8_t state);
void statusInit(void); void statusInit(void);

@ -39,28 +39,6 @@
<tr><td>Configured hostname</td><td id="wifi-hostname"></td></tr> <tr><td>Configured hostname</td><td id="wifi-hostname"></td></tr>
</tbody> </table> </tbody> </table>
</div> </div>
<div class="card">
<h1>TCP client</h1>
<form action="#" id="tcpform" class="pure-form">
<legend>TCP client support in esp-link</legend>
<div class="form-horizontal">
<input type="checkbox" name="tcp_enable"/>
<label>Enable serial port TCP client</label>
</div>
<br>
<legend>Grovestreams data push</legend>
<div class="form-horizontal">
<input type="checkbox" name="rssi_enable"/>
<label>Send RSSI</label>
</div>
<div class="pure-form-stacked">
<label>API key/passwd</label>
<input type="password" name="api_key"/>
</div>
<button id="tcp-button" type="submit"
class="pure-button button-primary">Change!</button>
</form>
</div>
</div> </div>
<div class="pure-u-1 pure-u-md-1-2"><div class="card"> <div class="pure-u-1 pure-u-md-1-2"><div class="card">
<h1>Pin assignment</h1> <h1>Pin assignment</h1>
@ -80,8 +58,6 @@
onLoad(function() { onLoad(function() {
fetchPins(); fetchPins();
getWifiInfo(); getWifiInfo();
fetchTcpClient();
bnd($("#tcpform"), "submit", changeTcpClient);
}); });
</script> </script>
</body></html> </body></html>

@ -69,9 +69,9 @@
<br> <br>
<legend>Status reporting settings</legend> <legend>Status reporting settings</legend>
<div class="pure-form-stacked"> <div class="pure-form-stacked">
<label>Topic prefix</label> <label>Topic</label>
<input type="text" name="mqtt-status-topic"/> <input type="text" name="mqtt-status-topic"/>
Suffixes: rssi, heap-free, ... Message: <tt id="mqtt-status-value"></tt>
</div> </div>
<button id="mqtt-status-button" type="submit" class="pure-button button-primary"> <button id="mqtt-status-button" type="submit" class="pure-button button-primary">
Update status settings! Update status settings!

@ -356,44 +356,6 @@ function setPins(v, name) {
}); });
} }
//===== TCP client card
function tcpEn(){return document.querySelector('input[name="tcp_enable"]')}
function rssiEn(){return document.querySelector('input[name="rssi_enable"]')}
function apiKey(){return document.querySelector('input[name="api_key"]')}
function changeTcpClient(e) {
e.preventDefault();
var url = "tcpclient";
url += "?tcp_enable=" + tcpEn().checked;
url += "&rssi_enable=" + rssiEn().checked;
url += "&api_key=" + encodeURIComponent(apiKey().value);
hideWarning();
var cb = $("#tcp-button");
addClass(cb, 'pure-button-disabled');
ajaxSpin("POST", url, function(resp) {
removeClass(cb, 'pure-button-disabled');
fetchTcpClient();
}, function(s, st) {
showWarning("Error: "+st);
removeClass(cb, 'pure-button-disabled');
fetchTcpClient();
});
}
function displayTcpClient(resp) {
tcpEn().checked = resp.tcp_enable > 0;
rssiEn().checked = resp.rssi_enable > 0;
apiKey().value = resp.api_key;
}
function fetchTcpClient() {
ajaxJson("GET", "/tcpclient", displayTcpClient, function() {
window.setTimeout(fetchTcpClient, 1000);
});
}
//===== MQTT cards //===== MQTT cards
function changeMqtt(e) { function changeMqtt(e) {

Loading…
Cancel
Save