I am new to arduino and I have some problems to make my robot advance through a web application.
I describe what my robot does: I am using 2 sensors (IR1 and IR2, see code) in the front of my robot cart, if you read WHITE / WHITE, the cart moves forward, if the sensor reads BLACK / WHITE, it should be accommodated and find WHITE / WHITE to follow advancing, I've also programmed other combinations, that's already programmed and let's say it works.
NOTE: Sensors 3, 4 and 5 are also programmed, but I am only using sensors 1 and 2, since what is urgent is to make me advance in the line and for this I use sensors 1 and 2.
Basically this scheme describes what I want to achieve.
THE PROBLEM: The problem is when I wanted to give instructions through a mobile application using the node8266 to connect through the WIFI (see image), for example when I press STATION 1, that is, I do 1 click, my robot apparently reads the first thing it finds in the loop and if it found WHITE / WHITE, it advances but if it finds something black, it does not respond and it goes straight ahead.
The detail is in that when I click the "SEASON 1" BUTTON, in this case if the robot responds, that is, it starts to read the black line, but if I stop pressing, it keeps the last thing it read. .
What I want is that when you press the STATION 1 button once, go to the point, following the line.
Please, I ask for your help to complete this project, any suggestion would help me a lot. Thanks in advance!
NOTE: To do my tests I am using only "STATION 1". That's why the other stations are commented on in the code.
This is the code:
#include <ESP8266WiFi.h>
const char *ssid = "WiFi-Bi";
const char *password = "Lu$.12345.";
int port = 10;
WiFiServer server(port);
int vel = 255;
int IR1 = LOW; //0
int IR2 = LOW; //0
int IR3 = LOW; //0
int IR4 = LOW;
int IR5 = LOW;
int MotorDerR1 = 15; /* GPIO15(D8) -> IN1 MOTOR 1*/
int MotorDerA1 = 13; /* GPIO13(D7) -> IN2 MOTOR 1*/
int MotorIzqA2 = 2; /* GPIO2(D4) -> IN3 MOTOR 2 */
int MotorIzqR2 = 0; /* GPIO0(D3) -> IN4 MOTOR 2 */
void setup() {
Serial.begin (115200);
delay(10);
/*-------Sensores------*/
pinMode(5, INPUT); //D1
pinMode(4, INPUT); //D2
pinMode(16, INPUT); //D0
pinMode(14, INPUT); //D5
pinMode(12, INPUT); //D6
/*-------Motores------*/
pinMode(MotorDerR1, OUTPUT);
pinMode(MotorDerA1, OUTPUT);
pinMode(MotorIzqA2, OUTPUT);
pinMode(MotorIzqR2, OUTPUT);
Serial.println();
Serial.print("CONECTANDO WIFI: ");
Serial.println(ssid);
WiFi.begin(ssid, password); //Conexión a la red
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
server.begin(); //Iniciamos el servidor
Serial.println("Servidor Iniciado");
Serial.println("IP Servidor:");
Serial.println(WiFi.localIP()); //Obtenemos la IP
Serial.print("Puerto:");
Serial.println(port);
}
void loop() {
IR1 = digitalRead(5);
IR2 = digitalRead(4);
IR3 = digitalRead(16);
IR4 = digitalRead(14);
IR5 = digitalRead(12);
//Serial.print(IR1);
//Serial.print(IR2);
//Serial.print(IR3);
//Serial.print(IR4);
//Serial.print(IR5);
WiFiClient client = server.available();
if (client) { //Si hay un cliente presente
Serial.println("Nuevo Cliente");
while (!client.available() && client.connected()) { //esperamos hasta que hayan datos disponibles
delay(1);
}
String orden = client.readStringUntil('\r');// Leemos la primera línea de la petición del cliente.
//Serial.print("LINEA: ");
Serial.println(orden);
if(orden.indexOf("Estacion1")>0){
SeguirLinea(IR1,IR2);
/*if(IR3==HIGH){
if(IR4==HIGH && IR5==LOW){
Parada();
Serial.println("Estación 1");
}
}*/
}
if(orden.indexOf("PARAR")>0){
Parada();
}
/*
else if(orden.indexOf("Estacion2")>0){
SeguirLinea(IR1,IR2);
if(IR3==HIGH){
if(IR4==HIGH && IR5==LOW){
Parada();
Serial.println("Estación 2");
}
}
}else if(orden.indexOf("Estacion3")>0){
SeguirLinea(IR1,IR2);
if(IR3==HIGH){
if(IR4==HIGH && IR5==LOW){
Parada();
Serial.println("Estación 3");
}
}
}else if(orden.indexOf("Estacion4")>0){
SeguirLinea(IR1,IR2);
if(IR3==HIGH){
if(IR4==HIGH && IR5==LOW){
Parada();
Serial.println("Estación 4");
}
}
}else{
//Parada();
}
*/
client.flush();
Serial.println("Enviando respuesta...");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");// La conexión se cierra después de finalizar de la respuesta
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>CARRITO WIFI</title>");
client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
client.println("<style>button{background-color:#f44336;border:none;color:white;padding:15px 32px;text-align:center;text-decoration:none;display:inline-block;font-size: 24px; transition-duration: 0.4s;}button:hover{background-color: #4CAF50; color: white;}</style>");
client.println("</head>");
client.println("<body>");
client.println("<div style='text-align:center;'>");
client.println("<h1 align='center'>CARRITO WIFI</h1>");
client.println("<br />");
client.println("<button onClick=location.href='./?Estacion1'>Estacion 1</button><br>");
client.println("<button onClick=location.href='./?Estacion2'>Estacion 2</button><br>");
client.println("<button onClick=location.href='./?Estacion3'>Estacion 3</button><br>");
client.println("<button onClick=location.href='./?Estacion4'>Estacion 4</button><br>");
client.println("<button onClick=location.href='./?PARAR'>PARAR</button>");
client.println("<br />");
client.println("</div>");
client.println("</body>");
client.println("</html>");
delay(1);
Serial.println("RESPUESTA ENVIADA");
Serial.println();
}
}
/********************************************* SEGUIR LINEA *****************************************************/
void SeguirLinea(int IR1, int IR2){
if (IR1 == LOW && IR2==LOW ) { /*B B B(no hay punto)*/
Serial.println("linea negra detectada - avanzar");
Adelante();
}//else if(IR1 == LOW && IR2==LOW ){ /*B N N(hay punto) */
//Serial.println("detecté un punto, PARAR!");
//Parada();
//}
else if(IR1 == LOW && IR2==HIGH ){ /*B N B(no hay punto)*/
Serial.println("se salio de la linea negra, mover a la derecha");
Derecha();
}else if(IR1 == HIGH && IR2==LOW ){ /*N B B(no hay punto)*/
Serial.println("se salio de la linea negra, mover a la izquierda");
Izquierda();
}else if(IR1 == HIGH && IR2==HIGH ){ /*N N N(hay punto)*/
Serial.println("parar");
Parada();
}
}
/********************************************* LEER ESTACIÓN *****************************************************/
/*void LeerEstacion(IR4,IR5){
if(IR4==HIGH && IR5==LOW){
Serial.println("Estación 1");
}else if(IR4==LOW && IR5==HIGH){
Serial.println("Estación 2");
}else if(IR4==HIGH && IR5==HIGH){
Serial.println("Estación 3");
}else{
Serial.println("Estación 4");
}
}*/
/********************************************* FORWARD *****************************************************/
void Adelante(void){
digitalWrite(MotorDerR1,0); // 0
analogWrite(MotorDerA1,vel); //velocidad
digitalWrite(MotorIzqR2,0);
analogWrite(MotorIzqA2,vel);
}
/********************************************* BACKWARD *****************************************************/
void Retroceder(void) {
digitalWrite(MotorDerR1,0);
digitalWrite(MotorDerA1,0);
analogWrite(MotorIzqR2,vel);
digitalWrite(MotorIzqA2,0);
}
/********************************************* TURN LEFT *****************************************************/
void Izquierda(void) {
digitalWrite(MotorDerR1,0);
analogWrite(MotorDerA1,vel);
analogWrite(MotorIzqR2,vel);
digitalWrite(MotorIzqA2,0);
}
/********************************************* TURN RIGHT *****************************************************/
void Derecha(void) {
analogWrite(MotorDerR1,vel);
digitalWrite(MotorDerA1,0);
digitalWrite(MotorIzqR2,0);
analogWrite(MotorIzqA2,vel);
}
/********************************************* STOP *****************************************************/
void Parada(void) {
digitalWrite(MotorDerR1,0);
digitalWrite(MotorDerA1,0);
digitalWrite(MotorIzqA2,0);
digitalWrite(MotorIzqR2,0);
}