Go Down

Topic: WiFiClient connect fails EVERY time (Read 235 times) previous topic - next topic

mattlogue

Apr 02, 2019, 08:39 am Last Edit: Apr 02, 2019, 12:40 pm by mattlogue
I have an ESP8266 12E unit. I have only tested this one unit as I have no other avaiable to test.
I can get to smtp.mail.att.net : 587, www.google.com:80, 192.168.1.71:15080 (my BlueIris httpd) work fine with a telnet application from my PC.
However, with a simple SMTP lib, with a complex secure SMTP sketch, AND with a telnet sketch I still get connect = 0 for all three hosts. http.get works fine, BTW. Is my board defective?



Here is the .cpp of the Simple SMTP client
Code: [Select]
#include "SmtpClient.h"
#include "Client.h"
#define SMTP_PORT 25
#define ZERO_IP IPAddress(0, 0, 0, 0)
#define TIMEOUT 10000
#define CRLF "\r\n"

SmtpClient::SmtpClient(Client* client, char *server) : _client(client), _server(server), _serverIP(ZERO_IP), _port(SMTP_PORT) {
}

SmtpClient::SmtpClient(Client* client, char *server, uint16_t port) : _client(client), _server(server), _serverIP(ZERO_IP), _port(port) {
}

SmtpClient::SmtpClient(Client* client, IPAddress serverIP) : _client(client), _serverIP(serverIP), _server(""), _port(SMTP_PORT) {
}

SmtpClient::SmtpClient(Client* client, IPAddress serverIP, uint16_t port) : _client(client), _serverIP(serverIP), _server(""), _port(port) {
}


int SmtpClient::send(Mail *mail) {
  int result = connect();
 Serial.printf("send mail connect result %d",result);
  if (!result) {
    _client->stop();
    return 0;
  }
  result = _send(mail);
  _client->stop();
  return result;
}

int SmtpClient::_send(Mail *mail) {
  if (readStatus() != 220) {
    return 0;
  }
  if (helo() != 250) {
    return 0;
  }
  if (mailFrom(mail->_from) != 250) {
    return 0;
  }
  if (rcptTo(mail) != 250) {
    return 0;
  }
  if (data() != 354) {
    return 0;
  }
  headers(mail);
  body(mail->_body);
  if (finishBody() != 250) {
    return 0;
  }
  return 1;
}

int SmtpClient::connect() {
 int cc;
  if (strlen(_server) > 0) {
  cc = _client->connect(_server, _port);
  Serial.printf("ClientConnect: server %s:%d cc: %d",_server,_port,cc);
    return cc;
  } else {
 cc = _client->connect(_serverIP, _port);
Serial.printf("ClientConnect: server %s:%d cc: %d",(uint32_t)_serverIP,_port,cc);
    return cc;
  }
}

int SmtpClient::helo() {
  _client->print("EHLO");
  _client->print(CRLF);
  int status = readStatus();
  if (status == 250) {
    return status;
  }
  // IF server doesn't understand EHLO, try HELO
  _client->print("HELO");
  _client->print(CRLF);
  return readStatus();
}

(TRUNCATED)


Here is the function of the secure one:
Code: [Select]
bool Gsender_Send() {
  DEBUG_AM("Connecting to :");
  DEBUG_AM(smtp_server);
  DEBUG_AM(smtp_port);
  clients.connect(smtp_server, smtp_port);

  do {
    DEBUG_AM("Awaiting connection");
    delay(100);
  }
  while (clients._clientConnectedState() < 100);

  long stmpTimeOut = 5000;
  char *response = "";
  String responseString = "";
  long doMS = millis();
  do {
    DEBUG_AM(clients.available());
    responseString = clients.readStringUntil('\n');
    responseString.toCharArray(response, 16);
    if (millis() - doMS > stmpTimeOut) {
      DEBUG_AM("Timed out");
      return false;
    }
    delay(100);
  }
  while (strlen(response) < 2);
  if (response == "220") {
    DEBUG_AM("220 OK");
  }
  else {
    DEBUG_AM(response); return false;
  }

  DEBUG_AM("HELO friend:");
  clients.println("HELO friend");
  doMS = millis();
  do {
    while (clients.available()) {
      responseString = clients.readStringUntil('\n');
      responseString.toCharArray(response, 16);
      if (millis() - doMS > stmpTimeOut) {
        DEBUG_AM("Timed out");
        return false;
      }
    }
  }
  while (strlen(response) < 2);
  if (response == "250") {
    DEBUG_AM("250 HELO OK");
  }
  else {
    DEBUG_AM(response); return false;
  }

  DEBUG_AM("AUTH LOGIN:");
  clients.println("AUTH LOGIN");
  doMS = millis();
  do  {
    while (clients.available()) {
      responseString = clients.readStringUntil('\n');
      responseString.toCharArray(response, 16);
      if (millis() - doMS > stmpTimeOut) {
        DEBUG_AM("Timed out");
        return false;
      }
    }
  }
  while (strlen(response) < 2);
  if (response == "334") {
    DEBUG_AM("334 AUTH OK");
  }
  else {
    DEBUG_AM(response); return false;
  }
(TRUNCATED)

Serial transcript of telent client with debug core on:
Code: [Select]
SDK:3.0.0-dev(c0f7b44)/Core:2.5.0=20500000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1/BearSSL:6778687
:ref 1
:ur 1
:close
:del
Connect status 0,connection failed

disconnecting.
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt

connected with JMR, channel 1
ip:192.168.1.139,mask:255.255.255.0,gw:192.168.1.254
ip:192.168.1.139,mask:255.255.255.0,gw:192.168.1.254
pm open,type:2 0

Finally, here is a Serial transcript of my secure sketch... it's a bit messy.
Code: [Select]
*ALERTME: Connecting to :
*ALERTME: smtp.mail.att.net
*ALERTME: 587
WiFiClientSecureBearSSL: connect host: smtp.mail.att.net, port 587WiFiClientSecureBearSSL: WiFi hostByName: 1WiFiClientSecureBearSSL: WiFi, port 587_clientConnected client: 1073692700 client_state: 4WiFiClientSecure::_run_until state: 6, BR_SSL_CLOSED: 1
WiFiClientSecure::_run_until state: 4, BR_SSL_CLOSED: 1
WiFiClientSecure::_run_until state: 1, BR_SSL_CLOSED: 1
WiFiClientSecure::_wait_for_handshake: ret -1
WiFiClientSecureBearSSL: _connectSSL: 0
*ALERTME: Awaiting connection
WiFiClientSecure::_run_until state: 1, BR_SSL_CLOSED: 1
*ALERTME: 0
*ALERTME: Timed out
*ALERTME: Message sending failed. (
*ALERTME:
*ALERTME: )
Just because I live in the states don't mean I care

Go Up