cc3000 http client

Dependencies:   HTTPClient NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 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 #include "mbed.h"
00017 #include "cc3000.h"
00018 #include "main.h"
00019 
00020 #include "HTTPClient.h"
00021 
00022 using namespace mbed_cc3000;
00023 
00024 /* cc3000 module declaration specific for user's board. Check also init() */
00025 #if (MY_BOARD == WIGO)
00026 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false);
00027 Serial pc(USBTX, USBRX);
00028 #elif (MY_BOARD == WIFI_DIPCORTEX)
00029 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false);
00030 Serial pc(UART_TX, UART_RX);
00031 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
00032 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false);
00033 Serial pc(USBTX, USBRX);
00034 #else
00035 
00036 #endif
00037 
00038 HTTPClient http;
00039 char str[512];
00040 /**
00041  *  \brief HTTP client demo
00042  *  \param  none
00043  *  \return int
00044  */
00045 int main() {
00046     init(); /* board dependent init */
00047     pc.baud(115200);
00048 
00049     printf("cc3000 HTTP client demo. \r\n");
00050     wifi.init();
00051     if (wifi.connect() == -1) {
00052         printf("Failed to connect. Please verify connection details and try again. \r\n");
00053         while(1);
00054     } else {
00055         printf("IP address: %s \r\n",wifi.getIPAddress());
00056     }
00057     
00058     //GET data
00059     printf("\r\nTrying to fetch page... \r\n");
00060     int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
00061     if (!ret)
00062     {
00063       printf("Page fetched successfully - read %d characters \r\n", strlen(str));
00064       printf("Result: %s \r\n", str);
00065     }
00066     else
00067     {
00068       printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
00069     }
00070 
00071     //POST data
00072     HTTPMap map;
00073     HTTPText inText(str, 512);
00074     map.put("Hello", "World");
00075     map.put("test", "1234");
00076     printf(" \r\nTrying to post data... \r\n");
00077     ret = http.post("http://httpbin.org/post", map, &inText);
00078     if (!ret)
00079     {
00080       printf("Executed POST successfully - read %d characters \r\n", strlen(str));
00081       printf("Result: %s \r\n", str);
00082     }
00083     else
00084     {
00085       printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
00086     }
00087 
00088     //PUT data
00089     strcpy(str, "This is a PUT test!");
00090     HTTPText outText(str);
00091     //HTTPText inText(str, 512);
00092     printf(" \r\nTrying to put resource... \r\n");
00093     ret = http.put("http://httpbin.org/put", outText, &inText);
00094     if (!ret)
00095     {
00096       printf("Executed PUT successfully - read %d characters \r\n", strlen(str));
00097       printf("Result: %s \r\n", str);
00098     }
00099     else
00100     {
00101       printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
00102     }
00103 
00104     //DELETE data
00105     //HTTPText inText(str, 512);
00106     printf(" \r\nTrying to delete resource... \r\n");
00107     ret = http.del("http://httpbin.org/delete", &inText);
00108     if (!ret)
00109     {
00110       printf("Executed DELETE successfully - read %d characters \r\n", strlen(str));
00111       printf("Result: %s \r\n", str);
00112     }
00113     else
00114     {
00115       printf("Error - ret = %d - HTTP return code = %d \r\n", ret, http.getHTTPResponseCode());
00116     }
00117 
00118     printf("Demo completed. \r\n");
00119     wifi.disconnect();
00120 }