Send Location and current time from GPS data to Twitter with Proxy server

Dependencies:   GPS WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "GPS.h"
00005 
00006 GPS gpsAda(D1, D0, 9600);
00007 
00008 InterruptIn mysw(D7);
00009 DigitalOut myled1(LED3);
00010 
00011 #define TOKEN "3252156354-fG0b1utXYAg5IqeJNMSJFlenx1rgSRXm5wgk21l"
00012 
00013 void exin() {
00014     
00015     int phy_link;
00016     printf("Wait a second...\r\n");
00017     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x03, 0x04, 0x02}; 
00018     
00019     EthernetInterface eth;
00020     eth.init(mac_addr); //Use DHCP
00021     
00022     while(1){
00023         
00024     eth.connect();
00025     
00026     /* phy link */
00027      do{
00028         phy_link = eth.ethernet_link();
00029         printf("...");
00030         wait(2);
00031      }while(!phy_link);
00032     printf("\r\n");
00033      
00034     printf("IP Address is %s\r\n", eth.getIPAddress());
00035     
00036     
00037     /* TCP socket connect */
00038     TCPSocketConnection sock;
00039     sock.connect("arduino-tweet.appspot.com", 80);
00040     
00041     printf("connected\r\n");
00042    
00043     float longi = 0;
00044     float latit = 0;
00045     int time;
00046     
00047     char message1[] = "Alert!!\r\nSunae-Station";
00048     char message2[] = "\r\nTime:";
00049     
00050     char message[36] = {0};
00051     
00052     /* get gps data */
00053     if(gpsAda.sample()) 
00054     {      
00055         longi = gpsAda.longitude;
00056         latit = gpsAda.latitude;
00057         time = gpsAda.time + 90000; 
00058 //       printf("%f,%f,%d\r\n\r\n",longi,latit,time);
00059     }
00060     
00061     /* gps time data */
00062     char time_data[6]={0};
00063     int hour10 = 0;
00064     int hour1 = 0;
00065     int min10 = 0;
00066     int min1 = 0;
00067     int sec10 = 0;
00068     int sec1 = 0;
00069 
00070     hour10 = time/100000;
00071     hour1 = (time-hour10*100000)/10000;
00072     min10 = (time-hour10*100000-hour1*10000)/1000;
00073     min1 = (time-hour10*100000-hour1*10000-min10*1000)/100;
00074     sec10 = (time-hour10*100000-hour1*10000-min10*1000-min1*100)/10;
00075     sec1 = time-hour10*100000-hour1*10000-min10*1000-min1*100-sec10*10;
00076     
00077     time_data[5] = sec1 + 48;
00078     time_data[4] = sec10 + 48;
00079     time_data[3] = min1 + 48;
00080     time_data[2] = min10 + 48;
00081     time_data[1] = hour1 + 48;
00082     time_data[0] = hour10 + 48;
00083 
00084 //    printf("%s\r\n",time_data);
00085 
00086     
00087     /* gps location data, time data on message */
00088     if((longi> 27.109287)&&(longi<127.120232))
00089     {
00090         if((latit>37.374869)&&(latit<37.381587))
00091         {
00092 //            printf("alright\r\n");
00093             sprintf(message, "%s%s%s", message1, message2, time_data);
00094         }else{
00095             sprintf(message, "%s%s%s", message1, message2, time_data);
00096         }
00097     }else{
00098         sprintf(message, "%s%s%s", message1, message2, time_data);
00099     }
00100 
00101     //sprintf(message, "%s%s%s", message1, message2, time_data);
00102 
00103     /* data length measure */
00104     char data_len[2]={0};
00105     int ten=0;
00106     int one=0;
00107     
00108     int length = sizeof(message) - 1 + sizeof(TOKEN) - 1 + 14;
00109     
00110 //    printf("%d\r\n\r\n",length);
00111     
00112     ten = length/10;
00113     one = length%10;
00114     data_len[1] = one + 48;
00115     data_len[0] = ten + 48;
00116 
00117     /* send message on format data to proxy server */
00118     char *cmd1 = "POST http://arduino-tweet.appspot.com/update HTTP/1.0\r\nContent-Length:";
00119     char *cmd2 = data_len;
00120     char *cmd3 = "\r\n\r\ntoken=";
00121     char *cmd4 = TOKEN;
00122     char *cmd5 = "&status=";
00123     char *cmd6 = message;
00124     char *cmd7 = "\r\n";
00125     
00126     char send_data[1024];
00127     sprintf(send_data, "%s%s%s%s%s%s%s", cmd1, cmd2, cmd3, cmd4, cmd5, cmd6, cmd7);
00128 
00129     sock.send_all(send_data, sizeof(send_data)-1);
00130     
00131     printf("%s\r\n",send_data);
00132     printf("send message done\r\n");
00133     
00134     /* receive data from server */
00135     char buffer[1024];
00136     
00137     sock.receive(buffer, sizeof(buffer)-1); 
00138     printf("%s\r\n",buffer);
00139     
00140     /* sock close */
00141     sock.close();
00142     
00143     eth.disconnect();
00144     
00145     wait(70.0);
00146     
00147     };
00148 
00149 }
00150 
00151 int main() {
00152     
00153     printf("Run now...\r\n");
00154     
00155     mysw.rise(&exin);
00156     
00157     while(1) {   
00158         myled1 = !myled1;
00159         wait(1.0);
00160     } 
00161 }
00162 
00163  
00164 
00165 /*
00166 #include "mbed.h"
00167 #include "EthernetInterface.h"
00168 
00169 #define TOKEN "3252156354-fG0b1utXYAg5IqeJNMSJFlenx1rgSRXm5wgk21l"
00170 
00171 int main()
00172 {
00173 
00174     int phy_link;
00175     printf("wait a second...\r\n");
00176     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x03, 0x04, 0x02};
00177 
00178     EthernetInterface eth;
00179     eth.init(mac_addr); //Use DHCP
00180 
00181     while(1) {
00182 
00183         eth.connect();
00184 
00185          phy link 
00186         do {
00187             phy_link = eth.ethernet_link();
00188             printf("...");
00189             wait(2);
00190         } while(!phy_link);
00191         printf("\r\n");
00192 
00193         printf("IP Address is %s\r\n", eth.getIPAddress());
00194 
00195          TCP socket connect 
00196         TCPSocketConnection sock;
00197         sock.connect("arduino-tweet.appspot.com", 80);
00198 
00199         printf("connected\r\n");
00200 
00201         char message[] = "test1234";
00202 
00203         char len[10];
00204         char str[10];
00205 
00206         int length = sizeof(message) - 1 + sizeof(TOKEN) - 1 + 14;
00207 
00208         printf("%d\r\n",length);
00209 
00210           
00211         char *cmd1 = "POST http://arduino-tweet.appspot.com/update HTTP/1.0\r\nContent-Length:";
00212         char *cmd2 = "72";
00213         char *cmd3 = "\r\n\r\ntoken=";
00214         char *cmd4 = TOKEN;
00215         char *cmd5 = "&status=";
00216         char *cmd6 = message;
00217         char *cmd7 = "\r\n";
00218 
00219 
00220         char send_data[1024];
00221         char buffer[1024];
00222         sprintf(send_data, "%s%s%s%s%s%s%s", cmd1, cmd2, cmd3, cmd4, cmd5, cmd6, cmd7);
00223 
00224         sock.send_all(send_data, sizeof(send_data)-1);
00225 
00226         printf("%s\r\n",send_data);
00227         printf("send message done\r\n");
00228 
00229         sock.receive(buffer, sizeof(buffer)-1);
00230 
00231         printf("%s\r\n",buffer);
00232 
00233         sock.close();
00234 
00235         eth.disconnect();
00236 
00237         wait(70.0);
00238 
00239     };
00240 
00241 }
00242 
00243 */