qianzhe

WIFI Tank code test

Dec 19th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.11 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <SabertoothSimplified.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. const char* ssid = "YOURSSIDHERE";//Enter your Wifi SSID/name here.
  6. const char* password = "YOURPASSWORDHERE";//Enter your password for your wifi here.
  7.  
  8. int ledPin = 13; // GPIO13
  9. WiFiServer server(80);
  10. SabertoothSimplified ST;//Declaring the Sabertooth controller for the Arduino. Connect S1 of the Sabertooth to Tx of the Arduino OR
  11. //in this case, connect to the NodeMCU.
  12. char breceived = 'S';
  13. char prevreceived;
  14.  
  15.  
  16. void setup() {
  17.   SabertoothTXPinSerial.begin(9600);//Communication to the Sabertooth controller at 9600.S
  18.   Serial.begin(115200);
  19.   delay(10);
  20.  
  21.  
  22.   pinMode(ledPin, OUTPUT);
  23.   digitalWrite(ledPin, LOW);
  24.  
  25.   // Connect to WiFi network
  26.   Serial.println();
  27.   Serial.println();
  28.   Serial.print("Connecting to ");
  29.   Serial.println(ssid);
  30.  
  31.   WiFi.begin(ssid, password);
  32.  
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     delay(500);
  35.     Serial.print(".");
  36.   }
  37.   Serial.println("");
  38.   Serial.println("WiFi connected");
  39.  
  40.   // Start the server
  41.   server.begin();
  42.   Serial.println("Server started");
  43.  
  44.   // Print the IP address
  45.   Serial.print("Use this URL to connect: ");
  46.   Serial.print("http://");
  47.   Serial.print(WiFi.localIP());
  48.   Serial.println("/");
  49.  
  50. }
  51.  
  52. void loop() {
  53.   // Check if a client has connected
  54.   WiFiClient client = server.available();
  55.   if (!client) {
  56.     return;
  57.   }
  58.  
  59.   // Wait until the client sends some data
  60.   Serial.println("new client");
  61.   while(!client.available()){
  62.     delay(1);
  63.   }
  64.  
  65.   // Read the first line of the request
  66.   String request = client.readStringUntil('\r');
  67.   Serial.println(request);
  68.   client.flush();
  69.  
  70.   // Match the request
  71.  
  72.   int value = LOW;
  73.   if (request.indexOf("/LED=ON") != -1)  {
  74.     digitalWrite(ledPin, HIGH);
  75.     value = HIGH;
  76.   }
  77.   if (request.indexOf("/LED=OFF") != -1)  {
  78.     digitalWrite(ledPin, LOW);
  79.     value = LOW;
  80.   }
  81.   int motorState = 0;
  82.   if (request.indexOf("/MOTOR=FORWARD") != -1)  {
  83.     ST.motor(1,-50);
  84.     ST.motor(2,-50);
  85.     motorState = 1;
  86.    
  87.   }
  88.   if (request.indexOf("/MOTOR=BACKWARD") != -1)  {
  89.     ST.motor(1,50);
  90.     ST.motor(2,50);
  91.     motorState = 2;
  92.   }
  93.   if (request.indexOf("/MOTOR=LEFT") != -1)  {
  94.     ST.motor(1,0);
  95.     ST.motor(2,50);
  96.     motorState = 3;
  97.   }
  98.   if (request.indexOf("/MOTOR=RIGHT") != -1)  {
  99.     ST.motor(1,50);
  100.     ST.motor(2,0);
  101.     motorState = 4;
  102.   }
  103.   if (request.indexOf("/MOTOR=STOP") != -1)  {
  104.     ST.motor(1,0);
  105.     ST.motor(2,0);
  106.     motorState = 0;
  107.   }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. // Set ledPin according to the request
  114. //digitalWrite(ledPin, value);
  115.  
  116.   // Return the response
  117.   client.println("HTTP/1.1 200 OK");
  118.   client.println("Content-Type: text/html");
  119.   client.println(""); //  do not forget this one
  120.   client.println("<!DOCTYPE HTML>");
  121.   client.println("<html>");
  122.  
  123.   client.print("Led pin is now: ");
  124.  
  125.   if(value == HIGH) {
  126.     client.print("On");
  127.   } else {
  128.     client.print("Off");
  129.   }
  130.   client.print("    Robot is now: ");
  131.   if(motorState == 0){
  132.     client.print("Stop");
  133.   }
  134.   if(motorState == 1){
  135.     client.print("Moving Forward");
  136.   }
  137.   if(motorState == 2){
  138.     client.print("Moving Backward");
  139.   }    
  140.   if(motorState == 3){
  141.     client.print("Rotating Left");
  142.   }  
  143.   if(motorState == 4){
  144.     client.print("Rotating Right");
  145.   }
  146.    
  147.   client.println("<br><br>");
  148.   client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  149.   client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");
  150.   client.println("<br><br>");
  151.   client.println("<br><br>");
  152.   client.println("<a href=\"/MOTOR=FORWARD\"\"><button>Forward </button></a>");
  153.   client.println("<a href=\"/MOTOR=BACKWARD\"\"><button>Backward </button></a><br />");
  154.   client.println("<a href=\"/MOTOR=LEFT\"\"><button>Rotate Left </button></a>");
  155.   client.println("<a href=\"/MOTOR=RIGHT\"\"><button>Rotate Right </button></a><br />");
  156.   client.println("<a href=\"/MOTOR=STOP\"\"><button>Stop Moving </button></a><br />");          
  157.   client.println("</html>");
  158.  
  159.   delay(1);
  160.   Serial.println("Client disonnected");
  161.   Serial.println("");
  162.  
  163. }
Add Comment
Please, Sign In to add comment