/*
Email client sketch for WiFi shield
Posted 29 May 2015 by SurferTim
*/
#include <SPI.h>
#include <WiFi.h>
WiFiClient client;
char ssid[] = "MySSID"; // the name of your network
char pass[] = "mypassphrase"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "smtpcorp.com";
int port = 2525;
void setup()
{
delay(2000);
Serial.begin(115200);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while(true);
}
// check firmware version
Serial.print(F("Firmware version: "));
Serial.println(WiFi.firmwareVersion());
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to open SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid,pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print(F("You're connected to the network"));
printCurrentNet();
printWifiData();
delay(2000);
Serial.println(F("Ready. Press 'e' to send."));
}
void loop()
{
byte inChar;
inChar = Serial.read();
if(inChar == 'e')
{
if(sendEmail()) Serial.println(F("Email sent"));
else Serial.println(F("Email failed"));
}
}
byte sendEmail()
{
byte thisByte = 0;
byte respCode;
char tBuf[64];
if(client.connect(server,port) == 1) {
Serial.println(F("connected"));
} else {
client.stop();
Serial.println(F("connection failed"));
return 0;
}
if(!eRcv()) return 0;
Serial.println(F("Sending hello"));
// change to the IP of your Arduino
strcpy_P(tBuf,PSTR("EHLO 192.168.0.2\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(F("Sending auth login"));
strcpy_P(tBuf,PSTR("auth login\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(F("Sending User"));
strcpy_P(tBuf,PSTR("dGltQHByb2xlY3Ryb24uY29t\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(F("Sending Password"));
strcpy_P(tBuf,PSTR("cGhlbGFu\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
// change to your email address (sender)
Serial.println(F("Sending From"));
strcpy_P(tBuf,PSTR("MAIL From: <tim@prolectron.com>\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
strcpy_P(tBuf,PSTR("RCPT To: <tim@prolectron.com>\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(F("Sending DATA"));
strcpy_P(tBuf,PSTR("DATA\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(F("Sending email"));
// change to recipient address
strcpy_P(tBuf,PSTR("To: Tim Dicus <tim@prolectron.com>\r\n"));
client.write(tBuf);
// change to your address
strcpy_P(tBuf,PSTR("From: Tim Dicus <tim@prolectron.com>\r\n"));
client.write(tBuf);
client.println("Subject: Arduino email test\r\n");
client.println("This is from my Arduino!");
client.println(".");
if(!eRcv()) return 0;
Serial.println(F("Sending QUIT"));
strcpy_P(tBuf,PSTR("QUIT\r\n"));
client.write(tBuf);
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;
}
void efail()
{
byte thisByte = 0;
int loopCount = 0;
client.println("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"));
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print(F("SSID: "));
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print(F("BSSID: "));
Serial.print(bssid[5],HEX);
Serial.print(F(":"));
Serial.print(bssid[4],HEX);
Serial.print(F(":"));
Serial.print(bssid[3],HEX);
Serial.print(F(":"));
Serial.print(bssid[2],HEX);
Serial.print(F(":"));
Serial.print(bssid[1],HEX);
Serial.print(F(":"));
Serial.println(bssid[0],HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(F("signal strength (RSSI):"));
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print(F("Encryption Type:"));
Serial.println(encryption,HEX);
}
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print(F("IP Address: "));
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print(F("MAC address: "));
Serial.print(mac[5],HEX);
Serial.print(F(":"));
Serial.print(mac[4],HEX);
Serial.print(F(":"));
Serial.print(mac[3],HEX);
Serial.print(F(":"));
Serial.print(mac[2],HEX);
Serial.print(F(":"));
Serial.print(mac[1],HEX);
Serial.print(F(":"));
Serial.println(mac[0],HEX);
// print your subnet mask:
IPAddress subnet = WiFi.subnetMask();
Serial.print(F("NetMask: "));
Serial.println(subnet);
// print your gateway address:
IPAddress gateway = WiFi.gatewayIP();
Serial.print(F("Gateway: "));
Serial.println(gateway);
}