Advertisement
Cookins

robot Arduino+esp8266

Sep 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. //include libraries
  2. #include <SoftwareSerial.h>
  3. #include <Stepper.h>
  4.  
  5. SoftwareSerial esp8266(3, 2); //RX pin = 3, TX pin = 2
  6.  
  7. //definition of variables
  8. #define DEBUG true //show messages between ESP8266 and Arduino in serial port
  9. const int stepsPerRevolution = 500;
  10. int state = 5;
  11.  
  12. Stepper rightStepper(stepsPerRevolution, 8,9,10,11);
  13. Stepper leftStepper(stepsPerRevolution, 4,5,6,7);
  14.  
  15. //*****
  16. //SETUP
  17. //*****
  18. void setup()
  19. {
  20. //start communication
  21. Serial.begin(9600);
  22. esp8266.begin(115200);
  23.  
  24. sendData("AT+RST\r\n", 2000, DEBUG); //reset module
  25. sendData("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
  26. sendData("AT+CWJAP=\"Access point name\",\"password\"\r\n", 2000, DEBUG); //connect wi-fi network
  27. while(!esp8266.find("OK")) { //wait for connection
  28. }
  29. sendData("AT+CIFSR\r\n", 1000, DEBUG); //show IP address
  30. sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); //allow multiple connections
  31. sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // start web server on port 80
  32.  
  33. //Define motor speed
  34. rightStepper.setSpeed(60);
  35. leftStepper.setSpeed(60);
  36.  
  37. }
  38.  
  39. void loop()
  40. {
  41.  
  42. if (esp8266.available()) //verify incoming data
  43. {
  44. if (esp8266.find("+IPD,")) //if there is a message
  45. {
  46. String msg;
  47. esp8266.find("?"); //look for the message
  48. msg = esp8266.readStringUntil(' '); //read whole message
  49. String command = msg.substring(0, 3); //first 3 characters = command
  50. String valueStr = msg.substring(4); //next 3 characters = value
  51. int value = valueStr.toInt();
  52. if (DEBUG) {
  53. Serial.println(command);
  54. Serial.println(value);
  55. }
  56.  
  57. //move forward
  58. if(command == "cm1") {
  59. state = 1;
  60. }
  61.  
  62. //move backward
  63. if(command == "cm2") {
  64. state = 2;
  65. }
  66.  
  67. //turn right
  68. if(command == "cm3") {
  69. state = 3;
  70. }
  71.  
  72. //turn left
  73. if(command == "cm4") {
  74. state = 4;
  75. }
  76.  
  77. //do nothing
  78. if(command == "cm5") {
  79. state = 5;
  80. }
  81.  
  82. }
  83. }
  84.  
  85. //move forward
  86. if (state == 1) {
  87. rightStepper.step(1);
  88. leftStepper.step(-1);
  89. }
  90. //move backward
  91. if (state == 2) {
  92. rightStepper.step(-1);
  93. leftStepper.step(1);
  94. }
  95. //move right
  96. if (state == 3) {
  97. rightStepper.step(1);
  98. leftStepper.step(1);
  99. }
  100. //move left
  101. if (state == 4) {
  102. rightStepper.step(-1);
  103. leftStepper.step(-1);
  104. }
  105. //do nothing
  106. if (state == 5) {
  107. }
  108.  
  109. }
  110.  
  111. //*******************
  112. //Auxiliary functions
  113. //*******************
  114.  
  115. String sendData(String command, const int timeout, boolean debug)
  116. {
  117. String response = "";
  118. esp8266.print(command);
  119. long int time = millis();
  120. while ( (time + timeout) > millis())
  121. {
  122. while (esp8266.available())
  123. {
  124. char c = esp8266.read();
  125. response += c;
  126. }
  127. }
  128. if (debug)
  129. {
  130. Serial.print(response);
  131. }
  132. return response;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement