Pages: [1]   Go Down
Author Topic: EthernetServer + EthernetClient  (Read 403 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

  I am trying to make a telnet server and have some code to connect to a email server and send an email.
  The mail code is working fine as stand alone. If I am connected in the Telnet Server I can't connect to mail server and send commands.
  I am using a RGB LED just to test commands and the '@' execute the function to send email.

  I try to stop the first client connection (telnet) and wait about 5 minutes before trying to send e-mail (a coded a button) and don't work.

  Thanks for any help or idea.

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

int ledState = 0;
int red_value = 0;
int green_value = 0;
int blue_value = 0;
long previousMillis = 0;
long interval = 200;
int currentColor = 0;
int lastcolor = 1;
int currentState = 0;

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;

#define LED_PIN_RED 3
#define LED_PIN_GREEN 5
#define LED_PIN_BLUE 6

void setup() {
//  Serial.begin(9600); // Turn on debug to serial
  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();
  updateLED();
}

void updateLED(){
  unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
   previousMillis = currentMillis;   
    // if the LED is off turn it on and vice-versa:
    if (ledState == 0)
      ledState = 1;
    else
      ledState = 0;
    // set the LED with the ledState of the variable:
    if (currentColor != 6)
      setLED(currentColor, ledState);
    else {
      if (ledState == 1) {
        lastcolor++;
        if (lastcolor > 3) lastcolor = 1;
      }
      setLED(lastcolor, ledState);
    }
  }
}

void setLED(int color, int led_state){
  if (led_state==1){
 
  switch (color) {
    case 0 : red_value=0; green_value=0; blue_value=0; break;
    case 1 : red_value=255; green_value=0; blue_value=0; break;
    case 2 : red_value=0; green_value=255; blue_value=0; break;
    case 3 : red_value=0; green_value=0; blue_value=255; break;
    case 4 : red_value=255; green_value=255; blue_value=0; break;
    case 5 : red_value=255; green_value=255; blue_value=255; break;
    default: client.println("Unknown color value"); break;
  }}
  else{
  red_value=0; green_value=0; blue_value=0;
  }
  analogWrite(LED_PIN_RED, red_value);
  analogWrite(LED_PIN_GREEN, green_value);
  analogWrite(LED_PIN_BLUE, blue_value);
}

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 'b' : blinkLED(); break;
    case 'o' : currentColor=0; client.println("Off");break; //turn led off
    case 'x' : checkCloseConnection();   break;
    case 's' : getStatus();             break;
    case '?' : printHelpMessage(); break;
    case '@' : client.println("Enviando e-mail"); sendEmail(); break;
    case 0x0d :   break;  //ignore a carriage return
    default: printErrorMessage();   break;
  }
 }

void getStatus(){
  switch (currentColor) {
    case 0 : client.println("Off"); break;
    case 1 : client.println("Blink Red"); break;
    case 2 : client.println("Blink Green"); break;
    case 3 : client.println("Blink Blue"); break;
    case 4 : client.println("Blink Yellow"); break;
    case 5 : client.println("Blink White"); break;
    case 6 : client.println("Blink All"); break;
    default: client.println("Unknown color value"); break;
  }
}

void blinkLED()
{
  switch (textBuff[1]) {
    case 'r' : currentColor=1; currentState=0; client.println("Blink Red"); break;
    case 'g' : currentColor=2; currentState=0; client.println("Blink Green"); break;
    case 'b' : currentColor=3; currentState=0; client.println("Blink Blue");break;
    case 'y' : currentColor=4; currentState=0; client.println("Blink Yellow");break;
    case 'w' : currentColor=5; currentState=0; client.println("Blink White");break;
    case 'a' : currentColor=6; currentState=0; client.println("Blink All"); break;
    default: client.println("Unknown color value"); 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 printHelpMessage()
{
  client.println("Commands:\n");
  client.println("  br -blink red");
  client.println("  bg -blink green");
  client.println("  bb -blink blue");
  client.println("  by -blink yellow");
  client.println("  bw -blink white");
  client.println("  ba -blink all");
  client.println("  @  -send e-mail");
  client.println("  o  -led off");
  client.println("  s  -return current led color");
  client.println("  x  -close connection");
  client.println("  ?  -print this help message");
}

void sendEmail()
{
 if (email.connect(smtp, 587)) {
   client.println("connected");
   email.println("EHLO MYSERVER");
   email.println("AUTH PLAIN xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=");  // 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@xxxxx.com.br>");
   email.println("TO: Ismael Carelli <ismael@xxxxxx.com.br>");
   email.println("SUBJECT: This is the subject");
   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");
 }

}
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9685
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
//  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");
 }

}
« Last Edit: November 24, 2013, 12:49:59 am by iscarelli » Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9685
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Have you tried port 25 instead of 587? does the email work stand alone using port 587I've got some combined http client/server code that seems to work in combination.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Have you tried port 25 instead of 587? does the email work stand alone using port 587I've got some combined http client/server code that seems to work in combination.

Hi!

  I will start again tomorrow. I very tired now. Now is 5AM and need some rest.

  With this code I can send email with no problem:

  Thank you!

 
Code:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x00, 0x1F, 0xC6, 0xD5, 0x0B, 0x1A };
byte server[] = { 189, 124, 16, 3 }; // Mail server address  (this is for yahoo's mobile smtp)

EthernetClient client;  // yahoo's mobile smtp server ip/port

void setup()
{
 Ethernet.begin(mac);
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
 if (client.connect(server, 587)) {
   Serial.println("connected");
   client.println("EHLO MYSERVER");
   client.println("AUTH PLAIN xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=");  // replace the **'s with your auth info from the perl script.
   client.println("MAIL FROM:<ismael@xxxxxxxxxxxxxxxxcom.br>");
   client.println("RCPT TO:<ismael@xxxxxxxxxxxxxxxxxx.com.br>");
   client.println("DATA");
   client.println("From: Ismael Carelli <ismael@net-rosas.com.br>");
   client.println("TO: Ismael Carelli <ismael@barbacena.com.br>");
   client.println("SUBJECT: GERADOR FALHA AC");
   client.println();
   client.println("This is the body.");
   client.println("This is another line of the body.");
   client.println(".");
   client.println("quit");
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }
}
Logged

Pages: [1]   Go Up
Jump to: