SHARE
TWEET

Untitled

a guest Jan 23rd, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "Nxtkh";
  4. const char* password = "glnf5265";
  5.  
  6. int ledPin = 13; // GPIO13
  7. WiFiServer server(80);
  8. IPAddress IP(192,168,1,1);
  9. IPAddress mask=(255, 255, 255, 0);
  10.  
  11. void setup() {
  12.   Serial.begin(115200);
  13.  
  14.   WiFi.mode(WIFI_AP);
  15.   WiFi.softAPConfig(IP, IP, mask);
  16.   WiFi.softAP("Tokopedia IoT", "tokopedia789");
  17.   delay(1000);
  18.  
  19.   server.begin();
  20.   pinMode(ledPin, OUTPUT);
  21.  
  22.   Serial.println("Server started");
  23.  
  24.   // Print the IP address
  25.   Serial.print("Use this URL to connect: ");
  26.   Serial.print("http://");
  27.   Serial.print(WiFi.localIP());
  28.   Serial.println("/");
  29. }
  30.  
  31. void loop() {
  32.   // Check if a client has connected
  33.   WiFiClient client = server.available();
  34.   if (!client) {
  35.     return;
  36.   }
  37.  
  38.   // Wait until the client sends some data
  39.   Serial.println("new client");
  40.   while(!client.available()){
  41.     delay(1);
  42.   }
  43.  
  44.   // Read the first line of the request
  45.   String request = client.readStringUntil('\r');
  46.   Serial.println(request);
  47.   client.flush();
  48.  
  49.   // Match the request
  50.  
  51.   int value = LOW;
  52.   if (request.indexOf("/LED=ON") != -1)  {
  53.     Serial.println("LED ON");
  54.     digitalWrite(ledPin, HIGH);
  55.     value = HIGH;
  56.   }
  57.   if (request.indexOf("/LED=OFF") != -1)  {
  58.     Serial.println("LED OFF");
  59.     digitalWrite(ledPin, LOW);
  60.     value = LOW;
  61.   }
  62.  
  63. // Set ledPin according to the request
  64. //digitalWrite(ledPin, value);
  65.  
  66.   // Return the response
  67.   client.println("HTTP/1.1 200 OK");
  68.   client.println("Content-Type: text/html");
  69.   client.println(""); //  do not forget this one
  70.   client.println("<!DOCTYPE HTML>");
  71.   client.println("<html>");
  72.  
  73.   client.print("Led pin is now: ");
  74.  
  75.   if(value == HIGH) {
  76.     client.print("On");
  77.   } else {
  78.     client.print("Off");
  79.   }
  80.   client.println("<br><br>");
  81.   client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  82.   client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
  83.   client.println("</html>");
  84.  
  85.   delay(1);
  86.   Serial.println("Client disonnected");
  87.   Serial.println("");
  88.  
  89. }
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