Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1.  
  2. #include <SoftwareSerial.h>
  3. #include <WiFi.h>
  4. #include <WiFiClient.h>
  5. #include <WiFiAP.h>
  6. #include <ESP32Servo.h>
  7. //#include <Servo.h>
  8.  
  9. static const int servoPin = 4;
  10.  
  11. static const int motorLeftTop = 22;
  12. static const int motorLeftBottom = 23;
  13. static const int motorRightTop = 18;
  14. static const int motorRightBottom = 19;
  15.  
  16. static const int polarityLeft[] = {10,13};
  17. static const int polarityRight[] = {34,35};
  18.  
  19. ESP32PWM motorLT;
  20. ESP32PWM motorLB;
  21. ESP32PWM motorRT;
  22. ESP32PWM motorRB;
  23.  
  24. int freq = 2500;
  25.  
  26.  
  27. Servo servo1;
  28.  
  29.  
  30. //Web server off an example from this library https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFi
  31.  
  32.  
  33. // Serial1 Create2 connection
  34. int rxPin=21;
  35. int txPin=23;
  36. int lastByte=0;
  37. int sensorVal=0;
  38. int ddPin = 22;
  39. int servoPos = 80;
  40.  
  41. const float motorHigh = 0.70;
  42.  
  43. SoftwareSerial Roomba;
  44.  
  45. /*
  46. WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.
  47. Steps:
  48. 1. Connect to the access point "yourAp"
  49. 2. Go to http://192.168.4.1
  50.  
  51. */
  52.  
  53. // Set these to your desired credentials.
  54. const char *ssid = "yourAP";
  55. const char *password = "yourPassword";
  56.  
  57. WiFiServer server(80);
  58.  
  59.  
  60. void setup() {
  61.  
  62. pinMode(polarityRight[0], OUTPUT);
  63. pinMode(polarityRight[1], OUTPUT);
  64. pinMode(polarityLeft[0], OUTPUT);
  65. pinMode(polarityLeft[1], OUTPUT);
  66.  
  67. digitalWrite(polarityRight[0], LOW);
  68. digitalWrite(polarityRight[1], HIGH);
  69. digitalWrite(polarityLeft[0], LOW);
  70. digitalWrite(polarityLeft[1], HIGH);
  71.  
  72.  
  73.  
  74. motorLT.attachPin(motorLeftTop, freq, 10);
  75. motorRT.attachPin(motorRightTop, freq, 10);
  76. motorRB.attachPin(motorRightBottom, freq, 10);
  77. motorLB.attachPin(motorLeftBottom, freq, 10);
  78.  
  79. //motorRT.writeScaled(1.0);
  80. //delay(1000);
  81.  
  82.  
  83. servo1.attach(servoPin);
  84. servo1.write(servoPos);
  85. pinMode(LED_BUILTIN, OUTPUT);
  86. Serial.begin(115200);
  87.  
  88.  
  89.  
  90. Serial.println();
  91. Serial.println("Configuring access point...");
  92.  
  93. // You can remove the password parameter if you want the AP to be open.
  94. WiFi.softAP(ssid, password);
  95. IPAddress myIP = WiFi.softAPIP();
  96. Serial.print("AP IP address: ");
  97. Serial.println(myIP);
  98. server.begin();
  99.  
  100. Serial.println("Server started");
  101. Roomba.begin(19200, rxPin,txPin);
  102.  
  103. pinMode(ddPin, OUTPUT);
  104. digitalWrite(ddPin, HIGH);
  105. delay(100);
  106. digitalWrite(ddPin, LOW);
  107. delay(500);
  108. digitalWrite(ddPin, HIGH);
  109. delay(2000);
  110.  
  111.  
  112. Roomba.write(128); //Start
  113. Roomba.write(131); //Safe mode
  114. delay(1000);
  115.  
  116.  
  117. }
  118.  
  119. int getBattery() //work in progress
  120. {
  121. Roomba.write(142);
  122. Roomba.write(25);
  123. float batVal = Roomba.read();
  124. Roomba.write(142);
  125. Roomba.write(26);
  126. float batCap = Roomba.read();
  127. return (batVal/batCap)*100;
  128. }
  129.  
  130.  
  131. void setDigitLEDs(byte digit1, byte digit2, byte digit3, byte digit4)
  132. {
  133. Roomba.write(163);
  134. Roomba.write(digit1);
  135. Roomba.write(digit2);
  136. Roomba.write(digit3);
  137. Roomba.write(digit4);
  138. }
  139.  
  140.  
  141. void setPowerLED(byte setColor, byte setIntensity)
  142. {
  143. int color = setColor;
  144. int intensity = setIntensity;
  145. Roomba.write(139);
  146. Roomba.write((byte)0x00);
  147. Roomba.write((byte)color);
  148. Roomba.write((byte)intensity);
  149. }
  150.  
  151. void driveWheels(int right, int left)
  152. {
  153. Roomba.write(145);
  154. Roomba.write(right >> 8);
  155. Roomba.write(right);
  156. Roomba.write(left >> 8);
  157. Roomba.write(left);
  158. }
  159.  
  160. void loop() {
  161. WiFiClient client = server.available(); // listen for incoming clients
  162.  
  163. if (client) { // if you get a client,
  164. Serial.println("New Client."); // print a message out the serial port
  165. String currentLine = ""; // make a String to hold incoming data from the client
  166. while (client.connected()) { // loop while the client's connected
  167. if (client.available()) { // if there's bytes to read from the client,
  168. char c = client.read(); // read a byte, then
  169. Serial.write(c); // print it out the serial monitor
  170. if (c == '\n') { // if the byte is a newline character
  171.  
  172. // if the current line is blank, you got two newline characters in a row.
  173. // that's the end of the client HTTP request, so send a response:
  174. if (currentLine.length() == 0) {
  175. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  176. // and a content-type so the client knows what's coming, then a blank line:
  177. client.println("HTTP/1.1 200 OK");
  178. client.println("Content-type:text/html");
  179. client.println();
  180.  
  181. // the content of the HTTP response follows the header:
  182. //client.print("<meta http-equiv=\"refresh\" content=\"1\">");
  183. client.print("<a href=\"/W\" ><button type=\"button\" style=\"width:90px;height:90px;margin-left:45%;\">^</button></a> <br><br>");
  184. client.print("<a href=\"/A\"><button type=\"button\" style=\"width:90px;height:90px;margin-left:40%;\"><</button></a> <a href=\"/S\"><button type=\"button\" style=\"width:90px;height:90px;margin-left:0%;\">v</button></a> <a href=\"/D\"><button type=\"button\" style=\"width:90px;height:90px;margin-left:0%;\">></button></a> <br>");
  185. client.print("<a href=\"/R\"><button type=\"button\" style=\"width:90px;height:90px;margin-left:40%;\">S1</button></a> <a href=\"/E\"><button type=\"button\" style=\"width:90px;height:90px;margin-left:0%;\">S2</button></a> <br>");
  186.  
  187.  
  188. // client.print("<img src=\"https://i.imgur.com/fAcyGS7.jpg\" alt=\"Italian Trulli\">");
  189. client.print("<br><br> battery value here");
  190. // The HTTP response ends with another blank line:
  191. client.println();
  192. // break out of the while loop:
  193. break;
  194. } else { // if you got a newline, then clear currentLine:
  195. currentLine = "";
  196. }
  197.  
  198. } else if (c != '\r') { // if you got anything else but a carriage return character,
  199. currentLine += c; // add it to the end of the currentLine
  200. }
  201.  
  202.  
  203. //Serial.println(currentLine);
  204.  
  205. //This is pretty simple, checks to see if the HTML request ends in any of these letters
  206. //A letter corresponds to a direction (basically arrow keys)
  207.  
  208. if (currentLine.endsWith("GET /W")) {
  209.  
  210. digitalWrite(polarityRight[0], LOW);
  211. digitalWrite(polarityRight[1], HIGH);
  212. digitalWrite(polarityLeft[0], LOW);
  213. digitalWrite(polarityLeft[1], HIGH);
  214.  
  215. motorLT.writeScaled(motorHigh);
  216. motorRT.writeScaled(motorHigh);
  217. motorLB.writeScaled(motorHigh);
  218. motorRB.writeScaled(motorHigh);
  219. delay(500);
  220. motorLT.writeScaled(0.0);
  221. motorRT.writeScaled(0.0);
  222. motorLB.writeScaled(0.0);
  223. motorRB.writeScaled(0.0);
  224.  
  225. }
  226. if (currentLine.endsWith("GET /A")) {
  227.  
  228. digitalWrite(polarityRight[0], LOW);
  229. digitalWrite(polarityRight[1], HIGH);
  230. digitalWrite(polarityLeft[0], LOW);
  231. digitalWrite(polarityLeft[1], HIGH);
  232.  
  233. motorRT.writeScaled(motorHigh);
  234. //motorRT.writeScaled(motorHigh);
  235. motorRB.writeScaled(motorHigh);
  236. //motorRB.writeScaled(motorHigh);
  237. delay(500);
  238. motorLT.writeScaled(0.0);
  239. motorRT.writeScaled(0.0);
  240. motorLB.writeScaled(0.0);
  241. motorRB.writeScaled(0.0);
  242.  
  243.  
  244. }
  245. if (currentLine.endsWith("GET /S")) {
  246. digitalWrite(polarityRight[0], LOW);
  247. digitalWrite(polarityRight[1], HIGH);
  248. digitalWrite(polarityLeft[0], LOW);
  249. digitalWrite(polarityLeft[1], HIGH);
  250.  
  251. motorLT.writeScaled(motorHigh);
  252. motorRT.writeScaled(motorHigh);
  253. motorLB.writeScaled(motorHigh);
  254. motorRB.writeScaled(motorHigh);
  255. delay(500);
  256. motorLT.writeScaled(0.0);
  257. motorRT.writeScaled(0.0);
  258. motorLB.writeScaled(0.0);
  259. motorRB.writeScaled(0.0);
  260. //skip
  261.  
  262. }
  263. if (currentLine.endsWith("GET /D")) {
  264.  
  265. digitalWrite(polarityRight[0], HIGH);
  266. digitalWrite(polarityRight[1], LOW);
  267. digitalWrite(polarityLeft[0], HIGH);
  268. digitalWrite(polarityLeft[1], LOW);
  269.  
  270. motorLT.writeScaled(motorHigh);
  271. //motorRT.writeScaled(motorHigh);
  272. motorLB.writeScaled(motorHigh);
  273. //motorRB.writeScaled(motorHigh);
  274. delay(500);
  275. motorLT.writeScaled(0.0);
  276. motorRT.writeScaled(0.0);
  277. motorLB.writeScaled(0.0);
  278. motorRB.writeScaled(0.0);
  279. }
  280. if(currentLine.endsWith("GET /E") && servoPos < 180){
  281. servoPos += 10;
  282. //Serial.print("WORKEDDDD");
  283. }
  284. if(currentLine.endsWith("GET /R") && servoPos > 80){
  285. servoPos -= 10;
  286. //Serial.println(servoPos);
  287. }
  288.  
  289.  
  290. servo1.write(servoPos);
  291.  
  292.  
  293.  
  294. }
  295. }
  296. // close the connection:
  297. client.stop();
  298. Serial.println("Client Disconnected.");
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement