Sending E-mails, and calling NTP server

I have recently been able to get the iComSat GSM/GPRS shield to work with my arduino mega. Sending text messages, and calling phones seems easy enough; however I am lost when trying to figure out how to send an e-mail containing data, or how to call for the time from an NTP server. :~

Does anyone out there have any guidance or know of a good tutorial they could point me at. I appreciate any help!

Regards

The Arduino Time library

http://arduino.cc/playground/Code/Time

includes an example on using NTP.

Email is unfortunately a little more complicated, due to the complexity that various security / spam concerns have added over the years which make SMTP not so "S" any more. The easiest method is probably to find a trusted host that will do the actual email relay for you, connect to it on port 25 with one of the telnet client libraries, and do the basic HELO, MAIL-FROM, etc. commands with prints. Here's an example:

http://arduino.cc/forum/index.php/topic,50541.0.html

Good luck!

Email test code.

/*
12/21/11
SEND AN EMAIL WITH ARDUINO
 for IDE 1.0
 This code was created by modifying the connect example from 
 arduino.cc and with the help of the YABB forums and their very 
 helpful members. This code sends an email to any email address 
 and then disconnects. Make sure to replace all the *'s with 
 your own information.
 Send e in serial monitor to send email
 */

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

// Change these next 4 entries to your network settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
byte ip[] = { 192, 168, 1, 102 };  
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 192, 168, 1, 1 };
byte server[] = { abc, abc, abc, abc }; //your smtp server
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  delay(10);
  //Ethernet.begin(mac, ip); //subnet and gateway may not be needed
  Ethernet.begin(mac, ip, subnet, gateway); 
  delay(1000);
  Serial.println("Ready");
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
    else Serial.println("Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(server,25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }
  
  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: me@athome.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: you@athome.net"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: you@athome.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
  
  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

Great! Thanks for the help guys!

Using this code I was able to figure out how to send an e-mail using the SIM900’s AT command set.
After uploading tbarats code to communicate with the iComSat via a terminal program:

#include <string.h>

char incoming_char=0;

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);

Serial.println(“Starting Communication…”);
}

void loop() {

if(Serial1.available() >0)
{
incoming_char=Serial1.read();
Serial.print(incoming_char);
}

if(Serial.available() >0)
{
incoming_char=Serial.read();
Serial1.print(incoming_char);
}
}

After that I performed the following steps:

  1. First make sure the SIM900 does not currently have an TCP connection using: “AT+CIPSHUT”
    You should get a response of: “SHUT OK”

  2. Next set your APN settings using: AT+CSTT=“APN”,“USERNAME”,“PASSWORD”
    (I’m not sure I actually needed to do this, but to correctly identify all the steps followed, I have included it here)

  3. Close the connection again with: “AT+CIPSHUT”
    You should get a response of: "SHUT OK

  4. Next I setup my TCP connection to the SMTP server using: AT+CIPSTART=“TCP”,“smtp.rogerswirelessdata.com”,25
    (Depending on your service provider this may be differnt, just do a Google search for your providers SMTP server. TCP and port number will probably be the same.)
    I got the response:
    “OK
    CONNECT OK
    220 to5email.gprs.rogers.com ESMTP Sendmail Sentrion-MTA-4.1.1/Sentrion-MTA-4.1.1; Mon, 16 Jan 2012 12:30:57 -0500”

  5. Next we need to tell the modem we want it to send with: AT+CIPSEND
    You will be prompted with a “>” symbol
    Type in “HELO gprs.rogers.com” (replace the server with your own which you can find from the response of the previous step)
    followed by a ‘carriage return’ and ‘line feed’ (This can be setup using Brays terminal by clicking the CR=CR+LF and +CR check boxes. This way you will only have to press the send button after typing “HELO gprs.rogers.com
    After that you need to press ‘SUB’. To do that with Brays terminal you must create a macro that makes M1 = “#026
    Once the macro is setup press M1

You should get a response of: “SEND OK
250 to5email3.gprs.rogers.com Hello [0.0.0.0], pleased to meet you”

  1. As the macro we created for M1 ends the previous message sent we need to start a new one with: AT+CIPSEND
    You will be prompted with a “>” symbol
    Type in “MAIL From: sender@gmx.com” and hit send
    Press M1 (macro)

You should get a response of “SEND OK
250 0.0.0 sender@gmx.com… Sender ok”

  1. Again start a message like the previous two steps with: AT+CIPSEND
    You will be prompted with a “>” symbol
    Type in “RCPT To: receiver@gmail.com” and hit send
    Press M1 (macro)

You should get a response of “SEND OK
250 0.0.0 receiver@gmail.com… Recipient ok”

8 Again start a message like the previous three steps with: AT+CIPSEND
You will be prompted with a “>” symbol
Type in “DATA” and hit send
Press M1 (macro)

Response should be: “SEND OK
354 Enter mail, end with “.” on a line by itself”

  1. Again… : AT+CIPSEND
    You will be prompted with a “>” symbol
    Type
    "From: “SENDER"sender@gmx.com” (Press Send)
    "To: “RECEIVER"receiver@gmail.com” (Press Send)
    “Subject: Email Test” (Press Send)
    “This message was sent for testing using the iComSat shield with an Arduino Mega. Hello World!” (Press Send)
    . (Press Send)
    Press M1 (macro)

You should get a response of: "SEND OK
250 0.0.0 q0GIHXSg006727 Message accepted for delivery

That is how I got it to work. Anyway time to put this method into a program!

Nicely done! Look at all those messages from the server...very clean! ;)

