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. 118
      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);

@ -1,87 +1,63 @@
<div id="main"> <div id="main">
<div class="header"> <div class="header">
<div><img src="favicon.ico" height="64"><span class="jl">JEELABS</span></div> <div><img src="favicon.ico" height="64"><span class="jl">JEELABS</span></div>
<h1 style="margin-top:0"><span class="esp">esp</span>-link</h1> <h1 style="margin-top:0"><span class="esp">esp</span>-link</h1>
<h2 id="version"></h2> <h2 id="version"></h2>
</div> </div>
<div class="content"> <div class="content">
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1"><div class="card"> <div class="pure-u-1"><div class="card">
<p>The JeeLabs esp-link firmware bridges the ESP8266 serial port to Wifi and can <p>The JeeLabs esp-link firmware bridges the ESP8266 serial port to Wifi and can
program microcontrollers over the serial port, in particular Arduinos, AVRs, and program microcontrollers over the serial port, in particular Arduinos, AVRs, and
NXP's LPC800 and other ARM processors.</p> NXP's LPC800 and other ARM processors.</p>
<p style="margin-bottom:0;">Program an Arduino/AVR using avrdude using a command <p style="margin-bottom:0;">Program an Arduino/AVR using avrdude using a command
line similar to:</p> line similar to:</p>
<div class="tt">/home/arduino-1.0.5/hardware/tools/avrdude \<br> <div class="tt">/home/arduino-1.0.5/hardware/tools/avrdude \<br>
&nbsp;&nbsp;-DV -patmega328p -Pnet:esp-link.local:23 -carduino -b115200 -U \<br> &nbsp;&nbsp;-DV -patmega328p -Pnet:esp-link.local:23 -carduino -b115200 -U \<br>
&nbsp;&nbsp;-C /home/arduino-1.0.5/hardware/tools/avrdude.conf flash:w:my_sketch.hex:i &nbsp;&nbsp;-C /home/arduino-1.0.5/hardware/tools/avrdude.conf flash:w:my_sketch.hex:i
</div> </div>
<p>where <tt>-Pnet:esp-link.local:23</tt> tells avrdude to connect to port 23 of esp-link. <p>where <tt>-Pnet:esp-link.local:23</tt> tells avrdude to connect to port 23 of esp-link.
You can substitute the IP address of your esp-link for esp-link.local if necessary.</p> You can substitute the IP address of your esp-link for esp-link.local if necessary.</p>
<p>Please refer to <p>Please refer to
<a href="https://github.com/jeelabs/esp-link/blob/master/README.md">the online README</a> <a href="https://github.com/jeelabs/esp-link/blob/master/README.md">the online README</a>
for up-to-date help and to the forthcoming for up-to-date help and to the forthcoming
<a href="http://jeelabs.org">JeeLabs blog</a> for an intro to the codebase.</p> <a href="http://jeelabs.org">JeeLabs blog</a> for an intro to the codebase.</p>
</div></div> </div></div>
</div> </div>
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1 pure-u-md-1-2"> <div class="pure-u-1 pure-u-md-1-2">
<div class="card"> <div class="card">
<h1>Wifi summary</h1> <h1>Wifi summary</h1>
<div id="wifi-spinner" class="spinner spinner-small"></div> <div id="wifi-spinner" class="spinner spinner-small"></div>
<table id="wifi-table" class="pure-table pure-table-horizontal" hidden><tbody> <table id="wifi-table" class="pure-table pure-table-horizontal" hidden><tbody>
<tr><td>WiFi mode</td><td id="wifi-mode"></td></tr> <tr><td>WiFi mode</td><td id="wifi-mode"></td></tr>
<tr><td>Configured network</td><td id="wifi-ssid"></td></tr> <tr><td>Configured network</td><td id="wifi-ssid"></td></tr>
<tr><td>Wifi channel</td><td id="wifi-chan"></td></tr> <tr><td>Wifi channel</td><td id="wifi-chan"></td></tr>
<tr><td>Wifi status</td><td id="wifi-status"></td></tr> <tr><td>Wifi status</td><td id="wifi-status"></td></tr>
<tr><td>Wifi address</td><td id="wifi-ip"></td></tr> <tr><td>Wifi address</td><td id="wifi-ip"></td></tr>
<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"> </div>
<h1>TCP client</h1> <div class="pure-u-1 pure-u-md-1-2"><div class="card">
<form action="#" id="tcpform" class="pure-form"> <h1>Pin assignment</h1>
<legend>TCP client support in esp-link</legend> <legend>Select one of the following signal/pin assignments to match your hardware</legend>
<div class="form-horizontal"> <fieldset class='radios' id='pin-mux'>
<input type="checkbox" name="tcp_enable"/> <div class="spinner spinner-small"></div>
<label>Enable serial port TCP client</label> </fieldset>
</div> </div></div>
<br> </div>
<legend>Grovestreams data push</legend> <div class="pure-g">
<div class="form-horizontal"> </div>
<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 class="pure-u-1 pure-u-md-1-2"><div class="card">
<h1>Pin assignment</h1>
<legend>Select one of the following signal/pin assignments to match your hardware</legend>
<fieldset class='radios' id='pin-mux'>
<div class="spinner spinner-small"></div>
</fieldset>
</div></div>
</div>
<div class="pure-g">
</div>
</div> </div>
</div> </div>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
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