Explorar o código

support mDNS and SSDP

Chris Mullins %!s(int64=8) %!d(string=hai) anos
pai
achega
8b3cc78d37
Modificáronse 3 ficheiros con 29 adicións e 0 borrados
  1. 10 0
      lib/WebServer/MiLightHttpServer.cpp
  2. 2 0
      lib/WebServer/MiLightHttpServer.h
  3. 17 0
      src/main.cpp

+ 10 - 0
lib/WebServer/MiLightHttpServer.cpp

@@ -7,6 +7,7 @@
 #include <GithubClient.h>
 #include <string.h>
 #include <TokenIterator.h>
+#include <ESP8266SSDP.h>
 
 void MiLightHttpServer::begin() {
   applySettings(settings);
@@ -24,6 +25,7 @@ void MiLightHttpServer::begin() {
   server.on("/about", HTTP_GET, [this]() { handleAbout(); });
   server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
   server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
+  server.on("/description.xml", HTTP_GET, [this]() { SSDP.schema(server.client()); });
   server.on("/firmware", HTTP_POST,
     [this](){
       server.sendHeader("Connection", "close");
@@ -106,6 +108,14 @@ void MiLightHttpServer::handleClient() {
   server.handleClient();
 }
 
+void MiLightHttpServer::on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler) {
+  server.on(path, method, handler);
+}
+
+WiFiClient MiLightHttpServer::client() {
+  return server.client();
+}
+
 void MiLightHttpServer::handleSystemPost() {
   DynamicJsonBuffer buffer;
   JsonObject& request = buffer.parse(server.arg("plain"));

+ 2 - 0
lib/WebServer/MiLightHttpServer.h

@@ -25,6 +25,8 @@ public:
   void begin();
   void handleClient();
   void onSettingsSaved(SettingsSavedHandler handler);
+  void on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler);
+  WiFiClient client();
 
 protected:
   ESP8266WebServer::THandlerFunction handleServeFile(

+ 17 - 0
src/main.cpp

@@ -11,6 +11,8 @@
 #include <MiLightHttpServer.h>
 #include <Settings.h>
 #include <MiLightUdpServer.h>
+#include <ESP8266mDNS.h>
+#include <ESP8266SSDP.h>
 
 WiFiManager wifiManager;
 
@@ -92,8 +94,23 @@ void setup() {
   Settings::load(settings);
   applySettings();
 
+  if (! MDNS.begin("milight-hub")) {
+    Serial.println(F("Error setting up MDNS responder"));
+  }
+
+  MDNS.addService("http", "tcp", 80);
+
+  SSDP.setSchemaURL("description.xml");
+  SSDP.setHTTPPort(80);
+  SSDP.setName("ESP8266 MiLight Gateway");
+  SSDP.setSerialNumber(ESP.getChipId());
+  SSDP.setURL("/");
+  SSDP.setDeviceType("upnp:rootdevice");
+  SSDP.begin();
+
   httpServer = new MiLightHttpServer(settings, milightClient);
   httpServer->onSettingsSaved(applySettings);
+  httpServer->on("/description.xml", HTTP_GET, []() { SSDP.schema(httpServer->client()); });
   httpServer->begin();
 }