Removed deprecated projects, Added DS5 control

This commit is contained in:
Nis Wechselberg 2017-09-17 16:54:50 +02:00
parent e239197a91
commit 145984c2f8
15 changed files with 368 additions and 567 deletions

View file

@ -1,36 +0,0 @@
#include "ABROModel.c"
// Declarations
void setup() {
// Initializations
pinMode(11, INPUT);
pinMode(8, INPUT);
pinMode(5, INPUT);
pinMode(13, OUTPUT);
// Init model file
reset();
}
void loop() {
// Inputs
A = digitalRead(11) ? HIGH : LOW;
B = digitalRead(8) ? HIGH : LOW;
R = digitalRead(5) ? HIGH : LOW;
// Reaction of model
tick();
// Outputs
if (O) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}

View file

@ -1,129 +0,0 @@
/*****************************************************************************/
/* G E N E R A T E D C C O D E */
/*****************************************************************************/
/* KIELER - Kiel Integrated Environment for Layout Eclipse RichClient */
/* */
/* http://www.informatik.uni-kiel.de/rtsys/kieler/ */
/* Copyright 2014 by */
/* + Kiel University */
/* + Department of Computer Science */
/* + Real-Time and Embedded Systems Group */
/* */
/* This code is provided under the terms of the Eclipse Public License (EPL).*/
/*****************************************************************************/
char A;
char B;
char R;
char O;
char _trig;
char _trig2;
char _ABRO_local__termRegion;
char _ABRO_local__termRegion2;
char _GO;
char g0;
char g1;
char PRE_g1;
char g2;
char g4;
char g5;
char g6;
char g7;
char PRE_g7;
char g8;
char g9;
char g11;
char g12;
char g13;
char g14;
char PRE_g14;
char g15;
char g17;
char g18;
char g19;
char g20;
char PRE_g20;
char g21;
char g22;
char g23;
char g24;
char PRE_g24;
char g25;
char _condg4;
char _condg13;
char _condg19;
char g6_e1;
char g12_e2;
char g18_e3;
void reset() {
_GO = 1;
PRE_g1 = 0;
PRE_g7 = 0;
PRE_g14 = 0;
PRE_g20 = 0;
PRE_g24 = 0;
return;
}
void tick() {
{
g0 = _GO;
g25 = (PRE_g24);
g8 = (PRE_g7);
g9 = (g8 && R);
if (g9) {
_trig = 1;
}
g2 = (PRE_g1);
if (g2) {
_trig = 0;
_trig2 = 0;
_ABRO_local__termRegion = 0;
_ABRO_local__termRegion2 = 0;
}
g21 = (PRE_g20);
g17 = (g2 || g21);
g19 = (g17 && (!(B)));
_condg19 = _trig;
g18 = ((g17 && B) || (g19 && _condg19));
if (g18) {
_ABRO_local__termRegion2 = 1;
}
g15 = (PRE_g14);
g11 = (g2 || g15);
g13 = (g11 && (!(A)));
_condg13 = _trig;
g12 = ((g13 && _condg13) || (g11 && A));
if (g12) {
_ABRO_local__termRegion = 1;
}
g4 = (g2 || (g8 && (!(R))));
_condg4 = (_ABRO_local__termRegion && _ABRO_local__termRegion2);
g5 = (g4 && _condg4);
if (g5) {
_trig2 = 1;
}
g6 = (g9 || g5);
g7 = (g4 && (!(_condg4)));
g14 = (g13 && (!(_condg13)));
g20 = (g19 && (!(_condg19)));
g12_e2 = (!(g14));
g6_e1 = (!(g7));
g18_e3 = (!(g20));
g22 = ((g6_e1 || g6) && (g12_e2 || g12) && (g18_e3 || g18) && (g6 || g12 || g18));
g1 = (_GO || (g25 && R) || (g22 && _trig));
if (g1) {
O = 0;
}
g23 = (g22 && (!(_trig)));
if (g23) {
O = 1;
}
g24 = (g23 || (g25 && (!(R))));
}
PRE_g1 = g1;
PRE_g7 = g7;
PRE_g14 = g14;
PRE_g20 = g20;
PRE_g24 = g24;
_GO = 0;
return;
}

View file

@ -1,23 +0,0 @@
void setup() {
}
void loop() {
convert1(129);
convert2(129);
convert3(129);
}
int convert1(int color_in) {
return 4096 / ( 256 / color_in);
}
int convert2(int color_in) {
return color_in << 4;
}
int convert3(int color_in) {
return (int) (4096 / ( 256 / (float) color_in));
}

1
ESP8266-MQTT-Teufel-DS5/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
ESP8266-MQTT-Teufel-DS5.h

View file

