mirror of https://github.com/jeelabs/esp-link.git
parent
9e4cbb0dbd
commit
d698b94ac6
@ -0,0 +1,89 @@ |
||||
// Copyright 2015 by Thorsten von Eicken, see LICENSE.txt
|
||||
// // TCP Client settings
|
||||
|
||||
#include <esp8266.h> |
||||
#include "cgi.h" |
||||
#include "config.h" |
||||
#include "cgimqtt.h" |
||||
|
||||
// Cgi to return MQTT settings
|
||||
int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) { |
||||
char buff[2048]; |
||||
int len; |
||||
|
||||
if (connData->conn==NULL) return HTTPD_CGI_DONE; |
||||
|
||||
len = os_sprintf(buff, "{ " |
||||
"\"slip-enable\":%d, " |
||||
"\"mqtt-enable\":%d, " |
||||
"\"mqtt-status-enable\":%d, " |
||||
"\"mqtt-port\":%d, " |
||||
"\"mqtt-host\":\"%s\", " |
||||
"\"mqtt-client-id\":\"%s\", " |
||||
"\"mqtt-username\":\"%s\", " |
||||
"\"mqtt-password\":\"%s\", " |
||||
"\"mqtt-status-topic\":\"%s\", " |
||||
"\"mqtt-state\":\"%s\" }", |
||||
flashConfig.slip_enable, flashConfig.mqtt_enable, flashConfig.mqtt_status_enable, |
||||
flashConfig.mqtt_port, flashConfig.mqtt_hostname, flashConfig.mqtt_client, |
||||
flashConfig.mqtt_username, flashConfig.mqtt_password, |
||||
flashConfig.mqtt_status_topic, "connected"); |
||||
|
||||
jsonHeader(connData, 200); |
||||
httpdSend(connData, buff, len); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
|
||||
// Cgi to change choice of pin assignments
|
||||
int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) { |
||||
if (connData->conn==NULL) return HTTPD_CGI_DONE; |
||||
|
||||
#if 0 |
||||
// Handle tcp_enable flag
|
||||
char buff[128]; |
||||
int len = httpdFindArg(connData->getArgs, "tcp_enable", buff, sizeof(buff)); |
||||
if (len <= 0) { |
||||
jsonHeader(connData, 400); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
flashConfig.tcp_enable = os_strcmp(buff, "true") == 0; |
||||
|
||||
// Handle rssi_enable flag
|
||||
len = httpdFindArg(connData->getArgs, "rssi_enable", buff, sizeof(buff)); |
||||
if (len <= 0) { |
||||
jsonHeader(connData, 400); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
flashConfig.rssi_enable = os_strcmp(buff, "true") == 0; |
||||
|
||||
// Handle api_key flag
|
||||
len = httpdFindArg(connData->getArgs, "api_key", buff, sizeof(buff)); |
||||
if (len < 0) { |
||||
jsonHeader(connData, 400); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
buff[sizeof(flashConfig.api_key)-1] = 0; // ensure we don't get an overrun
|
||||
os_strcpy(flashConfig.api_key, buff); |
||||
#endif |
||||
|
||||
if (configSave()) { |
||||
httpdStartResponse(connData, 200); |
||||
httpdEndHeaders(connData); |
||||
} else { |
||||
httpdStartResponse(connData, 500); |
||||
httpdEndHeaders(connData); |
||||
httpdSend(connData, "Failed to save config", -1); |
||||
} |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
|
||||
int ICACHE_FLASH_ATTR cgiMqtt(HttpdConnData *connData) { |
||||
if (connData->requestType == HTTPD_METHOD_GET) { |
||||
return cgiMqttGet(connData); |
||||
} else if (connData->requestType == HTTPD_METHOD_POST) { |
||||
return cgiMqttSet(connData); |
||||
} else { |
||||
jsonHeader(connData, 404); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
} |
@ -0,0 +1,8 @@ |
||||
#ifndef CGIMQTT_H |
||||
#define CGIMQTT_H |
||||
|
||||
#include "httpd.h" |
||||
|
||||
int cgiMqtt(HttpdConnData *connData); |
||||
|
||||
#endif |
@ -0,0 +1,99 @@ |
||||
<div id="main"> |
||||
<div class="header"> |
||||
<h1>REST & MQTT</h1> |
||||
</div> |
||||
|
||||
<div class="content"> |
||||
<div class="pure-g"> |
||||
<div class="pure-u-1"><div class="card"> |
||||
<p>The REST & MQTT support uses the SLIP protocol over the serial port to enable |
||||
the attached microcontroller to initiate outbound connections. |
||||
The REST support lets the uC initiate simple HTTP requests while the MQTT support |
||||
lets it communicate with an MQTT server bidirectionally at QoS 0 thru 2.</p> |
||||
<p>The MQTT support is in the form of a built-in client that connects to a server |
||||
using parameters set below and stored in esp-link's flash settings. This allows |
||||
esp-link to take care of connection parameters and disconnect/reconnect operations.</p> |
||||
<p>The MQTT client also supports sending periodic status messages about esp-link itself, |
||||
including Wifi RSSI, and free heap memory.</p> |
||||
<div class="form-horizontal"> |
||||
<input type="checkbox" name="slip-enable"/> |
||||
<label>Enable SLIP on serial port</label> |
||||
</div> |
||||
</div></div> |
||||
</div> |
||||
<div class="pure-g"> |
||||
<div class="pure-u-1 pure-u-md-1-2"> |
||||
<div class="card"> |
||||
<h1>MQTT |
||||
<div id="mqtt-spinner" class="spinner spinner-small"></div> |
||||
</h1> |
||||
<form action="#" id="mqtt-form" class="pure-form" hidden> |
||||
<div class="form-horizontal"> |
||||
<input type="checkbox" name="mqtt-enable"/> |
||||
<label>Enable MQTT client</label> |
||||
</div> |
||||
<div class="form-horizontal"> |
||||
<label>MQTT client state: </label> |
||||
<b id="mqtt-state"></b> |
||||
</div> |
||||
<br> |
||||
<legend>MQTT server settings</legend> |
||||
<div class="pure-form-stacked"> |
||||
<label>Server hostname/ip</label> |
||||
<input type="text" name="mqtt-host"/> |
||||
<label>Server port/ip</label> |
||||
<input type="text" name="mqtt-port"/> |
||||
<label>Client ID</label> |
||||
<input type="text" name="mqtt-client-id"/> |
||||
<label>Username</label> |
||||
<input type="text" name="mqtt-username"/> |
||||
<label>Password</label> |
||||
<input type="password" name="mqtt-password"/> |
||||
</div> |
||||
<button id="mqtt-button" type="submit" class="pure-button button-primary"> |
||||
Update server settings! |
||||
</button> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
<div class="pure-u-1 pure-u-md-1-2"> |
||||
<div class="card"> |
||||
<h1>Status reporting |
||||
<div id="mqtt-status-spinner" class="spinner spinner-small"></div> |
||||
</h1> |
||||
<form action="#" id="mqtt-status-form" class="pure-form" hidden> |
||||
<div class="form-horizontal"> |
||||
<input type="checkbox" name="mqtt-status-enable"/> |
||||
<label>Enable status reporting via MQTT</label> |
||||
</div> |
||||
<br> |
||||
<legend>Status reporting settings</legend> |
||||
<div class="pure-form-stacked"> |
||||
<label>Topic prefix</label> |
||||
<input type="text" name="mqtt-status-topic"/> |
||||
</div> |
||||
</form> |
||||
</div> |
||||
<div class="card"> |
||||
<h1>REST</h1> |
||||
<p>REST requests are enabled as soon as SLIP is enabled. |
||||
There are no REST-specific settings.</p> |
||||
<!-- div id="rest-spinner" class="spinner spinner-small"></div> |
||||
<div class="form-horizontal"> |
||||
<input type="checkbox" name="tcp-enable"/> |
||||
<label>Enable REST client</label> |
||||
</div --> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
|
||||
<script type="text/javascript"> |
||||
onLoad(function() { |
||||
fetchMqtt(); |
||||
bnd($("#mqtt-form"), "submit", changeMqtt); |
||||
}); |
||||
</script> |
||||
</body></html> |
Loading…
Reference in new issue