Guest User

Untitled

a guest
Jul 3rd, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.69 KB | None | 0 0
  1. #include <WiFiEsp.h>
  2. #include <WiFiEspClient.h>
  3. #include <WiFiEspServer.h>
  4. #include <WiFiEspUdp.h>
  5. #include <Servo.h>
  6. #include <Wire.h>
  7. #include <Adafruit_MotorShield.h>
  8. #include "utility/Adafruit_MS_PWMServoDriver.h"
  9.  
  10. /*
  11.  WiFiEsp example: WebServerLed
  12.  
  13.  A simple web server that lets you turn on and of an LED via a web page.
  14.  This sketch will print the IP address of your ESP8266 module (once connected)
  15.  to the Serial monitor. From there, you can open that address in a web browser
  16.  to turn on and off the LED on pin 13.
  17.  
  18.  For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
  19. */
  20.  
  21.  
  22. // Emulate Serial1 on pins 6/7 if not present
  23. #ifndef HAVE_HWSERIAL1
  24. #include <SoftwareSerial.h>
  25. SoftwareSerial Serial2(17, 16); // RX, TX
  26.  
  27. #endif
  28.  
  29. char ssid[] = "SSID";            // your network SSID (name)
  30. char pass[] = "password";        // your network password
  31. int status = WL_IDLE_STATUS;
  32.  
  33. int ledStatusBLUE = LOW;
  34. int ledStatusRED = LOW;
  35. int ledStatusYELLOW = LOW;
  36. int ledStatusGREEN = LOW;
  37. int BLUE_LED = 6;
  38. int RED_LED = 5;
  39. int YELLOW_LED = 4;
  40. int GREEN_LED = 3;
  41.  
  42.  
  43. WiFiEspServer server(80);
  44.  
  45. Servo servo1;
  46. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  47. Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
  48. Adafruit_DCMotor *myMotor2 = AFMS.getMotor(3);
  49. int pos = 0;
  50.  
  51. // use a ring buffer to increase speed and reduce memory allocation
  52. RingBuffer buf(8);
  53.  
  54. void setup()
  55. {
  56.   pinMode(BLUE_LED, OUTPUT);  // initialize digital pin LED_BUILTIN as an output.
  57.   pinMode(RED_LED, OUTPUT);
  58.   pinMode(YELLOW_LED, OUTPUT);
  59.   pinMode(GREEN_LED, OUTPUT);
  60.   Serial.begin(115200);   // initialize serial for debugging
  61.   Serial2.begin(115200);    // initialize serial for ESP module
  62.   WiFi.init(&Serial2);    // initialize ESP module
  63.   AFMS.begin();
  64.  
  65.   // check for the presence of the shield
  66.   if (WiFi.status() == WL_NO_SHIELD) {
  67.     Serial.println("WiFi shield not present");
  68.     // don't continue
  69.     while (true);
  70.   }
  71.  
  72.   // attempt to connect to WiFi network
  73.   while (status != WL_CONNECTED) {
  74.     Serial.print("Attempting to connect to WPA SSID: ");
  75.     Serial.println(ssid);
  76.     // Connect to WPA/WPA2 network
  77.     status = WiFi.begin(ssid, pass);
  78.   }
  79.  
  80.   Serial.println("You're connected to the network");
  81.   printWifiStatus();
  82.   
  83.   // start the web server on port 80
  84.   server.begin();
  85.  
  86.   servo1.attach(9);
  87.   servo1.write(90);
  88.  
  89.   myMotor1->setSpeed(150);
  90.   //myMotor1->run(FORWARD);
  91.   // turn on motor
  92.   //myMotor1->run(RELEASE);
  93. }
  94.  
  95.  
  96. void loop()
  97. {
  98.   WiFiEspClient client = server.available();  // listen for incoming clients
  99.  
  100.   if (client) {                               // if you get a client,
  101.     Serial.println("New client");             // print a message out the serial port
  102.     buf.init();                               // initialize the circular buffer
  103.     while (client.connected()) {              // loop while the client's connected
  104.       if (client.available()) {               // if there's bytes to read from the client,
  105.         char c = client.read();               // read a byte, then
  106.         buf.push(c);                          // push it to the ring buffer
  107.  
  108.         // printing the stream to the serial monitor will slow down
  109.         // the receiving of data from the ESP filling the serial buffer
  110.         //Serial.write(c);
  111.         
  112.         // you got two newline characters in a row
  113.         // that's the end of the HTTP request, so send a response
  114.         if (buf.endsWith("\r\n\r\n")) {
  115.           sendHttpResponse(client);
  116.           break;
  117.         }
  118.  
  119.         // Check to see if the client request was "GET /H" or "GET /L":
  120.         if (buf.endsWith("GET /HB")) {
  121.           Serial.println("Turn led ON");
  122.           ledStatusBLUE = HIGH;
  123.           digitalWrite(BLUE_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  124.         }
  125.         else if (buf.endsWith("GET /LB")) {
  126.           Serial.println("Turn led OFF");
  127.           ledStatusBLUE = LOW;
  128.           digitalWrite(BLUE_LED, LOW);    // turn the LED off by making the voltage LOW
  129.         }
  130.         else if (buf.endsWith("GET /HR")) {
  131.           Serial.println("Turn led ON");
  132.           ledStatusRED = HIGH;
  133.           digitalWrite(RED_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  134.         }
  135.         else if (buf.endsWith("GET /LR")) {
  136.           Serial.println("Turn led OFF");
  137.           ledStatusRED = LOW;
  138.           digitalWrite(RED_LED, LOW);    // turn the LED off by making the voltage LOW
  139.         }
  140.         else if (buf.endsWith("GET /HY")) {
  141.           Serial.println("Turn led ON");
  142.           ledStatusYELLOW = HIGH;
  143.           digitalWrite(YELLOW_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  144.         }
  145.         else if (buf.endsWith("GET /LY")) {
  146.           Serial.println("Turn led OFF");
  147.           ledStatusYELLOW = LOW;
  148.           digitalWrite(YELLOW_LED, LOW);    // turn the LED off by making the voltage LOW
  149.         }
  150.         else if (buf.endsWith("GET /HG")) {
  151.           Serial.println("Turn led ON");
  152.           ledStatusGREEN = HIGH;
  153.           digitalWrite(GREEN_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  154.         }
  155.         else if (buf.endsWith("GET /LG")) {
  156.           Serial.println("Turn led OFF");
  157.           ledStatusGREEN = LOW;
  158.           digitalWrite(GREEN_LED, LOW);    // turn the LED off by making the voltage LOW
  159.         }/* else if (buf.endsWith("GET /SR")) { //Turning the servo to the right
  160.             while (pos < 180) {
  161.               servo1.write(pos++);
  162.               delay(5);
  163.             }
  164.         } else if (buf.endsWith("GET /SL")) {   //Just testing turning the servo to the left
  165.             while(pos > 3) {
  166.             servo1.write(pos--);
  167.             delay(5);
  168.             }
  169.         }*/ /*else if (buf.endsWith("GET /M")) {  //this command moves the DC motor forward
  170.             myMotor1->run(FORWARD);
  171.           } else if (buf.endsWith("GET /MS")) { //this command moves the DC motor backwards
  172.             myMotor1->run(BACKWARD);
  173.           }*/
  174.         }
  175.       }
  176.               
  177.       // close the connection
  178.       client.stop();
  179.       Serial.println("Client disconnected");
  180.     }
  181. }
  182.  
  183.  
  184. void sendHttpResponse(WiFiEspClient client)
  185. {
  186.   // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  187.   // and a content-type so the client knows what's coming, then a blank line:
  188.   client.println("HTTP/1.1 200 OK");
  189.   client.println("Content-type:text/html");
  190.   client.println();
  191.   
  192.   
  193.   // the content of the HTTP response follows the header:
  194.  
  195.   //client.println("<script src=\"keypress.js\"></script>");
  196.  
  197.   
  198.   client.println("<br><br>Click <a href=\"/HB\">here</a> turn the LED on<br>Click <a href=\"/LB\">here</a> turn the LED off<br>Click <a href=\"/HR\">here</a> turn the LED on<br>Click <a href=\"/LR\">here</a> turn the LED off<br>");
  199.   client.println("Click <a href=\"/HY\">here</a> turn the LED on<br>Click <a href=\"/LY\">here</a> turn the LED off<br>Click <a href=\"/HG\">here</a> turn the LED on<br>Click <a href=\"/LG\">here</a> turn the LED off<br>");
  200.   //client.println("Click <a href=\"/HR\">here</a> turn the LED on<br>Click <a href=\"/LR\">here</a> turn the LED off<br>");
  201.   //client.println("Click <a href=\"/HY\">here</a> turn the LED on<br>Click <a href=\"/LY\">here</a> turn the LED off<br>");
  202.   //client.println("Click <a href=\"/HG\">here</a> turn the LED on<br>Click <a href=\"/LG\">here</a> turn the LED off<br>");
  203.   //client.println();
  204.   //client.println("Click <a href=\"/SR\">here</a> to turn servo right<br>");
  205.   //client.println("Click <a href=\"/SL\">here</a> to turn servo left<br>");
  206.   //client.println("Click <a href=\"/M\">here</a> turn motor<br>");
  207.   //client.println("Click <a href=\"/MS\">here</a> stop motor<br>");
  208.   
  209.   // The HTTP response ends with another blank line:
  210.   client.println();
  211. }
  212.  
  213. void printWifiStatus()
  214. {
  215.   // print the SSID of the network you're attached to
  216.   Serial.print("SSID: ");
  217.   Serial.println(WiFi.SSID());
  218.  
  219.   // print your WiFi shield's IP address
  220.   IPAddress ip = WiFi.localIP();
  221.   Serial.print("IP Address: ");
  222.   Serial.println(ip);
  223.  
  224.   // print where to go in the browser
  225.   Serial.println();
  226.   Serial.print("To see this page in action, open a browser to http://");
  227.   Serial.println(ip);
  228.   Serial.println();
  229. }
Advertisement
Add Comment
Please, Sign In to add comment