Browse Source

Method to send websocket messages from progmem

fastled
Xose Pérez 7 years ago
parent
commit
90617963e3
5 changed files with 32 additions and 18 deletions
  1. +2
    -2
      code/espurna/ntp.ino
  2. +2
    -2
      code/espurna/ota.ino
  3. +1
    -1
      code/espurna/relay.ino
  4. +1
    -1
      code/espurna/rfbridge.ino
  5. +26
    -12
      code/espurna/web.ino

+ 2
- 2
code/espurna/ntp.ino View File

@ -52,12 +52,12 @@ void ntpSetup() {
DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
} }
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend("{\"ntpStatus\": false}");
wsSend_P(PSTR("{\"ntpStatus\": false}"));
#endif #endif
} else { } else {
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend("{\"ntpStatus\": true}");
wsSend_P(PSTR("{\"ntpStatus\": true}"));
#endif #endif
} }
}); });


+ 2
- 2
code/espurna/ota.ino View File

@ -25,7 +25,7 @@ void otaSetup() {
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
DEBUG_MSG_P(PSTR("[OTA] Start\n")); DEBUG_MSG_P(PSTR("[OTA] Start\n"));
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend("{\"message\": \"OTA update started\"}");
wsSend_P(PSTR("{\"message\": \"OTA update started\"}"));
#endif #endif
}); });
@ -33,7 +33,7 @@ void otaSetup() {
customReset(CUSTOM_RESET_OTA); customReset(CUSTOM_RESET_OTA);
DEBUG_MSG_P(PSTR("\n[OTA] End\n")); DEBUG_MSG_P(PSTR("\n[OTA] End\n"));
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend("{\"action\": \"reload\"}");
wsSend_P(PSTR("{\"action\": \"reload\"}"));
#endif #endif
delay(100); delay(100);
}); });


+ 1
- 1
code/espurna/relay.ino View File

@ -153,7 +153,7 @@ void relayPulseMode(unsigned int value, bool report) {
#if WEB_SUPPORT #if WEB_SUPPORT
char message[20]; char message[20];
sprintf_P(message, PSTR("{\"relayPulseMode\": %d}"), value);
snprintf_P(message, strlen(message), PSTR("{\"relayPulseMode\": %d}"), value);
wsSend(message); wsSend(message);
#endif #endif


+ 1
- 1
code/espurna/rfbridge.ino View File

@ -102,7 +102,7 @@ void _rfbDecode() {
_rfbAck(); _rfbAck();
DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn timeout\n")); DEBUG_MSG_P(PSTR("[RFBRIDGE] Learn timeout\n"));
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend("{\"action\": \"rfbTimeout\"}");
wsSend_P(PSTR("{\"action\": \"rfbTimeout\"}"));
#endif #endif
} }


+ 26
- 12
code/espurna/web.ino View File

@ -52,11 +52,11 @@ std::vector<web_api_t> _apis;
void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) { void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
wsSend("{\"mqttStatus\": true}");
wsSend_P(PSTR("{\"mqttStatus\": true}"));
} }
if (type == MQTT_DISCONNECT_EVENT) { if (type == MQTT_DISCONNECT_EVENT) {
wsSend("{\"mqttStatus\": false}");
wsSend_P(PSTR("{\"mqttStatus\": false}"));
} }
} }
@ -68,7 +68,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
JsonObject& root = jsonBuffer.parseObject((char *) payload); JsonObject& root = jsonBuffer.parseObject((char *) payload);
if (!root.success()) { if (!root.success()) {
DEBUG_MSG_P(PSTR("[WEBSOCKET] Error parsing data\n")); DEBUG_MSG_P(PSTR("[WEBSOCKET] Error parsing data\n"));
wsSend(client_id, "{\"message\": \"Error parsing data!\"}");
wsSend_P(client_id, PSTR("{\"message\": \"Error parsing data!\"}"));
return; return;
} }
@ -103,7 +103,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
JsonObject& data = root["data"]; JsonObject& data = root["data"];
if (!data.containsKey("app") || (data["app"] != APP_NAME)) { if (!data.containsKey("app") || (data["app"] != APP_NAME)) {
wsSend(client_id, "{\"message\": \"The file does not look like a valid configuration backup.\"}");
wsSend_P(client_id, PSTR("{\"message\": \"The file does not look like a valid configuration backup.\"}"));
return; return;
} }
@ -119,7 +119,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
saveSettings(); saveSettings();
wsSend(client_id, "{\"message\": \"Changes saved. You should reboot your board now.\"}");
wsSend_P(client_id, PSTR("{\"message\": \"Changes saved. You should reboot your board now.\"}"));
} }
@ -264,11 +264,11 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
} }
if (key == "adminPass2") { if (key == "adminPass2") {
if (!value.equals(adminPass)) { if (!value.equals(adminPass)) {
wsSend(client_id, "{\"message\": \"Passwords do not match!\"}");
wsSend_P(client_id, PSTR("{\"message\": \"Passwords do not match!\"}"));
return; return;
} }
if (value.length() == 0) continue; if (value.length() == 0) continue;
wsSend(client_id, "{\"action\": \"reload\"}");
wsSend_P(client_id, PSTR("{\"action\": \"reload\"}"));
key = String("adminPass"); key = String("adminPass");
} }
@ -371,9 +371,9 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
} }
if (changed) { if (changed) {
wsSend(client_id, "{\"message\": \"Changes saved\"}");
wsSend_P(client_id, PSTR("{\"message\": \"Changes saved\"}"));
} else { } else {
wsSend(client_id, "{\"message\": \"No changes detected\"}");
wsSend_P(client_id, PSTR("{\"message\": \"No changes detected\"}"));
} }
} }
@ -608,7 +608,7 @@ bool _wsAuth(AsyncWebSocketClient * client) {
if (index == WS_BUFFER_SIZE) { if (index == WS_BUFFER_SIZE) {
DEBUG_MSG_P(PSTR("[WEBSOCKET] Validation check failed\n")); DEBUG_MSG_P(PSTR("[WEBSOCKET] Validation check failed\n"));
wsSend(client->id(), "{\"message\": \"Session expired, please reload page...\"}");
wsSend_P(client->id(), PSTR("{\"message\": \"Session expired, please reload page...\"}"));
return false; return false;
} }
@ -663,16 +663,30 @@ bool wsConnected() {
return (_ws.count() > 0); return (_ws.count() > 0);
} }
bool wsSend(const char * payload) {
void wsSend(const char * payload) {
if (_ws.count() > 0) { if (_ws.count() > 0) {
_ws.textAll(payload); _ws.textAll(payload);
} }
} }
bool wsSend(uint32_t client_id, const char * payload) {
void wsSend_P(PGM_P payload) {
if (_ws.count() > 0) {
char buffer[strlen_P(payload)];
strcpy_P(buffer, payload);
_ws.textAll(buffer);
}
}
void wsSend(uint32_t client_id, const char * payload) {
_ws.text(client_id, payload); _ws.text(client_id, payload);
} }
void wsSend_P(uint32_t client_id, PGM_P payload) {
char buffer[strlen_P(payload)];
strcpy_P(buffer, payload);
_ws.text(client_id, buffer);
}
void wsSetup() { void wsSetup() {
_ws.onEvent(_wsEvent); _ws.onEvent(_wsEvent);
mqttRegister(_wsMQTTCallback); mqttRegister(_wsMQTTCallback);


Loading…
Cancel
Save