Dependencies: EthernetNetIf NTPClient_NetServices mbed
main.cpp@0:d441275f622f, 2010-08-09 (annotated)
- Committer:
- nucho
- Date:
- Mon Aug 09 07:01:43 2010 +0000
- Revision:
- 0:d441275f622f
- Child:
- 1:b494b1b91ba3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucho | 0:d441275f622f | 1 | #include "mbed.h" |
nucho | 0:d441275f622f | 2 | #include "TextLCD.h" |
nucho | 0:d441275f622f | 3 | #include "EthernetNetIf.h" |
nucho | 0:d441275f622f | 4 | #include "HTTPClient.h" |
nucho | 0:d441275f622f | 5 | #include "NTPClient.h" |
nucho | 0:d441275f622f | 6 | #include "ItemSet.h" |
nucho | 0:d441275f622f | 7 | #include "member.h" |
nucho | 0:d441275f622f | 8 | #include "MyRFID.h" |
nucho | 0:d441275f622f | 9 | |
nucho | 0:d441275f622f | 10 | |
nucho | 0:d441275f622f | 11 | EthernetNetIf eth; |
nucho | 0:d441275f622f | 12 | NTPClient ntp; |
nucho | 0:d441275f622f | 13 | |
nucho | 0:d441275f622f | 14 | LocalFileSystem local("local"); |
nucho | 0:d441275f622f | 15 | |
nucho | 0:d441275f622f | 16 | Serial pc(USBTX, USBRX); // tx, rx |
nucho | 0:d441275f622f | 17 | |
nucho | 0:d441275f622f | 18 | MyRFID rfid(p28, p27);//tx,rx |
nucho | 0:d441275f622f | 19 | InterruptIn rfid_irq(p21); |
nucho | 0:d441275f622f | 20 | |
nucho | 0:d441275f622f | 21 | TextLCD lcd(p5, p6, p11, p12, p13, p14); // rs, e, d0-d3 |
nucho | 0:d441275f622f | 22 | |
nucho | 0:d441275f622f | 23 | BusOut myleds(LED1, LED2, LED3, LED4); |
nucho | 0:d441275f622f | 24 | |
nucho | 0:d441275f622f | 25 | const char user[] = "hamrobot"; |
nucho | 0:d441275f622f | 26 | const char pass[] = "kesi56KN"; |
nucho | 0:d441275f622f | 27 | const char login_msg[] = " is login now."; |
nucho | 0:d441275f622f | 28 | const char logout_msg[] = " is logout now."; |
nucho | 0:d441275f622f | 29 | const char url[] = "http://api.supertweet.net/1/statuses/update.xml"; |
nucho | 0:d441275f622f | 30 | const char ntp_server[] = "0.uk.pool.ntp.org"; |
nucho | 0:d441275f622f | 31 | |
nucho | 0:d441275f622f | 32 | bool card_flag=false; |
nucho | 0:d441275f622f | 33 | |
nucho | 0:d441275f622f | 34 | void cardIRQ() { |
nucho | 0:d441275f622f | 35 | card_flag=true; |
nucho | 0:d441275f622f | 36 | } |
nucho | 0:d441275f622f | 37 | |
nucho | 0:d441275f622f | 38 | void LED_KnightRider(int rideTime) { |
nucho | 0:d441275f622f | 39 | for (int i=0; i<4; i++) { |
nucho | 0:d441275f622f | 40 | myleds = 1 << i; |
nucho | 0:d441275f622f | 41 | wait_ms(rideTime); |
nucho | 0:d441275f622f | 42 | } |
nucho | 0:d441275f622f | 43 | for (int i=0;i<3;i++) { |
nucho | 0:d441275f622f | 44 | myleds =4>>i; |
nucho | 0:d441275f622f | 45 | wait_ms(rideTime); |
nucho | 0:d441275f622f | 46 | } |
nucho | 0:d441275f622f | 47 | myleds=0; |
nucho | 0:d441275f622f | 48 | } |
nucho | 0:d441275f622f | 49 | |
nucho | 0:d441275f622f | 50 | int main() { |
nucho | 0:d441275f622f | 51 | ItemSet memberList; |
nucho | 0:d441275f622f | 52 | Object* theObj; |
nucho | 0:d441275f622f | 53 | |
nucho | 0:d441275f622f | 54 | rfid.baud(19200); |
nucho | 0:d441275f622f | 55 | printf("Init\n"); |
nucho | 0:d441275f622f | 56 | lcd.printf("Init\n"); |
nucho | 0:d441275f622f | 57 | |
nucho | 0:d441275f622f | 58 | char sendmsg[255]; |
nucho | 0:d441275f622f | 59 | char buf[40]; |
nucho | 0:d441275f622f | 60 | time_t ctTime; |
nucho | 0:d441275f622f | 61 | HTTPClient twitter; |
nucho | 0:d441275f622f | 62 | HTTPMap msg; |
nucho | 0:d441275f622f | 63 | |
nucho | 0:d441275f622f | 64 | unsigned int id; |
nucho | 0:d441275f622f | 65 | |
nucho | 0:d441275f622f | 66 | //ethernet setup |
nucho | 0:d441275f622f | 67 | printf("Setting up...\n"); |
nucho | 0:d441275f622f | 68 | EthernetErr ethErr = eth.setup(); |
nucho | 0:d441275f622f | 69 | if(ethErr) |
nucho | 0:d441275f622f | 70 | { |
nucho | 0:d441275f622f | 71 | printf("Error %d in setup.\n", ethErr); |
nucho | 0:d441275f622f | 72 | return -1; |
nucho | 0:d441275f622f | 73 | } |
nucho | 0:d441275f622f | 74 | printf("Setup OK\r\n"); |
nucho | 0:d441275f622f | 75 | |
nucho | 0:d441275f622f | 76 | //twitter auth |
nucho | 0:d441275f622f | 77 | twitter.basicAuth(user, pass); //We use basic authentication, replace with you account's parameters |
nucho | 0:d441275f622f | 78 | |
nucho | 0:d441275f622f | 79 | //RTC setup |
nucho | 0:d441275f622f | 80 | Host server(IpAddr(), 123, ntp_server); |
nucho | 0:d441275f622f | 81 | ntp.setTime(server); |
nucho | 0:d441275f622f | 82 | |
nucho | 0:d441275f622f | 83 | //read member.txt |
nucho | 0:d441275f622f | 84 | pc.printf("Opening File...\n"); // Drive should be marked as removed |
nucho | 0:d441275f622f | 85 | FILE *fp = fopen("/local/member.txt", "r"); |
nucho | 0:d441275f622f | 86 | char name_buf[255]; |
nucho | 0:d441275f622f | 87 | while (fscanf( fp, "%[^,],%d\n",name_buf, &id) != EOF ) { |
nucho | 0:d441275f622f | 88 | memberList.AddItem(new member(name_buf,id)); |
nucho | 0:d441275f622f | 89 | } |
nucho | 0:d441275f622f | 90 | delete [] name_buf; |
nucho | 0:d441275f622f | 91 | fclose(fp); |
nucho | 0:d441275f622f | 92 | |
nucho | 0:d441275f622f | 93 | rfid_irq.rise(&cardIRQ); |
nucho | 0:d441275f622f | 94 | |
nucho | 0:d441275f622f | 95 | while (1) { |
nucho | 0:d441275f622f | 96 | lcd.locate(0,0); |
nucho | 0:d441275f622f | 97 | ctTime = time(NULL)+32400;//JST time |
nucho | 0:d441275f622f | 98 | strftime(buf,sizeof(buf),"%Y/%m/%d %a\n%H:%M:%S Ready!", localtime(&ctTime)); |
nucho | 0:d441275f622f | 99 | printf("%s\n", buf); |
nucho | 0:d441275f622f | 100 | lcd.printf("%s", buf); |
nucho | 0:d441275f622f | 101 | wait(0.5); |
nucho | 0:d441275f622f | 102 | |
nucho | 0:d441275f622f | 103 | if (card_flag) { |
nucho | 0:d441275f622f | 104 | lcd.cls(); |
nucho | 0:d441275f622f | 105 | strcpy(sendmsg,""); |
nucho | 0:d441275f622f | 106 | |
nucho | 0:d441275f622f | 107 | id = rfid.read(); |
nucho | 0:d441275f622f | 108 | pc.printf("id=%u\n",id); |
nucho | 0:d441275f622f | 109 | theObj = memberList.SerchId(id);//checkId |
nucho | 0:d441275f622f | 110 | |
nucho | 0:d441275f622f | 111 | if (theObj != NULL) {//list has same ID |
nucho | 0:d441275f622f | 112 | lcd.locate(0,0); |
nucho | 0:d441275f622f | 113 | lcd.printf("%s\n",((member*)theObj)->GetName()); |
nucho | 0:d441275f622f | 114 | lcd.printf("id=%u",((member*)theObj)->GetId()); |
nucho | 0:d441275f622f | 115 | |
nucho | 0:d441275f622f | 116 | strcat(sendmsg,((member*)theObj)->GetName()); |
nucho | 0:d441275f622f | 117 | strftime(buf,sizeof(buf), " (%Y/%m/%d %a %H:%M:%S)", localtime(&ctTime)); |
nucho | 0:d441275f622f | 118 | |
nucho | 0:d441275f622f | 119 | if (((member*)theObj)->GetEnter()) {//member is logined |
nucho | 0:d441275f622f | 120 | strcat(sendmsg,logout_msg); |
nucho | 0:d441275f622f | 121 | strcat(sendmsg,buf); |
nucho | 0:d441275f622f | 122 | pc.printf("%s\n",sendmsg); |
nucho | 0:d441275f622f | 123 | msg["status"] = sendmsg; |
nucho | 0:d441275f622f | 124 | |
nucho | 0:d441275f622f | 125 | ((member*)theObj)->SetEnter(false); |
nucho | 0:d441275f622f | 126 | } else {//member is logouted |
nucho | 0:d441275f622f | 127 | strcat(sendmsg,login_msg); |
nucho | 0:d441275f622f | 128 | strcat(sendmsg,buf); |
nucho | 0:d441275f622f | 129 | pc.printf("%s\n",sendmsg); |
nucho | 0:d441275f622f | 130 | msg["status"] = sendmsg; |
nucho | 0:d441275f622f | 131 | |
nucho | 0:d441275f622f | 132 | ((member*)theObj)->SetEnter(true); |
nucho | 0:d441275f622f | 133 | } |
nucho | 0:d441275f622f | 134 | twitter.post(url, msg, NULL);//tweet |
nucho | 0:d441275f622f | 135 | |
nucho | 0:d441275f622f | 136 | LED_KnightRider(50); |
nucho | 0:d441275f622f | 137 | LED_KnightRider(50); |
nucho | 0:d441275f622f | 138 | |
nucho | 0:d441275f622f | 139 | lcd.cls(); |
nucho | 0:d441275f622f | 140 | } else {//list no have id |
nucho | 0:d441275f622f | 141 | lcd.cls(); |
nucho | 0:d441275f622f | 142 | lcd.locate(0,0); |
nucho | 0:d441275f622f | 143 | lcd.printf("Who are you?\n"); |
nucho | 0:d441275f622f | 144 | lcd.printf("id=%u\n",id); |
nucho | 0:d441275f622f | 145 | wait(5); |
nucho | 0:d441275f622f | 146 | |
nucho | 0:d441275f622f | 147 | lcd.cls(); |
nucho | 0:d441275f622f | 148 | } |
nucho | 0:d441275f622f | 149 | card_flag=false; |
nucho | 0:d441275f622f | 150 | } |
nucho | 0:d441275f622f | 151 | } |
nucho | 0:d441275f622f | 152 | } |
nucho | 0:d441275f622f | 153 | |
nucho | 0:d441275f622f | 154 |