Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #define DEBUG true
  2. #include<SoftwareSerial.h>
  3. #include <Servo.h>
  4.  
  5. SoftwareSerial mySerial(9,10);
  6. //192.168.4.1
  7. //parola123
  8. //netGratis
  9. // Pinii motor 1
  10. #define mpin00 5
  11. #define mpin01 6
  12. // Pinii motor 2
  13. #define mpin10 3
  14. #define mpin11 11
  15. Servo srv;
  16. void setup() {
  17. Serial.begin(115200);
  18. mySerial.begin(115200);
  19. pinMode(LED_BUILTIN, OUTPUT);
  20. digitalWrite(LED_BUILTIN, LOW);
  21. sendData("AT+RST\r\n", 2000, false); // resetare modul
  22. sendData("AT+CWMODE=2\r\n", 1000, false); // configurare ca access point
  23. sendData("AT+CIFSR\r\n", 1000, DEBUG); // citeste adresa IP
  24. sendData("AT+CWSAP?\r\n", 2000, DEBUG); // citeste informatia SSID (nume retea)
  25. sendData("AT+CIPMUX=1\r\n", 1000, false); // configurare conexiuni multiple
  26. sendData("AT+CIPSERVER=1,80\r\n", 1000, false); // pornire server pe port 80
  27. digitalWrite(mpin00, 0);
  28. digitalWrite(mpin01, 0);
  29. digitalWrite(mpin10, 0);
  30. digitalWrite(mpin11, 0);
  31. pinMode (mpin00, OUTPUT);
  32. pinMode (mpin01, OUTPUT);
  33. pinMode (mpin10, OUTPUT);
  34. pinMode (mpin11, OUTPUT);
  35. // pin LED
  36. }
  37. void delayStopped(int ms)
  38. {
  39. StartMotor (mpin00, mpin01, 0, 0);
  40. StartMotor (mpin10, mpin11, 0, 0);
  41. delay(ms);
  42. }
  43. void StartMotor (int m1, int m2, int forward, int speed)
  44. {
  45.  
  46. if (speed==0) // oprire
  47. {
  48. digitalWrite(m1, 0);
  49. digitalWrite(m2, 0);
  50. }
  51. else
  52. {
  53. if (forward)
  54. {
  55. digitalWrite(m2, 0);
  56. analogWrite (m1, speed); // folosire PWM
  57. }
  58. else
  59. {
  60. digitalWrite(m1, 0);
  61. analogWrite(m2, speed);
  62. }
  63. }
  64. }
  65. void loop() {
  66. if (mySerial.available()) {
  67. if (mySerial.find("+IPD,")) {
  68. delay(500);
  69. int connectionId = mySerial.read() - 48; // functia read() returneaza valori zecimale ASCII
  70. // si caracterul ‘0’ are codul ASCII 48
  71. String webpage = "<h1>Robot Control!</h1><a href=\"/l0\"><button>Forward</button></a></br>";
  72. String cipSend = "AT+CIPSEND=";
  73. cipSend += connectionId;
  74. cipSend += ",";
  75. webpage += "<a href=\"/l3\"><button>Left</button></a>";
  76. webpage += "<a href=\"/l1\"><button>Stop</button></a>";
  77. webpage += "<a href=\"/l4\"><button>Right</button></a></br>";
  78. webpage += "<a href=\"/l2\"><button>Backward</button></a>";
  79.  
  80. if (readSensor() > 0) {
  81. webpage += "<h2>Millis:</h2>";
  82. webpage += readSensor();
  83. }
  84. cipSend += webpage.length();
  85. cipSend += "\r\n";
  86. sendData(cipSend, 100, DEBUG);
  87. sendData(webpage, 150, DEBUG);
  88.  
  89. String closeCommand = "AT+CIPCLOSE=";
  90. closeCommand += connectionId; //se adauga identificatorul conexiunii
  91. closeCommand += "\r\n";
  92. sendData(closeCommand, 300, DEBUG);
  93. }
  94. }
  95. }
  96. String sendData(String command, const int timeout, boolean debug) {
  97. String response = "";
  98. mySerial.print(command); // trimite comanda la esp8266
  99. long int time = millis();
  100. while ((time + timeout) > millis()) {
  101. while (mySerial.available()) {
  102. char c = mySerial.read(); // citeste caracter urmator
  103. response += c;
  104. }
  105. }
  106. if (response.indexOf("/l0") != -1) {
  107. digitalWrite(LED_BUILTIN, HIGH);
  108. StartMotor (mpin00, mpin01, 1, 255);
  109. StartMotor (mpin10, mpin11, 1, 255);
  110. }
  111. if (response.indexOf("/l2") != -1) {
  112. digitalWrite(LED_BUILTIN, HIGH);
  113. StartMotor (mpin00, mpin01, 0, 255);
  114. StartMotor (mpin10, mpin11, 0, 255);
  115. }
  116. if (response.indexOf("/l3") != -1) {
  117. digitalWrite(LED_BUILTIN, HIGH);
  118. StartMotor (mpin00, mpin01, 0, 90);
  119. StartMotor (mpin10, mpin11, 1, 90);
  120.  
  121. delay (500);
  122. delayStopped(500);
  123. }
  124. if (response.indexOf("/l4") != -1) {
  125. digitalWrite(LED_BUILTIN, HIGH);
  126. StartMotor (mpin00, mpin01, 1, 90);
  127. StartMotor (mpin10, mpin11, 0, 90);
  128.  
  129. delay (500);
  130. delayStopped(500);
  131. }
  132. if (response.indexOf("/l1") != -1) {
  133. digitalWrite(LED_BUILTIN, LOW);
  134. delayStopped(500);
  135. }
  136. if (debug) {
  137. Serial.print(response);
  138. }
  139. return response;
  140. }
  141. unsigned long readSensor() {
  142. return millis();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement