Arduino/WiFi-Subwoofer/WiFi-Subwoofer.ino

109 lines
3 KiB
Arduino
Raw Normal View History

2016-08-28 21:41:02 +02:00
/********************************************************************************
* 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 <ESP8266WiFi.h>
#include "WiFi-Subwoofer.h"
#define DEBUGTOSERIAL 1
// HTTP configuration
const unsigned char port = 80;
WiFiServer server(port);
void setup() {
// Set wifi mode to client
WiFi.mode(WIFI_STA);
delay(5);
Serial.begin(115200);
// Connect to WiFi network
if (DEBUGTOSERIAL) {
Serial.print("Connecting to ");
Serial.println(ssid);
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (DEBUGTOSERIAL) {
Serial.print(".");
}
}
if (DEBUGTOSERIAL) {
Serial.println("");
Serial.println("WiFi connected");
}
// Start the server
server.begin();
if (DEBUGTOSERIAL) {
Serial.print("Server started at IP address ");
// Print the IP address
Serial.println(WiFi.localIP());
}
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (client) {
int timeout = 5;
// Wait until the client sends some data or timeout hits
if (DEBUGTOSERIAL) {
Serial.println("Client connected");
}
while(!client.available() && timeout > 0){
delay(1000);
timeout = timeout - 1;
}
if (timeout <= 0) {
client.stop();
client.flush();
} else {
// Read the first line of the request
String req = client.readStringUntil('\r');
if (DEBUGTOSERIAL) {
Serial.println(req);
}
client.flush();
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
client.print(response);
delay(10);
client.flush();
}
}
}