Advertisement
Guest User

Untitled

a guest
Nov 1st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  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. const char* ssid = "TP-LINK_113";
  13. const char* password = "vladik2003";
  14.  
  15. // Create an instance of the server
  16. // specify the port to listen on as an argument
  17. WiFiServer server(80);
  18.  
  19. void setup() {
  20. Serial.begin(115200);
  21. delay(10);
  22.  
  23. // prepare GPIO2
  24. pinMode(2, OUTPUT);
  25. pinMode(15, OUTPUT);
  26. digitalWrite(2, 0);
  27. digitalWrite(15,0);
  28.  
  29. // Connect to WiFi network
  30. Serial.println();
  31. Serial.println();
  32. Serial.print("Connecting to ");
  33. Serial.println(ssid);
  34.  
  35. WiFi.begin(ssid, password);
  36.  
  37. while (WiFi.status() != WL_CONNECTED) {
  38. delay(500);
  39. Serial.print(".");
  40. }
  41. Serial.println("");
  42. Serial.println("WiFi connected");
  43.  
  44. // Start the server
  45. server.begin();
  46. Serial.println("Server started");
  47.  
  48. // Print the IP address
  49. Serial.println(WiFi.localIP());
  50. }
  51.  
  52. void loop() {
  53. // Check if a client has connected
  54. WiFiClient client = server.available();
  55. if (!client) {
  56. return;
  57. }
  58.  
  59. // Wait until the client sends some data
  60. Serial.println("new client");
  61. while(!client.available()){
  62. delay(1);
  63. }
  64.  
  65. // Read the first line of the request
  66. String req = client.readStringUntil('\r');
  67. Serial.println(req);
  68. client.flush();
  69.  
  70. // Match the request
  71. int val, val2;
  72. if (req.indexOf("/gpio/0") != -1)
  73. val = 0;
  74. else if (req.indexOf("/gpio/1") != -1)
  75. val = 1;
  76. else if(req.indexOf("/gpio/2") != -1)
  77. val2 = 0;
  78. else if(req.indexOf("/gpio/3") != -1)
  79. val2 = 1;
  80.  
  81.  
  82. else {
  83. Serial.println("invalid request");
  84. client.stop();
  85. return;
  86. }
  87.  
  88. // Set GPIO2 according to the request
  89. digitalWrite(2, val);
  90. digitalWrite(15, val2);
  91.  
  92. client.flush();
  93.  
  94. // Prepare the response
  95. String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  96. s += (val)?"high":"low";
  97. s += "</html>\n";
  98.  
  99. // Send the response to the client
  100. client.print(s);
  101. delay(1);
  102. Serial.println("Client disonnected");
  103.  
  104. // The client will actually be disconnected
  105. // when the function returns and 'client' object is detroyed
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement