Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Sketch: ESP8266_LED_Control_02
- * Control an LED from a web browser
- * Intended to be run on an ESP8266
- *
- * connect to the ESP8266 AP then
- * use web broswer to go to 192.168.4.1
- *
- */
- #include <ESP8266WiFi.h>
- #include <Arduino.h>
- #define REDPIN D1
- #define GREENPIN D2
- #define BLUEPIN D3
- #define FADESPEED 5 // make this higher to slow down
- const char AP_NameChar[] = "RoboTeamLED" ;
- const char WiFiPassword[] = "12345678";
- WiFiServer server(80);
- String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
- String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
- String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form><br>";
- String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form><br>";
- String html_4 = "</div></body></html>";
- String request = "";
- void setup()
- {
- pinMode(REDPIN, OUTPUT);
- pinMode(GREENPIN, OUTPUT);
- pinMode(BLUEPIN, OUTPUT);
- boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
- server.begin();
- } // void setup()
- void loop()
- {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) { return; }
- // Read the first line of the request
- request = client.readStringUntil('\r');
- if ( request.indexOf("LEDON") > 0 ) {
- // fade from blue to violet
- digitalWrite(REDPIN, 255);
- digitalWrite(GREENPIN, 255);
- digitalWrite(BLUEPIN, 255);
- }
- else if ( request.indexOf("LEDOFF") > 0 ) {
- digitalWrite(REDPIN, 0);
- digitalWrite(GREENPIN, 0);
- digitalWrite(BLUEPIN, 0);
- } else if ( request.indexOf("LEDRED") > 0 ) {
- digitalWrite(REDPIN, 255);
- digitalWrite(GREENPIN, 0);
- digitalWrite(BLUEPIN, 0);
- } else if ( request.indexOf("LEDBLUE") > 0 ) {
- digitalWrite(REDPIN, 0);
- digitalWrite(GREENPIN, 0);
- digitalWrite(BLUEPIN, 255);
- } else if ( request.indexOf("LEDGREEN") > 0 ) {
- digitalWrite(REDPIN, 0);
- digitalWrite(GREENPIN, 255);
- digitalWrite(BLUEPIN, 0);
- }
- client.flush();
- client.print( header );
- client.print( html_1 );
- client.print( html_2 );
- client.print( html_3 );
- client.print( html_4);
- delay(5);
- // The client will actually be disconnected when the function returns and 'client' object is detroyed
- } // void loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement