0

I am trying to communicate with my ESP8266-07 through Arduino Nano Serial. I am using AT commands to reset, connect wifi, connect TCP server and so on. I have got one functional script, so i know that ESP8266 is wired correctly. I am using 3.3V to 5V logic converter and so on... This is the older functional script:

void esp_connect() {
  Serial.println("AT+RST");
  delay(2);
  esp_timefuse = millis();
  while(Serial.find("ready") == false) {
  if(esp_timefuse - millis() >= 5000) 
    esp_connect();
  }  

  Serial.println("AT+CWMODE=3");
  delay(2);
  esp_timefuse = millis();
  while(Serial.find("OK") == false) {
    if(esp_timefuse - millis() >= 2000) 
      esp_connect();
  }

  Serial.println("AT+CWJAP=\"WiFi 7-pekiel\",\"43664366\"");
  delay(2);
  esp_timefuse = millis();
  while(Serial.find("OK") == false) {
    if(esp_timefuse - millis() >= 2000) 
      esp_connect();
  } 

  Serial.println("AT+CIPMUX=1");
  delay(2);
  esp_timefuse = millis();
  while(Serial.find("OK") == false) {
    if(esp_timefuse - millis() >= 2000) 
      esp_connect();
  } 

  Serial.println("AT+CIPSTART=0,\"TCP\",\"10.10.10.6\",3600");
  delay(2);
  esp_timefuse = millis();
  while(Serial.find("OK") == false) {
    if(esp_timefuse - millis() >= 2000) 
      esp_connect();
  } 
}

But I am not satisfied with it. I am trying to do it best. So i wrote this script:

void esp_connect() {
  Serial.println("AT+RST");
  Serial.flush();
  Serial.setTimeout(5000);
  if(Serial.find("ready")); 
  else esp_connect();

  Serial.println("AT+CWMODE=3");
  Serial.flush();
  Serial.setTimeout(2000);
  if(Serial.find("OK")); 
  else esp_connect();


  Serial.println("AT+CWJAP=\"WiFi 7-pekiel\",\"43664366\"");
  Serial.flush();
  if(Serial.find("OK")); 
  else esp_connect();

  Serial.println("AT+CIPMUX=1");
  Serial.flush();
  if(Serial.find("OK")); 
  else esp_connect();

  Serial.println("AT+CIPSTART=0,\"TCP\",\"10.10.10.6\",3600");
  Serial.flush();
  if(Serial.find("OK")); 
  else esp_connect();
}

When I am testing this new script through Serial monitor everything works. I am simulating ESP8266 so I just type "OK" and "OK" just like ESP8266. But when I connect it to ESP8266 nothing works. Where is the Mistake in my script? Is my usage of Serial.find() correct?

2 Answers 2

1

Your if/else statements are not correct at all. e.g.

  if(Serial.find("ready")); 
  else esp_connect();

Firstly the if condition shouldn't have a semicolon ; at the end and you aren't really testing if/else, just if so it should be in the form

  if(!Serial.find("ready")) {
    esp_connect();
  }

  if(!Serial.find("OK")) {
    esp_connect();
  }

Notice the exclamation mark ! this is logical NOT - so the code in the brackets only gets executed when the statement is FALSE.

Sign up to request clarification or add additional context in comments.

Comments

0

I found that code is working perfectly and the mistake was too short timeout for connect to wifi. Two second was too short time duration for ESP8266. Quite a dumb mistake...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.