Browse Source

Add option to merge messages into a JSON payload to web interface

fastled
Xose Pérez 7 years ago
parent
commit
eb4855d5e6
4 changed files with 3035 additions and 3006 deletions
  1. +1
    -1
      code/espurna/config/general.h
  2. +3006
    -2999
      code/espurna/static/index.html.gz.h
  3. +11
    -1
      code/espurna/web.ino
  4. +17
    -5
      code/html/index.html

+ 1
- 1
code/espurna/config/general.h View File

@ -203,7 +203,7 @@ PROGMEM const char* const custom_reset_string[] = {
#define MQTT_SKIP_RETAINED 1 #define MQTT_SKIP_RETAINED 1
#define MQTT_SKIP_TIME 1000 #define MQTT_SKIP_TIME 1000
#define MQTT_USE_JSON 1
#define MQTT_USE_JSON 0
#define MQTT_TOPIC_JSON "data" #define MQTT_TOPIC_JSON "data"
#define MQTT_TOPIC_ACTION "action" #define MQTT_TOPIC_ACTION "action"


+ 3006
- 2999
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 11
- 1
code/espurna/web.ino View File

@ -163,6 +163,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
bool changedNTP = false; bool changedNTP = false;
bool apiEnabled = false; bool apiEnabled = false;
bool dstEnabled = false; bool dstEnabled = false;
bool mqttUseJson = false;
#if ENABLE_FAUXMO #if ENABLE_FAUXMO
bool fauxmoEnabled = false; bool fauxmoEnabled = false;
#endif #endif
@ -256,6 +257,10 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
dstEnabled = true; dstEnabled = true;
continue; continue;
} }
if (key == "mqttUseJson") {
mqttUseJson = true;
continue;
}
#if ENABLE_FAUXMO #if ENABLE_FAUXMO
if (key == "fauxmoEnabled") { if (key == "fauxmoEnabled") {
fauxmoEnabled = true; fauxmoEnabled = true;
@ -304,6 +309,10 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
setSetting("ntpDST", dstEnabled); setSetting("ntpDST", dstEnabled);
save = changed = changedNTP = true; save = changed = changedNTP = true;
} }
if (mqttUseJson != (getSetting("mqttUseJson").toInt() == 1)) {
setSetting("mqttUseJson", mqttUseJson);
save = changed = true;
}
#if ENABLE_FAUXMO #if ENABLE_FAUXMO
if (fauxmoEnabled != (getSetting("fauxmoEnabled").toInt() == 1)) { if (fauxmoEnabled != (getSetting("fauxmoEnabled").toInt() == 1)) {
setSetting("fauxmoEnabled", fauxmoEnabled); setSetting("fauxmoEnabled", fauxmoEnabled);
@ -432,6 +441,7 @@ void _wsStart(uint32_t client_id) {
root["mqttUser"] = getSetting("mqttUser"); root["mqttUser"] = getSetting("mqttUser");
root["mqttPassword"] = getSetting("mqttPassword"); root["mqttPassword"] = getSetting("mqttPassword");
root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC);
root["mqttUseJson"] = getSetting("mqttUseJson", MQTT_USE_JSON).toInt() == 1;
JsonArray& relay = root.createNestedArray("relayStatus"); JsonArray& relay = root.createNestedArray("relayStatus");
for (unsigned char relayID=0; relayID<relayCount(); relayID++) { for (unsigned char relayID=0; relayID<relayCount(); relayID++) {
@ -780,7 +790,7 @@ void _onRPC(AsyncWebServerRequest *request) {
if (action.equals("reset")) { if (action.equals("reset")) {
response = 200; response = 200;
deferred.once_ms(100, []() {
deferred.once_ms(100, []() {
customReset(CUSTOM_RESET_RPC); customReset(CUSTOM_RESET_RPC);
ESP.restart(); ESP.restart();
}); });


+ 17
- 5
code/html/index.html View File

@ -464,15 +464,27 @@
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div> <div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint"> <div class="pure-u-1 pure-u-md-3-4 hint">
This is the root topic for this device. A trailing slash will be added if not preset. The {identifier} placeholder will be replaces by the device hostname.<br /> This is the root topic for this device. A trailing slash will be added if not preset. The {identifier} placeholder will be replaces by the device hostname.<br />
- <strong>&lt;root/&gt;relay/#</strong> Send a 0 or a 1 as a payload to this topic to switch it on or off. You can also send a 2 to toggle its current state. Replace # with the switch ID (starting from 0). If the board has only one switch it will be 0.<br />
<span class="module module-color">- <strong>&lt;root&gt;color</strong> The device will report the current color in #RRGGBB format to this topic. You can also set the color using this same topic.<br /></span>
- <strong>&lt;root/&gt;led/#</strong> Send a 0 or a 1 as a payload to this topic to set the onboard LED to the given state, send a 3 to turn it back to WIFI indicator. Replace # with the LED ID (starting from 0). If the board has only one LED it will be 0.<br />
- <strong>&lt;root/&gt;button/#</strong> For each button in the board subscribe to this topic to know when it is pressed (payload 1) or released (payload 0).<br />
- <strong>&lt;root/&gt;status</strong> The device will report a 1 to this topic every few minutes. Upon MQTT disconnecting this will be set to 0.<br />
- <strong>&lt;root&gt;/relay/#</strong> Send a 0 or a 1 as a payload to this topic to switch it on or off. You can also send a 2 to toggle its current state. Replace # with the switch ID (starting from 0). If the board has only one switch it will be 0.<br />
<span class="module module-color">- <strong>&lt;root&gt;/color</strong> The device will report the current color in #RRGGBB format to this topic. You can also set the color using this same topic.<br /></span>
- <strong>&lt;root&gt;/led/#</strong> Send a 0 or a 1 as a payload to this topic to set the onboard LED to the given state, send a 3 to turn it back to WIFI indicator. Replace # with the LED ID (starting from 0). If the board has only one LED it will be 0.<br />
- <strong>&lt;root&gt;/button/#</strong> For each button in the board subscribe to this topic to know when it is pressed (payload 1) or released (payload 0).<br />
- <strong>&lt;root&gt;/status</strong> The device will report a 1 to this topic every few minutes. Upon MQTT disconnecting this will be set to 0.<br />
- Other values reported (depending on the build) are: <strong>firmware</strong> and <strong>version</strong>, <strong>hostname</strong>, <strong>IP</strong>, <strong>MAC</strong>, signal strenth (<strong>RSSI</strong>), <strong>uptime</strong> (in seconds), <strong>free heap</strong> and <strong>power supply</strong>. - Other values reported (depending on the build) are: <strong>firmware</strong> and <strong>version</strong>, <strong>hostname</strong>, <strong>IP</strong>, <strong>MAC</strong>, signal strenth (<strong>RSSI</strong>), <strong>uptime</strong> (in seconds), <strong>free heap</strong> and <strong>power supply</strong>.
</div> </div>
</div> </div>
<div class="pure-g module module-fauxmo">
<div class="pure-u-1 pure-u-sm-1-4"><label for="mqttUseJson">Use JSON payload</label></div>
<div class="pure-u-1 pure-u-sm-3-4"><input type="checkbox" name="mqttUseJson" tabindex="26" /></div>
<div class="pure-u-1 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint">
All messages (except the device status) will be included in a JSON payload along with the timestamp and hostname
and sent under the <strong>&lt;root&gt;/data</strong> topic.<br />
Messages will be queued and sent after 100ms, so different messages could be merged into a single payload.<br />
Subscribtions will still be done to single topics.
</div>
</div>
</fieldset> </fieldset>
</div> </div>


Loading…
Cancel
Save