![](/media/cache/profiles/5f14615696649541a025d3d0f8e0447f.jpg.50x50_q85.jpg)
留守の見張り番プログラムです。
Dependencies: NetServices TextLCD mbed IniFileLib
main.cpp@0:7bc1d7c88b16, 2012-06-21 (annotated)
- Committer:
- jksoft
- Date:
- Thu Jun 21 10:44:19 2012 +0000
- Revision:
- 0:7bc1d7c88b16
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:7bc1d7c88b16 | 1 | #include "mbed.h" |
jksoft | 0:7bc1d7c88b16 | 2 | #include "EthernetNetIf.h" |
jksoft | 0:7bc1d7c88b16 | 3 | #include "NTPClient.h" |
jksoft | 0:7bc1d7c88b16 | 4 | #include "SMTPClient.h" |
jksoft | 0:7bc1d7c88b16 | 5 | #include "IniFile.h" |
jksoft | 0:7bc1d7c88b16 | 6 | #include "TextLCD.h" |
jksoft | 0:7bc1d7c88b16 | 7 | |
jksoft | 0:7bc1d7c88b16 | 8 | #define HOSTNAME "mbedSE" |
jksoft | 0:7bc1d7c88b16 | 9 | |
jksoft | 0:7bc1d7c88b16 | 10 | #define READ_BUF_SIZE 128 |
jksoft | 0:7bc1d7c88b16 | 11 | |
jksoft | 0:7bc1d7c88b16 | 12 | EthernetNetIf eth(HOSTNAME); |
jksoft | 0:7bc1d7c88b16 | 13 | DigitalOut led1(LED1, "led1"); |
jksoft | 0:7bc1d7c88b16 | 14 | DigitalIn sensor(p5); |
jksoft | 0:7bc1d7c88b16 | 15 | DigitalIn sw[] = { p13,p14,p9 }; |
jksoft | 0:7bc1d7c88b16 | 16 | TextLCD lcd(p11, p12, p27, p28, p29, p30); // rs, e, d4-d7 |
jksoft | 0:7bc1d7c88b16 | 17 | LocalFileSystem local("local"); |
jksoft | 0:7bc1d7c88b16 | 18 | Ticker flipper; |
jksoft | 0:7bc1d7c88b16 | 19 | IniFile ini("/local/setting.ini"); |
jksoft | 0:7bc1d7c88b16 | 20 | |
jksoft | 0:7bc1d7c88b16 | 21 | extern "C" void mbed_reset(); |
jksoft | 0:7bc1d7c88b16 | 22 | |
jksoft | 0:7bc1d7c88b16 | 23 | char password[4] = { 1,2,4,1 }; |
jksoft | 0:7bc1d7c88b16 | 24 | int timecount = 0; |
jksoft | 0:7bc1d7c88b16 | 25 | |
jksoft | 0:7bc1d7c88b16 | 26 | void TimeCount() |
jksoft | 0:7bc1d7c88b16 | 27 | { |
jksoft | 0:7bc1d7c88b16 | 28 | if(timecount != 0) timecount--; |
jksoft | 0:7bc1d7c88b16 | 29 | } |
jksoft | 0:7bc1d7c88b16 | 30 | |
jksoft | 0:7bc1d7c88b16 | 31 | int SendMail() |
jksoft | 0:7bc1d7c88b16 | 32 | { |
jksoft | 0:7bc1d7c88b16 | 33 | int ini_port; |
jksoft | 0:7bc1d7c88b16 | 34 | char ini_server[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 35 | char ini_domain[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 36 | char ini_user[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 37 | char ini_password[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 38 | |
jksoft | 0:7bc1d7c88b16 | 39 | ini.get("PORT", ini_port); |
jksoft | 0:7bc1d7c88b16 | 40 | ini.get("SERVER", ini_server , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 41 | ini.get("DOMAIN", ini_domain , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 42 | ini.get("USER", ini_user , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 43 | ini.get("PASSWORD", ini_password , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 44 | |
jksoft | 0:7bc1d7c88b16 | 45 | Host host(IpAddr(), ini_port, ini_server); |
jksoft | 0:7bc1d7c88b16 | 46 | SMTPClient smtp(host, ini_domain, ini_user, ini_password, SMTP_AUTH_PLAIN); |
jksoft | 0:7bc1d7c88b16 | 47 | |
jksoft | 0:7bc1d7c88b16 | 48 | char ini_from_addr[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 49 | char ini_to_addr[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 50 | char ini_msg[READ_BUF_SIZE]; |
jksoft | 0:7bc1d7c88b16 | 51 | |
jksoft | 0:7bc1d7c88b16 | 52 | ini.get("FROM_ADDRESS", ini_from_addr , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 53 | ini.get("TO_ADDRESS", ini_to_addr , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 54 | ini.get("MSG", ini_msg , READ_BUF_SIZE); |
jksoft | 0:7bc1d7c88b16 | 55 | |
jksoft | 0:7bc1d7c88b16 | 56 | time_t ctTime = time(NULL) + 32400; |
jksoft | 0:7bc1d7c88b16 | 57 | |
jksoft | 0:7bc1d7c88b16 | 58 | EmailMessage msg; |
jksoft | 0:7bc1d7c88b16 | 59 | msg.setFrom(ini_from_addr); |
jksoft | 0:7bc1d7c88b16 | 60 | msg.addTo(ini_to_addr); |
jksoft | 0:7bc1d7c88b16 | 61 | msg.printf("Subject: Mail %s", ctime(&ctTime)); |
jksoft | 0:7bc1d7c88b16 | 62 | msg.printf("%s",ini_msg); |
jksoft | 0:7bc1d7c88b16 | 63 | |
jksoft | 0:7bc1d7c88b16 | 64 | printf("Send result %d\n", smtp.send(&msg)); |
jksoft | 0:7bc1d7c88b16 | 65 | printf("Last response | %s", smtp.getLastResponse().c_str()); |
jksoft | 0:7bc1d7c88b16 | 66 | |
jksoft | 0:7bc1d7c88b16 | 67 | smtp.clearAuth(); |
jksoft | 0:7bc1d7c88b16 | 68 | |
jksoft | 0:7bc1d7c88b16 | 69 | return(0); |
jksoft | 0:7bc1d7c88b16 | 70 | } |
jksoft | 0:7bc1d7c88b16 | 71 | |
jksoft | 0:7bc1d7c88b16 | 72 | int CheckInputSw() |
jksoft | 0:7bc1d7c88b16 | 73 | { |
jksoft | 0:7bc1d7c88b16 | 74 | static int BeforeSw[3] = { 0,0,0 }; |
jksoft | 0:7bc1d7c88b16 | 75 | int NowSw[3]; |
jksoft | 0:7bc1d7c88b16 | 76 | int ret = 0; |
jksoft | 0:7bc1d7c88b16 | 77 | |
jksoft | 0:7bc1d7c88b16 | 78 | for(int i=0;i<3;i++) |
jksoft | 0:7bc1d7c88b16 | 79 | { |
jksoft | 0:7bc1d7c88b16 | 80 | NowSw[i] = sw[i]; |
jksoft | 0:7bc1d7c88b16 | 81 | |
jksoft | 0:7bc1d7c88b16 | 82 | if((BeforeSw[i])&&(NowSw[i]==0)) |
jksoft | 0:7bc1d7c88b16 | 83 | { |
jksoft | 0:7bc1d7c88b16 | 84 | ret |= 0x01 << i; |
jksoft | 0:7bc1d7c88b16 | 85 | } |
jksoft | 0:7bc1d7c88b16 | 86 | |
jksoft | 0:7bc1d7c88b16 | 87 | BeforeSw[i] = NowSw[i]; |
jksoft | 0:7bc1d7c88b16 | 88 | } |
jksoft | 0:7bc1d7c88b16 | 89 | |
jksoft | 0:7bc1d7c88b16 | 90 | if(ret != 0) |
jksoft | 0:7bc1d7c88b16 | 91 | { |
jksoft | 0:7bc1d7c88b16 | 92 | wait(0.2); |
jksoft | 0:7bc1d7c88b16 | 93 | |
jksoft | 0:7bc1d7c88b16 | 94 | } |
jksoft | 0:7bc1d7c88b16 | 95 | |
jksoft | 0:7bc1d7c88b16 | 96 | return(ret); |
jksoft | 0:7bc1d7c88b16 | 97 | } |
jksoft | 0:7bc1d7c88b16 | 98 | |
jksoft | 0:7bc1d7c88b16 | 99 | bool password_mode() |
jksoft | 0:7bc1d7c88b16 | 100 | { |
jksoft | 0:7bc1d7c88b16 | 101 | int input_num = 0; |
jksoft | 0:7bc1d7c88b16 | 102 | char input_button[4] = { 0,0,0,0 }; |
jksoft | 0:7bc1d7c88b16 | 103 | |
jksoft | 0:7bc1d7c88b16 | 104 | lcd.locate(0,1); |
jksoft | 0:7bc1d7c88b16 | 105 | lcd.printf("Password? "); |
jksoft | 0:7bc1d7c88b16 | 106 | |
jksoft | 0:7bc1d7c88b16 | 107 | timecount = 5; |
jksoft | 0:7bc1d7c88b16 | 108 | flipper.attach(&TimeCount, 1.0); |
jksoft | 0:7bc1d7c88b16 | 109 | |
jksoft | 0:7bc1d7c88b16 | 110 | while((input_num < 4)&&(timecount!=0)) |
jksoft | 0:7bc1d7c88b16 | 111 | { |
jksoft | 0:7bc1d7c88b16 | 112 | lcd.locate(10,0); |
jksoft | 0:7bc1d7c88b16 | 113 | lcd.printf("%dsec",timecount); |
jksoft | 0:7bc1d7c88b16 | 114 | int in = CheckInputSw(); |
jksoft | 0:7bc1d7c88b16 | 115 | |
jksoft | 0:7bc1d7c88b16 | 116 | if( in ) |
jksoft | 0:7bc1d7c88b16 | 117 | { |
jksoft | 0:7bc1d7c88b16 | 118 | input_button[input_num] = in; |
jksoft | 0:7bc1d7c88b16 | 119 | input_num++; |
jksoft | 0:7bc1d7c88b16 | 120 | lcd.locate(10+input_num,1); |
jksoft | 0:7bc1d7c88b16 | 121 | lcd.printf("*"); |
jksoft | 0:7bc1d7c88b16 | 122 | } |
jksoft | 0:7bc1d7c88b16 | 123 | } |
jksoft | 0:7bc1d7c88b16 | 124 | flipper.detach(); |
jksoft | 0:7bc1d7c88b16 | 125 | |
jksoft | 0:7bc1d7c88b16 | 126 | if(memcmp( &input_button[0] , &password[0],4) == 0) |
jksoft | 0:7bc1d7c88b16 | 127 | { |
jksoft | 0:7bc1d7c88b16 | 128 | lcd.locate(10,1); |
jksoft | 0:7bc1d7c88b16 | 129 | lcd.printf(" OK "); |
jksoft | 0:7bc1d7c88b16 | 130 | return(true); |
jksoft | 0:7bc1d7c88b16 | 131 | } |
jksoft | 0:7bc1d7c88b16 | 132 | else |
jksoft | 0:7bc1d7c88b16 | 133 | { |
jksoft | 0:7bc1d7c88b16 | 134 | lcd.locate(10,1); |
jksoft | 0:7bc1d7c88b16 | 135 | lcd.printf(" NG "); |
jksoft | 0:7bc1d7c88b16 | 136 | return(false); |
jksoft | 0:7bc1d7c88b16 | 137 | } |
jksoft | 0:7bc1d7c88b16 | 138 | |
jksoft | 0:7bc1d7c88b16 | 139 | return(false); |
jksoft | 0:7bc1d7c88b16 | 140 | } |
jksoft | 0:7bc1d7c88b16 | 141 | |
jksoft | 0:7bc1d7c88b16 | 142 | int main() { |
jksoft | 0:7bc1d7c88b16 | 143 | |
jksoft | 0:7bc1d7c88b16 | 144 | sensor.mode(PullDown); |
jksoft | 0:7bc1d7c88b16 | 145 | sw[0].mode(PullUp); |
jksoft | 0:7bc1d7c88b16 | 146 | sw[1].mode(PullUp); |
jksoft | 0:7bc1d7c88b16 | 147 | sw[2].mode(PullUp); |
jksoft | 0:7bc1d7c88b16 | 148 | |
jksoft | 0:7bc1d7c88b16 | 149 | lcd.cls(); |
jksoft | 0:7bc1d7c88b16 | 150 | lcd.locate(0,0); |
jksoft | 0:7bc1d7c88b16 | 151 | lcd.printf("EtherSetting.."); |
jksoft | 0:7bc1d7c88b16 | 152 | |
jksoft | 0:7bc1d7c88b16 | 153 | EthernetErr ethErr; |
jksoft | 0:7bc1d7c88b16 | 154 | |
jksoft | 0:7bc1d7c88b16 | 155 | do { |
jksoft | 0:7bc1d7c88b16 | 156 | ethErr = eth.setup(); |
jksoft | 0:7bc1d7c88b16 | 157 | if (ethErr) printf("Timeout\n", ethErr); |
jksoft | 0:7bc1d7c88b16 | 158 | } while (ethErr != ETH_OK); |
jksoft | 0:7bc1d7c88b16 | 159 | |
jksoft | 0:7bc1d7c88b16 | 160 | |
jksoft | 0:7bc1d7c88b16 | 161 | #if 0 |
jksoft | 0:7bc1d7c88b16 | 162 | printf("Connected OK\n"); |
jksoft | 0:7bc1d7c88b16 | 163 | const char* hwAddr = eth.getHwAddr(); |
jksoft | 0:7bc1d7c88b16 | 164 | printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n", |
jksoft | 0:7bc1d7c88b16 | 165 | hwAddr[0], hwAddr[1], hwAddr[2], |
jksoft | 0:7bc1d7c88b16 | 166 | hwAddr[3], hwAddr[4], hwAddr[5]); |
jksoft | 0:7bc1d7c88b16 | 167 | |
jksoft | 0:7bc1d7c88b16 | 168 | IpAddr ethIp = eth.getIp(); |
jksoft | 0:7bc1d7c88b16 | 169 | printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]); |
jksoft | 0:7bc1d7c88b16 | 170 | #endif |
jksoft | 0:7bc1d7c88b16 | 171 | |
jksoft | 0:7bc1d7c88b16 | 172 | // Get time |
jksoft | 0:7bc1d7c88b16 | 173 | NTPClient ntp; |
jksoft | 0:7bc1d7c88b16 | 174 | //printf("NTP setTime...\n"); |
jksoft | 0:7bc1d7c88b16 | 175 | Host server(IpAddr(), 123, "pool.ntp.org"); |
jksoft | 0:7bc1d7c88b16 | 176 | ntp.setTime(server); |
jksoft | 0:7bc1d7c88b16 | 177 | time_t ctTime = time(NULL) + 32400; |
jksoft | 0:7bc1d7c88b16 | 178 | //printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime)); |
jksoft | 0:7bc1d7c88b16 | 179 | |
jksoft | 0:7bc1d7c88b16 | 180 | Timer tm; |
jksoft | 0:7bc1d7c88b16 | 181 | tm.start(); |
jksoft | 0:7bc1d7c88b16 | 182 | |
jksoft | 0:7bc1d7c88b16 | 183 | bool checking = false; |
jksoft | 0:7bc1d7c88b16 | 184 | |
jksoft | 0:7bc1d7c88b16 | 185 | while (true) { |
jksoft | 0:7bc1d7c88b16 | 186 | |
jksoft | 0:7bc1d7c88b16 | 187 | time_t ctTime = time(NULL) + 32400; |
jksoft | 0:7bc1d7c88b16 | 188 | struct tm *timeObject = localtime(&ctTime); |
jksoft | 0:7bc1d7c88b16 | 189 | lcd.locate(0,1); |
jksoft | 0:7bc1d7c88b16 | 190 | lcd.printf("%02d/%02d %02d:%02d:%02d", timeObject->tm_mon,timeObject->tm_mday,timeObject->tm_hour,timeObject->tm_min,timeObject->tm_sec); |
jksoft | 0:7bc1d7c88b16 | 191 | |
jksoft | 0:7bc1d7c88b16 | 192 | if( checking ) |
jksoft | 0:7bc1d7c88b16 | 193 | { |
jksoft | 0:7bc1d7c88b16 | 194 | lcd.locate(0,0); |
jksoft | 0:7bc1d7c88b16 | 195 | lcd.printf("Watching Mode"); |
jksoft | 0:7bc1d7c88b16 | 196 | if (tm.read() > 0.5) { |
jksoft | 0:7bc1d7c88b16 | 197 | led1 = !led1; |
jksoft | 0:7bc1d7c88b16 | 198 | |
jksoft | 0:7bc1d7c88b16 | 199 | if(sensor) |
jksoft | 0:7bc1d7c88b16 | 200 | // if(CheckInputSw() == 0x04) |
jksoft | 0:7bc1d7c88b16 | 201 | { |
jksoft | 0:7bc1d7c88b16 | 202 | lcd.cls(); |
jksoft | 0:7bc1d7c88b16 | 203 | lcd.locate(0,0); |
jksoft | 0:7bc1d7c88b16 | 204 | lcd.printf("Alert On!"); |
jksoft | 0:7bc1d7c88b16 | 205 | if(password_mode()) |
jksoft | 0:7bc1d7c88b16 | 206 | { |
jksoft | 0:7bc1d7c88b16 | 207 | wait(0.5); |
jksoft | 0:7bc1d7c88b16 | 208 | } |
jksoft | 0:7bc1d7c88b16 | 209 | else |
jksoft | 0:7bc1d7c88b16 | 210 | { |
jksoft | 0:7bc1d7c88b16 | 211 | lcd.locate(0,0); |
jksoft | 0:7bc1d7c88b16 | 212 | lcd.printf("Send Alert Mail!"); |
jksoft | 0:7bc1d7c88b16 | 213 | SendMail(); |
jksoft | 0:7bc1d7c88b16 | 214 | |
jksoft | 0:7bc1d7c88b16 | 215 | mbed_reset(); |
jksoft | 0:7bc1d7c88b16 | 216 | } |
jksoft | 0:7bc1d7c88b16 | 217 | checking = false; |
jksoft | 0:7bc1d7c88b16 | 218 | lcd.cls(); |
jksoft | 0:7bc1d7c88b16 | 219 | } |
jksoft | 0:7bc1d7c88b16 | 220 | tm.start(); |
jksoft | 0:7bc1d7c88b16 | 221 | } |
jksoft | 0:7bc1d7c88b16 | 222 | } |
jksoft | 0:7bc1d7c88b16 | 223 | else |
jksoft | 0:7bc1d7c88b16 | 224 | { |
jksoft | 0:7bc1d7c88b16 | 225 | lcd.locate(0,0); |
jksoft | 0:7bc1d7c88b16 | 226 | lcd.printf("Free Mode"); |
jksoft | 0:7bc1d7c88b16 | 227 | |
jksoft | 0:7bc1d7c88b16 | 228 | if( CheckInputSw() == 0x01 ) |
jksoft | 0:7bc1d7c88b16 | 229 | { |
jksoft | 0:7bc1d7c88b16 | 230 | lcd.locate(0,0); |
jksoft | 0:7bc1d7c88b16 | 231 | lcd.printf("Mode Change..."); |
jksoft | 0:7bc1d7c88b16 | 232 | checking = true; |
jksoft | 0:7bc1d7c88b16 | 233 | wait(5); |
jksoft | 0:7bc1d7c88b16 | 234 | lcd.cls(); |
jksoft | 0:7bc1d7c88b16 | 235 | } |
jksoft | 0:7bc1d7c88b16 | 236 | } |
jksoft | 0:7bc1d7c88b16 | 237 | Net::poll(); |
jksoft | 0:7bc1d7c88b16 | 238 | } |
jksoft | 0:7bc1d7c88b16 | 239 | } |