Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2 KiB

7 years ago
  1. /*
  2. RENTALITO
  3. WEBSERVER MODULE
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #include <WebSocketsServer.h>
  7. #include <Hash.h>
  8. #include <ArduinoJson.h>
  9. WebSocketsServer webSocket = WebSocketsServer(81);
  10. // -----------------------------------------------------------------------------
  11. // WEBSOCKETS
  12. // -----------------------------------------------------------------------------
  13. bool webSocketSend(char * payload) {
  14. //DEBUG_MSG("[WEBSOCKET] Broadcasting '%s'\n", payload);
  15. webSocket.broadcastTXT(payload);
  16. }
  17. bool webSocketSend(uint8_t num, char * payload) {
  18. //DEBUG_MSG("[WEBSOCKET] Sending '%s' to #%d\n", payload, num);
  19. webSocket.sendTXT(num, payload);
  20. }
  21. void webSocketStart(uint8_t num) {
  22. char buffer[64];
  23. sprintf(buffer, "%s %s", APP_NAME, APP_VERSION);
  24. DynamicJsonBuffer jsonBuffer;
  25. JsonObject& root = jsonBuffer.createObject();
  26. root["app"] = buffer;
  27. root["manufacturer"] = String(MANUFACTURER);
  28. root["device"] = String(DEVICE);
  29. root["hostname"] = getSetting("hostname", HOSTNAME);
  30. root["network"] = getNetwork();
  31. root["ip"] = getIP();
  32. root["mqttStatus"] = mqttConnected() ? "1" : "0";
  33. root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER);
  34. root["mqttPort"] = getSetting("mqttPort", String(MQTT_PORT));
  35. root["mqttUser"] = getSetting("mqttUser");
  36. root["mqttPassword"] = getSetting("mqttPassword");
  37. root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC);
  38. root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH;
  39. #if ENABLE_RF
  40. root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL));
  41. root["rfDevice"] = getSetting("rfDevice", String(RF_DEVICE));
  42. #endif
  43. #if ENABLE_EMON
  44. root["pwMainsVoltage"] = getSetting("pwMainsVoltage", String(EMON_MAINS_VOLTAGE));
  45. root["pwCurrentRatio"] = getSetting("pwCurrentRatio", String(EMON_CURRENT_RATIO));
  46. #endif
  47. JsonArray& wifi = root.createNestedArray("wifi");
  48. for (byte i=0; i<3; i++) {
  49. JsonObject& network = wifi.createNestedObject();
  50. network["ssid"] = getSetting("ssid" + String(i));
  51. network["pass"] = getSetting("pass" + String(i));
  52. }
  53. String output;
  54. root.printTo(output);
  55. webSocket.sendTXT(num, (char *) output.c_str());
  56. }
  57. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  58. switch(type) {
  59. case WStype_DISCONNECTED:
  60. DEBUG_MSG("[WEBSOCKET] #%u disconnected\n", num);
  61. break;
  62. case WStype_CONNECTED:
  63. #if DEBUG_PORT
  64. {
  65. IPAddress ip = webSocket.remoteIP(num);
  66. DEBUG_MSG("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  67. }
  68. #endif
  69. webSocketStart(num);
  70. break;
  71. case WStype_TEXT:
  72. DEBUG_MSG("[WEBSOCKET] #%u sent: %s\n", num, payload);
  73. break;
  74. case WStype_BIN:
  75. DEBUG_MSG("[WEBSOCKET] #%u sent binary length: %u\n", num, length);
  76. break;
  77. }
  78. }
  79. void webSocketSetup() {
  80. webSocket.begin();
  81. webSocket.onEvent(webSocketEvent);
  82. }
  83. void webSocketLoop() {
  84. webSocket.loop();
  85. }