Advertisement
Guest User

direct connect

a guest
Jul 17th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. #define ESP8266_SSID "MY_SSID"
  5. //password must be at least 8 characters
  6. #define ESP8266_PASSWORD "12345678"
  7.  
  8. ESP8266WebServer server;
  9.  
  10. const char INDEX_HTML[] =
  11. "<!DOCTYPE HTML>"
  12. "<html>"
  13. "<head>"
  14. "<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
  15. "<title>ESP8266</title>"
  16. "<style>"
  17. "body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: Maroon; }"
  18. "</style>"
  19. "</head>"
  20. "<body>"
  21. "<h1>ESP8266 Standalone Access Point Demo</h1>"
  22. "<button onclick='toggle()'>Toggle LED</button>"
  23. "<button onclick='buzz()'>Buzz Buzzer</button>"
  24. "<script>"
  25. "function toggle(){"
  26. "fetch('/toggle').then(stream=>stream.text()).then(text=>console.log(text))"
  27. "}"
  28. "function buzz(){"
  29. "fetch('/buzz').then(stream=>stream.text()).then(text=>console.log(text))"
  30. "}"
  31. "</script>"
  32. "</body>"
  33. "</html>";
  34.  
  35. void setup()
  36. {
  37. Serial.begin(9600);
  38. setupWiFi();
  39.  
  40. pinMode(LED_BUILTIN, OUTPUT);
  41. digitalWrite(LED_BUILTIN,LOW);
  42.  
  43. server.on("/",sendIndex);
  44. server.on("/toggle", toggleLED);
  45. server.on("/buzz", buzzBuzzer);
  46. server.begin();
  47. Serial.println("");
  48. Serial.println("");
  49. Serial.print("Server running on http://192.168.4.1/");
  50. }
  51.  
  52. void loop()
  53. {
  54. server.handleClient();
  55. }
  56.  
  57. void sendIndex(){
  58. server.send(200,"text/html",INDEX_HTML);
  59. }
  60.  
  61. void toggleLED(){
  62. digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
  63. //server.send(204,"");
  64. server.send(200,"text/plain","Toggle!\n");
  65. }
  66.  
  67. void buzzBuzzer(){
  68. //tone( pin number, frequency in hertz, duration in milliseconds);
  69. tone(buzzerPin,1300,500);
  70. delay(500);
  71. digitalWrite(buzzerPin,LOW);
  72. server.send(200,"text/plain","Buzz!\n");
  73. }
  74.  
  75. void setupWiFi()
  76. {
  77. WiFi.mode(WIFI_AP);
  78. WiFi.softAP(ESP8266_SSID, ESP8266_PASSWORD);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement