Dependencies:   EthernetNetIf NTPClient_NetServices mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "EthernetNetIf.h"
00004 #include "HTTPClient.h"
00005 #include "NTPClient.h"
00006 #include "ItemSet.h"
00007 #include "member.h"
00008 #include "MyRFID.h"
00009 
00010 
00011 EthernetNetIf eth;
00012 NTPClient ntp;
00013 
00014 LocalFileSystem local("local");
00015 
00016 Serial pc(USBTX, USBRX); // tx, rx
00017 
00018 MyRFID rfid(p28, p27);//tx,rx
00019 InterruptIn rfid_irq(p21);
00020 
00021 TextLCD lcd(p5, p6, p11, p12, p13, p14); // rs, e, d0-d3
00022 
00023 BusOut myleds(LED1, LED2, LED3, LED4);
00024 
00025 const char user[] = "username";
00026 const char pass[] = "password";
00027 const char login_msg[] = " is login now.";
00028 const char logout_msg[] = " is logout now.";
00029 const char url[]  = "http://api.supertweet.net/1/statuses/update.xml";
00030 const char ntp_server[]  = "0.uk.pool.ntp.org";
00031 
00032 bool card_flag=false;
00033 
00034 void cardIRQ() {
00035     card_flag=true;
00036 }
00037 
00038 void LED_KnightRider(int rideTime) {
00039         for (int i=0; i<4; i++) {
00040             myleds = 1 << i;
00041             wait_ms(rideTime);
00042         }
00043         for (int i=0;i<3;i++) {
00044             myleds =4>>i;
00045             wait_ms(rideTime);
00046         }
00047     myleds=0;
00048 }
00049 
00050 int main() {
00051     ItemSet memberList;
00052     Object* theObj;
00053 
00054     rfid.baud(19200);
00055     printf("Init\n");
00056     lcd.printf("Init\n");
00057 
00058     char sendmsg[255];
00059     char buf[40];
00060     time_t ctTime;
00061     HTTPClient twitter;
00062     HTTPMap msg;
00063     
00064     unsigned int id;
00065     
00066     //ethernet setup
00067     printf("Setting up...\n");
00068     EthernetErr ethErr = eth.setup();
00069     if(ethErr)
00070     {
00071       printf("Error %d in setup.\n", ethErr);
00072       return -1;
00073     }
00074     printf("Setup OK\r\n");
00075 
00076     //twitter auth
00077     twitter.basicAuth(user, pass); //We use basic authentication, replace with you account's parameters
00078     
00079     //RTC setup
00080     Host server(IpAddr(), 123, ntp_server);
00081     ntp.setTime(server);
00082 
00083     //read member.txt
00084     pc.printf("Opening File...\n"); // Drive should be marked as removed
00085     FILE *fp = fopen("/local/member.txt", "r");
00086     char name_buf[255];
00087     while (fscanf( fp, "%[^,],%d\n",name_buf, &id) != EOF ) {
00088         memberList.AddItem(new member(name_buf,id));
00089     }
00090     delete [] name_buf;
00091     fclose(fp);
00092 
00093     rfid_irq.rise(&cardIRQ);
00094 
00095     while (1) {
00096         lcd.locate(0,0);
00097         ctTime = time(NULL)+32400;//JST time
00098         strftime(buf,sizeof(buf),"%Y/%m/%d %a\n%H:%M:%S  Ready!", localtime(&ctTime));
00099         printf("%s\n", buf);
00100         lcd.printf("%s", buf);
00101         wait(0.5);
00102 
00103         if (card_flag) {
00104             lcd.cls();
00105             strcpy(sendmsg,"");
00106 
00107             id = rfid.read();
00108             pc.printf("id=%u\n",id);
00109             theObj = memberList.SerchId(id);//checkId
00110 
00111             if (theObj != NULL) {//list has same ID
00112                 lcd.locate(0,0);
00113                 lcd.printf("%s\n",((member*)theObj)->GetName());
00114                 lcd.printf("id=%u",((member*)theObj)->GetId());
00115 
00116                 strcat(sendmsg,((member*)theObj)->GetName());
00117                 strftime(buf,sizeof(buf), " (%Y/%m/%d %a %H:%M:%S)", localtime(&ctTime));
00118 
00119                 if (((member*)theObj)->GetEnter()) {//member is logined
00120                     strcat(sendmsg,logout_msg);
00121                     strcat(sendmsg,buf);
00122                     pc.printf("%s\n",sendmsg);
00123                     msg["status"] = sendmsg;
00124 
00125                     ((member*)theObj)->SetEnter(false);
00126                 } else {//member is logouted
00127                     strcat(sendmsg,login_msg);
00128                     strcat(sendmsg,buf);
00129                     pc.printf("%s\n",sendmsg);
00130                     msg["status"] = sendmsg;
00131 
00132                     ((member*)theObj)->SetEnter(true);
00133                 }
00134                 twitter.post(url, msg, NULL);//tweet
00135 
00136                 LED_KnightRider(50);
00137                 LED_KnightRider(50);
00138 
00139                 lcd.cls();
00140             } else {//list no have id
00141                 lcd.cls();
00142                 lcd.locate(0,0);
00143                 lcd.printf("Who are you?\n");
00144                 lcd.printf("id=%u\n",id);
00145                 wait(5);
00146                 
00147                 lcd.cls();
00148             }
00149             card_flag=false;
00150         }
00151     }
00152 }
00153 
00154