Ryo Iizuka / Mbed 2 deprecated HttpClientSamlpe

Dependencies:   NyFileSystems libMiMic mbed-rtos mbed

Fork of TcpSocketClientSamlpe by Ryo Iizuka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * TCP client socket sample.<br/>
00004  * This program is to test of TCP client.
00005  * Connect to a TCP server, and send back the received data as is.
00006  * 
00007  */
00008 #include "mbed.h"
00009 #include "rtos.h"
00010 #include "SDFileSystem.h"
00011 #include "mimic.h"
00012 #include "utils/PlatformInfo.h"
00013 #include "fsdata.h"
00014 
00015 LocalFileSystem2 lf("local");
00016 
00017 DigitalOut led1(LED1);
00018 DigitalOut led2(LED2);
00019 DigitalOut led3(LED3);
00020 DigitalOut led4(LED4);
00021 
00022     MiMicNetIf netif;
00023     NetConfig cfg; //create network configulation  with onchip-setting.
00024 
00025 int main()
00026 {
00027     Net net(netif);//Net constructor must be created after started RTOS
00028 
00029     // manual setting
00030     cfg.setIpAddr(192,168,128,39);
00031     cfg.setNetMask(255,255,255,0);
00032     cfg.setGateway(192,168,128,254);    
00033     cfg.setSrvUPnP(false);
00034     cfg.setSrvMdns(false);
00035 
00036     // Create http client.
00037     // Socket must create between "net.start" with "new Net()"
00038     HttpClient http;
00039 led1=1;
00040     
00041     //Start network
00042     net.start(cfg);
00043 led2=2;
00044     if(http.connect(IpAddr(192,168,128,1),80)){
00045         if(http.sendMethod(HttpClient::HTTP_GET,"/mimic/")){            
00046             FILE *fp=fopen("/local/out.txt", "w");
00047             if(fp!=NULL){            
00048                 int s=http.getStatus();
00049                 fprintf(fp, "Status:%d\n",s);
00050                 if(s==200){
00051                     for(;;){
00052                         short l;
00053                         char b[32];
00054                         if(!http.read(b,32,l)){
00055                             //Error
00056                             led1=1;
00057                             break;
00058                         }
00059                         if(fwrite(b,1,l,fp)<l){
00060                             //EOS
00061                             led2=1;
00062                             break;
00063                         }
00064                         if(l==0){
00065                             //EOS
00066                             led3=1;
00067                             break;
00068                         }
00069                     }
00070                 }            
00071                 fclose(fp);
00072             }
00073         }
00074         http.close();
00075     }
00076     if(http.connect(IpAddr(192,168,128,254),80)){
00077         if(http.sendMethod(HttpClient::HTTP_POST,"/mimic/")){
00078             const char* DATA="{json}";
00079             if(http.write(DATA,strlen(DATA))){
00080                 if(http.getStatus()==200){
00081                     char buf[256];
00082                     short len;
00083                     if(http.read(buf,256,len)){
00084                         printf("%.*s",len,buf);
00085                     }
00086                 }
00087             }
00088         }
00089         http.close();
00090     }    
00091     for(int c=0;;c=(c+1)%2){
00092         led4=c;
00093         Thread::wait(500);
00094     }
00095     return 0;
00096 }
00097