hanpa

Control Wemos D1 mini with Relay Shield from web page

May 19th, 2017
3,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. // Turn ON Wemos D1 R2 Relay shield and LED using http://192.168.0.160/LED=ON
  4. // Turn OFF Wemos D1 R2 Relay shield and LED using http://192.168.0.160/LED=OFF
  5. // Or use buttons on the web page from the IP address used
  6. // See below regarding not using static IP
  7.  
  8. const char* ssid = "SSID";
  9. const char* password = "password";
  10.  
  11. const int ledPin = 2; // GPIO2
  12. const int relayPin = D1;
  13.  
  14. // Remove/comment these three and the WiFi.begin row if static IP address is not used
  15. IPAddress ip(192, 168, 0, 160);
  16. IPAddress gateway(192, 168, 0, 1);
  17. IPAddress subnet(255, 255, 255, 0);
  18.  
  19. WiFiServer server(80);
  20.  
  21. void setup() {
  22.   Serial.begin(115200);
  23.   delay(10);
  24.  
  25.   pinMode(ledPin, OUTPUT);
  26.   pinMode(relayPin, OUTPUT);
  27.  
  28.   digitalWrite(ledPin, HIGH);   // turn off LED with voltage HIGH
  29.   digitalWrite(relayPin, LOW);  // turn off relay with voltage LOW
  30.  
  31.   // Connect to WiFi network
  32.   Serial.println();
  33.   Serial.println();
  34.   Serial.print("Connecting to ");
  35.   Serial.println(ssid);
  36.  
  37.   WiFi.config(ip, gateway, subnet); // Remove/comment if static IP is not used
  38.  
  39.   WiFi.begin(ssid, password);
  40.  
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.   Serial.println("");
  46.   Serial.println("WiFi connected");
  47.  
  48.   // Start the server
  49.   server.begin();
  50.   Serial.println("Server started");
  51.  
  52.   // Print the IP address
  53.   Serial.print("Use this URL to connect: ");
  54.   Serial.print("http://");
  55.   Serial.print(WiFi.localIP());
  56.   Serial.println("/");
  57.  
  58. }
  59.  
  60. void loop() {
  61.   // Check if a client has connected
  62.   WiFiClient client = server.available();
  63.   if (!client) {
  64.     return;
  65.   }
  66.  
  67.   // Wait until the client sends some data
  68.   Serial.println("new client");
  69.   while(!client.available()){
  70.     delay(1);
  71.   }
  72.  
  73.   // Read the first line of the request
  74.   String request = client.readStringUntil('\r');
  75.   Serial.println(request);
  76.   client.flush();
  77.  
  78.   // Match the request
  79.  
  80.   int value = HIGH;
  81.   if (request.indexOf("/LED=ON") != -1)  {
  82.     digitalWrite(ledPin, LOW);    // turn on LED with voltage LOW
  83.     digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
  84.     value = LOW;
  85.   }
  86.   if (request.indexOf("/LED=OFF") != -1)  {
  87.     digitalWrite(ledPin, HIGH);   // turn off LED with voltage HIGH
  88.     digitalWrite(relayPin, LOW);  // turn off relay with voltage LOW
  89.     value = HIGH;
  90.   }
  91.  
  92.   // Return the response
  93.   client.println("HTTP/1.1 200 OK");
  94.   client.println("Content-Type: text/html");
  95.   client.println(""); //  do not forget this one
  96.   client.println("<!DOCTYPE HTML>");
  97.   client.println("<html>");
  98.  
  99.   client.print("Led/relay pin is now: ");
  100.  
  101.   if(value == LOW) {
  102.     client.print("On");
  103.   } else {
  104.     client.print("Off");
  105.   }
  106.   client.println("<br><br>");
  107.   client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  108.   client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
  109.   client.println("</html>");
  110.  
  111.   delay(1);
  112.   Serial.println("Client disonnected");
  113.   Serial.println("");
  114. }
Advertisement
Add Comment
Please, Sign In to add comment