esp8266 communication

Hello,

im trying to connect esp8266 to arduino and when i upload the Bareminimum sketch and i write AT at the console, i get ok. But when i m trying to do the same thing through the following code using Serial.print("AT") it dowsnt work. Any ideas?

void setup()
{
   
  Serial.begin(9600);
  
  
 
}

void loop(){
  Serial.println("AT");
  delay(2000);

  if(Serial.find("OK")){

    Serial.println("ok");
    
    }

}

Any ideas?

Are you using THE serial port to talk to the ESP? Or are you using it to talk to the PC?

Quit using find(). Just print what came in.

I am using it to talk to the esp. Below is the code i used 1 year ago and worked fine, but not any more. Maybe something changed with new Arduino IDE versions.

#include<stdlib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define trigPin 3
#define echoPin 2

#define SSID "Louis"
#define PASS "asdf1234"
#define IP "184.106.153.149" // thingspeak.com
String GET = "GET /update?key=IO3SAK1ZBUVJRMO7&field1=";
String GET2 = "GET /update?key=IO3SAK1ZBUVJRMO7&field2=";


void setup()
{
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  Serial.begin(9600);
  sensors.begin();
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();
  }
}

void loop(){
  
  
  float duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH); //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  Serial.println(distance);
  
  
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);
    //tempC = DallasTemperature::toFahrenheit(tempC);
  char buffer[10];
 
  String tempF = dtostrf(tempC, 4, 1, buffer);
  updateTemp(tempF);
  delay(15000);
  String distanced = dtostrf(distance, 4, 1, buffer);
  updateDistance(distanced);
  delay(15000);
}

void updateTemp(String tenmpF){
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("Error")){
    return;
  }
  cmd = GET;
  cmd += tenmpF;
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
}

void updateDistance(String ddistance){
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("Error")){
    return;
  }
  cmd = GET2;
  cmd += ddistance;
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
}

 
boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  delay(5000);
  if(Serial.find("OK")){
    return true;
  }else{
    return false;
  }
}