/******************************************************************************** * 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 #include "WiFi-Serial-Bridge.h" #define DEBUGTOSERIAL 0 // 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()); } } 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(); String data = "\r"; data += req; data += "\r"; Serial.print(data); delay(500); String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; while (Serial.available() > 0) { char inc[2]; inc[0] = Serial.read(); inc[1] = '\0'; response += inc; } client.print(response); delay(10); client.flush(); } } }