Skip to content

Commit

Permalink
TCP client support; Grovestreams RSSI submission support
Browse files Browse the repository at this point in the history
  • Loading branch information
tve committed Aug 1, 2015
1 parent 886ce62 commit 2fbd995
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 247 deletions.
66 changes: 36 additions & 30 deletions html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,40 @@ <h2 id="version"></h2>
</div></div>
</div>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-2"><div class="card">
<h1>Wifi summary</h1>
<div id="wifi-spinner" class="spinner spinner-small"></div>
<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>Configured network</td><td id="wifi-ssid"></td></tr>
<tr><td>Wifi status</td><td id="wifi-status"></td></tr>
<tr><td>Wifi address</td><td id="wifi-ip"></td></tr>
</tbody> </table>
</div></div>
<div class="pure-u-1 pure-u-md-1-2">
<div class="card">
<h1>Wifi summary</h1>
<div id="wifi-spinner" class="spinner spinner-small"></div>
<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>Configured network</td><td id="wifi-ssid"></td></tr>
<tr><td>Wifi status</td><td id="wifi-status"></td></tr>
<tr><td>Wifi address</td><td id="wifi-ip"></td></tr>
</tbody> </table>
</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 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>
Expand All @@ -45,26 +69,6 @@ <h1>Pin assignment</h1>
</div></div>
</div>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-2"><div class="card">
<h1>HTTP service</h1>
<form action="#" id="httpform" class="pure-form">
<legend>External web service configuration</legend>
<div class="form-horizontal">
<label>Enable serial port HTTP</label>
<input type="checkbox" name="http_enable"/>
<label>Send RSSI</label>
<input type="checkbox" name="send_rssi"/>
</div>
<div class="pure-form-stacked">
<label>Server hostname</label>
<input type="text" name="hostname"/>
<label>API key/passwd</label>
<input type="password" name="api_key"/>
</div>
<button id="special-button" type="submit"
class="pure-button button-primary">Change!</button>
</form>
</div></div>
</div>
</div>
</div>
Expand All @@ -74,6 +78,8 @@ <h1>HTTP service</h1>
onLoad(function() {
fetchPins();
getWifiInfo();
fetchTcpClient();
bnd($("#tcpform"), "submit", changeTcpClient);
});
</script>
</body></html>
39 changes: 39 additions & 0 deletions html/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,42 @@ 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');
getWifiInfo();
}, function(s, st) {
showWarning("Error: "+st);
removeClass(cb, 'pure-button-disabled');
getWifiInfo();
});
}

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


6 changes: 3 additions & 3 deletions httpd/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,13 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) {
conn->cgi=NULL; //mark for destruction.
}
if (r==HTTPD_CGI_NOTFOUND || r==HTTPD_CGI_AUTHENTICATED) {
os_printf("%s ERROR! CGI fn returns code %d after sending data! Bad CGI!\n", connStr, r);
os_printf("%s ERROR! Bad CGI code %d\n", connStr, r);
conn->cgi=NULL; //mark for destruction.
}
xmitSendBuff(conn);
}

static const char *httpNotFoundHeader="HTTP/1.0 404 Not Found\r\nServer: esp8266-httpd/"HTTPDVER"\r\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\nNot Found.\r\n";
static const char *httpNotFoundHeader="HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\nNot Found.\r\n";