zoomkat: Email test code.

/*
12/21/11
SEND AN EMAIL WITH ARDUINO
 for IDE 1.0
  ......
[/quote]

When I try this, I get avrdude: stk500v2_ReceiveMessage(): timeout ?

:)

When I try this, I get avrdude: stk500v2_ReceiveMessage(): timeout

This message indicates that the loader cannot find the Arduino. Do you have the correct serial port selected for this device?

SurferTim: This message indicates that the loader cannot find the Arduino. Do you have the correct serial port selected for this device?

Absolutely. And if I try to upload a known working sketch to the same port, it works fine.

The transfer light even flashes for a time before stopping when uploading the above sketch. Odd.

This is not correct if you are using IDE v1.0. Ethernet.begin(mac, ip, subnet, gateway);

The v1.0 format is Ethernet.begin(mac, ip, dns, gateway, subnet);

But that should have no effect on the trouble you are having.

Have you opened the serial monitor to see if there was anything sent from the Arduino to your computer?

It almost sounds like you are running out of memory.

SurferTim:
This is not correct if you are using IDE v1.0.
Ethernet.begin(mac, ip, subnet, gateway);

The v1.0 format is
Ethernet.begin(mac, ip, dns, gateway, subnet);

But that should have no effect on the trouble you are having.

Have you opened the serial monitor to see if there was anything sent from the Arduino to your computer?

I’m using an ethermega, but I don’t see why that would cause any issues? It’s 100% arduino compatible.

I also tried updating the code:-

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


byte mac[] = { 
  0x00, 0x11, 0x22, 0x33, 0xFB, 0x11 }; 
byte server[] = { 
  212, 74, 114, 24 }; // my smtp server ip
EthernetClient client;

void setup()
{
  Serial.begin(57600);
  Ethernet.begin(mac);	
  Serial.println("Ready!");
}

Same thing.

Ethernet test:

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

byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0xFB, 0x11 }; 
byte server[] = { 212, 74, 114, 24 }; // my smtp server ip
EthernetClient client;

void setup()
{
  Ethernet.begin(mac);	
}

void loop()
{
}

Serial test:

void setup()
{
  Serial.begin(57600);
  Serial.println("Ready!");
}

void loop()
{
}

One must cause the upload fail. Which one?

SurferTim:
Ethernet test:

.

Serial test:

.

One must cause the upload fail. Which one?

Neither.

This

/*
12/21/11
 SEND AN EMAIL WITH ARDUINO
 for IDE 1.0
 This code was created by modifying the connect example from 
 arduino.cc and with the help of the YABB forums and their very 
 helpful members. This code sends an email to any email address 
 and then disconnects. Make sure to replace all the *'s with 
 your own information.
 Send e in serial monitor to send email
 */

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


byte mac[] = { 
  0x00, 0x11, 0x22, 0x33, 0xFB, 0x11 }; 
byte server[] = { 
  212, 74, 114, 24 }; // my smtp server ip
EthernetClient client;

