Added small template for service autodiscovery
This commit is contained in:
parent
abe6c9c440
commit
aeef952908
3 changed files with 149 additions and 0 deletions
1
ESP8266-mDNS-Subwoofer/.gitignore
vendored
Normal file
1
ESP8266-mDNS-Subwoofer/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
ESP8266-mDNS-Subwoofer.h
|
3
ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.h.template
Normal file
3
ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.h.template
Normal file
|
@ -0,0 +1,3 @@
|
|||
// WiFi configuration
|
||||
const char* ssid = "http://kiel.freifunk.net/";
|
||||
const char* password = "";
|
145
ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.ino
Normal file
145
ESP8266-mDNS-Subwoofer/ESP8266-mDNS-Subwoofer.ino
Normal file
|
@ -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 <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
|
||||
#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);
|
||||
}
|
Loading…
Reference in a new issue