Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "Tri musketara";
  4. const char* password = "Proxy159";
  5.  
  6. int ledPin = D5;
  7. WiFiServer server(80);
  8.  
  9. void setup() {
  10.   Serial.begin(9600);
  11.   delay(10);
  12.  
  13.  
  14.   pinMode(ledPin, OUTPUT);
  15.   digitalWrite(ledPin, LOW);
  16.  
  17.   // Connect to WiFi network
  18.   Serial.println();
  19.   Serial.println();
  20.   Serial.print("Connecting to ");
  21.   Serial.println(ssid);
  22.  
  23.   WiFi.begin(ssid, password);
  24.  
  25.   while (WiFi.status() != WL_CONNECTED) {
  26.     delay(500);
  27.     Serial.print(".");
  28.   }
  29.   Serial.println("");
  30.   Serial.println("WiFi connected");
  31.  
  32.   // Start the server
  33.   server.begin();
  34.   Serial.println("Server started");
  35.  
  36.   // Print the IP address
  37.   Serial.print("Use this URL : ");
  38.   Serial.print("http://");
  39.   Serial.print(WiFi.localIP());
  40.   Serial.println("/");
  41.  
  42. }
  43.  
  44. void loop() {
  45.   // Check if a client has connected
  46.   WiFiClient client = server.available();
  47.   if (!client) {
  48.     return;
  49.   }
  50.  
  51.   // Wait until the client sends some data
  52.   Serial.println("new client");
  53.   while(!client.available()){
  54.     delay(1);
  55.   }
  56.  
  57.   // Read the first line of the request
  58.   String request = client.readStringUntil('\r');
  59.   Serial.println(request);
  60.   client.flush();
  61.  
  62.   // Match the request
  63.  
  64.   int value = LOW;
  65.   if (request.indexOf("/LED=ON") != -1) {
  66.     digitalWrite(ledPin, HIGH);
  67.     value = HIGH;
  68.   }
  69.   if (request.indexOf("/LED=OFF") != -1){
  70.     digitalWrite(ledPin, LOW);
  71.     value = LOW;
  72.   }
  73.  
  74.  
  75.  
  76.   // Return the response
  77.   client.println("HTTP/1.1 200 OK");
  78.   client.println("Content-Type: text/html");
  79.   client.println(""); //  do not forget this one
  80.   client.println("<!DOCTYPE HTML>");
  81.   client.println("<html>");
  82.  
  83.   client.print("Led pin is now: ");
  84.  
  85.   if(value == HIGH) {
  86.     client.print("On");  
  87.   } else {
  88.     client.print("Off");
  89.   }
  90.   client.println("<br><br>");
  91.   client.println("Click <a href=\"/LED=ON\">here</a> Turn relay ON<br>");
  92.   client.println("Click <a href=\"/LED=OFF\">here</a> Turn relay OFF<br>");
  93.   client.println("</html>");
  94.  
  95.   delay(1);
  96.   Serial.println("Client disconnected");
  97.   Serial.println("");
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement