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 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.
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?
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.)
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 !
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.
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!
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.