@ -0,0 +1,13 @@
// WiFi configuration
const char* ssid = "http://kiel.freifunk.net/";
const char* password = "";
// MQTT Broker
const char* mqtt_server = "broker.mqtt-dashboard.com";
// Device identification
const char* mqtt_device_name = "esp8266benq"
// MQTT topics
const char* mqtt_topic_power = "benq/power";
const char* mqtt_topic_lamptime = "benq/lamptime";

View file

@ -0,0 +1,354 @@
/**********************************************************************
* 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.
**********************************************************************/
/**********************************************************************
* ESP8266-MQTT-Teufel-DS5-Control
*
* Links the ESP8266 based control board to an mqtt broker.
* The program uses multiple topics for incoming and outgoing messages.
* Incoming topics:
* - <mqtt_device>/command
* Outgoing topics:
* - <mqtt_device>/powerState
**********************************************************************/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include "ESP8266-MQTT-Teufel-DS5.h"
#define DEBUGTOSERIAL 1
// Timing for publishing powerState
unsigned long lastMsg = 0;
unsigned long publishInterval = 5000;
// Message buffer
char msg[50];
// Last known state
int powerState = 0;
// Volume control. Valid range is [-55,10]
int curVol = 0;
int targetVol = 0;
unsigned long lastVolChange = 0;
unsigned long volumeInterval = 500;
// Process tracker for calibration. 0 = nothing, 1 = decreasing, 2 = increasing
uint8_t recalibrateState = 0;
int calibrateLowerLimit = -55;
int calibrateUpperLimit = -22;
// Pin config
const int irSendPin = 13;
const int powerProbePin = 12;
// Global mqtt client object
WiFiClient espClient;
PubSubClient client(espClient);
// Command/code mapping
char* commands[] = {
"power", "mute", "51", "opt1",
"opt2", "coax1", "coax2", "tv",
"cd", "aux", "display", "return",
"mode", "speaker", "test", //"volUp",
"left", "ok", "right", //"volDown",
"fLeft", "center", "fRight", "sLeft",
"sub", "sRight"};
uint32_t codes[] = {
0x807F50AF, 0x807FD02F, 0x807F708F, 0x807F609F,
0x807FF00F, 0x807F48B7, 0x807FE01F, 0x807FC837,
0x807F6897, 0x807F40BF, 0x807FE817, 0x807FC03F,
0x807FE21D, 0x807F629D, 0x807FA25D, //0x807F7A85,
0x807FDA25, 0x807F5AA5, 0x807F1AE5, //0x807F6A95,
0x807FCA35, 0x807F4AB5, 0x807F8A75, 0x807FF20D,
0x807F728D, 0x807FB24D};
/*
* Initial setup for arduino
*/
void setup() {
// Configure pins
pinMode(irSendPin, OUTPUT);
digitalWrite(irSendPin, HIGH);
// Configure serial port
Serial.begin(115200);
delay(10);
// Prepare WiFi connection
setup_wifi();
// Connect to mqtt broker
client.setServer(mqtt_server, 1883);
client.setCallback(incoming_mqtt);
}
/*
* Prepares the wireless connection
*/
void setup_wifi() {
// Connect to the WiFi as a client
WiFi.mode(WIFI_STA);
// Do the connection
if (DEBUGTOSERIAL) {
Serial.println();
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");
// Print IP address to serial
Serial.print("My IP address: ");
Serial.println(WiFi.localIP());
}
ArduinoOTA.setHostname("esp8266-TeufelDS5");
ArduinoOTA.begin();
}
/*
* Callback method for incoming mqtt messages
*/
void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
if (DEBUGTOSERIAL) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (uint8_t i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
char* msgCopy = (char*) malloc(length + 1);
memcpy(msgCopy, payload, length);
msgCopy[length] = 0;
// Variable to store the IR code
uint32_t code = 0;
if (strcmp(topic, mqtt_topic_command) == 0) {
// Regular command channel triggered
for (uint8_t i = 0; i < 24; ++i) {
// Compare payload against known commands
if (strcmp(msgCopy, commands[i]) == 0) {
// Store code and be done
code = codes[i];
break;
}
}
// Check other special commands
if (strcmp(msgCopy, "volReset") == 0) {
// Recalibrate volume
recalibrateState = 1;
curVol = 10;
targetVol = calibrateLowerLimit;
}
if (strcmp(msgCopy, "volUp") == 0) {
if (recalibrateState == 0 && targetVol < 10) {
if (DEBUGTOSERIAL) {
Serial.print("Setting target volume to ");
Serial.println(targetVol + 1);
}
targetVol += 1;
}
}
if (strcmp(msgCopy, "volDown") == 0) {
if (recalibrateState == 0 && targetVol > -55) {
if (DEBUGTOSERIAL) {
Serial.print("Setting target volume to ");
Serial.println(targetVol - 1);
}
targetVol -= 1;
}
}
}
if (strcmp(topic, mqtt_topic_volume) == 0) {
int newTargetVol = atoi(msgCopy);
if (recalibrateState == 0) {
if (DEBUGTOSERIAL) {
Serial.print("Setting target volume to ");
Serial.println(newTargetVol);
}
targetVol = newTargetVol;
}
}
free(msgCopy);
// Check if we found a code to send
if (code != 0) {
replicateNEC(irSendPin, code, 32);
}
}
/*
* Blocking reconnect to the mqtt broker
*/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
if (DEBUGTOSERIAL) {
Serial.print("Attempting MQTT connection...");
}
// Attempt to connect
if (client.connect(mqtt_device_name)) {
if (DEBUGTOSERIAL) {
Serial.println("connected");
}
// subscribe to incoming topics
client.subscribe(mqtt_topic_command);
client.subscribe(mqtt_topic_volume);
if (DEBUGTOSERIAL) {
Serial.println("Subscriptions done");
}
} else {
if (DEBUGTOSERIAL) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
}
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void updateState() {
// Read value from digital pin (Levels are inverted)
powerState = digitalRead(powerProbePin);
if (DEBUGTOSERIAL) {
Serial.print("New power state determined: ");
Serial.println(powerState);
}
snprintf(msg, 49, "%d", powerState);
client.publish(mqtt_topic_powerState, msg);
// Update volume state
snprintf(msg, 49, "%d", curVol);
client.publish(mqtt_topic_volumeState, msg);
}
void updateVolume() {
// Increase volume a notch if requested
if (targetVol > curVol) {
replicateNEC(irSendPin, 0x807F7A85, 32);
curVol += 1;
snprintf(msg, 49, "%d", curVol);
client.publish(mqtt_topic_volumeState, msg);
}
// Decrease volume a notch if requested
if (targetVol < curVol) {
replicateNEC(irSendPin, 0x807F6A95, 32);
curVol -= 1;
snprintf(msg, 49, "%d", curVol);
client.publish(mqtt_topic_volumeState, msg);
}
// Check recalibration state
if (recalibrateState == 2 && curVol == calibrateUpperLimit) {
recalibrateState = 0;
}
if (recalibrateState == 1 && curVol == calibrateLowerLimit) {
recalibrateState = 2;
targetVol = calibrateUpperLimit;
}
}
void replicateNEC(int pin, uint32_t data, int dataLength) {
if (DEBUGTOSERIAL) {
Serial.print("Sending code ");
Serial.println(data, HEX);
}
// Prepare mask
uint32_t mask = 1L << (dataLength -1);
// Write initial 9ms+4.5ms pulse
digitalWrite(pin, LOW);
delayMicroseconds(9000);
digitalWrite(pin, HIGH);
delayMicroseconds(4500);
// Write pulses from data
while (mask > 0) {
digitalWrite(pin, LOW);
delayMicroseconds(562);
digitalWrite(pin, HIGH);
delayMicroseconds(562);
if (data & mask) {
delayMicroseconds(1124);
}
mask = mask >> 1;
}
// Write trailing pulse
digitalWrite(pin, LOW);
delayMicroseconds(562);
digitalWrite(pin, HIGH);
}
void loop() {
// Check OTA update
ArduinoOTA.handle();
// Ensure MQTT connection
if (!client.connected()) {
reconnect();
}
// Check the inbox
client.loop();
// Maybe update the volume
unsigned long now = millis();
if (now - lastVolChange > volumeInterval) {
lastVolChange = now;
updateVolume();
}
// Maybe push the current status
if (now - lastMsg > publishInterval) {
lastMsg = now;
updateState();
}
}

View file

@ -1 +0,0 @@
ESP8266-mDNS-Subwoofer.h

View file

@ -1,3 +0,0 @@
// WiFi configuration
const char* ssid = "http://kiel.freifunk.net/";
const char* password = "";

View file

@ -1,145 +0,0 @@
/********************************************************************************
* 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);
}

View file

@ -1 +0,0 @@
WiFi-Serial-Bridge.h

View file

@ -1,3 +0,0 @@
// WiFi configuration
const char* ssid = "http://kiel.freifunk.net/";
const char* password = "";

View file

@ -1,114 +0,0 @@
/********************************************************************************
* 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-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();
}
}
}

View file

@ -1 +0,0 @@
WiFi-Subwoofer.h

View file

@ -1,3 +0,0 @@
// WiFi configuration
const char* ssid = "http://kiel.freifunk.net/";
const char* password = "";

View file

@ -1,108 +0,0 @@
/********************************************************************************
* 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();
}
}
}