Email with WifiESP library

Anyone know how to do this?

I tried adapting this sample code: http://playground.arduino.cc/Code/Email for WidiESP but it does work properly.

If I try and send an email then it puts it into an infinite loop where it logs in to my home wifi network OK, then logs into my bigpond email account OK but then the arduino is reset and it goes back to setup() and login to my wifi network again.

I wont attach my entire sketch, just the relevant parts of it.

This is what I have so far any way:

bool CWifi::waitAck(WiFiEspClient& WifiClient)
{
  uint16_t nCount = 0;
  bool bAckReceived = true;
  char cCh = 0;
  
  while (!WifiClient.available()) 
  {
    delay(1);
    nCount++;
 
    // If nothing received for 10 seconds, timeout
    if (nCount > 10000) 
      bAckReceived = false;
  }
  return bAckReceived;
}

bool CWifi::sendEmailCommand(WiFiEspClient& WifiClient, const __FlashStringHelper * strCommand)
{
  byte nResponseCode = 0;
  bool bSuccess = true;
  
  debugS(strCommand);
  WifiClient.println(strCommand);
 
  if (waitAck(WifiClient))
  {
    nResponseCode = WifiClient.peek();
    
    while(WifiClient.available())
    {  
      nResponseCode = WifiClient.read();    
      debugS(String(nResponseCode) + F(" "), false);
    }
    debugS("\n");
    
    if (nResponseCode >= '4')
    {
      sendEmailCommand(WifiClient, F("QUIT"));
      bSuccess = false;
      WifiClient.stop();
    }
  }
  return bSuccess;
}

bool CWifi::sendEmail(String strAlarms)
{
  byte nThisByte = 0;
  bool bSuccess = false;
  WiFiEspClient WifiClient;

  debugS(String(F("Attempting to connect to '")) + m_strMailServer + F("'..."), false);
  if (!WifiClient.connect(m_strMailServer.c_str(), 25)) 
    debugS(F("failed!"));
  else 
  {
    debugS(F("connected!"));
    if (waitAck(WifiClient))
    {
      IPAddress IPAddr = WiFi.localIP();
      String strIPAddr = String(IPAddr[0]) + String(F(".")) + String(IPAddr[1]) + String(F(".")) + String(IPAddr[2]) + String(F(".")) + String(IPAddr[3]); 
      if (sendEmailCommand(WifiClient, String(F("EHLO ")) + strIPAddr))
      {
        if (sendEmailCommand(WifiClient, F("auth login")))
        {
          if (sendEmailCommand(WifiClient, m_strUsername.c_str()))
          {
            if (sendEmailCommand(WifiClient, m_strPassword.c_str()))
            {
              if (sendEmailCommand(WifiClient, F("MAIL From: <>")))
              {
                if (sendEmailCommand(WifiClient, String(F("RCPT To: <")) + m_strEmail.c_str() + F(">")))
                {
                  if (sendEmailCommand(WifiClient, F("DATA")))
                  {
                    if (sendEmailCommand(WifiClient, F("DATA")))
                    {
                      if (sendEmailCommand(WifiClient, String(F("To: Whom it may concern<")) + m_strEmail.c_str() + F(">\n") + 
                                                        F("From: Greg's Irrigation Controller <>\n") + F("Subject: Alarm(s) triggered\r\n\n") + 
                                                        strAlarms + F(".\n")))
                      {
                        if (sendEmailCommand(WifiClient, F("QUIT")))
                        {
                          WifiClient.stop();
                          bSuccess = true;
                          debugS(F("Disconnected!")); 
                        }
                        else
                          printError(__FILE__, __LINE__);
                      }
                      else
                        printError(__FILE__, __LINE__);
                    }
                    else
                      printError(__FILE__, __LINE__);
                  }
                  else
                    printError(__FILE__, __LINE__);
                }
                else
                  printError(__FILE__, __LINE__);
              }
              else
                printError(__FILE__, __LINE__);
            }
            else
              printError(__FILE__, __LINE__);
          }
          else
            printError(__FILE__, __LINE__);
        }
        else
          printError(__FILE__, __LINE__);
      }
      else
        printError(__FILE__, __LINE__);
    }
    else
      printError(__FILE__, __LINE__);
  }  
  return bSuccess;
}

