Guest User

Untitled

a guest
Aug 19th, 2019
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This sketch demonstrates how to set up a simple HTTP-like server.
  3. The server will set a GPIO pin depending on the request
  4. http://server_ip/gpio/0 will set the GPIO2 low,
  5. http://server_ip/gpio/1 will set the GPIO2 high
  6. server_ip is the IP address of the ESP8266 module, will be
  7. printed to Serial when the module is connected.
  8. */
  9.  
  10. #include <ESP8266WiFi.h>
  11.  
  12. #ifndef STASSID
  13. #define STASSID "final1"
  14. #define STAPSK "11111111"
  15. #endif
  16.  
  17. const char* ssid = STASSID;
  18. const char* password = STAPSK;
  19.  
  20. // Create an instance of the server
  21. // specify the port to listen on as an argument
  22. WiFiServer server(80);
  23.  
  24. void setup() {
  25. Serial.begin(115200);
  26.  
  27. // prepare LED
  28. pinMode(LED_BUILTIN, OUTPUT);
  29. digitalWrite(LED_BUILTIN, 0);
  30.  
  31. // Connect to WiFi network
  32. Serial.println();
  33. Serial.println();
  34. Serial.print(F("Connecting to "));
  35. Serial.println(ssid);
  36.  
  37. WiFi.mode(WIFI_STA);
  38. WiFi.begin(ssid, password);
  39.  
  40. while (WiFi.status() != WL_CONNECTED) {
  41. delay(500);
  42. Serial.print(F("."));
  43. }
  44. Serial.println();
  45. Serial.println(F("WiFi connected"));
  46.  
  47. // Start the server
  48. server.begin();
  49. Serial.println(F("Server started"));
  50.  
  51. // Print the IP address
  52. Serial.println(WiFi.localIP());
  53. }
  54.  
  55. void loop() {
  56. // Check if a client has connected
  57. WiFiClient client = server.available();
  58. if (!client) {
  59. return;
  60. }
  61. Serial.println(F("new client"));
  62.  
  63. client.setTimeout(5000); // default is 1000
  64.  
  65. // Read the first line of the request
  66. String req = client.readStringUntil('\r');
  67. Serial.println(F("request: "));
  68. Serial.println(req);
  69.  
  70. // Match the request
  71. int val;
  72. if (req.indexOf(F("/gpio/0")) != -1) {
  73. val = 0;
  74. } else if (req.indexOf(F("/gpio/1")) != -1) {
  75. val = 1;
  76. } else {
  77. Serial.println(F("invalid request"));
  78. val = digitalRead(LED_BUILTIN);
  79. }
  80.  
  81. // Set LED according to the request
  82. digitalWrite(LED_BUILTIN, val);
  83.  
  84. // read/ignore the rest of the request
  85. // do not client.flush(): it is for output only, see below
  86. while (client.available()) {
  87. // byte by byte is not very efficient
  88. client.read();
  89. }
  90.  
  91. // Send the response to the client
  92. // it is OK for multiple small client.print/write,
  93. // because nagle algorithm will group them into one single packet
  94. client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "));
  95. client.print((val) ? F("high") : F("low"));
  96. client.print(F("<br><br>Click <a href='http://"));
  97. client.print(WiFi.localIP());
  98. client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
  99. client.print(WiFi.localIP());
  100. client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));
  101.  
  102. // The client will actually be *flushed* then disconnected
  103. // when the function returns and 'client' object is destroyed (out-of-scope)
  104. // flush = ensure written data are received by the other side
  105. Serial.println(F("Disconnecting from client"));
  106. }
RAW Paste Data