//include for time synch #include #include "time.h" #include //define the leds (1 is monday and so on) const int L1 = 26; const int L2 = 33; const int L3 = 12; const int L4 = 13; const int L5 = 18; const int L6 = 22; //Possibly used by WiFi const int L7 = 19; //define the reflection signals (1 is monday and so on) const int S1 = 25; const int S2 = 35; const int S3 = 14; //Possibly used by WiFi const int S4 = 27; const int S5 = 5; const int S6 = 21; const int S7 = 4; // initialize the signals value: int SS1 = 0; int SS2 = 0; int SS3 = 0; int SS4 = 0; int SS5 = 0; int SS6 = 0; int SS7 = 0; // these are some definitions to program with: bool pil_taken = false; bool email_send = false; int current_day = 0; unsigned long previousMillis = 0; // will store last time LED was updated const long interval = 500; // interval at which to blink (milliseconds) int ledState = LOW; // ledState used to set the LED ////////// WIFI ////////// //network name const char* ssid = "MyNetwork"; //password const char* password = "MyPassword"; //reading the status of the network const char* wl_status_to_string(wl_status_t status) { switch (status) { case WL_NO_SHIELD: return "WL_NO_SHIELD"; case WL_IDLE_STATUS: return "WL_IDLE_STATUS"; case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL"; case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED"; case WL_CONNECTED: return "WL_CONNECTED"; case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED"; case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST"; case WL_DISCONNECTED: return "WL_DISCONNECTED"; default: return "NONE"; }//switch } ////////// EMAIL ////////// #define SMTP_HOST "smtp.gmail.com" #define SMTP_PORT 465 /* The sign in credentials */ #define AUTHOR_EMAIL "USER@gmail.com" #define AUTHOR_PASSWORD "USERPassword" /* Recipient's email*/ #define RECIPIENT_EMAIL "youremail@me.com" /* The SMTP Session object used for Email sending */ SMTPSession smtp; // /* Callback function to get the Email sending status */ // void smtpCallback(SMTP_Status status); /* Declare the message class */ SMTP_Message message; /* Declare the session config data */ ESP_Mail_Session session; /////////// TIME /////////// //timeserver const char* ntpServer = "pool.ntp.org"; //difference between your timezone and GMT in seconds const long gmtOffset_sec = 3600; //the amount of sec for daylight saving time const int daylightOffset_sec = 3600; //Will contain all the time info struct tm timeinfo; const int start_hour = 9; //When should the program start checking for the pil const int check_hour = 2; //How many hours after start_hour should an email be send //////////VOID SETUP////////// void setup() { // serial monitor Serial.begin (9600); ///////////////// WIFI ///////////////// // Connect to Wi-Fi Serial.print("Connecting to "); Serial.println(ssid); // WiFi.disconnect(); // delay(1000); //Show networks byte numSsid = WiFi.scanNetworks(); Serial.print("Number of available WiFi networks discovered:"); Serial.println(numSsid); for (int thisNet = 0; thisNet

Hi Fabuloser... you forgot some something this morning!

- Sent from your pillbox

"; message.html.content = htmlMsg.c_str(); message.html.content = htmlMsg.c_str(); message.text.charSet = "us-ascii"; message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit; // if (!MailClient.sendMail(&smtp, &message)) { // Serial.println("Error sending Email, " + smtp.errorReason()); // }//if ////////////////////////////////////// // initialize the leds as outputs pinMode(L1, OUTPUT); pinMode(L2, OUTPUT); pinMode(L3, OUTPUT); pinMode(L4, OUTPUT); pinMode(L5, OUTPUT); pinMode(L6, OUTPUT); pinMode(L7, OUTPUT); // initialize the reflection signals as an input: pinMode(S1, INPUT); pinMode(S2, INPUT); pinMode(S3, INPUT); //Pin 14 seems to also be used for WiFi interrupt pinMode(S4, INPUT); pinMode(S5, INPUT); pinMode(S6, INPUT); pinMode(S7, INPUT); //Starting sensor reset midnightReset(); }//setup //////////LOOP////////// void loop() { //Update the current time getTime(); //Check pil signals signals(); //Check if its the next day, and if yes, reset midnightReset(); delay(1000); }//void loop /////////// TIMEINFO ////////// // time synch and serial print void getTime(){ if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); return; }//failed to obtain time // Serial.print("Start hour: "); // Serial.println(timeinfo.tm_hour); }//getTime /////////////// RESET /////////////// void midnightReset() { if(current_day != timeinfo.tm_wday) { //Reset variables email_send = false; pil_taken = false; current_day = timeinfo.tm_wday; //Set all LEDs to LOW digitalWrite (L1, LOW); digitalWrite (L2, LOW); digitalWrite (L3, LOW); digitalWrite (L4, LOW); digitalWrite (L5, LOW); digitalWrite (L6, LOW); digitalWrite (L7, LOW); }//if }//midnightReset ///////////SIGNAL AND LED////////// //signal read and led from box1 void signals(){ if(timeinfo.tm_wday == 0) { //Sunday checkDay(0, L7, S7); } else if(timeinfo.tm_wday == 1) { //Monday checkDay(1, L1, S1); } else if(timeinfo.tm_wday == 2) { //Tuesday checkDay(2, L2, S2); } else if(timeinfo.tm_wday == 3) { //Wednesday checkDay(3, L3, S3); } else if(timeinfo.tm_wday == 4) { //Thursday checkDay(4, L4, S4); } else if(timeinfo.tm_wday == 5) { //Friday checkDay(5, L5, S5); }else if(timeinfo.tm_wday == 6) { //Saturday checkDay(6, L6, S6); }//else if } void checkDay(int w_day, int L, int S) { int SS; if(pil_taken == false) { Serial.print("Today is "); Serial.println(&timeinfo, "%A"); //Check the correct sensor status //pil = inside -> HIGH, pil away -> LOW SS = digitalRead (S); if(timeinfo.tm_hour >= start_hour && timeinfo.tm_hour <= start_hour + check_hour) { digitalWrite (L, HIGH); //Turn LED on if(SS == 0) { //Pil is taken digitalWrite (L, LOW); Serial.print(&timeinfo, "%A"); Serial.println(" pill taken"); pil_taken = true; }//if }//if else if(timeinfo.tm_hour > start_hour + check_hour) { //Pil not taken at required time if(SS == 1 && email_send == false) { //Send an email Serial.println("going to send email"); sendEmail(); Serial.println("email send"); email_send = true; }//if if(SS == 1) blink(L); if(SS == 0) { //Pil is taken digitalWrite (L, LOW); Serial.print(&timeinfo, "%A"); Serial.println(" pill taken"); pil_taken = true; }//if }//else if }//if pil not taken }//checkDay /////////////// BLINK LED /////////////// void blink(int PIN_LED) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else ledState = LOW; // set the LED with the ledState of the variable: digitalWrite(PIN_LED, ledState); }//if }//blink ///////////////// EMAIL ///////////////// void sendEmail() { /* Connect to server with the session config */ if (!smtp.connect(&session)) return; /* Start sending Email and close the session */ if (!MailClient.sendMail(&smtp, &message)) { Serial.println("Error sending Email, " + smtp.errorReason()); }//if }//sendEmail /* Callback function to get the Email sending status */ void smtpCallback(SMTP_Status status){ /* Print the current status */ Serial.println(status.info()); /* Print the sending result */ if (status.success()){ Serial.println("----------------"); Serial.printf("Message sent success: %d\n", status.completedCount()); Serial.printf("Message sent failled: %d\n", status.failedCount()); Serial.println("----------------\n"); struct tm dt; for (size_t i = 0; i < smtp.sendingResult.size(); i++){ /* Get the result item */ SMTP_Result result = smtp.sendingResult.getItem(i); //localtime_r(&result.timesstamp, &dt); Serial.printf("Message No: %d\n", i + 1); Serial.printf("Status: %s\n", result.completed ? "success" : "failed"); //Serial.printf("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); Serial.printf("Recipient: %s\n", result.recipients); Serial.printf("Subject: %s\n", result.subject); } Serial.println("----------------\n"); }//if }//smtpCallback