Good morning,
Ive been stuggeling working with the code from 'http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235534880/3' which sends an email. The idea is to code it eventually that when D1 is closed it will send an email to notify the user of the circuit being closed.
The code I currently have is:-
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
byte mac[] = { 0x*, 0x*, 0x*, 0x*, 0x*, 0x* };
byte ip[] = { *, *, 1, * };
byte server[] = { 192, 168, 1, 2 }; // Local SMTP server
int time = 5000;
int wait = 1000;
EthernetClient client(server, 25);
void setup()
{
delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("HELO userEmail@email.com"); /* say hello*/
delay(wait); /* wait for a response */
client.println("MAIL From:<ardunio@email.com>"); /* identify sender */
delay(wait); /* wait for a response */
client.println("RCPT To: userEmail@email.com>"); /* identify recipient */
delay(wait); /* wait for a response */
client.println("DATA");
delay(wait); /* wait for a response */
client.println("To: userEmail@email.com"); /* identify recipient */
client.println("Subject: You Have Mail!!"); /* insert subject */
client.println("Please let me know it worked!!!"); /* insert body */
client.println("QUIT"); /* terminate connection */
delay(wait); /* wait for a response */
client.println();
} 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(;;)
;
}
}
However when I try and verify the code, I get a whole list of errors I dont seem to be able to figure out what is wrong:-
sketch_Email.cpp:9:16: error: invalid suffix "x" on integer constant
sketch_Email.cpp:9:21: error: invalid suffix "x" on integer constant
sketch_Email.cpp:9:26: error: invalid suffix "x" on integer constant
sketch_Email.cpp:9:31: error: invalid suffix "x" on integer constant
sketch_Email.cpp:9:36: error: invalid suffix "x" on integer constant
sketch_Email.cpp:9:41: error: invalid suffix "x" on integer constant
sketch_Email:5: error: expected primary-expression before ',' token
sketch_Email:5: error: expected primary-expression before ',' token
sketch_Email:5: error: expected primary-expression before ',' token
sketch_Email:5: error: expected primary-expression before ',' token
sketch_Email:5: error: expected primary-expression before ',' token
sketch_Email:5: error: expected primary-expression before '}' token
sketch_Email:6: error: expected primary-expression before ',' token
sketch_Email:6: error: expected primary-expression before ',' token
sketch_Email:6: error: expected primary-expression before '}' token
sketch_Email:11: error: no matching function for call to 'EthernetClient::EthernetClient(byte [4], int)'
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:12: note: candidates are: EthernetClient::EthernetClient(uint8_t)
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:11: note: EthernetClient::EthernetClient()
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:8: note: EthernetClient::EthernetClient(const EthernetClient&)
sketch_Email.cpp: In function 'void setup()':
sketch_Email:24: error: no matching function for call to 'EthernetClient::connect()'
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:15: note: candidates are: virtual int EthernetClient::connect(IPAddress, uint16_t)
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:16: note: virtual int EthernetClient::connect(const char*, uint16_t)
Does anyone have any ideas on what Im doing wrong? :-S
Good morning,
Thank you for your quick reply. Sorry I thought * would assign via DHCP, but I have changed the code now to a static MAC and IP address, however Im still getting the error:-
error: no matching function for call to 'EthernetClient::EthernetClient(byte [4], int)'
So my code now stands at (Ive reset the addresses, I know they are not correct, but I just want it to compile at the moment):-
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
byte mac[] = { 0x48, 0xDD, 0x8D, 0xF8, 0x6F, 0xC4 };
byte ip[] = { 192, 168, 1, 220 };
byte server[] = { 192, 168, 1, 2 }; // Local SMTP server
int time = 5000;
int wait = 1000;
EthernetClient client(server, 25);
void setup()
{
delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("HELO thisisme@gmail.com"); /* say hello*/
delay(wait); /* wait for a response */
client.println("MAIL From:<thisisme@gmail.com>"); /* identify sender */
delay(wait); /* wait for a response */
client.println("RCPT To: thisisyou@gmail.com>"); /* identify recipient */
delay(wait); /* wait for a response */
client.println("DATA");
delay(wait); /* wait for a response */
client.println("To: thisisyou@gmail.com"); /* identify recipient */
client.println("Subject: You Have Mail!!"); /* insert subject */
client.println("Please let me know it worked!!!"); /* insert body */
client.println("QUIT"); /* terminate connection */
delay(wait); /* wait for a response */
client.println();
} 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(;;)
;
}
}
So now my errors are:-
sketch_Email:10: error: no matching function for call to 'EthernetClient::EthernetClient(byte [4], int)'
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:12: note: candidates are: EthernetClient::EthernetClient(uint8_t)
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:11: note: EthernetClient::EthernetClient()
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:8: note: EthernetClient::EthernetClient(const EthernetClient&)
sketch_Email.cpp: In function 'void setup()':
sketch_Email:23: error: no matching function for call to 'EthernetClient::connect()'
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:15: note: candidates are: virtual int EthernetClient::connect(IPAddress, uint16_t)
C:\Program Files\arduino-1.0\libraries\Ethernet/EthernetClient.h:16: note: virtual int EthernetClient::connect(const char*, uint16_t)
Now that it is compiling, you should add error checking to that code, so if it doesn't work after it is compiled, you will know what is failing. The last post in this thread has an email client with error checking, just in case... http://arduino.cc/forum/index.php/topic,69647.15.html
You will need to modify it to the new V1.0 code. It is written for V0022. Maybe someday I will get around to updating it to V1.0.
Edit: That code does not work with my email server. The email server log says that the client is "rude", so it won't take email from it. "Rude" means it is receiving the next line of the send before it has responded to the last line. This is what the server expects, and mine requires:
Client connects and sends "helo myserver.mydomain.com"
Server responds with "glad to meet you, myserver.mydomain,com" or "Go away. I don't take email from that client"
Client sends "I want to send email. This is my email address"
Server responds with "OK" or "Go away. I don't take email from you"
Client sends "I want to send email to this address"
Server responds with "OK" or "I don't take email for that address"
Client sends the message
Server responds with "OK, I sent it" or "That didn't work"
Client sends "QUIT"
Server responds with "OK" and closes the connection.
Client closes connection
If my email server receives the "helo" and then receives "I want to send email" before it responds to "helo", it will not take your email. It expects you to be polite and listen to what it is telling you.
That first response from the server may take a while. If it is like mine, I use a spam filter database. It takes a couple seconds sometimes for the database server to let my server know if it is ok to take email from that client.
Below is some IDE 1.0 test code you can try that sends email when an e is sent via the serial monitor, or when pin 4 (on a 328 arduino) is momentairly touched to ground.
/*
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 ip[] = { 192, 168, 1, 102 };
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 192, 168, 1, 1 };
byte server[] = { abc, abc, abc, abc }; //smtp server ip address
EthernetClient client;
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
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);
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");
}
}
//////////////////////////////// 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@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
}
@zoomkat: Nice conversion. It does not verify the return from the server, but it does wait for the server to respond.
I have not checked it on V1.0, but modified back to V0022, it is no longer "rude".