ARM Shanghai IoT Team (Internal) / Mbed 2 deprecated WifiaddThread

Dependencies:   C12832 HTTPClient wifiontros wifirtos mbed

Fork of frdm_rtos by Freescale

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "sysinterface.h"
00004 #include "WiflyInterface.h"
00005 #include "HTTPClient.h"
00006 #include "C12832.h"
00007 //Serial console(USBTX, USBRX); // Primary output to demonstrate library
00008 // Using Arduino pin notation
00009 C12832 lcd(D11, D13, D12, D7, D10);
00010 
00011 /* wifly interface:
00012 *     - D1 is for the serial communication, Tx
00013 *     - D0 is for the serial communication, Rx
00014 *     - D3 is for the reset pin
00015 *     - D2 is for the connection status
00016 *     - "AndroidAP11" is the ssid of the network
00017 *     - "" is the password
00018 *     - NONE is the security (or WPA)
00019 */
00020 WiflyInterface wifly(D1, D0, D3, D2, "WIFITest", "54510000", WPA);
00021 DigitalOut myled(LED1);
00022 DigitalOut myled2(LED2);
00023 HTTPClient http;
00024 char str[512];
00025 int main()
00026 {
00027 
00028     // Setup
00029 
00030     // Diagnostic printing of K64F
00031     printf("\r\n\nmbed K64F !\r\n");
00032 
00033     lcd.cls();
00034 
00035     lcd.printf( "Start to initialise Wi-Fi!\n" );
00036 
00037     int s = wifly.init(); //Use DHCP
00038 
00039     if( s != NULL )
00040     {
00041         lcd.printf( "Could not initialise. Will halt!\n" );
00042         exit( 0 );
00043     }
00044 
00045     lcd.printf( "Start to connect Wi-Fi!\n" );
00046 
00047     while(!wifly.connect())
00048      ;
00049 
00050         
00051     lcd.printf( "IP: %s\n", wifly.getIPAddress() );
00052     wait( 2.0 );
00053     lcd.cls();
00054 
00055     //GET data
00056     printf("Trying to fetch page...\r\n");  
00057         net_system_start;
00058     int ret = http.get("http://httpbin.org/ip", str, 128);
00059     if (!ret) {
00060         printf("Page fetched successfully - read %d characters\r\n", strlen(str));
00061         printf("Result: %s\r\n", str);
00062         lcd.printf( "GET: %d OK\n", strlen(str));
00063     } else {
00064         printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
00065         lcd.printf( "GET: ERROR %d !\n", http.getHTTPResponseCode());
00066     }
00067 
00068     //POST dataGet
00069     HTTPMap map;
00070     HTTPText text(str, 512);
00071     map.put("Hello", "World");
00072     map.put("test", "1234");
00073     printf("Trying to post data...\r\n");
00074     ret = http.post("http://httpbin.org/post", map, &text);
00075     if (!ret) {
00076         printf("Executed POST successfully - read %d characters\r\n", strlen(str));
00077         printf("Result: %s\r\n", str);
00078         lcd.printf( "PUT: %d OK\n", strlen(str));
00079     } else {
00080         printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
00081         lcd.printf( "PUT: ERROR %d !\n", http.getHTTPResponseCode());
00082     }
00083 
00084     wifly.disconnect();
00085 
00086     lcd.printf( "Start to disconnect Wi-Fi!\n" );
00087 
00088     while (true) {
00089       wait( 1.0 );
00090       //lcd.printf(".");
00091     }
00092 }