Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <WiFi.h>
  2.  
  3. const char* ssid = "ezff";
  4. const char* password = "password";
  5.  
  6. const uint8_t led = 2;
  7.  
  8. WiFiServer server(10000);
  9.  
  10. void setup() {
  11.     Serial.begin(115200);
  12.     WiFi.mode(WIFI_AP);
  13.     WiFi.softAP(ssid, password);
  14.  
  15.     pinMode(led, OUTPUT);      // set the LED pin mode
  16.  
  17.     server.setTimeout(5);
  18.     server.begin();
  19. }
  20.  
  21. void loop() {
  22.     WiFiClient client = server.available();
  23.  
  24.     if (client) {
  25.         unsigned long timer = millis();
  26.         Serial.println("got new client");
  27.         String command = "";
  28.         while (client.connected()) {
  29.             while (client.available()) {
  30.                 char c = client.read();
  31.                 if (c == 0) {
  32.                     goto abort;
  33.                 } else if (c == ';') {
  34.                     command = "";
  35.                 } else {
  36.                     command += c;
  37.                 }
  38.  
  39.                 // Check to see if the client request was "GET /H" or "GET /L":
  40.                 if (command.endsWith("LED(ON)")) {
  41.                     Serial.println("ON");
  42.                     digitalWrite(led, HIGH);
  43.                     command = "";
  44.                 }
  45.                 if (command.endsWith("LED(OFF)")) {
  46.                     Serial.println("OFF");
  47.                     digitalWrite(led, LOW);
  48.                     command = "";
  49.                 }
  50.             }
  51.  
  52.             if (millis() - timer > 1000) {
  53.                 Serial.println("ping");
  54.                 client.write(";", 1);
  55.                 timer = millis();
  56.             }
  57.         }
  58.         abort: Serial.println("closing connection");
  59.         // close the connection:
  60.         client.stop();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement