Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. /*
  2. * Sketch: ESP8266_LED_Control_02
  3. * Control an LED from a web browser
  4. * Intended to be run on an ESP8266
  5. *
  6. * connect to the ESP8266 AP then
  7. * use web broswer to go to 192.168.4.1
  8. *
  9. */
  10.  
  11.  
  12. #include <ESP8266WiFi.h>
  13. #include <Arduino.h>
  14.  
  15. #define REDPIN D1
  16. #define GREENPIN D2
  17. #define BLUEPIN D3
  18.  
  19. #define FADESPEED 5 // make this higher to slow down
  20.  
  21. const char AP_NameChar[] = "RoboTeamLED" ;
  22. const char WiFiPassword[] = "12345678";
  23.  
  24. WiFiServer server(80);
  25.  
  26. String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
  27. String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
  28. String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form><br>";
  29. String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form><br>";
  30. String html_4 = "</div></body></html>";
  31.  
  32. String request = "";
  33.  
  34. void setup()
  35. {
  36.  
  37.  
  38. pinMode(REDPIN, OUTPUT);
  39. pinMode(GREENPIN, OUTPUT);
  40. pinMode(BLUEPIN, OUTPUT);
  41.  
  42. boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
  43. server.begin();
  44.  
  45. } // void setup()
  46.  
  47. void loop()
  48. {
  49.  
  50. // Check if a client has connected
  51. WiFiClient client = server.available();
  52. if (!client) { return; }
  53.  
  54. // Read the first line of the request
  55. request = client.readStringUntil('\r');
  56.  
  57. if ( request.indexOf("LEDON") > 0 ) {
  58. // fade from blue to violet
  59. digitalWrite(REDPIN, 255);
  60. digitalWrite(GREENPIN, 255);
  61. digitalWrite(BLUEPIN, 255);
  62. }
  63. else if ( request.indexOf("LEDOFF") > 0 ) {
  64. digitalWrite(REDPIN, 0);
  65. digitalWrite(GREENPIN, 0);
  66. digitalWrite(BLUEPIN, 0);
  67. } else if ( request.indexOf("LEDRED") > 0 ) {
  68. digitalWrite(REDPIN, 255);
  69. digitalWrite(GREENPIN, 0);
  70. digitalWrite(BLUEPIN, 0);
  71. } else if ( request.indexOf("LEDBLUE") > 0 ) {
  72. digitalWrite(REDPIN, 0);
  73. digitalWrite(GREENPIN, 0);
  74. digitalWrite(BLUEPIN, 255);
  75. } else if ( request.indexOf("LEDGREEN") > 0 ) {
  76. digitalWrite(REDPIN, 0);
  77. digitalWrite(GREENPIN, 255);
  78. digitalWrite(BLUEPIN, 0);
  79. }
  80.  
  81. client.flush();
  82.  
  83. client.print( header );
  84. client.print( html_1 );
  85. client.print( html_2 );
  86. client.print( html_3 );
  87. client.print( html_4);
  88.  
  89. delay(5);
  90. // The client will actually be disconnected when the function returns and 'client' object is detroyed
  91.  
  92. } // void loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement