Advertisement
safwan092

Untitled

Dec 14th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <WiFi101.h>
  3.  
  4. char ssid[] = "network"; // your network SSID (name) between the " "
  5. char pass[] = "123456789"; // your network password between the " "
  6. int keyIndex = 0; // your network key Index number (needed only for WEP)
  7. int status = WL_IDLE_STATUS; //connection status
  8. WiFiServer server(80); //server socket
  9.  
  10. WiFiClient client = server.available();
  11.  
  12. int ledPin = 2;
  13.  
  14. void setup() {
  15. Serial.begin(9600);
  16. pinMode(ledPin, OUTPUT);
  17. while (!Serial);
  18.  
  19. enable_WiFi();
  20. connect_WiFi();
  21.  
  22. server.begin();
  23. printWifiStatus();
  24. }
  25.  
  26. void loop() {
  27. client = server.available();
  28.  
  29. if (client) {
  30. printWEB();
  31. }
  32. }
  33.  
  34. void printWifiStatus() {
  35. // print the SSID of the network you're attached to:
  36. Serial.print("SSID: ");
  37. Serial.println(WiFi.SSID());
  38.  
  39. // print your board's IP address:
  40. IPAddress ip = WiFi.localIP();
  41. Serial.print("IP Address: ");
  42. Serial.println(ip);
  43.  
  44. // print the received signal strength:
  45. long rssi = WiFi.RSSI();
  46. Serial.print("signal strength (RSSI):");
  47. Serial.print(rssi);
  48. Serial.println(" dBm");
  49.  
  50. Serial.print("To see this page in action, open a browser to http://");
  51. Serial.println(ip);
  52. }
  53.  
  54. void enable_WiFi() {
  55.  
  56. String fv = WiFi.firmwareVersion();
  57. if (fv < "1.0.0") {
  58. Serial.println("Please upgrade the firmware");
  59. }
  60. }
  61.  
  62. void connect_WiFi() {
  63. // attempt to connect to Wifi network:
  64. while (status != WL_CONNECTED) {
  65. Serial.print("Attempting to connect to SSID: ");
  66. Serial.println(ssid);
  67. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  68. status = WiFi.begin(ssid, pass);
  69.  
  70. // wait 10 seconds for connection:
  71. delay(10000);
  72. }
  73. }
  74.  
  75. void printWEB() {
  76.  
  77. if (client) { // if you get a client,
  78. Serial.println("new client"); // print a message out the serial port
  79. String currentLine = ""; // make a String to hold incoming data from the client
  80. while (client.connected()) { // loop while the client's connected
  81. if (client.available()) { // if there's bytes to read from the client,
  82. char c = client.read(); // read a byte, then
  83. Serial.write(c); // print it out the serial monitor
  84. if (c == '\n') { // if the byte is a newline character
  85.  
  86. // if the current line is blank, you got two newline characters in a row.
  87. // that's the end of the client HTTP request, so send a response:
  88. if (currentLine.length() == 0) {
  89.  
  90. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  91. // and a content-type so the client knows what's coming, then a blank line:
  92. client.println("HTTP/1.1 200 OK");
  93. client.println("Content-type:text/html");
  94. client.println();
  95.  
  96. //create the buttons
  97. client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
  98. client.print("Click <a href=\"/L\">here</a> turn the LED off<br><br>");
  99.  
  100. int randomReading = analogRead(A1);
  101. client.print("Random reading from analog pin: ");
  102. client.print(randomReading);
  103.  
  104.  
  105.  
  106.  
  107. // The HTTP response ends with another blank line:
  108. client.println();
  109. // break out of the while loop:
  110. break;
  111. }
  112. else { // if you got a newline, then clear currentLine:
  113. currentLine = "";
  114. }
  115. }
  116. else if (c != '\r') { // if you got anything else but a carriage return character,
  117. currentLine += c; // add it to the end of the currentLine
  118. }
  119.  
  120. if (currentLine.endsWith("GET /H")) {
  121. digitalWrite(ledPin, HIGH);
  122. }
  123. if (currentLine.endsWith("GET /L")) {
  124. digitalWrite(ledPin, LOW);
  125. }
  126.  
  127. }
  128. }
  129. // close the connection:
  130. client.stop();
  131. Serial.println("client disconnected");
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement