#include #include #include char server[] = "mail.smtp2go.com"; const char *ssid = "Enter your SSID"; const char *password = "Enter your password"; const int ir = 2; String tt; int cnt =0; WiFiClient Client; //define wifi client as client WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // You can specify the time server pool and the offset (in seconds, can be // changed later with setTimeOffset() ). Additionaly you can specify the // update interval (in milliseconds, can be changed using setUpdateInterval() ). void setup() { Serial.begin(115200); pinMode(ir, INPUT); WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } timeClient.begin(); } void loop() { timeClient.update(); int obs = digitalRead(ir); tt=timeClient.getFormattedTime(); Serial.println(tt); Serial.print("Box Closed/Open : "); Serial.println(obs); Serial.print("No. of times Box Opened : "); Serial.println(cnt); if(tt<"15:02:00") { if(obs==0) // condition when box is open before the time { cnt++; } } if(tt=="15:02:00") { if(cnt==0) { Serial.println("Take the medicines"); sendEmail(); Serial.print("Mail sent to:"); Serial.println(" The recipient"); Serial.println(""); } else { Serial.println("Medicine already taken"); } } delay(1000); } byte sendEmail() { if (Client.connect(server, 2525) == 1) // connect to smtp server with port address 2525 { Serial.println(F("connected to server")); } else { Serial.println(F("connection failed")); return 0; } if (!emailResp()) // if connection failed return now return 0; // Serial.println(F("Sending EHLO")); Client.println("EHLO www.example.com"); // Send command EHLO previosly it was HELO******************** if (!emailResp()) return 0; Serial.println(F("Sending auth login")); Client.println("AUTH LOGIN"); if (!emailResp()) return 0; // Serial.println(F("Sending User")); Client.println("c2VuZGVyQHh5ei5jb20="); //base64, ASCII encoded SMTP Username if (!emailResp()) return 0; Serial.println(F("Sending Password")); Client.println("cGFzc3dvcmQ="); //base64, ASCII encoded SMTP Password if (!emailResp()) return 0; Serial.println(F("Sending From")); Client.println(F("MAIL From: sender@xyz.com")); if (!emailResp()) return 0; // change to recipient address Serial.println(F("Sending To")); Client.println(F("RCPT To: receiver@xyz.com")); if (!emailResp()) return 0; Serial.println(F("Sending DATA")); Client.println(F("DATA")); if (!emailResp()) return 0; Serial.println(F("Sending email")); Client.println(F("To: receiver@xyz.com ")); Client.println(F("From: sender@xyz.com ")); Client.println(F("Subject: Medicine Reminder\r\n")); Client.println(F("Hey Abhishek\n")); Client.println(F("This is a reminder regarding your medicines")); Client.println(F("You forgot to take your medicines on time. Please take them immediately.")); // Client.println(F(".")); if (!emailResp()) return 0; // Serial.println(F("Sending QUIT")); Client.println(F("QUIT")); if (!emailResp()) return 0; // Client.stop(); Serial.println(F("disconnected")); return 1; } byte emailResp() { byte responseCode; byte readByte; int loopCount = 0; while (!Client.available()) { delay(1); loopCount++; // Wait for 20 seconds and if nothing is received, stop. if (loopCount > 20000) { Client.stop(); Serial.println(F("\r\nTimeout")); return 0; } } responseCode = Client.peek(); while (Client.available()) { readByte = Client.read(); Serial.write(readByte); } if (responseCode >= '4') { // efail(); return 0; } return 1; }