const byte led = D1; const byte button = D5; bool LEDstate = false; #include #define WIFI_MODE_CLIENT //can be WIFI_MODE_MASTER // Replace with your network credentials char* ssid = "DPWL";//"WIV"; //"DasSchmalter"; char* password = "3p7iX/2gV";//"DF~12qa.40"; //"12345678"; // 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() { 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: pinMode(led, OUTPUT); pinMode(button, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(button), toggleLED, FALLING); 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.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); //Serial.print("AP IP address: "); //Serial.println(myIP); #endif #ifdef WIFI_MODE_CLIENT 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 server.begin(); } void loop() { // put your main code here, to run repeatedly: WiFiClient client = server.available(); // Listen for incoming clients 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 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 (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: 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 if (header.indexOf("GET /api/on") >= 0) { //Serial.println("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"); LEDstate = false; digitalWrite(led, LOW); client.println("{\"command\":\"off\",\"response\":\"OK\"}"); } else if (header.indexOf("GET /api/state") >= 0) { if(LEDstate) { client.println("{\"command\":\"state\",\"response\":\"ON\"}"); } else { client.println("{\"command\":\"state\",\"response\":\"OFF\"}"); } //Serial.println("LAMP status"); } else { // Display 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 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 } } } // Clear the header variable header = ""; // Close the connection client.stop(); //Serial.println("Client disconnected."); //Serial.println(""); } }