From aeef952908fe50eaa3165216739f1ee67126403c Mon Sep 17 00:00:00 2001 From: Nis Wechselberg Date: Fri, 24 Mar 2017 12:40:46 +0100 Subject: [PATCH] Added small template for service autodiscovery --- ESP8266-mDNS-Subwoofer/.gitignore | 1 + .../ESP8266-mDNS-Subwoofer.h.template | 3 + .../ESP8266-mDNS-Subwoofer.ino | 145 ++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 ESP8266-mDNS-Subwoofer/.gitignore create mode 100644 ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.h.template create mode 100644 ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.ino diff --git a/ESP8266-mDNS-Subwoofer/.gitignore b/ESP8266-mDNS-Subwoofer/.gitignore new file mode 100644 index 0000000..14064a9 --- /dev/null +++ b/ESP8266-mDNS-Subwoofer/.gitignore @@ -0,0 +1 @@ +ESP8266-mDNS-Subwoofer.h diff --git a/ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.h.template b/ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.h.template new file mode 100644 index 0000000..f279001 --- /dev/null +++ b/ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.h.template @@ -0,0 +1,3 @@ +// WiFi configuration +const char* ssid = "http://kiel.freifunk.net/"; +const char* password = ""; diff --git a/ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.ino b/ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.ino new file mode 100644 index 0000000..1b48503 --- /dev/null +++ b/ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.ino @@ -0,0 +1,145 @@ +/******************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2015 Nis Wechselberg + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ********************************************************************************/ +#include "ESP8266-mDNS-Subwoofer.h" +#include +#include + +#define SERIAL + +const char* hostString = "ESP_HT_SUB"; +const unsigned int port = 4721; + +// Server object +WiFiServer server(port); + +void setup() { + // Configure output pin + pinMode(12, OUTPUT); + digitalWrite(12, LOW); + + // Setup serial console for debugging +#ifdef SERIAL + Serial.begin(115200); + delay(100); + Serial.println("\r\nsetup()"); + + Serial.print("Hostname: "); + Serial.println(hostString); +#endif + + // Set the Hostname for the WiFi connection + WiFi.hostname(hostString); + + // Connect to WiFi, wait for established connection + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(250); +#ifdef SERIAL + Serial.print("."); +#endif + } + + // Print established connection data to console +#ifdef SERIAL + Serial.println(""); + Serial.print("Connected to "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); +#endif + + // Initialize server + server.begin(); + + // Initialize Multicast DNS responder + if (!MDNS.begin(hostString)) { +#ifdef SERIAL + Serial.println("Error setting up MDNS responder!"); +#endif + } +#ifdef SERIAL + Serial.println("mDNS responder started"); +#endif + + // Announce hometheater service + MDNS.addService("hometheater", "tcp", port); + MDNS.addServiceTxt("hometheater", "tcp", "Component", "Subwoofer"); + +#ifdef SERIAL + Serial.println("loop() next"); +#endif +} + +void loop() { + // Update the mDNS responder + MDNS.update(); + + // Check if a client connected + WiFiClient client = server.available(); + if (client) { +#ifdef SERIAL + Serial.println("Client connected"); +#endif + // Wait until the client sends some data or timeout hits + unsigned char timeout = 5; + while(!client.available() && timeout > 0){ + delay(1000); + timeout = timeout - 1; + } + + if (timeout <= 0) { + // Timeout was triggered, cleanup and leave + } else { + // Some data was received + String req = client.readStringUntil('\r'); + client.flush(); + +#ifdef SERIAL + Serial.println(req); +#endif + + String response; + // Check for valid command + if (req.equals("Dominik")) { + // Command valid, trigger switch + digitalWrite(12, HIGH); + delay(100); + digitalWrite(12, LOW); + response = "OK\n"; + } else { + // Invalid data, discard + response = "ERR\n"; + } + + client.print(response); + delay(10); + + } + client.flush(); + client.stop(); + } + // Throttle looping a bit + delay(500); +}