EthernetServer + EthernetClient

zoomkat:
For testing, have you tried simplifying the code by skipping the LED stuff and just printing what is received by the telnet server back to the serial monitor?

Hi!

I am testing with this simplified code.

I test with some debug and this line (if (email.connect(smtp, 587)) {) never is true.

Thanks,
Ismael

//  Telnet server reference http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278686415
//
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte subnet[] = {255,255,255,0};
byte ip[] = {10, 1, 2, 90 };
byte gateway[] = {10, 1, 2, 254 };
byte smtp[] = { 189, 124, 16, 3 };

#define textBuffSize 9 //length of longest command string plus two spaces for CR + LF
char textBuff[textBuffSize]; //someplace to put received text
int charsReceived = 0;

boolean connectFlag = 0; //we'll use a flag separate from client.connected
				 //so we can recognize when a new connection has been created
unsigned long timeOfLastActivity; //time in milliseconds of last activity
unsigned long allowedConnectTime = 300000; //30 seconds

EthernetServer server(23);
EthernetClient client;
EthernetClient email;

void setup() {
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}
  
void loop() {
  // look to see if a new connection is created,
  // print welcome message, set connected flag
  if (server.available() && !connectFlag) {
    connectFlag = 1;
    client = server.available();
    client.println("\nWelcome to the Arduino Notifier");
    client.println("? for help");
    printPrompt();
  }
  // check to see if text received
  if (client.connected() && client.available()) getReceivedText();
  // check to see if connection has timed out
  if (connectFlag) checkConnectionTimeout();
}

void printPrompt()
{
  timeOfLastActivity = millis();
  client.flush();
  charsReceived = 0; //count of characters received
  client.print("\n>");
}

void checkConnectionTimeout()
{
  if(millis() - timeOfLastActivity > allowedConnectTime) {
    client.println();
    client.println("Timeout disconnect.");
    client.stop();
    connectFlag = 0;
  }
}

void getReceivedText()
{
  char c;
  int charsWaiting;
  // copy waiting characters into textBuff
  //until textBuff full, CR received, or no more characters
  charsWaiting = client.available();
  do {
    c = client.read();
    textBuff[charsReceived] = c;
    charsReceived++;
    charsWaiting--;
  }
  while(charsReceived <= textBuffSize && c != 0x0d && charsWaiting > 0);
  //if CR found go look at received text and execute command
  if(c == 0x0d) {
    parseReceivedText();
    // after completing command, print a new prompt
    printPrompt();
  }
  // if textBuff full without reaching a CR, print an error message
  if(charsReceived >= textBuffSize) {
    client.println();
    printErrorMessage();
    printPrompt();
  }
  // if textBuff not full and no CR, do nothing else;
  // go back to loop until more characters are received
}

void parseReceivedText()
{
  // look at first character and decide what to do
  switch (textBuff[0]) {
    case 'x' : checkCloseConnection();   break;
    case '@' : client.println("Enviando e-mail"); sendEmail(); break; 
    case 0x0d :				  break;  //ignore a carriage return
    default: printErrorMessage();	  break;
  }
 }

void printErrorMessage()
{
  client.println("Unrecognized command.  ? for help.");
}

void checkCloseConnection()
{
  if (textBuff[1] == 0x0d) {
    client.println("\nBye.\n");
    client.stop();
    connectFlag = 0;
  } 
   else
    printErrorMessage();
}

void sendEmail()
{
 if (email.connect(smtp, 587)) {
   client.println("connected");
   email.println("EHLO MYSERVER");
   email.println("AUTH PLAIN xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=");  // replace the **'s with your auth info from the perl script.
   email.println("MAIL FROM:<ismael@net-rosas.com.br>");
   email.println("RCPT TO:<ismael@barbacena.com.br>");
   email.println("DATA");
   email.println("From: Ismael Carelli <ismael@net-rosas.com.br>");
   email.println("TO: Ismael Carelli <ismael@barbacena.com.br>");
   email.println("SUBJECT: GERADOR FALHA AC");
   email.println();
   email.println("This is the body.");
   email.println("This is another line of the body.");
   email.println(".");
   email.println("quit");
   email.stop();
 } else {
   client.println("connection failed");
 }

}