Siguelineas
Siguelineas
h>
2
3 // Replace with your network credentials
4 const char* ssid = "--------";
5 const char* password = "-------";
6
7 // Set web server port number to 80
8 WiFiServer server(80);
9 String header;
10
11 String outputsensor0State = "off";
12 String outputsensor1State = "off";
13
14 const int sensor0=12;
15 const int sensor1=13;
16 int valor = 0;
17 int valor2 = 0;
18
19 // Pines para control de motores
20 const int motA0= 27;
21 const int motA1= 26;
22 const int motB0= 25;
23 const int motB1= 33;
24
25 // Funciones
26 void adelante();
27 void atras();
28 void derecha();
29 void izquierda();
30 void stopP();
31
32 // Velocidad maxima 255.
33 //Si el robot oscila mucho o pierde la linea puedes bajar un poco
34 este valor, no menos de 150.
35 int vel=255;
36
37 unsigned long currentTime = millis();
38 // Previous time
39 unsigned long previousTime = 0;
40 // Define timeout time in milliseconds (example: 2000ms = 2s)
41 const long timeoutTime = 2000;
42
43
44 void setup() {
45 Serial.begin(115200);
46
47 pinMode(sensor0,INPUT); // Configuracion de pines
48 pinMode(sensor1,INPUT);
49 pinMode(motA0,OUTPUT);
50 pinMode(motA1,OUTPUT);
51 pinMode(motB0,OUTPUT);
52 pinMode(motB1,OUTPUT);
53
54
55 Serial.print("Connecting to ");
56 Serial.println(ssid);
57 WiFi.begin(ssid, password);
58 while (WiFi.status() != WL_CONNECTED) {
59 delay(500);
60 Serial.print(".");
61 }
62 // Print local IP address and start web server
63 Serial.println("");
64 Serial.println("WiFi connected.");
65 Serial.println("IP address: ");
66 Serial.println(WiFi.localIP());
67 server.begin();
68
69
70 }
71
72 //Seguidor de linea negra
73
74 void loop() // Ciclo Infinito
75 {
76 // Comparamos el valor de los sensores
77 if(digitalRead(sensor0)==1 && digitalRead(sensor1)==1)
78 {
79 adelante();
80 }
81 else if(digitalRead(sensor0)==1 && digitalRead(sensor1)==0)
82 {
83 derecha();
84 }
85 else if(digitalRead(sensor0)==0 && digitalRead(sensor1)==1)
86 {
87 izquierda();
88 }
89 WiFiClient client = server.available(); // Listen for incoming
90 clients
91
92 if (client) { // If a new client
93 connects,
94 currentTime = millis();
95 previousTime = currentTime;
96 Serial.println("New Client."); // print a message out in
97 the serial port
98 String currentLine = ""; // make a String to hold
99 incoming data from the client
100 while (client.connected() && currentTime - previousTime <=
101 timeoutTime) { // loop while the client's connected
102 currentTime = millis();
103 if (client.available()) { // if there's bytes to
104 read from the client,
105 char c = client.read(); // read a byte, then
106 Serial.write(c); // print it out the
107 serial monitor
108 header += c;
109 if (c == '\n') { // if the byte is a
110 newline character
111 // if the current line is blank, you got two newline
112 characters in a row.
113 // that's the end of the client HTTP request, so send a
114 response:
115 if (currentLine.length() == 0) {
116 // HTTP headers always start with a response code (e.g.
117 HTTP/1.1 200 OK)
118 // and a content-type so the client knows what's coming,
119 then a blank line:
120 client.println("HTTP/1.1 200 OK");
121 client.println("Content-type:text/html");
122 client.println("Connection: close");
123 client.println();
124
125 // turns the GPIOs on and off
126
127 // Display the HTML web page
128 client.println("<!DOCTYPE html><html>");
129 client.println("<head><meta name=\"viewport\"
130 content=\"width=device-width, initial-scale=1\">");
131 client.println("<link rel=\"icon\" href=\"data:,\">");
132 client.println("<META HTTP-EQUIV='Refresh'
133 CONTENT='1'>");
134 // CSS to style the on/off buttons
135 // Feel free to change the background-color and font-size
136 attributes to fit your preferences
137 client.println("<style>html { font-family: Helvetica;
138 display: inline-block; margin: 0px auto; text-align: center;}");
139 client.println(".button { background-color: #4CAF50;
140 border: none; color: white; padding: 16px 40px;");
141 client.println("text-decoration: none; font-size: 30px;
142 margin: 2px; cursor: pointer;}");
143 client.println(".button2 {background-color:
144 #555555;}</style></head>");
145
146 // Web Page Heading
147 client.println("<body><h1>ESP32 Carro seguidor de
148 lineas</h1>");
149
150 // Display current state, and ON/OFF buttons for GPIO 26
151
152
153 // Display current state, and ON/OFF buttons for GPIO 27
154 client.println("<p></p>");
155 // If the output27State is off, it displays the ON button
156 valor = digitalRead (sensor0);
157 if (valor==LOW) {
158 client.println("<p><a href=\"/12/HIGH\">EN
159 CAMINO</a></p>");
160 } else {
161 client.println("<p><a href=\"/12/LOW\">SIN
162 CAMINO</a></p>");
163 }
164 valor2 = digitalRead (sensor1);
165 if (valor==LOW) {
166 client.println("<p><a href=\"/13/HIGH\">SIN
167 CAMINO</a></p>");
168 } else {
169 client.println("<p><a href=\"/13/LOW\">EN
170 CAMINO</a></p>");
171 }
172 client.println("</body></html>");
173
174 // The HTTP response ends with another blank line
175 client.println();
176 // Break out of the while loop
177 break;
178 } else { // if you got a newline, then clear currentLine
179 currentLine = "";
180 }
181 } else if (c != '\r') { // if you got anything else but a
182 carriage return character,
183 currentLine += c; // add it to the end of the
184 currentLine
185 }
186 }
187 }
188 // Clear the header variable
189 header = "";
190 // Close the connection
191 client.stop();
192 Serial.println("Client disconnected.");
193 Serial.println("");
194 }
195
196
197
198
199
200 }
201
202 void stopP()
203 {
digitalWrite(motA0,HIGH); // Paro Total
digitalWrite(motA1,HIGH);
digitalWrite(motB0,HIGH);
digitalWrite(motB1,HIGH);
}
void adelante()
{
digitalWrite(motA0,LOW); // Avanza adelante
analogWrite(motA1,HIGH);
digitalWrite(motB0,HIGH);
analogWrite(motB1,LOW);
}
void derecha()
{
digitalWrite(motA0,LOW); // Gira Derecha
analogWrite(motA1,HIGH);
digitalWrite(motB0,LOW);
digitalWrite(motB1,HIGH);
}
void izquierda()
{
digitalWrite(motA0,HIGH); // Gira Izquierda
digitalWrite(motA1,LOW);
digitalWrite(motB0,HIGH);
analogWrite(motB1,LOW);
}