Junichi Katsu / Mbed 2 deprecated WatchSensorMail

Dependencies:   NetServices TextLCD mbed IniFileLib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "NTPClient.h"
00004 #include "SMTPClient.h"
00005 #include "IniFile.h"
00006 #include "TextLCD.h"
00007 
00008 #define HOSTNAME "mbedSE"
00009 
00010 #define READ_BUF_SIZE 128
00011 
00012 EthernetNetIf eth(HOSTNAME);
00013 DigitalOut led1(LED1, "led1");
00014 DigitalIn sensor(p5);
00015 DigitalIn sw[] = { p13,p14,p9 };
00016 TextLCD lcd(p11, p12, p27, p28, p29, p30);  // rs, e, d4-d7
00017 LocalFileSystem local("local");
00018 Ticker flipper;
00019 IniFile ini("/local/setting.ini");
00020 
00021 extern "C" void mbed_reset();
00022 
00023 char password[4] = { 1,2,4,1 };
00024 int timecount = 0;
00025 
00026 void TimeCount()
00027 {
00028     if(timecount != 0)    timecount--;
00029 }
00030 
00031 int SendMail()
00032 {
00033     int ini_port;
00034     char ini_server[READ_BUF_SIZE];
00035     char ini_domain[READ_BUF_SIZE];
00036     char ini_user[READ_BUF_SIZE];
00037     char ini_password[READ_BUF_SIZE];
00038     
00039     ini.get("PORT",        ini_port);
00040     ini.get("SERVER",    ini_server , READ_BUF_SIZE);
00041     ini.get("DOMAIN",    ini_domain , READ_BUF_SIZE);
00042     ini.get("USER",        ini_user , READ_BUF_SIZE);
00043     ini.get("PASSWORD",    ini_password , READ_BUF_SIZE);
00044     
00045     Host host(IpAddr(), ini_port, ini_server);
00046     SMTPClient smtp(host, ini_domain, ini_user, ini_password, SMTP_AUTH_PLAIN);
00047     
00048     char ini_from_addr[READ_BUF_SIZE];
00049     char ini_to_addr[READ_BUF_SIZE];
00050     char ini_msg[READ_BUF_SIZE];
00051     
00052     ini.get("FROM_ADDRESS",    ini_from_addr , READ_BUF_SIZE);
00053     ini.get("TO_ADDRESS",    ini_to_addr , READ_BUF_SIZE);
00054     ini.get("MSG",            ini_msg , READ_BUF_SIZE);
00055     
00056     time_t ctTime = time(NULL) + 32400;
00057     
00058     EmailMessage msg;
00059     msg.setFrom(ini_from_addr);
00060     msg.addTo(ini_to_addr);
00061     msg.printf("Subject: Mail %s", ctime(&ctTime));
00062     msg.printf("%s",ini_msg);
00063 
00064     printf("Send result %d\n", smtp.send(&msg));
00065     printf("Last response | %s", smtp.getLastResponse().c_str());
00066     
00067     smtp.clearAuth();
00068 
00069     return(0);
00070 }
00071 
00072 int CheckInputSw()
00073 {
00074     static int BeforeSw[3] = { 0,0,0 };
00075     int NowSw[3];
00076     int ret = 0;
00077     
00078     for(int i=0;i<3;i++)
00079     {
00080         NowSw[i] = sw[i];
00081         
00082         if((BeforeSw[i])&&(NowSw[i]==0))
00083         {
00084             ret |= 0x01 << i;
00085         }
00086         
00087         BeforeSw[i] = NowSw[i];
00088     }
00089     
00090     if(ret != 0)
00091     {
00092         wait(0.2);
00093         
00094     }
00095     
00096     return(ret);
00097 }
00098 
00099 bool password_mode()
00100 {
00101     int input_num = 0;
00102     char input_button[4] = { 0,0,0,0 };
00103     
00104     lcd.locate(0,1);
00105     lcd.printf("Password? ");
00106     
00107     timecount = 5;
00108     flipper.attach(&TimeCount, 1.0);
00109     
00110     while((input_num < 4)&&(timecount!=0))
00111     {
00112         lcd.locate(10,0);
00113         lcd.printf("%dsec",timecount);
00114         int in = CheckInputSw();
00115         
00116         if( in )
00117         {
00118             input_button[input_num] = in;
00119             input_num++;
00120             lcd.locate(10+input_num,1);
00121             lcd.printf("*");
00122         }
00123     }
00124     flipper.detach();
00125     
00126     if(memcmp( &input_button[0] , &password[0],4) == 0)
00127     {
00128         lcd.locate(10,1);
00129         lcd.printf("  OK  ");
00130         return(true);
00131     }
00132     else
00133     {
00134         lcd.locate(10,1);
00135         lcd.printf("  NG  ");
00136         return(false);
00137     }
00138     
00139     return(false);
00140 }
00141 
00142 int main() {
00143     
00144     sensor.mode(PullDown);
00145     sw[0].mode(PullUp);
00146     sw[1].mode(PullUp);
00147     sw[2].mode(PullUp);
00148     
00149     lcd.cls();
00150     lcd.locate(0,0);
00151     lcd.printf("EtherSetting..");
00152     
00153     EthernetErr ethErr;
00154     
00155     do {
00156         ethErr = eth.setup();
00157         if (ethErr) printf("Timeout\n", ethErr);
00158     } while (ethErr != ETH_OK);
00159     
00160     
00161 #if 0
00162     printf("Connected OK\n");
00163     const char* hwAddr = eth.getHwAddr();
00164     printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
00165            hwAddr[0], hwAddr[1], hwAddr[2],
00166            hwAddr[3], hwAddr[4], hwAddr[5]);
00167 
00168     IpAddr ethIp = eth.getIp();
00169     printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
00170 #endif
00171     
00172     // Get time
00173     NTPClient ntp;
00174     //printf("NTP setTime...\n");
00175     Host server(IpAddr(), 123, "pool.ntp.org");
00176     ntp.setTime(server);
00177     time_t ctTime = time(NULL) + 32400;
00178     //printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));
00179 
00180     Timer tm;
00181     tm.start();
00182     
00183     bool checking = false;
00184 
00185     while (true) {
00186         
00187         time_t ctTime = time(NULL) + 32400;
00188         struct tm *timeObject = localtime(&ctTime);
00189         lcd.locate(0,1);
00190         lcd.printf("%02d/%02d %02d:%02d:%02d", timeObject->tm_mon,timeObject->tm_mday,timeObject->tm_hour,timeObject->tm_min,timeObject->tm_sec);
00191         
00192         if( checking )
00193         {
00194             lcd.locate(0,0);
00195             lcd.printf("Watching Mode");
00196             if (tm.read() > 0.5) {
00197                 led1 = !led1;
00198                 
00199                 if(sensor)
00200 //                if(CheckInputSw() == 0x04)
00201                 {
00202                     lcd.cls();
00203                     lcd.locate(0,0);
00204                     lcd.printf("Alert On!");
00205                     if(password_mode())
00206                     {     
00207                         wait(0.5);
00208                     }
00209                     else
00210                     {
00211                         lcd.locate(0,0);
00212                         lcd.printf("Send Alert Mail!");
00213                         SendMail();
00214                         
00215                         mbed_reset();
00216                     }
00217                     checking = false;
00218                     lcd.cls();
00219                 }
00220                 tm.start();
00221             }
00222         }
00223         else
00224         {
00225             lcd.locate(0,0);
00226             lcd.printf("Free Mode");
00227             
00228             if( CheckInputSw() == 0x01 )
00229             {
00230                 lcd.locate(0,0);
00231                 lcd.printf("Mode Change...");
00232                 checking = true;
00233                 wait(5);
00234                 lcd.cls();
00235             }
00236         }
00237         Net::poll();
00238     }
00239 }