Confused how to communicate esp8266 12e with arduino nano

I have a hcsr04 module and i connected it to the arduino nano and i want the arduino nano to send data to the esp8266 12e when the module detects a wall and the esp8266 controls the motor driver.
I have done a ton of research but still cannot find a good code.if anybody please can supply the code i would be vey grateful

but still cannot find a good code

You need to write some.

if anybody please can supply the code i would be vey grateful

PM me with an e-mail address and I send you some code. It drives a laser projector, but that shouldn't matter, because you don't have any real requirements.

nano to send data to the esp8266 12e

Is the ESP8266 using the AT command firmware or are you programming the ESP8266 with the Arduino IDE?

With the arduino ide

Why not just connect the hc-sr04 to the esp? You probably need level shifters, as I seem to remember the hc-sr04 only works at 5V

Thanks

Use just the ESP-12E.

You don't need the Arduino.

Look at the example sketches in the IDE.

#include <ESP8266WiFi.h>
#include <Servo.h>
Servo myservo;




/*
  If you have no idea about this library, you can watch the tutorial below
  NodeMCU ESP8266 Tutorial 01: Programming NodeMCU ESP-12E Using Arduino IDE
  https://youtu.be/NtyFx1frYB0
*/

/* define port */
WiFiClient client;
WiFiServer server(80);

/* WIFI settings */
const char* ssid = "TP-LINK_C959D0";
const char* password = "12341234a";
#define TRIGGER 5
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO





/* data received from application */
String  data = "";

/* define L298N or L293D motor control pins */
int leftMotorForward = 2;     /* GPIO2(D4) -> IN3   */
int rightMotorForward = 15;   /* GPIO15(D8) -> IN1  */
int leftMotorBackward = 0;    /* GPIO0(D3) -> IN4   */
int rightMotorBackward = 13;  /* GPIO13(D7) -> IN2  */



/* define L298N or L293D enable pins */
int rightMotorENB = 14; /* GPIO14(D5) -> Motor-A Enable */
int leftMotorENB = 12;  /* GPIO12(D6) -> Motor-B Enable */

void setup()
{
   Serial.begin (9600);
 /* initialize motor control pins as output */
 pinMode(leftMotorForward, OUTPUT);
 pinMode(rightMotorForward, OUTPUT);
 pinMode(leftMotorBackward, OUTPUT);
 pinMode(rightMotorBackward, OUTPUT);
 pinMode(TRIGGER, OUTPUT);
 pinMode(ECHO, INPUT);
 pinMode(BUILTIN_LED, OUTPUT);

 myservo.attach(16);


 

 /* initialize motor enable pins as output */
 pinMode(leftMotorENB, OUTPUT);
 pinMode(rightMotorENB, OUTPUT);

 /* start server communication */
 server.begin();
}