//This is called when the headers have been received and the connection is ready to send
//the result headers and data.
Expand Down Expand Up @@ -419,7 +419,7 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
if (conn->getArgs!=0) {
*conn->getArgs=0;
conn->getArgs++;
os_printf("%s GET args = %s\n", connStr, conn->getArgs);
os_printf("%s args = %s\n", connStr, conn->getArgs);
} else {
conn->getArgs=NULL;
}
Expand Down
66 changes: 57 additions & 9 deletions serial/serbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,13 @@ enum {
TC_idle, // in-between commands
TC_newline, // newline seen
TC_start, // start character (~) seen
TC_cmd, // command character (0) seen
TC_cmd, // command start (@) seen
TC_cmdChar, // command character seen
TC_cmdLine, // accumulating command
TC_tdata, // data character (1-9) seen, and in text data mode
TC_tdchan, // saw data channel character
TC_tdlen1, // saw first data length character
TC_tdata0, // accumulate data, zero-terminated
TC_tdataN, // accumulate data, length-terminated
};
static uint8_t tcState = TC_newline;
static uint8_t tcChan; // channel for current command (index into tcConn)
Expand All @@ -274,6 +278,7 @@ static uint8_t tcChan; // channel for current command (index into tcConn)
static char tcCmdBuf[CMD_MAX];
static short tcCmdBufLen = 0;
static char tcCmdChar;
static short tcLen;

// scan a buffer for tcp client commands
static int ICACHE_FLASH_ATTR
Expand All @@ -291,17 +296,27 @@ tcpClientProcess(char *buf, int len)
if (c == '~') tcState = TC_start;
continue; // gobble up the ~
case TC_start: // saw ~, expect channel number
if (c == '0') {
if (c == '@') {
tcState = TC_cmd;
continue;
} else if (c >= '1' && c <= '9') {
tcChan = c-'1';
tcState = TC_tdata;
} else if (c >= '0' && c <= '9') {
tcChan = c-'0';
tcState = TC_tdchan;
continue;
}
*out++ = '~'; // make up for '~' we skipped
break;
case TC_cmd: // saw channel number 0 (command), expect command char
case TC_cmd: // saw control char (@), expect channel char
if (c >= '0' && c <= '9') {
tcChan = c-'0';
tcState = TC_cmdChar;
continue;
} else {
*out++ = '~'; // make up for '~' we skipped
*out++ = '@'; // make up for '@' we skipped
break;
}
case TC_cmdChar: // saw channel number, expect command char
tcCmdChar = c; // save command character
tcCmdBufLen = 0; // empty the command buffer
tcState = TC_cmdLine;
Expand All @@ -310,18 +325,51 @@ tcpClientProcess(char *buf, int len)
if (c != '\n') {
if (tcCmdBufLen < CMD_MAX) tcCmdBuf[tcCmdBufLen++] = c;
} else {
tcpClientCommand(tcCmdChar, tcCmdBuf);
tcpClientCommand(tcChan, tcCmdChar, tcCmdBuf);
tcState = TC_newline;
}
continue;
case TC_tdata: // saw channel number, getting data characters
case TC_tdchan: // saw channel number, getting first length char
if (c >= '0' && c <= '9') {
tcLen = c-'0';
} else if (c >= 'A' && c <= 'F') {
tcLen = c-'A'+10;
} else {
*out++ = '~'; // make up for '~' we skipped
*out++ = '0'+tcChan;
break;
}
tcState = TC_tdlen1;
continue;
case TC_tdlen1: // saw first length char, get second
tcLen *= 16;
if (c >= '0' && c <= '9') {
tcLen += c-'0';
} else if (c >= 'A' && c <= 'F') {
tcLen += c-'A'+10;
} else {
*out++ = '~'; // make up for '~' we skipped
*out++ = '0'+tcChan;
break;
}
tcState = tcLen == 0 ? TC_tdata0 : TC_tdataN;
continue;
case TC_tdata0: // saw data length, getting data characters zero-terminated
if (c != 0) {
tcpClientSendChar(tcChan, c);
} else {
tcpClientSendPush(tcChan);
tcState = TC_idle;
}
continue;
case TC_tdataN: // saw data length, getting data characters length-terminated
tcpClientSendChar(tcChan, c);
tcLen--;
if (tcLen == 0) {
tcpClientSendPush(tcChan);
tcState = TC_idle;
}
continue;
}
*out++ = c;
}
Expand Down
Loading

0 comments on commit 2fbd995

Please sign in to comment.