An example of request Http POST with X_NUCLEO_IDW01M1
Dependencies: NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of TestUpload by
main.cpp@12:6dc4d1fe7604, 2017-02-01 (annotated)
- Committer:
- erbistiandrea
- Date:
- Wed Feb 01 16:20:20 2017 +0000
- Revision:
- 12:6dc4d1fe7604
- Parent:
- 11:0138f6cb216a
An example of request HTTP POST with X_NUCLEO_IDW01M1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:dee849b0e6e6 | 1 | /* SpwfInterface NetworkSocketAPI Example Program |
mridup | 0:dee849b0e6e6 | 2 | * Copyright (c) 2015 ARM Limited |
mridup | 0:dee849b0e6e6 | 3 | * |
mridup | 0:dee849b0e6e6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 0:dee849b0e6e6 | 5 | * you may not use this file except in compliance with the License. |
mridup | 0:dee849b0e6e6 | 6 | * You may obtain a copy of the License at |
mridup | 0:dee849b0e6e6 | 7 | * |
mridup | 0:dee849b0e6e6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 0:dee849b0e6e6 | 9 | * |
mridup | 0:dee849b0e6e6 | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 0:dee849b0e6e6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 0:dee849b0e6e6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 0:dee849b0e6e6 | 13 | * See the License for the specific language governing permissions and |
mridup | 0:dee849b0e6e6 | 14 | * limitations under the License. |
mridup | 0:dee849b0e6e6 | 15 | */ |
mridup | 0:dee849b0e6e6 | 16 | |
mridup | 0:dee849b0e6e6 | 17 | #include "mbed.h" |
mridup | 0:dee849b0e6e6 | 18 | #include "SpwfInterface.h" |
mridup | 0:dee849b0e6e6 | 19 | #include "TCPSocket.h" |
mridup | 0:dee849b0e6e6 | 20 | |
mapellil | 8:74b827befe72 | 21 | |
mapellil | 10:cc61a766cd1f | 22 | |
mridup | 0:dee849b0e6e6 | 23 | //------------------------------------ |
mridup | 0:dee849b0e6e6 | 24 | // Hyperterminal configuration |
mridup | 0:dee849b0e6e6 | 25 | // 9600 bauds, 8-bit data, no parity |
mridup | 0:dee849b0e6e6 | 26 | //------------------------------------ |
mridup | 0:dee849b0e6e6 | 27 | |
mridup | 2:3a87dbea07a7 | 28 | Serial pc(USBTX, USBRX); |
mridup | 0:dee849b0e6e6 | 29 | DigitalOut myled(LED1); |
mridup | 1:daf71fa3674c | 30 | |
fdalforno | 11:0138f6cb216a | 31 | SpwfSAInterface spwf(D8, D2, false); |
fdalforno | 11:0138f6cb216a | 32 | TCPSocket socket; |
mridup | 1:daf71fa3674c | 33 | |
fdalforno | 11:0138f6cb216a | 34 | int main() { |
erbistiandrea | 12:6dc4d1fe7604 | 35 | char* ssid = "fablab_guest"; |
erbistiandrea | 12:6dc4d1fe7604 | 36 | char* seckey = "veronafablab"; |
mridup | 0:dee849b0e6e6 | 37 | |
mridup | 0:dee849b0e6e6 | 38 | pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); |
mridup | 0:dee849b0e6e6 | 39 | pc.printf("\r\nconnecting to AP\r\n"); |
fdalforno | 11:0138f6cb216a | 40 | |
mridup | 0:dee849b0e6e6 | 41 | if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { |
mridup | 0:dee849b0e6e6 | 42 | pc.printf("\r\nnow connected\r\n"); |
mridup | 0:dee849b0e6e6 | 43 | } else { |
mridup | 0:dee849b0e6e6 | 44 | pc.printf("\r\nerror connecting to AP.\r\n"); |
mridup | 0:dee849b0e6e6 | 45 | return -1; |
mridup | 0:dee849b0e6e6 | 46 | } |
fdalforno | 11:0138f6cb216a | 47 | |
mridup | 0:dee849b0e6e6 | 48 | const char *ip = spwf.get_ip_address(); |
mridup | 0:dee849b0e6e6 | 49 | const char *mac = spwf.get_mac_address(); |
mridup | 0:dee849b0e6e6 | 50 | |
mridup | 0:dee849b0e6e6 | 51 | pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP"); |
erbistiandrea | 12:6dc4d1fe7604 | 52 | pc.printf("MAC Address is: %s\r\n", (mac) ? mac : "No MAC"); |
erbistiandrea | 12:6dc4d1fe7604 | 53 | |
erbistiandrea | 12:6dc4d1fe7604 | 54 | |
fdalforno | 11:0138f6cb216a | 55 | socket.open(&spwf); |
erbistiandrea | 12:6dc4d1fe7604 | 56 | socket.connect("fabio.infosistem.it", 80); |
erbistiandrea | 12:6dc4d1fe7604 | 57 | char url[] = "http://fabio.infosistem.it/post.php"; |
erbistiandrea | 12:6dc4d1fe7604 | 58 | char host[] = "fabio.infosistem.it"; |
erbistiandrea | 12:6dc4d1fe7604 | 59 | char path[] = "/post.php"; |
erbistiandrea | 12:6dc4d1fe7604 | 60 | char meth[] = "POST"; //http method ('POST', 'GET', 'PUT', 'DELETE') |
erbistiandrea | 12:6dc4d1fe7604 | 61 | //char key[] = "Hello"; |
erbistiandrea | 12:6dc4d1fe7604 | 62 | //char value[] = "World"; |
erbistiandrea | 12:6dc4d1fe7604 | 63 | // Body of the message |
erbistiandrea | 12:6dc4d1fe7604 | 64 | char data[] = "Hello=World"; |
erbistiandrea | 12:6dc4d1fe7604 | 65 | //snprintf(data, sizeof(data), "%s=%s", key, value); |
erbistiandrea | 12:6dc4d1fe7604 | 66 | printf("\r\n%s\r\n", data); |
erbistiandrea | 12:6dc4d1fe7604 | 67 | char type[] = "Content-Type: application/x-www-form-urlencoded"; |
erbistiandrea | 12:6dc4d1fe7604 | 68 | char length[32]; |
erbistiandrea | 12:6dc4d1fe7604 | 69 | snprintf(length, sizeof(length), "Content-Length: %s", sizeof(data)); |
fdalforno | 11:0138f6cb216a | 70 | // Send a simple http request |
erbistiandrea | 12:6dc4d1fe7604 | 71 | char sbuffer[256]; |
erbistiandrea | 12:6dc4d1fe7604 | 72 | 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); |
fdalforno | 11:0138f6cb216a | 73 | int scount = socket.send(sbuffer, sizeof sbuffer); |
erbistiandrea | 12:6dc4d1fe7604 | 74 | printf("sent %d\r\n%s", scount, sbuffer); |
mapellil | 8:74b827befe72 | 75 | |
fdalforno | 11:0138f6cb216a | 76 | // Recieve a simple http response and print out the response line |
fdalforno | 11:0138f6cb216a | 77 | char rbuffer[64]; |
fdalforno | 11:0138f6cb216a | 78 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
fdalforno | 11:0138f6cb216a | 79 | printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
fdalforno | 11:0138f6cb216a | 80 | |
fdalforno | 11:0138f6cb216a | 81 | // Close the socket to return its memory and bring down the network interface |
fdalforno | 11:0138f6cb216a | 82 | socket.close(); |
mridup | 2:3a87dbea07a7 | 83 | spwf.disconnect(); |
mapellil | 8:74b827befe72 | 84 | |
fdalforno | 11:0138f6cb216a | 85 | printf("Done\n"); |
fdalforno | 11:0138f6cb216a | 86 | |
mridup | 0:dee849b0e6e6 | 87 | while(1) { |
fdalforno | 11:0138f6cb216a | 88 | wait(1); |
fdalforno | 11:0138f6cb216a | 89 | myled = !myled; |
mridup | 0:dee849b0e6e6 | 90 | } |
mridup | 0:dee849b0e6e6 | 91 | } |