Hi friends,
I'm trying to send email by arduino uno r3 as a client.
First I created a SMTP local server in my laptop with hMailserver,
then I connected my laptop to a router with Ethernet cable,
finally I connected my arduino uno r3 to the same router with ethernet cable.
The question is, when I choose the Ethernet IPV4 of my laptop as the local server, then the email can be sent successfully,
But when I unplug the ethernet cable of my laptop and using the WLAN IPV4 address of my laptop as the server, then the arduino can't connect to my local server,
I don't know why.
could you please tell me the principle?
below is my code, the code is working well when I choose the server as the Ethernet IPV4 of my laptop,
But when I unplug the ethernet cable of my laptop and using the WLAN IPV4 address of my laptop as the server, then the arduino can't connect to my local server,
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x49, 0x67 }; // orgineel
IPAddress ip( 192, 168, 0, 105 );
IPAddress gateway( 192, 168, 0, 1 );
IPAddress dns( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );
char server[] = "192.168.0.106";
int port = 587;
String gegevens; //De gegevens ontvangen van de boot. "Aquatico495 alarm\r\n" is de alarmstring
EthernetClient client;
void setup()
{
Serial.begin(9600);
Serial.setTimeout(50);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);//Ethernet.begin(mac);
Ethernet.begin(mac, ip, dns, gateway, subnet);
Serial.println("Waiting......");
Serial.println(F("Ready. Press 'e' to send."));
Serial.print("IP = ");
Serial.println(Ethernet.localIP());
Serial.println("Ready");
}
// Dit is de hoofdloop **************************************************
void loop() {
byte inChar;
inChar = Serial.read();
if (inChar == 'e')
{
if (sendEmail()) Serial.println(F("Email sent"));
else Serial.println(F("Email failed"));
}
Ethernet.maintain(); //Needed to refresh lease time
}
byte sendEmail()
{
byte thisByte = 0;
byte respCode;
if (client.connect(server, port) == 1) {
Serial.println("connected");
} else {
Serial.println(F("connection failed"));
return 0;
}
delay(500);
if (!eRcv()) return 0;
Serial.println(F("Sending hello"));
// replace 1.2.3.4 with your Arduino's ip
client.println("EHLO 192.168.0.105");
if (!eRcv()) return 0;
Serial.println(F("Sending AUTH LOGIN"));
client.println("AUTH LOGIN");
if (!eRcv()) return 0;
Serial.println(F("Sending User"));
// Change to your base64 encoded user : "email@domein.com"
client.println("YWRtaW5AbG9jYWxzZXJ2ZXIuY29t");
if (!eRcv()) return 0;
Serial.println(F("Sending Password"));
// change to your base64 encoded password =: password
client.println("MTIzNDU2Nzg5");
if (!eRcv()) return 0;
//**************** Hier wordt een mail gestuurd ***********************
// change to your email address (sender)
Serial.println(F("Sending From"));
client.println("MAIL From: admin@localserver.com");
if (!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
client.println("RCPT To: peterchen66666688@163.com");
if (!eRcv()) return 0;
Serial.println(F("Sending DATA"));
client.println("DATA");
if (!eRcv()) return 0;
Serial.println(F("Sending email"));
// change to recipient address
client.println("To: peterchen66666688@163.com");
// change to your address
client.println("From: admin@localserver.com");
client.println("Subject: Give a good subject\r\n");
client.println("Test line number one");
client.println("Test line number two");
client.println(".");
if (!eRcv()) return 0;
delay(500);
//**************** Hier eindigt de 1e mail ***********************
Serial.println(F("Sending QUIT"));
client.println("QUIT");
if (!eRcv()) return 0;
client.stop();
Serial.println(F("disconnected"));
return 1;
}
//******************************************************************
byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;
while (!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if (loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
respCode = client.peek();
while (client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
if (respCode >= '4')
{
efail();
return 0;
}
return 1; // alles goed verlopen
}
//******************************************************************
void efail()
{
byte thisByte = 0;
int loopCount = 0;
client.println(F("QUIT"));
while (!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if (loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return;
}
}
while (client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
client.stop();
Serial.println(F("disconnected"));
}