Need help for Arduino Ethernet Webserver y email service at the same time

Hello everyone,

this is my 1st post at forum and my 1st project with Arduino so please be gentle

I’m trying to setup a web server and an email notification/alert system on an Arduino Ethernet R3 board.

For web server I used the great tutorial from http://startingelectronics.com and for email I used the example from Arduino.

Now that everything work fine, I have to compine them and have a sinlge sketch:astonished:

What I don’t understand very well and I need your help is how to use start/stop server y client commands and when.

I’m embedding “my” code and I wait for you help.
Hope not asking too much

PS I’m at the edge of memory overflow (flash y SRAM too)

#include <Ethernet.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>

IPAddress ip     (x, y, z, w );
byte gateway[] = { x, y, z, 1 };
byte my_dns[] =  { x, y, z, 1 };
byte subnet[] =  { 255, 255, 255,   0 };
byte smtp[] =    { s1, s2,  s3, s4 };
byte mac[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB };

EthernetServer server(80);
File webFile; // the web page file on the SD card

void setup()
{
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  SD.begin(4);
  Ethernet.begin(mac, ip, my_dns, gateway, subnet);
  server.begin();
}

void loop()
{
  if (seconds < 5) SendAlarm(1); // every minute at 5th second an email with subject "Alarm" will be posted
  {
    SendAlarm(1);
  }
  EthernetClient client = server.available();
  if (client)
  {
    CurLineBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        c = client.read();
        if (ReqIndx < (ReqBuff_Size - 1))
        {
          ReqHTTP[ReqIndx] = c;
          ReqIndx++;
        }
        if (c == '\n' && CurLineBlank)
        {
          client.println("HTTP/1.1 200 OK");
          if (StrContains(ReqHTTP, "ajax_inputs"))
          {
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
//            SetMoto();
//            XML_response(client);
          }
          else
          {
            client.println("Content-Type: text/html");
            client.println("Connection: keep-alive");
            client.println();
            webFile = SD.open("index.htm");
            while(webFile.available())
            {
              client.write(webFile.read());
            }
            webFile.close();
          }
          ReqIndx = 0;
          ClrStr(ReqHTTP, ReqBuff_Size);
          break;
        }
        if (c == '\n')
        {
          CurLineBlank = true;
        } 
        else if (c != '\r')
        {
          CurLineBlank = false;
        }
      }
    }
    delay(5);
    client.stop();
  }
}

void SendAlarm(byte alarm)
{
  EthernetClient client;
  delay(200);
  if (client.connect(smtp, 25))
  {
    client.println("EHLO");
    client.println("AUTH LOGIN");
    client.println("loginame-encoded");
    client.println("password-encoded");
    client.println("MAIL FROM:<arduino@mail.com>");
    client.println("RCPT TO:<myself@mail.com>");
    client.println("DATA");
    client.println("From:arduino@mail.com");
    client.println("To:myself@mail.com");
    if(alarm)
    {
      client.println("SUBJECT: This is an alarm");
    }
    else
    {
      client.println("SUBJECT: This is a Report");
    }
    client.println(".");
    client.println("QUIT");
    delay(200);
    client.stop();
  }
  else
  {
//    Serial.println("connection failed");
  }
}

I don't think you are going to have enough of either memory. I run these together on a Mega2560, but the code is over the memory limit (program and SRAM) for the Uno or Arduino ethernet board. But here are the sketches anyway. They are easier to combine using my examples. http://playground.arduino.cc/Code/WebServerST http://playground.arduino.cc/Code/Email

SurferTim: I don't think you are going to have enough of either memory. I run these together on a Mega2560, but the code is over the memory limit (program and SRAM) for the Uno or Arduino ethernet board. But here are the sketches anyway. They are easier to combine using my examples. http://playground.arduino.cc/Code/WebServerST http://playground.arduino.cc/Code/Email

Thank you for your reply

Don't you thing it is chatty?

Still I don't think that with my current code I'm overfloating any type of memory, yet.

My concern is using commands like: - EthernetServer server(80); - Ethernet.begin(mac, ip, my_dns, gateway, subnet); - server.begin(); - EthernetClient client = server.available(); - client.stop(); - EthernetClient client; - if (client.connect(smtp, 25))

and to be more specific: when to use them and if the alias client is duplicated and I have to use clientHTTP and clientSMTP instead

Don't you thing it is chatty?

I'm not certain what you mean by "chatty". If you mean it has error checking, and you prefer to not use it, then please go ahead. It won't crash very often, just when you need it.