Advertisement
Guest User

Dual LED Monitor

a guest
Jan 25th, 2017
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "MIND CONTROL BG";
  4. const char* password = "P@55w0rd";
  5.  
  6. int ledPin = 13; // GPIO13
  7. int ledPin2 = 12; // GPIO12
  8. WiFiServer server(80);
  9.  
  10. void setup() {
  11.   Serial.begin(115200);
  12.   delay(10);
  13.  
  14.   pinMode(ledPin, OUTPUT);
  15.   digitalWrite(ledPin, LOW);
  16.   pinMode(ledPin2, OUTPUT);
  17.   digitalWrite(ledPin2, LOW);
  18.  
  19.   // Connect to WiFi network
  20.   Serial.println();
  21.   Serial.println();
  22.   Serial.print("Connecting to ");
  23.   Serial.println(ssid);
  24.  
  25.   WiFi.begin(ssid, password);
  26.  
  27.   while (WiFi.status() != WL_CONNECTED) {
  28.     delay(500);
  29.     Serial.print(".");
  30.   }
  31.   Serial.println("");
  32.   Serial.println("WiFi connected");
  33.  
  34.   // Start the server
  35.   server.begin();
  36.   Serial.println("Server started");
  37.  
  38.   // Print the IP address
  39.   Serial.print("Use this URL to connect: ");
  40.   Serial.print("http://");
  41.   Serial.print(WiFi.localIP());
  42.   Serial.println("/");
  43.  
  44. }
  45.  
  46. void loop() {
  47.   // Check if a client has connected
  48.   WiFiClient client = server.available();
  49.   if (!client) {
  50.     return;
  51.   }
  52.  
  53.   // Wait until the client sends some data
  54.   Serial.println("new client");
  55.   while(!client.available()){
  56.     delay(1);
  57.   }
  58.  
  59.   // Read the first line of the request
  60.   String request = client.readStringUntil('\r');
  61.   Serial.println(request);
  62.   client.flush();
  63.  
  64.   // Match the request
  65.  
  66.   int value = LOW;
  67.   if (request.indexOf("/LED=ON") != -1)  {
  68.     digitalWrite(ledPin, HIGH);
  69.     value = HIGH;
  70.   }
  71.   if (request.indexOf("/LED=OFF") != -1)  {
  72.     digitalWrite(ledPin, LOW);
  73.     value = LOW;
  74.   }
  75.   int value2 = LOW;
  76.   if (request.indexOf("/LED2=ON") != -1)  {
  77.     digitalWrite(ledPin2, HIGH);
  78.     value2 = HIGH;
  79.   }
  80.   if (request.indexOf("/LED2=OFF") != -1)  {
  81.     digitalWrite(ledPin2, LOW);
  82.     value2 = LOW;
  83.   }
  84.  
  85. // Set ledPin according to the request
  86. //digitalWrite(ledPin, value);
  87.  
  88.   // Return the response
  89.   client.println("HTTP/1.1 200 OK");
  90.   client.println("Content-Type: text/html");
  91.   client.println(""); //  do not forget this one
  92.   client.println("<!DOCTYPE HTML>");
  93.   client.println("<html>");
  94.  
  95.   client.print("Led1 pin is now: ");
  96.  
  97.   if(value == HIGH) {
  98.     client.print("On");
  99.   } else {
  100.     client.print("Off");
  101.   }
  102.  
  103.  
  104.   client.println("<br><br>");
  105.   client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  106.   client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
  107.   client.println("<br><br>");
  108.  
  109.  
  110.   client.print("Led2 pin is now: ");
  111.  
  112.   if(value2 == HIGH) {
  113.     client.print("On");
  114.   } else {
  115.     client.print("Off");
  116.   }
  117.  
  118.   client.println("<br><br>");
  119.   client.println("<a href=\"/LED2=ON\"\"><button>Turn On </button></a>");
  120.   client.println("<a href=\"/LED2=OFF\"\"><button>Turn Off </button></a><br />");
  121.   client.println("</html>");
  122.  
  123.   delay(1);
  124.   Serial.println("Client disonnected");
  125.   Serial.println("");
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement