/** * ESP WiFi RGB-Controller * * (c) Finn Strack Dec 2017 */ #include const char* ssid = "YOUR_WLAN_SSID"; const char* password = "YOUR_WLAN_PASSWORD"; // config static IP IPAddress ip(192, 168, 1, 30); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); //Hier Pins definieren int r_pin = D5; int g_pin = D6; int b_pin = D7; int r = 0; int g = 0; int b = 0; WiFiServer server(80); void setup() { Serial.begin(9600); delay(1); WiFi.mode(WIFI_STA); WiFi.config(ip, gateway, subnet); WiFiStart(); Serial.println("SmartHome yourself RGB-Controller"); delay(800); Serial.println("Initialisiere ..."); pinMode(r_pin, OUTPUT); pinMode(g_pin, OUTPUT); pinMode(b_pin, OUTPUT); digitalWrite(r_pin,HIGH); delay(800); digitalWrite(g_pin,HIGH); delay(800); digitalWrite(b_pin,HIGH); delay(1200); digitalWrite(r_pin,LOW); digitalWrite(g_pin,LOW); digitalWrite(b_pin,LOW); } void WiFiStart() { Serial.print("Verbindung aufbauen zu: "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("Verbunden !"); server.begin(); Serial.println("Webserver gestartet mit der IP: "); Serial.println(WiFi.localIP()); } void loop() { if (WiFi.status() != WL_CONNECTED){ WiFiStart(); } WiFiClient client = server.available(); if (!client) { return; } Serial.println("Neue Anfrage"); unsigned long ultimeout = millis()+250; while(!client.available() && (millis()ultimeout) { Serial.println("Client Verbindung Timeout"); return; } String sRequest = client.readStringUntil('\r'); client.flush(); Serial.println(sRequest); if(sRequest.indexOf("favicon")<0){ int r_pos_1 = sRequest.indexOf("red="); int r_pos_2 = sRequest.indexOf("&",r_pos_1); if(r_pos_2 == -1) { r_pos_2 = sRequest.indexOf(" ",r_pos_1); } String r_str = sRequest.substring(r_pos_1+4,r_pos_2); int g_pos_1 = sRequest.indexOf("green="); int g_pos_2 = sRequest.indexOf("&",g_pos_1); if(g_pos_2 == -1) { g_pos_2 = sRequest.indexOf(" ",g_pos_1); } String g_str = sRequest.substring(g_pos_1+6,g_pos_2); int b_pos_1 = sRequest.indexOf("blue="); int b_pos_2 = sRequest.indexOf("&",b_pos_1); if(b_pos_2 == -1) { b_pos_2 = sRequest.indexOf(" ",b_pos_1); } String b_str = sRequest.substring(b_pos_1+5); if(r_str != "" && g_str != "" && b_str != "") { r = map( r_str.toInt() ,0,255,0,1023); g = map( g_str.toInt() ,0,255,0,1023); b = map( b_str.toInt() ,0,255,0,1023); } Serial.print("Schalte R: "); Serial.print(r); Serial.print(" G: "); Serial.print(g); Serial.print(" B: "); Serial.println(b); analogWrite(r_pin, r ); analogWrite(g_pin, g ); analogWrite(b_pin, b ); String sResponse,sHeader; sResponse = "ESP8266 RGB-Controller"; sResponse += "

ESP8266 WiFi RGB Controller


"; sResponse += "Bitte Parameter für red, green und blue in die URL eintragen !
"; sResponse += ""; sHeader = "HTTP/1.1 200 OK\r\n"; sHeader += "Content-Length: "; sHeader += sResponse.length(); sHeader += "\r\n"; sHeader += "Content-Type: text/html\r\n"; sHeader += "Connection: close\r\n"; sHeader += "\r\n"; client.print(sHeader); client.print(sResponse); client.stop(); Serial.println("Anfrage beendet"); } }