Add two threads into the IOT WIFIDemo for test_JacobShi
Dependencies: C12832 HTTPClient wifiontros wifirtos mbed
Fork of frdm_rtos by
Diff: main.cpp
- Revision:
- 3:5f921ff0868d
- Parent:
- 0:7fd883bfe9a3
diff -r bc844894f0a4 -r 5f921ff0868d main.cpp --- a/main.cpp Mon Apr 07 20:31:03 2014 +0000 +++ b/main.cpp Fri Nov 28 12:52:56 2014 +0000 @@ -1,43 +1,92 @@ #include "mbed.h" #include "rtos.h" +#include "sysinterface.h" +#include "WiflyInterface.h" +#include "HTTPClient.h" +#include "C12832.h" +//Serial console(USBTX, USBRX); // Primary output to demonstrate library +// Using Arduino pin notation +C12832 lcd(D11, D13, D12, D7, D10); -DigitalOut led1(LED1); -InterruptIn sw2(SW2); -uint32_t button_pressed; -Thread *thread2; - -void sw2_press(void) +/* wifly interface: +* - D1 is for the serial communication, Tx +* - D0 is for the serial communication, Rx +* - D3 is for the reset pin +* - D2 is for the connection status +* - "AndroidAP11" is the ssid of the network +* - "" is the password +* - NONE is the security (or WPA) +*/ +WiflyInterface wifly(D1, D0, D3, D2, "WIFITest", "54510000", WPA); +DigitalOut myled(LED1); +DigitalOut myled2(LED2); +HTTPClient http; +char str[512]; +int main() { - thread2->signal_set(0x1); -} + + // Setup + + // Diagnostic printing of K64F + printf("\r\n\nmbed K64F !\r\n"); + + lcd.cls(); + + lcd.printf( "Start to initialise Wi-Fi!\n" ); + + int s = wifly.init(); //Use DHCP + + if( s != NULL ) + { + lcd.printf( "Could not initialise. Will halt!\n" ); + exit( 0 ); + } + + lcd.printf( "Start to connect Wi-Fi!\n" ); + + while(!wifly.connect()) + ; + + + lcd.printf( "IP: %s\n", wifly.getIPAddress() ); + wait( 2.0 ); + lcd.cls(); -void led_thread(void const *argument) -{ + //GET data + printf("Trying to fetch page...\r\n"); + net_system_start; + int ret = http.get("http://httpbin.org/ip", str, 128); + if (!ret) { + printf("Page fetched successfully - read %d characters\r\n", strlen(str)); + printf("Result: %s\r\n", str); + lcd.printf( "GET: %d OK\n", strlen(str)); + } else { + printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); + lcd.printf( "GET: ERROR %d !\n", http.getHTTPResponseCode()); + } + + //POST dataGet + HTTPMap map; + HTTPText text(str, 512); + map.put("Hello", "World"); + map.put("test", "1234"); + printf("Trying to post data...\r\n"); + ret = http.post("http://httpbin.org/post", map, &text); + if (!ret) { + printf("Executed POST successfully - read %d characters\r\n", strlen(str)); + printf("Result: %s\r\n", str); + lcd.printf( "PUT: %d OK\n", strlen(str)); + } else { + printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); + lcd.printf( "PUT: ERROR %d !\n", http.getHTTPResponseCode()); + } + + wifly.disconnect(); + + lcd.printf( "Start to disconnect Wi-Fi!\n" ); + while (true) { - led1 = !led1; - Thread::wait(1000); + wait( 1.0 ); + //lcd.printf("."); } } - -void button_thread(void const *argument) -{ - while (true) { - Thread::signal_wait(0x1); - button_pressed++; - } -} - -int main() -{ - Thread thread(led_thread); - thread2 = new Thread(button_thread); - - button_pressed = 0; - sw2.fall(&sw2_press); - while (true) { - Thread::wait(5000); - printf("SW2 was pressed (last 5 seconds): %d \n", button_pressed); - fflush(stdout); - button_pressed = 0; - } -}