Sfoglia il codice sorgente

Merge pull request #53 from sidoh/device_discovery

Implement mDNS and SSDP
Chris Mullins 8 anni fa
parent
commit
5d2e8a9fba
4 ha cambiato i file con 37 aggiunte e 0 eliminazioni
  1. 8 0
      README.md
  2. 10 0
      lib/WebServer/MiLightHttpServer.cpp
  3. 2 0
      lib/WebServer/MiLightHttpServer.h
  4. 17 0
      src/main.cpp

+ 8 - 0
README.md

@@ -54,6 +54,14 @@ This project uses [WiFiManager](https://github.com/tzapu/WiFiManager) to avoid t
 
 When the ESP powers on, you should be able to see a network named "ESPXXXXX", with XXXXX being an identifier for your ESP. Connect to this AP and a window should pop up prompting you to enter WiFi credentials.
 
+#### Get IP Address
+
+Both mDNS and SSDP are supported.
+
+* OS X - you should be able to navigate to http://milight-hub.local.
+* Windows - you should see a device called "ESP8266 MiLight Gateway" show up in your network explorer.
+* Linux users can install [avahi](http://www.avahi.org/) (`sudo apt-get install avahi-daemon` on Ubuntu), and should then be able to navigate to http://milight-hub.local.
+
 #### Use it!
 
 The HTTP endpoints (shown below) will be fully functional at this point. You should also be able to navigate to `http://<ip_of_esp>`. The UI should look like this:

+ 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();
 }