diff --git a/Arduino/DasSchmalter/.gitignore b/Arduino/DasSchmalter/.gitignore new file mode 100644 index 0000000..d0e81a2 --- /dev/null +++ b/Arduino/DasSchmalter/.gitignore @@ -0,0 +1 @@ +env.h diff --git a/Arduino/DasSchmalter/DasSchmalter.ino b/Arduino/DasSchmalter/DasSchmalter.ino index ec23569..87a6c42 100644 --- a/Arduino/DasSchmalter/DasSchmalter.ino +++ b/Arduino/DasSchmalter/DasSchmalter.ino @@ -1,68 +1,65 @@ -const byte led = D1; -const byte button = D5; -bool LEDstate = false; - #include -#define WIFI_MODE_CLIENT //can be WIFI_MODE_MASTER +// !!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 !! -// Replace with your network credentials -char* ssid = "WIV"; //"DasSchmalter"; -char* password = "DF~12qa.40"; //"12345678"; +#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); -String header; -// the code for the whole ebinterface -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\n"; -// Configure the static ip adress -#ifdef WIFI_MODE_CLIENT -IPAddress local_IP(192, 168, 4, 10); -#endif -#ifdef WIFI_MODE_MASTER -IPAddress local_IP(192, 168, 4, 1); -#endif -IPAddress gateway(192, 168, 4, 1); -IPAddress subnet(255, 255, 255, 0); - -volatile unsigned long oldTime = 0, debounceTime = 200; -void toggleLED() { +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() { - // put your setup code here, to run once: + // configure all pins pinMode(led, OUTPUT); pinMode(button, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(button), toggleLED, FALLING); + + // attach an interrupt for the hardware switch + attachInterrupt(digitalPinToInterrupt(button), handleInterrupt, FALLING); + + // start the serial monitor Serial.begin(9600); - /*if (!WiFi.config(local_IP, gateway, subnet)) { - //Serial.println("STA Failed to configure"); - } */ #ifdef WIFI_MODE_MASTER - //Serial.print("Configuring access point..."); - //WiFi.softAPConfig(local_IP, gateway, subnet); + // 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); + 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("."); @@ -73,79 +70,99 @@ void setup() { Serial.println(WiFi.localIP()); #endif + // start the web server server.begin(); } void loop() { - // put your main code here, to run repeatedly: - WiFiClient client = server.available(); // Listen for incoming clients + // check if a client has sent a request + WiFiClient client = server.available(); - if (client) { // If a new client connects, - //Serial.println("New Client."); // print a message out in the serial port - String currentLine = ""; // make a String to hold incoming data from the client - while (client.connected()) { // loop while the client's connected - if (client.available()) { // if there's bytes to read from the client, - char c = client.read(); // read a byte, then - //Serial.write(c); // print it out the serial monitor + 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 byte is a newline character - // if the current line is blank, you got two newline characters in a row. - // that's the end of the client HTTP request, so send a response: + + if (c == '\n') { + // if the data was a new line char if (currentLine.length() == 0) { - // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) - // and a content-type so the client knows what's coming, then a blank line: + // 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(); - // turns the GPIOs on and off + // check what the client wants ... if (header.indexOf("GET /api/on") >= 0) { - //Serial.println("LAMP on"); + // ... turn the lamp on + LEDstate = true; digitalWrite(led, HIGH); client.println("{\"command\":\"on\",\"response\":\"OK\"}"); + } else if (header.indexOf("GET /api/off") >= 0) { - //Serial.println("LAMP off"); + // ... 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\"}"); } - //Serial.println("LAMP status"); + + } else if (header.indexOf("GET /old") >= 0) { + // ... get the old HTML web page + + client.println(oldWebInterface); } else { - // Display the HTML web page + // ... 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 you got a newline, then clear currentLine + + } + else { + // if we got a newline -> clear currentLine currentLine = ""; } - } else if (c != '\r') { // if you got anything else but a carriage return character, - currentLine += c; // add it to the end of the 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(); - //Serial.println("Client disconnected."); - //Serial.println(""); } } diff --git a/Arduino/DasSchmalter/env.h.example b/Arduino/DasSchmalter/env.h.example new file mode 100644 index 0000000..a2c70b9 --- /dev/null +++ b/Arduino/DasSchmalter/env.h.example @@ -0,0 +1,25 @@ +// ------------------------------- +// --- DasSchmalter enviroment --- +// ------------------------------- + +// all user specific variables are defined here + +#ifndef ENV_H +#define ENV_H + +// --- wifi + + //can be set to WIFI_MODE_MASTER so that the schmalter will emit its own wifi network + #define WIFI_MODE_CLIENT + + // wifi ssid + char* ssid = ""; + + // wifi password + char* password = ""; + +// --- other + const byte led = D1; + const byte button = D5; + +#endif // ENV_H