void loop()
{
 /* If the server available, run the "checkClient" function */
 client = server.available();
 if (!client) return;
 data = checkClient ();

 long duration, distance;
 digitalWrite(TRIGGER, LOW);  
 delayMicroseconds(2); 
 
 digitalWrite(TRIGGER, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(TRIGGER, LOW);
 duration = pulseIn(ECHO, HIGH);
 distance = (duration/2) / 29.1;
 
if (distance < 25)MotorStop();
 
 

  /************************ Run function according to incoming data from application *************************/

 /* If the incoming data is "forward", run the "MotorForward" function */
 if (data == "forward") MotorForward();
 /* If the incoming data is "backward", run the "MotorBackward" function */
 else if (data == "backward") MotorBackward();
 /* If the incoming data is "left", run the "TurnLeft" function */
 else if (data == "left") TurnLeft();
 /* If the incoming data is "right", run the "TurnRight" function */
 else if (data == "right") TurnRight();
 /* If the incoming data is "stop", run the "MotorStop" function */
 else if (data == "stop") MotorStop();
 

 
 else if (data == "servo forward") servoforward ();
 else if (data == "servo backward") servobackward ();
 else if (data == "servo left") servoleft ();
 else if (data == "servo right") servoright ();
 else if (data == "servoforwardright") servoforwardright ();
 else if (data == "servoforwardrleft") servoforwardleft ();
 else if (data == "servobackwardleft") servobackwardleft ();
 else if (data == "servobackwardright") servobackwardright ();
 

}

/********************************************* FORWARD *****************************************************/

void servoforward(void)
{
myservo.write(45);

}
void servobackward(void)
{
myservo.write(90);

}
void servoleft(void)
{
myservo.write(135);

}
void servoright(void)
{
myservo.write(45);

}
void servoforwardleft(void)
{
myservo.write(160);

}
void servoforwardright(void)
{
myservo.write(30);

}
void servobackwardleft(void)
{
myservo.write(130);

}
void servobackwardright(void)
{
myservo.write(75);

}

void MotorForward(void)
{
 digitalWrite(leftMotorENB, HIGH);
 digitalWrite(rightMotorENB, HIGH);
 digitalWrite(leftMotorForward, HIGH);
 digitalWrite(rightMotorForward, HIGH);
 digitalWrite(leftMotorBackward, LOW);
 digitalWrite(rightMotorBackward, LOW);
}

/********************************************* BACKWARD *****************************************************/
void MotorBackward(void)
{
 digitalWrite(leftMotorENB, HIGH);
 digitalWrite(rightMotorENB, HIGH);
 digitalWrite(leftMotorBackward, HIGH);
 digitalWrite(rightMotorBackward, HIGH);
 digitalWrite(leftMotorForward, LOW);
 digitalWrite(rightMotorForward, LOW);
}

/********************************************* TURN LEFT *****************************************************/
void TurnLeft(void)
{
 digitalWrite(leftMotorENB, HIGH);
 digitalWrite(rightMotorENB, HIGH);
 digitalWrite(leftMotorForward, LOW);
 digitalWrite(rightMotorForward, HIGH);
 digitalWrite(rightMotorBackward, LOW);
 digitalWrite(leftMotorBackward, HIGH);
}

/********************************************* TURN RIGHT *****************************************************/
void TurnRight(void)
{
 digitalWrite(leftMotorENB, HIGH);
 digitalWrite(rightMotorENB, HIGH);
 digitalWrite(leftMotorForward, HIGH);
 digitalWrite(rightMotorForward, LOW);
 digitalWrite(rightMotorBackward, HIGH);
 digitalWrite(leftMotorBackward, LOW);
}

/********************************************* STOP *****************************************************/
void MotorStop(void)
{
 digitalWrite(leftMotorENB, LOW);
 digitalWrite(rightMotorENB, LOW);
 digitalWrite(leftMotorForward, LOW);
 digitalWrite(leftMotorBackward, LOW);
 digitalWrite(rightMotorForward, LOW);
 digitalWrite(rightMotorBackward, LOW);
}

/********************************** RECEIVE DATA FROM the APP ******************************************/
String checkClient (void)
{
 while (!client.available());
 String request = client.readStringUntil('\r');
 request.remove(0, 5);
 request.remove(request.length() - 9, 9);
 return request;
}

The problem is that when the sensor detects something, the motors do not stop.
The wheels are connected to the l298n motor driver

Please read the forum guidelines in the sticky post, then edit and correct your post #7.

How do you know the sensor is detecting something?

Reading your code, I suspect the sensor only pings once, immediately after a new command is received. Then the motors start. After that, no more pings until the next command is received. So an obstacle in the way is not detected. This is because of this line in your sketch:

if (!client) return;

So what should I do????

Well, first off, you should read the forum guidelines in the sticky post, then edit and correct your post #7.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also use the AutoFormat tool to indent your code for easier reading.

...R

Thanks. I pasted the code correctly again

Ok, thanks.

I suggest taking all the code that deals with the hc-sr04 and moving it to the top of loop(). That way, it will still get executed even if there is no new command.

Ok thanks I will try it out