/*
Email client sketch for W51000
Using SMTPClient Library
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include
<Mail.h>
// https://github.com/nopnop2002/SMTPClient
#include <SMTPClient.h> //
https://github.com/nopnop2002/SMTPClient
#define rxPin 2 // PD2
#define txPin 3 // PD3
#define BaudRate 4800
// this must be unique
byte mac[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char server[] = "mail.smtp2go.com";
int port = 2525;
char user64[] = "Email AddressをBase64に変換した文字列"; // Your
Username of SMTP2GO
char pass64[] = "PasswordをBase64に変換した文字列"; // Your
Password of SMTP2GO
char mail_from[] = "Me<MailFrom@example.com>"; //
Mail From
char mail_to[] = "You<MailTo@example.com>"; // Mail
To
bool connect;
EthernetClient ethClient;
SmtpClient smtpClient(ðClient, server, port);
SoftwareSerial mySerial(rxPin, txPin); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.print("Software Serial Recv Start
BaudRate=");
Serial.println(BaudRate);
mySerial.begin(BaudRate);
connect = 0;
}
void loop()
{
byte inChar;
Mail mail;
char *subject;
char *body;
int ret;
if (!connect) {
Serial.print("Connecting DHCP
Server....");
if (Ethernet.begin(mac)) {
connect=1;
Serial.println("OK");
Serial.println(Ethernet.localIP()); // IPアドレスを表示
Serial.println(F("Ready.
Press Tiny button to send."));
} else {
Serial.println("FAIL");
}
}
if (connect) {
subject = SerialItemGet();
body = SerialItemGet();
// Serial.print ("subject=");
// Serial.println(subject);
// Serial.print ("body=");
// Serial.println(body);
mail.login(user64);
mail.password(pass64);
mail.from(mail_from);
mail.to(mail_to);
mail.cc(mail_to);
mail.bcc(mail_to);
mail.subject(subject);
mail.body(body);
if (smtpClient.send(&mail)) {
Serial.println(F("Email
sent"));
Serial.println(F("Ready.
Press Tiny button to send."));
} else {
Serial.println(F("Email
failed"));
}
free(subject);
free(body);
ret = Ethernet.maintain();
if (ret == 1 || ret == 3) connect = 0;
}
}
char *SerialItemGet() {
int pos = 0;
int flag = 0;
int slen;
char *ptr;
while(1) {
if (mySerial.available()){
char c = mySerial.read();
if (flag == 0) {
slen = c;
ptr=(char
*)malloc(slen+1);
if (ptr ==
NULL) {
Serial.println("malloc error");
flag = 1;
} else {
*(ptr+pos)=0;
flag = 2;
}
} else if (flag == 1)
{ // malloc fail
pos++;
if (pos ==
slen) return ptr;
} else if (flag == 2)
{ // malloc success
*(ptr+pos)=c;
pos++;
*(ptr+pos)=0;
if (pos ==
slen) return ptr;
} // end if
} // end if
} // end while
}
|