Advertisement
zdenekpetrzd

Untitled

Mar 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. IPAddress ip(1, 1, 1, 125);
  3. IPAddress gateway(1, 1, 1, 1);
  4. IPAddress subnet(255, 255, 255, 0);
  5.  
  6. String ssid = "NAG";
  7. String heslo = "";
  8.  
  9. int led1 = D5;
  10. int tlacitko = D8;
  11. int pinMotor[] = {16, 5, 4, 0};
  12.  
  13. int delkaKroku = 500;
  14.  
  15. int buttonState = 0; // current state of the button
  16. int lastButtonState = 0; // previous state of the button
  17. int startPressed = 0; // the time button was pressed
  18. int endPressed = 0; // the time button was released
  19. int timeHold = 0; // the time button is hold
  20. int timeReleased = 0; // the time button is released
  21. bool stavLed = false;
  22. bool pripojOdpoj = false;
  23. int cas1;
  24. int cas2;
  25. int cas3;
  26. int poziceMot[8][4] = {
  27. {0,0,0,1},
  28. {0,0,1,1},
  29. {0,0,1,0},
  30. {0,1,1,0},
  31. {0,1,0,0},
  32. {1,1,0,0},
  33. {1,0,0,0},
  34. {1,0,0,1},
  35. };
  36. // Server na portu 80
  37. WiFiServer server(80);
  38. // Proměnná pro uložení HTTP requestu
  39. String header;
  40.  
  41. // Auxiliar variables to store the current output state
  42. String output5State = "off";
  43. String output4State = "off";
  44.  
  45. // Assign output variables to GPIO pins
  46. const int output5 = D0;
  47. const int output4 = D1;
  48.  
  49. void otaceni(int smer) {
  50. while(smer > 0) {
  51. for(int i = 0; i < 8; i++) {
  52. for(int d = 0; d < 4; d++) {
  53. digitalWrite(pinMotor[d], poziceMot[i][d]);
  54. }
  55. delay(1);
  56. }
  57. smer--;
  58. }
  59. while(smer < 0){
  60. for(int i = 7; i >= 0; i--) {
  61. for(int d = 0; d < 4; d++) {
  62. digitalWrite(pinMotor[d], poziceMot[i][d]);
  63. }
  64. delay(1);
  65. }
  66. smer++;
  67. }
  68. }
  69.  
  70. void pripojSeNaWIFI() {
  71. WiFi.config(ip, gateway, subnet);
  72. // Připojení k wifi síti
  73. WiFi.begin(ssid,heslo);
  74. Serial.print("Připojuji se k wifi síti");
  75. while (WiFi.status() != WL_CONNECTED)
  76. {
  77. delay(500);
  78. Serial.print(".");
  79. }
  80. Serial.println();
  81. Serial.print("Připojeno k WIFI, IP: : ");
  82. Serial.println(WiFi.localIP());
  83. }
  84.  
  85.  
  86. void kontrolaTlacitka() {
  87. buttonState = digitalRead(tlacitko);
  88. // button state changed
  89. if (buttonState != lastButtonState) {
  90. // the button was just pressed
  91. if (buttonState == HIGH) {
  92. startPressed = millis();
  93. timeReleased = startPressed - endPressed;
  94. if (timeReleased >= 1000) {
  95. Serial.print("Tlačítko stisknuto na více než 1 sekundu, ");
  96. pripojOdpoj = !pripojOdpoj;
  97. if(pripojOdpoj == true) {
  98. pripojSeNaWIFI();
  99. server.begin();
  100. }
  101. else {
  102. Serial.println("Odpojuji od wifi sítě");
  103. WiFi.disconnect();
  104. }
  105. }
  106. // the button was just released
  107. } else {
  108. endPressed = millis();
  109. timeHold = endPressed - startPressed;
  110. }
  111. }
  112. // save the current state as the last state,
  113. //for next time through the loop
  114. lastButtonState = buttonState;
  115. }
  116.  
  117. void handleKlient() {
  118. WiFiClient client = server.available(); // Listen for incoming clients
  119.  
  120. if (client) { // If a new client connects,
  121. Serial.println("New Client."); // print a message out in the serial port
  122. String currentLine = ""; // make a String to hold incoming data from the client
  123. while (client.connected()) { // loop while the client's connected
  124. if (client.available()) { // if there's bytes to read from the client,
  125. char c = client.read(); // read a byte, then
  126. Serial.write(c); // print it out the serial monitor
  127. header += c;
  128. if (c == '\n') { // if the byte is a newline character
  129. // if the current line is blank, you got two newline characters in a row.
  130. // that's the end of the client HTTP request, so send a response:
  131. if (currentLine.length() == 0) {
  132. client.println("HTTP/1.1 200 OK");
  133. client.println("Content-type:text/html");
  134. client.println("Connection: close");
  135. client.println();
  136.  
  137. // turns the GPIOs on and off
  138. if (header.indexOf("GET /5/on") >= 0) {
  139. Serial.println("GPIO 5 on");
  140. output5State = "on";
  141. output4State = "off";
  142. digitalWrite(output5, HIGH);
  143. } else if (header.indexOf("GET /5/off") >= 0) {
  144. Serial.println("GPIO 5 off");
  145. output5State = "off";
  146. digitalWrite(output5, LOW);
  147. } else if (header.indexOf("GET /4/on") >= 0) {
  148. Serial.println("GPIO 4 on");
  149. output4State = "on";
  150. output5State = "off";
  151. digitalWrite(output4, HIGH);
  152. } else if (header.indexOf("GET /4/off") >= 0) {
  153. Serial.println("GPIO 4 off");
  154. output4State = "off";
  155. digitalWrite(output4, LOW);
  156. }
  157.  
  158. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  159. // and a content-type so the client knows what's coming, then a blank line:
  160. client.println("<!DOCTYPE HTML><html>");
  161. client.println(" <head><meta charset='utf-8'><meta name='generator' content='PSPad editor, www.pspad.com'><title>VDA</title></head>");
  162. client.println("<body><h1><b>NAG-IOT-VDA-Ioutaci</b></h1><div><form '#' method='POST'><label for='CAS'>Trvani jednoho kroku</label>(ms)<input type='number' name='parametr' id='CAS'>");
  163. client.println("<input type='submit' value='SEND'></form><div>");
  164. if (output4State=="off") {
  165. client.println("<p><a href=\'/4/on\'><button class=\'button\'>Doleva</button></a></p>");
  166. } else {
  167. client.println("<p><a href=\'/4/off\'><button class=\'button button2\'>Doleva jede</button></a></p>");
  168. }
  169. if (output5State=="off") {
  170. client.println("<p><a href=\'/5/on\'><button class=\'button\'>Doprava</button></a></p>");
  171. } else {
  172. client.println("<p><a href=\'/5/off\'><button class=\'button button2\'>Doprava jede</button></a></p>");
  173. }
  174. client.println("</body></html>");
  175. // The HTTP response ends with another blank line
  176. client.println();
  177. // Break out of the while loop
  178. break;
  179. } else { // if you got a newline, then clear currentLine
  180. currentLine = "";
  181. }
  182. } else if (c != '\r') { // if you got anything else but a carriage return character,
  183. currentLine += c; // add it to the end of the currentLine
  184. }
  185. }
  186. }
  187. // Clear the header variable
  188. header = "";
  189. // Close the connection
  190. client.stop();
  191. Serial.println("Client disconnected.");
  192. Serial.println("");
  193. }
  194. }
  195.  
  196. void setup() {
  197. // Inicializace Sériové linky
  198. Serial.begin(115200);
  199. Serial.println();
  200. WiFi.disconnect();
  201. for(int i = 0; i < 4; i++) {
  202. pinMode(pinMotor[i], OUTPUT);
  203. }
  204. pinMode(led1,OUTPUT);
  205. pinMode(tlacitko,INPUT_PULLUP);
  206. output4State == "on";
  207.  
  208. }
  209.  
  210. void loop() {
  211. cas1 = millis();
  212. if(cas1-cas2 > delkaKroku && output4State == "on") {
  213. otaceni(1);
  214. cas2 = millis();
  215. Serial.println("Doleva");
  216. }
  217. if(cas1-cas2 > delkaKroku && output5State == "on") {
  218. otaceni(-1);
  219. Serial.println("Dprava");
  220. cas2 = millis();
  221. }
  222. if(WiFi.status() == WL_CONNECTED && cas1-cas3 > 500) {
  223. stavLed = !stavLed;
  224. digitalWrite(led1,stavLed);
  225. cas3 = millis();
  226. }
  227. else if(WiFi.status() != WL_CONNECTED) {
  228. digitalWrite(led1,LOW);
  229. }
  230. kontrolaTlacitka();
  231. handleKlient();
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement