Advertisement
roshan12121212

Untitled

Aug 21st, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <WiFi.h>
  2.  
  3. const char* ssid = "yourssid";
  4. const char* password = "yourpasswd";
  5.  
  6. WiFiServer server(80);
  7.  
  8. void setup()
  9. {
  10. Serial.begin(115200);
  11. pinMode(5, OUTPUT); // set the LED pin mode
  12.  
  13. delay(10);
  14.  
  15. // We start by connecting to a WiFi network
  16.  
  17. Serial.println();
  18. Serial.println();
  19. Serial.print("Connecting to ");
  20. Serial.println(ssid);
  21.  
  22. WiFi.begin(ssid, password);
  23.  
  24. while (WiFi.status() != WL_CONNECTED) {
  25. delay(500);
  26. Serial.print(".");
  27. }
  28.  
  29. Serial.println("");
  30. Serial.println("WiFi connected.");
  31. Serial.println("IP address: ");
  32. Serial.println(WiFi.localIP());
  33.  
  34. server.begin();
  35.  
  36. }
  37.  
  38. void loop(){
  39. WiFiClient client = server.available(); // listen for incoming clients
  40.  
  41. if (client) { // if you get a client,
  42. Serial.println("New Client."); // print a message out the serial port
  43. String currentLine = ""; // make a String to hold incoming data from the client
  44. while (client.connected()) { // loop while the client's connected
  45. if (client.available()) { // if there's bytes to read from the client,
  46. char c = client.read(); // read a byte, then
  47. Serial.write(c); // print it out the serial monitor
  48. if (c == '\n') { // if the byte is a newline character
  49.  
  50. // if the current line is blank, you got two newline characters in a row.
  51. // that's the end of the client HTTP request, so send a response:
  52. if (currentLine.length() == 0) {
  53. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  54. // and a content-type so the client knows what's coming, then a blank line:
  55. client.println("HTTP/1.1 200 OK");
  56. client.println("Content-type:text/html");
  57. client.println();
  58.  
  59. // the content of the HTTP response follows the header:
  60. client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");
  61. client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");
  62.  
  63. // The HTTP response ends with another blank line:
  64. client.println();
  65. // break out of the while loop:
  66. break;
  67. } else { // if you got a newline, then clear currentLine:
  68. currentLine = "";
  69. }
  70. } else if (c != '\r') { // if you got anything else but a carriage return character,
  71. currentLine += c; // add it to the end of the currentLine
  72. }
  73.  
  74. // Check to see if the client request was "GET /H" or "GET /L":
  75. if (currentLine.endsWith("GET /H")) {
  76. digitalWrite(5, HIGH); // GET /H turns the LED on
  77. }
  78. if (currentLine.endsWith("GET /L")) {
  79. digitalWrite(5, LOW); // GET /L turns the LED off
  80. }
  81. }
  82. }
  83. // close the connection:
  84. client.stop();
  85. Serial.println("Client Disconnected.");
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement