/*
DasSchmalter - The smart switch
Copyright (C) 2019 Itsblue Development ( contact@itsblue.de )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
#include
// !!IMPORTANT!!
// You need to rename the file 'env.h.example' to 'env.h' otherwise this line will throw an error !!
// All user specific variables (like wifi password and ssid) are configured in that file !!
#include "env.h"
// helper vars
String header;
bool LEDstate = false;
volatile unsigned long oldTime = 0, debounceTime = 200;
// the HTML code for the whole ebinterface, can be called with http:///
String webInterface = "\n\n\n\n\n\n\nDas Schmalter\n\n\n\n\n\n\n\n\n\n
Das Schmalter
\n
\n\n
\n\n\n\n\n\n";
// older version of the web interface, can be called with http:///old
String oldWebInterface = "\n\n\n\n\n\n\nDas Schmalter\n\n\n\n\n\n\n\n\n\n
Das Schmalter
\n\n\n\n\n\n\n\n\n\n";
// Set web server port number to 80
WiFiServer server(80);
void handleInterrupt() {
// function to handle the external hardware switch
if ((millis() - oldTime) > debounceTime) {
if (digitalRead(button) == LOW) {
LEDstate = ! LEDstate;
digitalWrite(led, LEDstate);
}
}
oldTime = millis();
}
void setup() {
// configure all pins
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
// attach an interrupt for the hardware switch
attachInterrupt(digitalPinToInterrupt(button), handleInterrupt, FALLING);
// start the serial monitor
Serial.begin(9600);
#ifdef WIFI_MODE_MASTER
// wifi mode master -> configure an access point
Serial.print("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
#endif
#ifdef WIFI_MODE_CLIENT
// wifi mode client -> connect to given wifi network
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi..");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
#endif
// start the web server
server.begin();
}
void loop() {
// check if a client has sent a request
WiFiClient client = server.available();
if (client) {
// if a client has connected
// string to hold incoming data from the client
String currentLine = "";
while (client.connected()) {
// while the client is connected we're in this loop
if (client.available()) {
// if the client has sent some data
// read id
char c = client.read();
header += c;
if (c == '\n') {
// if the data was a new line char
if (currentLine.length() == 0) {
// if the current line is empty -> client request (HTTP header) is over
// => send response to client
// send HTTP header
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Access-Control-Allow-Origin: *");
client.println("Connection: close");
client.println();
// check what the client wants ...
if (header.indexOf("GET /api/on") >= 0) {
// ... turn the lamp on
LEDstate = true;
digitalWrite(led, HIGH);
client.println("{\"command\":\"on\",\"response\":\"OK\"}");
} else if (header.indexOf("GET /api/off") >= 0) {
// ... turn the lamp off
LEDstate = false;
digitalWrite(led, LOW);
client.println("{\"command\":\"off\",\"response\":\"OK\"}");
} else if (header.indexOf("GET /api/state") >= 0) {
// ... get the state of the lamp
if(LEDstate) {
client.println("{\"command\":\"state\",\"response\":\"ON\"}");
}
else {
client.println("{\"command\":\"state\",\"response\":\"OFF\"}");
}
} else if (header.indexOf("GET /old") >= 0) {
// ... get the old HTML web page
client.println(oldWebInterface);
}
else {
// ... get the HTML web page
client.println(webInterface);
}
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
}
else {
// if we got a newline -> clear currentLine
currentLine = "";
}
} else if (c != '\r') {
// if the data was anything but a carriage return character -> add it to the end of the currentLine
currentLine += c;
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
}
}