struggling with Serial UART write delay during setup

I see plenty of posts relating to reading from serial and adding delays.

I am sending AT commands to an ESP8266 via softwareserial. But i need to delay the serial messages i sent. My AT commands read and write ok but i need to add delays.

Can anybody offer advice?

example of my code

Serial.begin(115200);
  while (!Serial) {
    ;
  }
  Serial.println("Started");

  // set the data rate for the SoftwareSerial port
  esp8266.begin(115200);
  esp8266.write("AT+RST\r\n");

  esp8266.write("AT+CIPMUX=1\r\n");
  delay(2000);
  esp8266.print("AT+CIFSR=1\r\n");
  delay(1000);
  esp8266.print("AT+CIPSTART=\"TCP\",\"google.com\",80\r\n");
  delay(1000);
  esp8266.print("AT+SIPSEND=0, 2048\r\n");

The delays i have in do not work, an example would be i want AT+RST to be sent and i would want to wait 5 seconds while the chip resets.

I am using my loop to read the data back

Have a look at how millis() is used to manage timing without blocking in several things at a time

Is your program running on an ESP8266 or on an Arduino?

...R

I did look at milis and the post from this forum, and it will definitely be an option i will explore but at this stage of my development i just want to add serial write delays. this is due to other people understanding.

I an using an arduino for the program sending AT commands over serial.

jmay24:
I an using an arduino for the program sending AT commands over serial.

Which Arduino?

Are you using SoftwareSerial for communication with the ESP8266? If so I think it would be wise to start with 9600 baud.

...R

Arduino UNO and SoftwareSerial for communication. i have used an AT tester and the chip if fine. but Software serial just sends the data too fast.

I have tried using millis but i just cant relate it to my code, its completely different, i am not trying to do two different things at once. there must be a way to add delays between functions?

there must be a way to add delays between functions?

Well, that would be what the delay() function is for. Of course, NOTHING else happens while you have your head in the sand, but that may be OK.

Many ways.

The simplest one is to use delay(), but that is only useful if there is ABSOLUTELY nothing else that the Arduino can do in the same time. For configuring and setting up an output, then it's probably OK. You don't need any sensor readings or button inputs and you don't need to be controlling any motors or other outputs.

Most of the time, you do have other tasks for the Arduino. It must respond to button pushes or other inputs while it's waiting for something to finish. Then you just need to record when you started that process and every iteration of your main loop will check if the correct number of milliseconds have passed since that time. (Also set another variable that says you are waiting - no need to check milliseconds if you've completed waiting and have moved on to the next job.)

jmay24:
i have used an AT tester and the chip if fine. but Software serial just sends the data too fast.

That gives me no details that I can use to help you.

I have tried using millis but i just cant relate it to my code, its completely different. there must be a way to add delays between functions?

AFAIK you have two choices. Either use the delay() function. Or knuckle down and learn how to use millis() as in the link I gave you.

...R

Thanks for your reply's guys. I really have tried millis().

The functions i want to send to the serial port need to be executed in the setup and NOT the loop, i need the loop for other functions. I have attached the code with the millis() added. can somebody steer me right.

I want the first function to execute , Then the serial to say OK
Then the second function AT+RST , In which i want to delay the next function for 5 seconds to allow the chip to reset

But instead its just sending everything at once.

void setup() {
  
  Serial.begin(9600); // Open serial communications and wait for port to open: Speed set to 9600 BAUD
  while (!Serial) {
    ; // Do nothing while serial port not available
  }
  
  Serial.println("Started"); // Print this line to serial monitor / Terminal
  esp8266.begin(115200); // set the data rate for the SoftwareSerial port / ESP8266 Uses 115200 BAUD
  
 unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    
    initialAT();

    reset(); // Even now reset is not being delayed

    webURL();
    
    }

 
}

void loop() {
  
  if (esp8266.available()) {
    Serial.write(esp8266.read());
  }
  if (Serial.available()) {
    esp8266.write(Serial.read());
  } 
}

  void initialAT (){
    esp8266.write("AT\r\n");
    delay(100);
  }

  void reset (){
    esp8266.write("AT+RST\r\n");
    delay(5000);
    }

void webURL (){
     esp8266.print("AT+CIPSTART=\"TCP\",\"google.com\",80\r\n");
    }

The functions i want to send to the serial port need to be executed in the setup and NOT the loop

Then delay() should do what you want. How did you reach the conclusion that delay() does not do what you want ?

You cannot use millis() for timing in setup() unless you use some sort of looping structure, such as while, that examines millis() until the required period has elapsed. If you do this then you might just as well use delay(), which you have anyway !

  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
   
    initialAT();

    reset(); // Even now reset is not being delayed

This is NOT all of your code. We can NOT see where currentMillis and previousMillis (stupid names) are assigned values.

Delayed with respect to what?

OK maybe i have not explained this properly.

Millis() as explained does not work for me as explained i do NOT want to use the delay in LOOP

I need to use the DELAY in setup

DELAY() does NOT delay anything for me in the setup.

I want to add a delay to the serial transmission , for example ...... My UNO sends the number 1 to my Serial port during setup, but i also want to send the number 2 but i want to wait 5 seconds before sending number 2.

I added a 5 second delay between 1 and 2 using delay(5000) , but it does NOT work, it sends 1, then 2 right away.

My Pi works perfectly , arduino dosnt.

I need to use the DELAY in setup

Permission granted.

DELAY() does NOT delay anything for me in the setup.

Maybe delay() would. It does for everybody else. Of course, that assumes that you put the delay() call in the right place AND that delay() is a useful thing to do.

My UNO sends the number 1 to my Serial port during setup, but i also want to send the number 2 but i want to wait 5 seconds before sending number 2.

You can do that.

I added a 5 second delay between 1 and 2 using delay(5000)

BUT YOU DID NOT SHOW US THAT CODE or any proof that the values are sent immediately.

My Pi works perfectly , arduino dosnt.

Nonsense. The program on the Pi may be perfect. The program on the Arduino may not be. But, it's your code that is the problem, NOT the Arduino. AND WE HAVEN"T SEEN THAT CODE!

Its OK, i fixed it.

didn't use millis() or delay

jmay24:
Its OK, i fixed it.

didn't use millis() or delay

You just gave the Arduino a stern lecture, and now it is behaving? Hard to believe.

DELAY() does NOT delay anything for me in the setup.

Rubbish.

Its OK, i fixed it.

didn't use millis() or delay

So the problem was not the delay() at all, I suspect. Would you care to share your solution with us ?

PaulS:
currentMillis and previousMillis (stupid names)

No they're not :slight_smile:

...R

jmay24:
Its OK, i fixed it.

didn't use millis() or delay

The only positive thing about this post is that it's on the first page, so kids searching Google 10 years from now don't have to read pages and pages to find out that their solution is not here.