mc_workshop

ESP82266 sta mode

Apr 25th, 2019
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. const char* ssid = "xxxxxxx-xxxxxxx";
  4. const char* password = "xxxxxxxxxxx";
  5. ESP8266WebServer server(80);
  6. uint8_t LED1pin = D0;
  7. bool LED1status = LOW;
  8. uint8_t LED2pin = D1;
  9. bool LED2status = LOW;
  10. void setup() {
  11. Serial.begin(115200);
  12. delay(100);
  13. pinMode(LED1pin, OUTPUT);
  14. pinMode(LED2pin, OUTPUT);
  15. Serial.println("Connecting to ");
  16. Serial.println(ssid);
  17. WiFi.begin(ssid, password);
  18. while (WiFi.status() != WL_CONNECTED) {
  19. delay(1000);
  20. Serial.print(".");
  21. }
  22. Serial.println("");
  23. Serial.println("WiFi connected..!");
  24. Serial.print("Got IP: "); Serial.println(WiFi.localIP());
  25. server.on("/", handle_OnConnect);
  26. server.on("/led1on", handle_led1on);
  27. server.on("/led1off", handle_led1off);
  28. server.on("/led2on", handle_led2on);
  29. server.on("/led2off", handle_led2off);
  30. server.onNotFound(handle_NotFound);
  31. server.begin();
  32. Serial.println("HTTP server started");
  33. }
  34. void loop() {
  35. server.handleClient();
  36. if(LED1status)
  37. {digitalWrite(LED1pin, HIGH);}
  38. else
  39. {digitalWrite(LED1pin, LOW);}
  40. if(LED2status)
  41. {digitalWrite(LED2pin, HIGH);}
  42. else
  43. {digitalWrite(LED2pin, LOW);}
  44. }
  45. void handle_OnConnect() {
  46. LED1status = LOW;
  47. LED2status = LOW;
  48. Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
  49. server.send(200, "text/html", SendHTML(LED1status,LED2status));
  50. }
  51. void handle_led1on() {
  52. LED1status = HIGH;
  53. Serial.println("GPIO7 Status: ON");
  54. server.send(200, "text/html", SendHTML(true,LED2status));
  55. }
  56. void handle_led1off() {
  57. LED1status = LOW;
  58. Serial.println("GPIO7 Status: OFF");
  59. server.send(200, "text/html", SendHTML(false,LED2status));
  60. }
  61. void handle_led2on() {
  62. LED2status = HIGH;
  63. Serial.println("GPIO6 Status: ON");
  64. server.send(200, "text/html", SendHTML(LED1status,true));
  65. }
  66. void handle_led2off() {
  67. LED2status = LOW;
  68. Serial.println("GPIO6 Status: OFF");
  69. server.send(200, "text/html", SendHTML(LED1status,false));
  70. }
  71. void handle_NotFound(){
  72. server.send(404, "text/plain", "Not found");
  73. }
  74. String SendHTML(uint8_t led1stat,uint8_t led2stat){
  75. String ptr = "<!DOCTYPE html> <html>\n";
  76. ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  77. ptr +="<title>LED Control</title>\n";
  78. ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  79. ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  80. ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  81. ptr +=".button-on {background-color: #1abc9c;}\n";
  82. ptr +=".button-on:active {background-color: #16a085;}\n";
  83. ptr +=".button-off {background-color: #34495e;}\n";
  84. ptr +=".button-off:active {background-color: #2c3e50;}\n";
  85. ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  86. ptr +="</style>\n";
  87. ptr +="</head>\n";
  88. ptr +="<body>\n";
  89. ptr +="<h1>ESP8266 Web Server</h1>\n";
  90. ptr +="<h3>Using Station(STA) Mode</h3>\n";
  91. if(led1stat)
  92. {ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
  93. else
  94. {ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
  95. if(led2stat)
  96. {ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
  97. else
  98. {ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
  99. ptr +="</body>\n";
  100. ptr +="</html>\n";
  101. return ptr;
  102. }
RAW Paste Data