Go Down

Topic: Sending E-mails, and calling NTP server (Read 10142 times) previous topic - next topic

Sparked

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

cmagagna

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!

zoomkat

Email test code.

Code: [Select]

/*
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
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Sparked

#3
Jan 16, 2012, 07:23 pm Last Edit: Jan 16, 2012, 08:37 pm by Sparked Reason: 1
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"

6)  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"

7) 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"

9) 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!



 



SurferTim

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

Dan_ce


Email test code.

Code: [Select]

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

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

:)

SurferTim

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

Dan_ce


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.

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?

It almost sounds like you are running out of memory.

Dan_ce


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:-

Code: [Select]
#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.

SurferTim

Ethernet test:
Code: [Select]
#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:
Code: [Select]
void setup()
{
  Serial.begin(57600);
  Serial.println("Ready!");
}

void loop()
{
}


One must cause the upload fail. Which one?

Dan_ce

#11
Feb 20, 2012, 06:21 pm Last Edit: Feb 20, 2012, 06:25 pm by Dane Reason: 1

Ethernet test:
Code: [Select]
.
Serial test:
Code: [Select]
.
One must cause the upload fail. Which one?


Neither.

This
Code: [Select]
/*
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
Code: [Select]
//////////////////////////////// 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.

SurferTim

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

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

Code: [Select]
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
}


PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

Dan_ce


Quote
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.

Code: [Select]
/*
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?

Go Up