Get basic debug info from the WNC M14A21.
Dependencies: JSON M2XStreamClient-JMF WNCInterface mbed-rtos mbed
Fork of WNCInterface_HTTP_example by
main.cpp@2:8fb99c9f5075, 2016-09-27 (annotated)
- Committer:
- JMF
- Date:
- Tue Sep 27 20:04:14 2016 +0000
- Revision:
- 2:8fb99c9f5075
- Parent:
- 1:7634d07c4310
- Child:
- 3:5cc9de44aea5
updating how stdio/stderr is initialized, moved out of WNCInterface.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:50979ffa39b6 | 1 | /* ===================================================================== |
JMF | 0:50979ffa39b6 | 2 | Copyright © 2016, Avnet (R) |
JMF | 0:50979ffa39b6 | 3 | |
JMF | 0:50979ffa39b6 | 4 | Contributors: |
JMF | 0:50979ffa39b6 | 5 | * James M Flynn, www.em.avnet.com |
JMF | 0:50979ffa39b6 | 6 | |
JMF | 0:50979ffa39b6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 0:50979ffa39b6 | 8 | you may not use this file except in compliance with the License. |
JMF | 0:50979ffa39b6 | 9 | You may obtain a copy of the License at |
JMF | 0:50979ffa39b6 | 10 | |
JMF | 0:50979ffa39b6 | 11 | http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:50979ffa39b6 | 12 | |
JMF | 0:50979ffa39b6 | 13 | Unless required by applicable law or agreed to in writing, |
JMF | 0:50979ffa39b6 | 14 | software distributed under the License is distributed on an |
JMF | 0:50979ffa39b6 | 15 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
JMF | 0:50979ffa39b6 | 16 | either express or implied. See the License for the specific |
JMF | 0:50979ffa39b6 | 17 | language governing permissions and limitations under the License. |
JMF | 0:50979ffa39b6 | 18 | |
JMF | 0:50979ffa39b6 | 19 | @file WNCInterface.cpp |
JMF | 0:50979ffa39b6 | 20 | @version 1.0 |
JMF | 0:50979ffa39b6 | 21 | @date Sept 2016 |
JMF | 0:50979ffa39b6 | 22 | |
JMF | 0:50979ffa39b6 | 23 | ======================================================================== */ |
JMF | 0:50979ffa39b6 | 24 | |
JMF | 0:50979ffa39b6 | 25 | #include "mbed.h" |
JMF | 0:50979ffa39b6 | 26 | #include "WNCInterface.h" |
JMF | 0:50979ffa39b6 | 27 | |
JMF | 0:50979ffa39b6 | 28 | #include "Socket/Socket.h" |
JMF | 0:50979ffa39b6 | 29 | #include "Socket/TCPSocketConnection.h" |
JMF | 0:50979ffa39b6 | 30 | #include "Socket/UDPSocket.h" |
JMF | 0:50979ffa39b6 | 31 | |
JMF | 0:50979ffa39b6 | 32 | #define DEBUG |
JMF | 0:50979ffa39b6 | 33 | #define MBED_PLATFORM |
JMF | 0:50979ffa39b6 | 34 | #include "HTTPClient.h" |
JMF | 0:50979ffa39b6 | 35 | |
JMF | 0:50979ffa39b6 | 36 | #define CRLF "\n\r" |
JMF | 0:50979ffa39b6 | 37 | |
JMF | 0:50979ffa39b6 | 38 | #define ntohl(n) ((n & 0xff) << 24) |\ |
JMF | 0:50979ffa39b6 | 39 | ((n & 0xff00) << 8) |\ |
JMF | 0:50979ffa39b6 | 40 | ((n & 0xff0000UL) >> 8) |\ |
JMF | 0:50979ffa39b6 | 41 | ((n & 0xff000000UL) >> 24) |
JMF | 0:50979ffa39b6 | 42 | |
JMF | 0:50979ffa39b6 | 43 | int main() { |
JMF | 0:50979ffa39b6 | 44 | int ret; |
JMF | 0:50979ffa39b6 | 45 | WNCInterface wnc; |
JMF | 2:8fb99c9f5075 | 46 | MODSERIAL pc(USBTX,USBRX,256,256); |
JMF | 2:8fb99c9f5075 | 47 | |
JMF | 2:8fb99c9f5075 | 48 | pc.baud(115200); |
JMF | 0:50979ffa39b6 | 49 | printf("STARTING WNCInterface & Socket Test" CRLF); |
JMF | 0:50979ffa39b6 | 50 | |
JMF | 0:50979ffa39b6 | 51 | ret = wnc.init(); |
JMF | 0:50979ffa39b6 | 52 | printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret); |
JMF | 0:50979ffa39b6 | 53 | if( !ret ) { |
JMF | 0:50979ffa39b6 | 54 | printf(" - - - - - - - ALL DONE - - - - - - - " CRLF); |
JMF | 0:50979ffa39b6 | 55 | while(1); |
JMF | 0:50979ffa39b6 | 56 | } |
JMF | 0:50979ffa39b6 | 57 | |
JMF | 0:50979ffa39b6 | 58 | ret = wnc.connect(); |
JMF | 0:50979ffa39b6 | 59 | printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress()); |
JMF | 0:50979ffa39b6 | 60 | // |
JMF | 0:50979ffa39b6 | 61 | //demonstrate a TCP connection |
JMF | 0:50979ffa39b6 | 62 | // |
JMF | 0:50979ffa39b6 | 63 | TCPSocketConnection tsock; |
JMF | 0:50979ffa39b6 | 64 | printf("\n\n\n\n------------------------------------" CRLF "starting TCP Socket Test..." CRLF); |
JMF | 0:50979ffa39b6 | 65 | |
JMF | 0:50979ffa39b6 | 66 | tsock.connect("mbed.org", 80); |
JMF | 0:50979ffa39b6 | 67 | printf("Connected!" CRLF ); |
JMF | 0:50979ffa39b6 | 68 | char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; |
JMF | 0:50979ffa39b6 | 69 | ret=tsock.send_all(http_cmd, sizeof(http_cmd)-1); |
JMF | 0:50979ffa39b6 | 70 | printf("TCP Socket sent %d bytes.\r\n",ret); |
JMF | 0:50979ffa39b6 | 71 | char buffer[1500]; |
JMF | 0:50979ffa39b6 | 72 | |
JMF | 0:50979ffa39b6 | 73 | tsock.set_blocking (false, 1000); |
JMF | 0:50979ffa39b6 | 74 | while (ret>0) { |
JMF | 0:50979ffa39b6 | 75 | ret = tsock.receive(buffer, sizeof(buffer)-1); |
JMF | 0:50979ffa39b6 | 76 | buffer[ret] = '\0'; |
JMF | 0:50979ffa39b6 | 77 | if( ret > 0 ) |
JMF | 0:50979ffa39b6 | 78 | printf("Received %d chars from server:" CRLF "<----->" CRLF "%s" CRLF |
JMF | 0:50979ffa39b6 | 79 | "<----->" CRLF, ret, buffer); |
JMF | 0:50979ffa39b6 | 80 | } |
JMF | 0:50979ffa39b6 | 81 | tsock.close(); |
JMF | 0:50979ffa39b6 | 82 | |
JMF | 0:50979ffa39b6 | 83 | // |
JMF | 0:50979ffa39b6 | 84 | //demonstrate a UDP connection |
JMF | 0:50979ffa39b6 | 85 | // |
JMF | 0:50979ffa39b6 | 86 | printf("\n\n\n\n-------------------------------------" CRLF); |
JMF | 0:50979ffa39b6 | 87 | printf( "starting UDP Socket Test..." CRLF); |
JMF | 0:50979ffa39b6 | 88 | |
JMF | 0:50979ffa39b6 | 89 | //#define UDP_URL "utcnist.colorado.edu" |
JMF | 0:50979ffa39b6 | 90 | //#define UDP_PORT 37 |
JMF | 0:50979ffa39b6 | 91 | #define UDP_URL "pool.ntp.org" |
JMF | 0:50979ffa39b6 | 92 | #define UDP_PORT 123 |
JMF | 0:50979ffa39b6 | 93 | |
JMF | 0:50979ffa39b6 | 94 | UDPSocket usock; |
JMF | 0:50979ffa39b6 | 95 | usock.init(); |
JMF | 0:50979ffa39b6 | 96 | Endpoint nist; |
JMF | 0:50979ffa39b6 | 97 | |
JMF | 0:50979ffa39b6 | 98 | nist.set_address(UDP_URL, UDP_PORT); |
JMF | 0:50979ffa39b6 | 99 | printf("Endpoing set to '%s'/%s/%d, try sending data." CRLF, |
JMF | 0:50979ffa39b6 | 100 | UDP_URL, nist.get_address(), nist.get_port()); |
JMF | 0:50979ffa39b6 | 101 | |
JMF | 0:50979ffa39b6 | 102 | // char out_buffer[] = "plop"; // Does not matter |
JMF | 0:50979ffa39b6 | 103 | |
JMF | 0:50979ffa39b6 | 104 | char out_buffer[48]; |
JMF | 0:50979ffa39b6 | 105 | out_buffer[0] = 0xE3; // LI, Version, Mode |
JMF | 0:50979ffa39b6 | 106 | out_buffer[1] = 0; // Stratum, or type of clock |
JMF | 0:50979ffa39b6 | 107 | out_buffer[2] = 6; // Polling Interval |
JMF | 0:50979ffa39b6 | 108 | out_buffer[3] = 0xEC; // Peer Clock Precision |
JMF | 0:50979ffa39b6 | 109 | // 8 bytes of zero for Root Delay & Root Dispersion |
JMF | 0:50979ffa39b6 | 110 | out_buffer[12] = 49; |
JMF | 0:50979ffa39b6 | 111 | out_buffer[13] = 0x4E; |
JMF | 0:50979ffa39b6 | 112 | out_buffer[14] = 49; |
JMF | 0:50979ffa39b6 | 113 | out_buffer[15] = 52; |
JMF | 0:50979ffa39b6 | 114 | |
JMF | 0:50979ffa39b6 | 115 | ret = usock.sendTo(nist, out_buffer, sizeof(out_buffer)); |
JMF | 0:50979ffa39b6 | 116 | printf("sent buffer to UDP IP/Port (ret=%d)" CRLF "Now try receiving." CRLF,ret); |
JMF | 0:50979ffa39b6 | 117 | |
JMF | 0:50979ffa39b6 | 118 | |
JMF | 1:7634d07c4310 | 119 | usock.set_blocking (false, 1000); |
JMF | 0:50979ffa39b6 | 120 | char in_buffer[50]; |
JMF | 0:50979ffa39b6 | 121 | int n = usock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); |
JMF | 0:50979ffa39b6 | 122 | |
JMF | 0:50979ffa39b6 | 123 | printf("Received %d bytes via UDP IP address %s on port %d." CRLF, |
JMF | 0:50979ffa39b6 | 124 | n, nist.get_address(), nist.get_port()); |
JMF | 0:50979ffa39b6 | 125 | |
JMF | 0:50979ffa39b6 | 126 | usock.close(); |
JMF | 0:50979ffa39b6 | 127 | |
JMF | 0:50979ffa39b6 | 128 | // |
JMF | 0:50979ffa39b6 | 129 | //demonstrate HTTPClient operations |
JMF | 0:50979ffa39b6 | 130 | // |
JMF | 0:50979ffa39b6 | 131 | HTTPClient http; |
JMF | 0:50979ffa39b6 | 132 | char str[512]; |
JMF | 0:50979ffa39b6 | 133 | |
JMF | 0:50979ffa39b6 | 134 | printf("\n\n\n\n-------------------------------------" CRLF); |
JMF | 0:50979ffa39b6 | 135 | printf( "starting HTTPClient Socket Test..." CRLF); |
JMF | 0:50979ffa39b6 | 136 | |
JMF | 0:50979ffa39b6 | 137 | //GET data |
JMF | 0:50979ffa39b6 | 138 | printf(">>>>Fetch a page..." CRLF); |
JMF | 0:50979ffa39b6 | 139 | |
JMF | 0:50979ffa39b6 | 140 | ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, strlen(str)); |
JMF | 0:50979ffa39b6 | 141 | if (!ret) |
JMF | 0:50979ffa39b6 | 142 | { |
JMF | 0:50979ffa39b6 | 143 | printf("Page fetched successfully - read %d characters" CRLF, strlen(str)); |
JMF | 0:50979ffa39b6 | 144 | printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str); |
JMF | 0:50979ffa39b6 | 145 | } |
JMF | 0:50979ffa39b6 | 146 | else |
JMF | 0:50979ffa39b6 | 147 | { |
JMF | 0:50979ffa39b6 | 148 | printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); |
JMF | 0:50979ffa39b6 | 149 | } |
JMF | 0:50979ffa39b6 | 150 | |
JMF | 0:50979ffa39b6 | 151 | //POST data |
JMF | 0:50979ffa39b6 | 152 | HTTPMap map; |
JMF | 0:50979ffa39b6 | 153 | HTTPText inText(str, 512); |
JMF | 0:50979ffa39b6 | 154 | map.put("Hello", "World"); |
JMF | 0:50979ffa39b6 | 155 | map.put("test", "1234"); |
JMF | 0:50979ffa39b6 | 156 | |
JMF | 0:50979ffa39b6 | 157 | printf(CRLF CRLF ">>>>Post data..." CRLF); |
JMF | 0:50979ffa39b6 | 158 | ret = http.post("http://httpbin.org/post", map, &inText); |
JMF | 0:50979ffa39b6 | 159 | if (!ret) |
JMF | 0:50979ffa39b6 | 160 | { |
JMF | 0:50979ffa39b6 | 161 | printf("Executed POST successfully - read %d characters" CRLF, strlen(str)); |
JMF | 0:50979ffa39b6 | 162 | printf("<----->" CRLF ); |
JMF | 0:50979ffa39b6 | 163 | printf("Result: %s" CRLF "<----->" CRLF, str); |
JMF | 0:50979ffa39b6 | 164 | } |
JMF | 0:50979ffa39b6 | 165 | else |
JMF | 0:50979ffa39b6 | 166 | { |
JMF | 0:50979ffa39b6 | 167 | printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); |
JMF | 0:50979ffa39b6 | 168 | } |
JMF | 0:50979ffa39b6 | 169 | |
JMF | 0:50979ffa39b6 | 170 | //PUT data |
JMF | 0:50979ffa39b6 | 171 | strcpy(str, "This is a PUT test!"); |
JMF | 0:50979ffa39b6 | 172 | HTTPText outText(str); |
JMF | 0:50979ffa39b6 | 173 | printf(CRLF CRLF ">>>>Put data..." CRLF); |
JMF | 0:50979ffa39b6 | 174 | ret = http.put("http://httpbin.org/put", outText, &inText); |
JMF | 0:50979ffa39b6 | 175 | if (!ret) |
JMF | 0:50979ffa39b6 | 176 | { |
JMF | 0:50979ffa39b6 | 177 | printf("Executed PUT successfully - read %d characters" CRLF, strlen(str)); |
JMF | 0:50979ffa39b6 | 178 | printf("<----->" CRLF ); |
JMF | 0:50979ffa39b6 | 179 | printf("Result: %s" CRLF "<----->" CRLF, str); |
JMF | 0:50979ffa39b6 | 180 | } |
JMF | 0:50979ffa39b6 | 181 | else |
JMF | 0:50979ffa39b6 | 182 | { |
JMF | 0:50979ffa39b6 | 183 | printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); |
JMF | 0:50979ffa39b6 | 184 | } |
JMF | 0:50979ffa39b6 | 185 | |
JMF | 0:50979ffa39b6 | 186 | //DELETE data |
JMF | 0:50979ffa39b6 | 187 | printf(CRLF CRLF ">>>>Delete data..." CRLF); |
JMF | 0:50979ffa39b6 | 188 | ret = http.del("http://httpbin.org/delete", &inText); |
JMF | 0:50979ffa39b6 | 189 | if (!ret) |
JMF | 0:50979ffa39b6 | 190 | { |
JMF | 0:50979ffa39b6 | 191 | printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str)); |
JMF | 0:50979ffa39b6 | 192 | printf("<----->" CRLF ); |
JMF | 0:50979ffa39b6 | 193 | printf("Result: %s" CRLF "<----->" CRLF, str); |
JMF | 0:50979ffa39b6 | 194 | } |
JMF | 0:50979ffa39b6 | 195 | else |
JMF | 0:50979ffa39b6 | 196 | { |
JMF | 0:50979ffa39b6 | 197 | printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode()); |
JMF | 0:50979ffa39b6 | 198 | } |
JMF | 0:50979ffa39b6 | 199 | |
JMF | 0:50979ffa39b6 | 200 | wnc.disconnect(); |
JMF | 0:50979ffa39b6 | 201 | printf(" - - - - - - - ALL DONE - - - - - - - " CRLF); |
JMF | 0:50979ffa39b6 | 202 | while(1) {} |
JMF | 0:50979ffa39b6 | 203 | } |
JMF | 0:50979ffa39b6 | 204 |