From 98a6b77afdf53fa9558b8babb52ce6ff96928a3f Mon Sep 17 00:00:00 2001 From: Nis Wechselberg Date: Sat, 2 Jul 2016 18:15:30 +0200 Subject: [PATCH] Added BenQ projector serial wiffi dongle code --- WiFi-Serial-Bridge/.gitignore | 1 + .../WiFi-Serial-Bridge.h.template | 3 + WiFi-Serial-Bridge/WiFi-Serial-Bridge.ino | 114 ++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 WiFi-Serial-Bridge/.gitignore create mode 100644 WiFi-Serial-Bridge/WiFi-Serial-Bridge.h.template create mode 100644 WiFi-Serial-Bridge/WiFi-Serial-Bridge.ino diff --git a/WiFi-Serial-Bridge/.gitignore b/WiFi-Serial-Bridge/.gitignore new file mode 100644 index 0000000..3df8c8e --- /dev/null +++ b/WiFi-Serial-Bridge/.gitignore @@ -0,0 +1 @@ +WiFi-Serial-Bridge.h diff --git a/WiFi-Serial-Bridge/WiFi-Serial-Bridge.h.template b/WiFi-Serial-Bridge/WiFi-Serial-Bridge.h.template new file mode 100644 index 0000000..f279001 --- /dev/null +++ b/WiFi-Serial-Bridge/WiFi-Serial-Bridge.h.template @@ -0,0 +1,3 @@ +// WiFi configuration +const char* ssid = "http://kiel.freifunk.net/"; +const char* password = ""; diff --git a/WiFi-Serial-Bridge/WiFi-Serial-Bridge.ino b/WiFi-Serial-Bridge/WiFi-Serial-Bridge.ino new file mode 100644 index 0000000..b081379 --- /dev/null +++ b/WiFi-Serial-Bridge/WiFi-Serial-Bridge.ino @@ -0,0 +1,114 @@ +/******************************************************************************** + * 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(); + } + } +} + +