SHARE
TWEET

ESP8266 WiFi Relay Control

learnelectronics Jun 6th, 2017 14,964 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * ESP8266 (WeMosD1) WiFi Relay Control
  3.  *
  4.  * learnelectronics
  5.  * 05 JUN 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  * arduino0169@gmail.com
  9. */
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. #include <ESP8266WiFi.h>
  17.  
  18. const char* ssid = "xxxxxxxxxxxxx";
  19. const char* password = "zzzzzzzzzzzzzz";
  20.  
  21. int ledPin = D5;
  22. WiFiServer server(80);
  23.  
  24. void setup() {
  25.   Serial.begin(9600);
  26.   delay(10);
  27.  
  28.  
  29.   pinMode(ledPin, OUTPUT);
  30.   digitalWrite(ledPin, LOW);
  31.  
  32.   // Connect to WiFi network
  33.   Serial.println();
  34.   Serial.println();
  35.   Serial.print("Connecting to ");
  36.   Serial.println(ssid);
  37.  
  38.   WiFi.begin(ssid, password);
  39.  
  40.   while (WiFi.status() != WL_CONNECTED) {
  41.     delay(500);
  42.     Serial.print(".");
  43.   }
  44.   Serial.println("");
  45.   Serial.println("WiFi connected");
  46.  
  47.   // Start the server
  48.   server.begin();
  49.   Serial.println("Server started");
  50.  
  51.   // Print the IP address
  52.   Serial.print("Use this URL : ");
  53.   Serial.print("http://");
  54.   Serial.print(WiFi.localIP());
  55.   Serial.println("/");
  56.  
  57. }
  58.  
  59. void loop() {
  60.   // Check if a client has connected
  61.   WiFiClient client = server.available();
  62.   if (!client) {
  63.     return;
  64.   }
  65.  
  66.   // Wait until the client sends some data
  67.   Serial.println("new client");
  68.   while(!client.available()){
  69.     delay(1);
  70.   }
  71.  
  72.   // Read the first line of the request
  73.   String request = client.readStringUntil('\r');
  74.   Serial.println(request);
  75.   client.flush();
  76.  
  77.   // Match the request
  78.  
  79.   int value = LOW;
  80.   if (request.indexOf("/LED=ON") != -1) {
  81.     digitalWrite(ledPin, HIGH);
  82.     value = HIGH;
  83.   }
  84.   if (request.indexOf("/LED=OFF") != -1){
  85.     digitalWrite(ledPin, LOW);
  86.     value = LOW;
  87.   }
  88.  
  89.  
  90.  
  91.   // Return the response
  92.   client.println("HTTP/1.1 200 OK");
  93.   client.println("Content-Type: text/html");
  94.   client.println(""); //  do not forget this one
  95.   client.println("<!DOCTYPE HTML>");
  96.   client.println("<html>");
  97.  
  98.   client.print("Led pin is now: ");
  99.  
  100.   if(value == HIGH) {
  101.     client.print("On");  
  102.   } else {
  103.     client.print("Off");
  104.   }
  105.   client.println("<br><br>");
  106.   client.println("Click <a href=\"/LED=ON\">here</a> Turn relay ON<br>");
  107.   client.println("Click <a href=\"/LED=OFF\">here</a> Turn relay OFF<br>");
  108.   client.println("</html>");
  109.  
  110.   delay(1);
  111.   Serial.println("Client disconnected");
  112.   Serial.println("");
  113.  
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top