Andrea Erbisti / Mbed 2 deprecated X-Nucleo_HTTP_POST

Dependencies:   NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of TestUpload by Fabio Dal Forno

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* SpwfInterface NetworkSocketAPI Example Program
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "SpwfInterface.h"
00019 #include "TCPSocket.h"
00020 
00021 
00022 
00023 //------------------------------------
00024 // Hyperterminal configuration
00025 // 9600 bauds, 8-bit data, no parity
00026 //------------------------------------
00027 
00028 Serial pc(USBTX, USBRX);
00029 DigitalOut myled(LED1);
00030 
00031 SpwfSAInterface spwf(D8, D2, false);
00032 TCPSocket socket;
00033 
00034 int main() { 
00035     char* ssid = "fablab_guest";
00036     char* seckey = "veronafablab";
00037     
00038     pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
00039     pc.printf("\r\nconnecting to AP\r\n");
00040     
00041     if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) {      
00042         pc.printf("\r\nnow connected\r\n");
00043     } else {
00044         pc.printf("\r\nerror connecting to AP.\r\n");
00045         return -1;
00046     }   
00047  
00048     const char *ip = spwf.get_ip_address();
00049     const char *mac = spwf.get_mac_address();
00050     
00051     pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP");
00052     pc.printf("MAC Address is: %s\r\n", (mac) ? mac : "No MAC"); 
00053        
00054  
00055     socket.open(&spwf);
00056     socket.connect("fabio.infosistem.it", 80);
00057     char url[] = "http://fabio.infosistem.it/post.php";
00058     char host[] = "fabio.infosistem.it";
00059     char path[] = "/post.php";
00060     char meth[] = "POST"; //http method ('POST', 'GET', 'PUT', 'DELETE')
00061     //char key[] = "Hello";
00062     //char value[] = "World";
00063     // Body of the message 
00064     char data[] = "Hello=World";
00065     //snprintf(data, sizeof(data), "%s=%s", key, value);
00066     printf("\r\n%s\r\n", data);
00067     char type[] = "Content-Type: application/x-www-form-urlencoded";
00068     char length[32];
00069     snprintf(length, sizeof(length), "Content-Length: %s", sizeof(data));
00070     // Send a simple http request
00071     char sbuffer[256];
00072     snprintf(sbuffer,sizeof(sbuffer),"%s %s HTTP/1.1\r\n\%s\r\nHost: %s\r\n%s\r\n\r\n%s\r\n", meth, path, type, host, length, data);
00073     int scount = socket.send(sbuffer, sizeof sbuffer);
00074     printf("sent %d\r\n%s", scount, sbuffer);
00075 
00076     // Recieve a simple http response and print out the response line
00077     char rbuffer[64];
00078     int rcount = socket.recv(rbuffer, sizeof rbuffer);
00079     printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00080 
00081     // Close the socket to return its memory and bring down the network interface
00082     socket.close();
00083     spwf.disconnect();
00084 
00085     printf("Done\n");
00086     
00087     while(1) { 
00088         wait(1);
00089         myled = !myled;
00090     }
00091 }