bool CWifi::begin()
{
  bool bResult = false;
  String strSSID = F("Telstra6A61B7"),
          strKey = F("0AC9703D29");
  uint8_t nStatus = WL_IDLE_STATUS;

  // Initialize serial for ESP module
  m_pSerial->begin(9600);
  
  // Initialize ESP module
  WiFi.init(m_pSerial);

  // Check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) 
    debugS("WiFi shield not present!");
  else if (readWifiSSIDKey(strSSID, strKey))
  {
    // Attempt to connect to WiFi network
    while ( nStatus != WL_CONNECTED) 
    {
      debugS(String("Attempting to connect to WPA SSID: ") + strSSID);

      // Connect to WPA/WPA2 network
      nStatus = WiFi.begin(strSSID.c_str(), strKey.c_str());
    }
    IPAddress IPAddr = WiFi.localIP();
    debugS(String(F("You're connected to the network with IP address: ")) + String(IPAddr));
    
    // Start the web server on port 80
    m_WifiServer.begin();
    if (SD.exists(m_strEmailFileName.c_str()))
    {
      CTextFile fileEmail(m_strEmailFileName.c_str(), FILE_READ);
      
      if (fileEmail)
      {
        fileEmail.readLine(m_strEmail);
        fileEmail.readLine(m_strMailServer);
        fileEmail.readLine(m_strUsername);
        fileEmail.readLine(m_strPassword);

        bResult = true;
      }
      else
        Serial.println(F("Could not open email.txt!"));
    }
    else
      Serial.println(F("Could not find email.txt!"));
   
//*********************************************
//*********************************************

    sendEmail("SOMETHING");

//*********************************************
//*********************************************
  }
  return bResult;
}

IrrigationController.ino (2.57 KB)

Wifi.cpp (20.1 KB)

Wifi.h (2.09 KB)

Common.cpp (1.6 KB)

Common.h (673 Bytes)

DateTime.cpp (9.54 KB)

DateTime.h (2.98 KB)

IrrigationController.ino (3.13 KB)

Program.cpp (4.01 KB)

Program.h (819 Bytes)

ProgramStart.cpp (1.56 KB)

ProgramStart.h (505 Bytes)

ProgramStation.cpp (6.03 KB)

ProgramStation.h (1.48 KB)

TextFile.cpp (2.25 KB)

TextFile.h (1.29 KB)

Wifi.cpp (20.1 KB)

Wifi.h (2.09 KB)

I use the ethernet shield and old wifi shield to send email, and they work ok.

Sounds like you are either running out of SRAM, or overflowing an array.

BTW, I recommend getting rid of the String data type. It has never failed to disappoint me by crashing my sketches. Use a character array instead.

edit: You should either post your entire sketch, or attach it if it is too big to post.

SurferTim: I use the ethernet shield and old wifi shield to send email, and they work ok.

Sounds like you are either running out of SRAM, or overflowing an array.

BTW, I recommend getting rid of the String data type. It has never failed to disappoint me by crashing my sketches. Use a character array instead.

edit: You should either post your entire sketch, or attach it if it is too big to post.

You are spot on because I am starting to get low memory warnings.

I have started modifying the PString library so it has more of the String functions that are so convenient.

I will start swapping my Strings for PStrings.

SurferTim: edit: You should either post your entire sketch, or attach it if it is too big to post.

I was getting low memory warnings because I was doing this:

printError(FILE, LINE) all over the place in order to make debugging easier.

But FILE is a full path resulting in many very long literal strings without the F macro.

So I changed them to this sort of thing:

printError(F("IrrigationController.ino"), LINE)

Back down to 70% memory usage with the vast majority of the coding done.

Some of my String class members could be changed to char str[] PROGMEM........

I attached my entire sketch but most the files are not going to be relevant to the email issue.

All the example sketches (ethernet shield library and WifiESP library) seem to connect to one 'thing' only.

Is it even possible to do two things in the same sketch? E.G. Serve up web pages and send an email?

Why do you have twice DATA?

          if (sendEmailCommand(WifiClient, F("DATA")))
                  {
                    if (sendEmailCommand(WifiClient, F("DATA")))
                    {...

J-M-L: Why do you have twice DATA?

          if (sendEmailCommand(WifiClient, F("DATA")))
                  {
                    if (sendEmailCommand(WifiClient, F("DATA")))
                    {...

Copy and paste mistake that I missed.

Is that causing any pb?