Add two threads into the IOT WIFIDemo for test_JacobShi
Dependencies: C12832 HTTPClient wifiontros wifirtos mbed
Fork of frdm_rtos by
main.cpp@4:242715d40e54, 2014-12-05 (annotated)
- Committer:
- shiyilei
- Date:
- Fri Dec 05 02:14:44 2014 +0000
- Revision:
- 4:242715d40e54
- Parent:
- 3:5f921ff0868d
wifibasedonrtos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:7fd883bfe9a3 | 1 | #include "mbed.h" |
Kojto | 0:7fd883bfe9a3 | 2 | #include "rtos.h" |
shiyilei | 3:5f921ff0868d | 3 | #include "sysinterface.h" |
shiyilei | 3:5f921ff0868d | 4 | #include "WiflyInterface.h" |
shiyilei | 3:5f921ff0868d | 5 | #include "HTTPClient.h" |
shiyilei | 3:5f921ff0868d | 6 | #include "C12832.h" |
shiyilei | 3:5f921ff0868d | 7 | //Serial console(USBTX, USBRX); // Primary output to demonstrate library |
shiyilei | 3:5f921ff0868d | 8 | // Using Arduino pin notation |
shiyilei | 3:5f921ff0868d | 9 | C12832 lcd(D11, D13, D12, D7, D10); |
Kojto | 0:7fd883bfe9a3 | 10 | |
shiyilei | 3:5f921ff0868d | 11 | /* wifly interface: |
shiyilei | 3:5f921ff0868d | 12 | * - D1 is for the serial communication, Tx |
shiyilei | 3:5f921ff0868d | 13 | * - D0 is for the serial communication, Rx |
shiyilei | 3:5f921ff0868d | 14 | * - D3 is for the reset pin |
shiyilei | 3:5f921ff0868d | 15 | * - D2 is for the connection status |
shiyilei | 3:5f921ff0868d | 16 | * - "AndroidAP11" is the ssid of the network |
shiyilei | 3:5f921ff0868d | 17 | * - "" is the password |
shiyilei | 3:5f921ff0868d | 18 | * - NONE is the security (or WPA) |
shiyilei | 3:5f921ff0868d | 19 | */ |
shiyilei | 3:5f921ff0868d | 20 | WiflyInterface wifly(D1, D0, D3, D2, "WIFITest", "54510000", WPA); |
shiyilei | 3:5f921ff0868d | 21 | DigitalOut myled(LED1); |
shiyilei | 3:5f921ff0868d | 22 | DigitalOut myled2(LED2); |
shiyilei | 3:5f921ff0868d | 23 | HTTPClient http; |
shiyilei | 3:5f921ff0868d | 24 | char str[512]; |
shiyilei | 3:5f921ff0868d | 25 | int main() |
Kojto | 0:7fd883bfe9a3 | 26 | { |
shiyilei | 3:5f921ff0868d | 27 | |
shiyilei | 3:5f921ff0868d | 28 | // Setup |
shiyilei | 3:5f921ff0868d | 29 | |
shiyilei | 3:5f921ff0868d | 30 | // Diagnostic printing of K64F |
shiyilei | 3:5f921ff0868d | 31 | printf("\r\n\nmbed K64F !\r\n"); |
shiyilei | 3:5f921ff0868d | 32 | |
shiyilei | 3:5f921ff0868d | 33 | lcd.cls(); |
shiyilei | 3:5f921ff0868d | 34 | |
shiyilei | 3:5f921ff0868d | 35 | lcd.printf( "Start to initialise Wi-Fi!\n" ); |
shiyilei | 3:5f921ff0868d | 36 | |
shiyilei | 3:5f921ff0868d | 37 | int s = wifly.init(); //Use DHCP |
shiyilei | 3:5f921ff0868d | 38 | |
shiyilei | 3:5f921ff0868d | 39 | if( s != NULL ) |
shiyilei | 3:5f921ff0868d | 40 | { |
shiyilei | 3:5f921ff0868d | 41 | lcd.printf( "Could not initialise. Will halt!\n" ); |
shiyilei | 3:5f921ff0868d | 42 | exit( 0 ); |
shiyilei | 3:5f921ff0868d | 43 | } |
shiyilei | 3:5f921ff0868d | 44 | |
shiyilei | 3:5f921ff0868d | 45 | lcd.printf( "Start to connect Wi-Fi!\n" ); |
shiyilei | 3:5f921ff0868d | 46 | |
shiyilei | 3:5f921ff0868d | 47 | while(!wifly.connect()) |
shiyilei | 3:5f921ff0868d | 48 | ; |
shiyilei | 3:5f921ff0868d | 49 | |
shiyilei | 3:5f921ff0868d | 50 | |
shiyilei | 3:5f921ff0868d | 51 | lcd.printf( "IP: %s\n", wifly.getIPAddress() ); |
shiyilei | 3:5f921ff0868d | 52 | wait( 2.0 ); |
shiyilei | 3:5f921ff0868d | 53 | lcd.cls(); |
Kojto | 0:7fd883bfe9a3 | 54 | |
shiyilei | 3:5f921ff0868d | 55 | //GET data |
shiyilei | 3:5f921ff0868d | 56 | printf("Trying to fetch page...\r\n"); |
shiyilei | 3:5f921ff0868d | 57 | net_system_start; |
shiyilei | 3:5f921ff0868d | 58 | int ret = http.get("http://httpbin.org/ip", str, 128); |
shiyilei | 3:5f921ff0868d | 59 | if (!ret) { |
shiyilei | 3:5f921ff0868d | 60 | printf("Page fetched successfully - read %d characters\r\n", strlen(str)); |
shiyilei | 3:5f921ff0868d | 61 | printf("Result: %s\r\n", str); |
shiyilei | 3:5f921ff0868d | 62 | lcd.printf( "GET: %d OK\n", strlen(str)); |
shiyilei | 3:5f921ff0868d | 63 | } else { |
shiyilei | 3:5f921ff0868d | 64 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
shiyilei | 3:5f921ff0868d | 65 | lcd.printf( "GET: ERROR %d !\n", http.getHTTPResponseCode()); |
shiyilei | 3:5f921ff0868d | 66 | } |
shiyilei | 3:5f921ff0868d | 67 | |
shiyilei | 3:5f921ff0868d | 68 | //POST dataGet |
shiyilei | 3:5f921ff0868d | 69 | HTTPMap map; |
shiyilei | 3:5f921ff0868d | 70 | HTTPText text(str, 512); |
shiyilei | 3:5f921ff0868d | 71 | map.put("Hello", "World"); |
shiyilei | 3:5f921ff0868d | 72 | map.put("test", "1234"); |
shiyilei | 3:5f921ff0868d | 73 | printf("Trying to post data...\r\n"); |
shiyilei | 3:5f921ff0868d | 74 | ret = http.post("http://httpbin.org/post", map, &text); |
shiyilei | 3:5f921ff0868d | 75 | if (!ret) { |
shiyilei | 3:5f921ff0868d | 76 | printf("Executed POST successfully - read %d characters\r\n", strlen(str)); |
shiyilei | 3:5f921ff0868d | 77 | printf("Result: %s\r\n", str); |
shiyilei | 3:5f921ff0868d | 78 | lcd.printf( "PUT: %d OK\n", strlen(str)); |
shiyilei | 3:5f921ff0868d | 79 | } else { |
shiyilei | 3:5f921ff0868d | 80 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
shiyilei | 3:5f921ff0868d | 81 | lcd.printf( "PUT: ERROR %d !\n", http.getHTTPResponseCode()); |
shiyilei | 3:5f921ff0868d | 82 | } |
shiyilei | 3:5f921ff0868d | 83 | |
shiyilei | 3:5f921ff0868d | 84 | wifly.disconnect(); |
shiyilei | 3:5f921ff0868d | 85 | |
shiyilei | 3:5f921ff0868d | 86 | lcd.printf( "Start to disconnect Wi-Fi!\n" ); |
shiyilei | 3:5f921ff0868d | 87 | |
Kojto | 0:7fd883bfe9a3 | 88 | while (true) { |
shiyilei | 3:5f921ff0868d | 89 | wait( 1.0 ); |
shiyilei | 3:5f921ff0868d | 90 | //lcd.printf("."); |
Kojto | 0:7fd883bfe9a3 | 91 | } |
Kojto | 0:7fd883bfe9a3 | 92 | } |