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