ハイパー・マイコン mbedでインターネット 電子工作 2章 リスト2-2 InformEmailのプログラム

Dependencies:   EthernetInterface NTPClient SimpleSMTPClient TextLCD mbed-rtos mbed

Committer:
sunifu
Date:
Wed Jul 09 13:42:34 2014 +0000
Revision:
0:cf3bc27b895f
InformEmail
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunifu 0:cf3bc27b895f 1 // -- InformEmail --
sunifu 0:cf3bc27b895f 2 #include "mbed.h"
sunifu 0:cf3bc27b895f 3 #include "EthernetInterface.h"
sunifu 0:cf3bc27b895f 4 #include "NTPClient.h"
sunifu 0:cf3bc27b895f 5 #include "SimpleSMTPClient.h"
sunifu 0:cf3bc27b895f 6 #include "TextLCD.h"
sunifu 0:cf3bc27b895f 7
sunifu 0:cf3bc27b895f 8 #define DOMAIN "mbed"
sunifu 0:cf3bc27b895f 9 #define SERVER "smtp.mail.yahoo.co.jp"
sunifu 0:cf3bc27b895f 10 #define PORT "587" //25 or 587(OutBound Port25 Blocking )
sunifu 0:cf3bc27b895f 11
sunifu 0:cf3bc27b895f 12 #define USER "yahoo-japan-ID"
sunifu 0:cf3bc27b895f 13 #define PWD "password"
sunifu 0:cf3bc27b895f 14 #define FROM_ADDRESS "yahoo-japan-ID@yahoo.co.jp"
sunifu 0:cf3bc27b895f 15 // TO_ADDRESS (Of some address is possible.)
sunifu 0:cf3bc27b895f 16 // to-user1@domain, to-user2@domain, to-user3@domain ....
sunifu 0:cf3bc27b895f 17 // The TO_ADDRESS are less than 128 characters.
sunifu 0:cf3bc27b895f 18 #define TO_ADDRESS "to-address@domain"
sunifu 0:cf3bc27b895f 19
sunifu 0:cf3bc27b895f 20 #define SUBJECT "Notice of the detection log"
sunifu 0:cf3bc27b895f 21
sunifu 0:cf3bc27b895f 22 #define INIT 5.0
sunifu 0:cf3bc27b895f 23 #define NOISE 3.0
sunifu 0:cf3bc27b895f 24 #define DELAY 10.0
sunifu 0:cf3bc27b895f 25 #define INTERVAL 3600
sunifu 0:cf3bc27b895f 26
sunifu 0:cf3bc27b895f 27 TextLCD lcd(p24, p26, p27, p28, p29, p30);
sunifu 0:cf3bc27b895f 28 DigitalOut led1(LED1);
sunifu 0:cf3bc27b895f 29 DigitalOut led2(LED2);
sunifu 0:cf3bc27b895f 30 DigitalOut led3(LED3);
sunifu 0:cf3bc27b895f 31 DigitalIn msensor(p20);
sunifu 0:cf3bc27b895f 32 int cnt,flag,flag1;
sunifu 0:cf3bc27b895f 33
sunifu 0:cf3bc27b895f 34 void emailsendInterval(void const *args)
sunifu 0:cf3bc27b895f 35 {
sunifu 0:cf3bc27b895f 36 while (true) {
sunifu 0:cf3bc27b895f 37 led1 = !led1;
sunifu 0:cf3bc27b895f 38 Thread::wait(1000);
sunifu 0:cf3bc27b895f 39 cnt++;
sunifu 0:cf3bc27b895f 40 if ( cnt == INTERVAL ){
sunifu 0:cf3bc27b895f 41 cnt = 0;
sunifu 0:cf3bc27b895f 42 flag = 1;
sunifu 0:cf3bc27b895f 43 }
sunifu 0:cf3bc27b895f 44 }
sunifu 0:cf3bc27b895f 45 }
sunifu 0:cf3bc27b895f 46
sunifu 0:cf3bc27b895f 47 void lcdUpdate(void const *args)
sunifu 0:cf3bc27b895f 48 {
sunifu 0:cf3bc27b895f 49 char lcdMsg[16];
sunifu 0:cf3bc27b895f 50 while(true){
sunifu 0:cf3bc27b895f 51 Thread::wait(30000);
sunifu 0:cf3bc27b895f 52 time_t ctTime = time(NULL)+32400;
sunifu 0:cf3bc27b895f 53 strftime(lcdMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime));
sunifu 0:cf3bc27b895f 54 lcd.locate(0,0);
sunifu 0:cf3bc27b895f 55 lcd.printf("[%s]",lcdMsg);
sunifu 0:cf3bc27b895f 56 }
sunifu 0:cf3bc27b895f 57 }
sunifu 0:cf3bc27b895f 58
sunifu 0:cf3bc27b895f 59 void flip(void const *args) {
sunifu 0:cf3bc27b895f 60 while (true) {
sunifu 0:cf3bc27b895f 61 led1 = !led1;
sunifu 0:cf3bc27b895f 62 Thread::wait(200);
sunifu 0:cf3bc27b895f 63 }
sunifu 0:cf3bc27b895f 64 }
sunifu 0:cf3bc27b895f 65
sunifu 0:cf3bc27b895f 66 int main()
sunifu 0:cf3bc27b895f 67 {
sunifu 0:cf3bc27b895f 68 SimpleSMTPClient smtp;
sunifu 0:cf3bc27b895f 69 EthernetInterface eth;
sunifu 0:cf3bc27b895f 70
sunifu 0:cf3bc27b895f 71 char sendMsg[512]="";
sunifu 0:cf3bc27b895f 72 char strTimeMsg[16];
sunifu 0:cf3bc27b895f 73 int ret;
sunifu 0:cf3bc27b895f 74 flag1 = 0;
sunifu 0:cf3bc27b895f 75
sunifu 0:cf3bc27b895f 76 lcd.cls();
sunifu 0:cf3bc27b895f 77 printf("\n\n/* Inform Email System */\n");
sunifu 0:cf3bc27b895f 78
sunifu 0:cf3bc27b895f 79 printf("Setting up ...\n");
sunifu 0:cf3bc27b895f 80 eth.init();
sunifu 0:cf3bc27b895f 81 eth.connect();
sunifu 0:cf3bc27b895f 82
sunifu 0:cf3bc27b895f 83 printf("Connected OK\n");
sunifu 0:cf3bc27b895f 84
sunifu 0:cf3bc27b895f 85 printf("IP Address is %s\n", eth.getIPAddress());
sunifu 0:cf3bc27b895f 86 lcd.locate(0,1);
sunifu 0:cf3bc27b895f 87 lcd.printf("%s", eth.getIPAddress());
sunifu 0:cf3bc27b895f 88
sunifu 0:cf3bc27b895f 89 printf("NTP setTime...\n");
sunifu 0:cf3bc27b895f 90 NTPClient ntp;
sunifu 0:cf3bc27b895f 91 ntp.setTime("ntp.nict.jp");
sunifu 0:cf3bc27b895f 92
sunifu 0:cf3bc27b895f 93 time_t ctTime = time(NULL)+32400;
sunifu 0:cf3bc27b895f 94 printf("\nTime is now (JST):%s\n", ctime(&ctTime));
sunifu 0:cf3bc27b895f 95 strftime(strTimeMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime));
sunifu 0:cf3bc27b895f 96 lcd.locate(0,0);
sunifu 0:cf3bc27b895f 97 lcd.printf("[%s]",strTimeMsg);
sunifu 0:cf3bc27b895f 98
sunifu 0:cf3bc27b895f 99 led1 = 1;
sunifu 0:cf3bc27b895f 100 Thread thread0(flip);
sunifu 0:cf3bc27b895f 101 wait(INIT);
sunifu 0:cf3bc27b895f 102 thread0.terminate();
sunifu 0:cf3bc27b895f 103 led1=0;
sunifu 0:cf3bc27b895f 104
sunifu 0:cf3bc27b895f 105
sunifu 0:cf3bc27b895f 106 Thread thread(emailsendInterval);
sunifu 0:cf3bc27b895f 107 Thread thread1(lcdUpdate);
sunifu 0:cf3bc27b895f 108
sunifu 0:cf3bc27b895f 109 smtp.setFromAddress(FROM_ADDRESS);
sunifu 0:cf3bc27b895f 110 smtp.setToAddress(TO_ADDRESS);
sunifu 0:cf3bc27b895f 111 smtp.setMessage(SUBJECT,sendMsg);
sunifu 0:cf3bc27b895f 112
sunifu 0:cf3bc27b895f 113 while(1)
sunifu 0:cf3bc27b895f 114 {
sunifu 0:cf3bc27b895f 115 led2 = msensor;
sunifu 0:cf3bc27b895f 116 if ( led2 == 1 ){
sunifu 0:cf3bc27b895f 117 wait( NOISE );
sunifu 0:cf3bc27b895f 118 led2 = msensor;
sunifu 0:cf3bc27b895f 119
sunifu 0:cf3bc27b895f 120 while ( led2 == 1 ){
sunifu 0:cf3bc27b895f 121 led3 = 1;
sunifu 0:cf3bc27b895f 122 wait( DELAY ) ;
sunifu 0:cf3bc27b895f 123 led2 = msensor;
sunifu 0:cf3bc27b895f 124 if ( flag1 == 0 ){
sunifu 0:cf3bc27b895f 125 ctTime = time(NULL)+32400; // JST
sunifu 0:cf3bc27b895f 126 strftime(strTimeMsg,16,"%m/%d %H:%M\r\n",localtime(&ctTime));
sunifu 0:cf3bc27b895f 127 smtp.addMessage(strTimeMsg);
sunifu 0:cf3bc27b895f 128 printf("[%s]",strTimeMsg);
sunifu 0:cf3bc27b895f 129 }
sunifu 0:cf3bc27b895f 130 flag1 = 1;
sunifu 0:cf3bc27b895f 131 }
sunifu 0:cf3bc27b895f 132 flag1 = 0;
sunifu 0:cf3bc27b895f 133 led3 = 0;
sunifu 0:cf3bc27b895f 134 }
sunifu 0:cf3bc27b895f 135 if ( flag == 1 ) {
sunifu 0:cf3bc27b895f 136 if ( smtp.msgLength() != 0 ){
sunifu 0:cf3bc27b895f 137 ret = smtp.sendmail(SERVER, USER, PWD, DOMAIN, PORT,SMTP_AUTH_LOGIN);
sunifu 0:cf3bc27b895f 138 smtp.clearMessage();
sunifu 0:cf3bc27b895f 139 if (ret) {
sunifu 0:cf3bc27b895f 140 printf("E-mail Transmission Error\r\n");
sunifu 0:cf3bc27b895f 141 } else {
sunifu 0:cf3bc27b895f 142 printf("E-mail Transmission OK\r\n");
sunifu 0:cf3bc27b895f 143 }
sunifu 0:cf3bc27b895f 144 }
sunifu 0:cf3bc27b895f 145 flag = 0;
sunifu 0:cf3bc27b895f 146 }
sunifu 0:cf3bc27b895f 147 }
sunifu 0:cf3bc27b895f 148 }
sunifu 0:cf3bc27b895f 149