void setup()
{
  Serial.begin(57600);
  Ethernet.begin(mac);	
  Serial.println("Ready!");
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  if(inChar == 'e')
  {

  }
}

also uploads without a hitch, but add

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(server,25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }

  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: me@provider.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: me@provider.net"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);

  //send email message to server
  client.println("To: me@provider.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */

  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

and the timeout error rears its ugly head.

I would remove that "////////////////////////// email function" comment.

Try this instead. It only connects and disconnects. Now can you upload it?

byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(server,25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

I would remove that "////////////////////////// email function" comment.

Agreed. Three forward slashes in a row trigger the bootloader to do something different. You want the bootloader to perform it's standard task, so don't trigger the alternate behavior.

PaulS:

I would remove that “////////////////////////// email function” comment.

Agreed. Three forward slashes in a row trigger the bootloader to do something different. You want the bootloader to perform it’s standard task, so don’t trigger the alternate behavior.

OK, I tried the below, and still no dice. It’s definitely something in byte sendEmail() because if I empty it of any code, it compiles and uploads fine.

/*
12/21/11 zoomkat
SEND AN EMAIL WITH ARDUINO
 for IDE 1.0
 This code was created by modifying the connect example from 
 arduino.cc and with the help of the YABB forums and their very 
 helpful members. This code sends an email to any email address 
 and then disconnects.
 
 Send e in serial monitor to send email
 touch pin 4 to ground to send email
 */

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

// Change these next 4 entries to your network settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
byte server[] = { 
  212, 74, 114, 24 }; // my smtp server ip
EthernetClient client;

int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;

void setup()
{
  Serial.begin(57600);
  delay(10);
  //Ethernet.begin(mac, ip); //subnet and gateway may not be needed
  Ethernet.begin(mac); 
  delay(1000);
  
  pinMode(button1, INPUT);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  
  Serial.println("Ready");
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  press1 = digitalRead(button1);
     
  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Serial Email sent"); //start sendEmail()
    else Serial.println("Serial Email failed");
  }
 
   if (press1 == LOW)
  {
    delay(2000);
    if(sendEmail()) Serial.println("Button Email sent"); //start sendEmail()
    else Serial.println("Button Email failed");
  }
}

byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(server,25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }
  
  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: me@myisp.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: you@yourisp.net"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: you@yourisp.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
  
  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

Does the code upload fine for you guys?

I identified the fault, but I have no idea WHY it's fault.

I went through the function line by line, commenting it out, until it uploaded fine. Paul, you would have clapped me on the back and had me over for dinner if you had seen it. ;)

The error is in this line, believe it or not

  client.println("Please let me know it worked!!!"); /* insert body */

I changed it to

 client.println("Let me know if it worked"); // insert body

and all is 90% well. An exclamation mark too far?

To be 100% well, the email would have to have had a subject and a from header. It sported neither on arrival.

  client.println("Please let me know it worked!!!"); /* insert body */

You know what? It wasn't three / in a row that caused the problem. It was three !.

PaulS:   client.println("Please let me know it worked!!!"); /* insert body */

You know what? It wasn't three '/' in a row that caused the problem. It was three '!'

I figured it would be those guys...but why is three !!! in that context a problem? What does it tell the board?

Looking at the telnet SMTP spec, I don't think I'm going to be able to reproduce a proper email subject header, which is a PITA, because that's what I need to get the email to SMS gateway working. The gateway gets the 'to' phone number from the subject. Odd also that the from header doesn't work. The email looks like it's come from Mr Nobody!

Three exclamation points!!! Must have been important. I would have posted sooner, but I was running around, flapping my arms, and clucking like a chicken. I thought the sky must be falling!!! LOL XD

OK, so much for the troll act.

The code in the link below is a bit more complete. I don't know if it is v1.0 compatible, but the send should be the same on both. You can see how you avoid the "Mr Nobody" thing. http://arduino.cc/forum/index.php/topic,80137.msg606219.html#msg606219

Three exclamation points!!! Must have been important. I would have posted sooner, but I was running around, flapping my arms, and clucking like a chicken. I thought the sky must be falling!!! LOL

You're killing me here.

I figured it would be those guys...but why is three !!! in that context a problem?

It does not matter where the three ! in a row are. Three in a row is a signal to the bootloader to do something.

  client.println("Please let me know it worked ! ! !")

would work fine.