Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. /*********
  2.   Rui Santos
  3.   Complete project details at http://randomnerdtutorials.com  
  4. *********/
  5.  
  6. #include <WiFi.h>
  7. #include <Servo.h>
  8.  
  9. Servo myservo;  // create servo object to control a servo
  10. // twelve servo objects can be created on most boards
  11.  
  12. // GPIO the servo is attached to
  13. static const int servoPin = 13;
  14.  
  15. // Replace with your network credentials
  16. const char* ssid     = "REPLACE_WITH_YOUR_SSID";
  17. const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  18.  
  19. // Set web server port number to 80
  20. WiFiServer server(80);
  21.  
  22. // Variable to store the HTTP request
  23. String header;
  24.  
  25. // Decode HTTP GET value
  26. String valueString = String(5);
  27. int pos1 = 0;
  28. int pos2 = 0;
  29.  
  30. void setup() {
  31.   Serial.begin(115200);
  32.  
  33.   myservo.attach(servoPin);  // attaches the servo on the servoPin to the servo object
  34.  
  35.   // Connect to Wi-Fi network with SSID and password
  36.   Serial.print("Connecting to ");
  37.   Serial.println(ssid);
  38.   WiFi.begin(ssid, password);
  39.   while (WiFi.status() != WL_CONNECTED) {
  40.     delay(500);
  41.     Serial.print(".");
  42.   }
  43.   // Print local IP address and start web server
  44.   Serial.println("");
  45.   Serial.println("WiFi connected.");
  46.   Serial.println("IP address: ");
  47.   Serial.println(WiFi.localIP());
  48.   server.begin();
  49. }
  50.  
  51. void loop(){
  52.   WiFiClient client = server.available();   // Listen for incoming clients
  53.  
  54.   if (client) {                             // If a new client connects,
  55.     Serial.println("New Client.");          // print a message out in the serial port
  56.     String currentLine = "";                // make a String to hold incoming data from the client
  57.     while (client.connected()) {            // loop while the client's connected
  58.       if (client.available()) {             // if there's bytes to read from the client,
  59.         char c = client.read();             // read a byte, then
  60.         Serial.write(c);                    // print it out the serial monitor
  61.         header += c;
  62.         if (c == '\n') {                    // if the byte is a newline character
  63.           // if the current line is blank, you got two newline characters in a row.
  64.           // that's the end of the client HTTP request, so send a response:
  65.           if (currentLine.length() == 0) {
  66.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  67.             // and a content-type so the client knows what's coming, then a blank line:
  68.             client.println("HTTP/1.1 200 OK");
  69.             client.println("Content-type:text/html");
  70.             client.println("Connection: close");
  71.             client.println();
  72.  
  73.             // Display the HTML web page
  74.             client.println("<!DOCTYPE html><html>");
  75.             client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  76.             client.println("<link rel=\"icon\" href=\"data:,\">");
  77.             // CSS to style the on/off buttons
  78.             // Feel free to change the background-color and font-size attributes to fit your preferences
  79.             client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial; margin-left:auto; margin-right:auto;}");
  80.             client.println(".slider { width: 300px; }</style>");
  81.             client.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>");
  82.                      
  83.             // Web Page
  84.             client.println("</head><body><h1>ESP32 with Servo</h1>");
  85.             client.println("<p>Position: <span id=\"servoPos\"></span></p>");          
  86.             client.println("<input type=\"range\" min=\"0\" max=\"180\" class=\"slider\" id=\"servoSlider\" onchange=\"servo(this.value)\" value=\""+valueString+"\"/>");
  87.            
  88.             client.println("<script>var slider = document.getElementById(\"servoSlider\");");
  89.             client.println("var servoP = document.getElementById(\"servoPos\"); servoP.innerHTML = slider.value;");
  90.             client.println("slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }");
  91.             client.println("$.ajaxSetup({timeout:1000}); function servo(pos) { ");
  92.             client.println("$.get(\"/?value=\" + pos + \"&\"); {Connection: close};}</script>");
  93.            
  94.             client.println("</body></html>");    
  95.            
  96.             //GET /?value=180& HTTP/1.1
  97.             if(header.indexOf("GET /?value=")>=0) {
  98.               pos1 = header.indexOf('=');
  99.               pos2 = header.indexOf('&');
  100.               valueString = header.substring(pos1+1, pos2);
  101.              
  102.               //Rotate the servo
  103.               myservo.write(valueString.toInt());
  104.               Serial.println(valueString);
  105.             }        
  106.             // The HTTP response ends with another blank line
  107.             client.println();
  108.             // Break out of the while loop
  109.             break;
  110.           } else { // if you got a newline, then clear currentLine
  111.             currentLine = "";
  112.           }
  113.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  114.           currentLine += c;      // add it to the end of the currentLine
  115.         }
  116.       }
  117.     }
  118.     // Clear the header variable
  119.     header = "";
  120.     // Close the connection
  121.     client.stop();
  122.     Serial.println("Client disconnected.");
  123.     Serial.println("");
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement