Get basic debug info from the WNC M14A21.

Dependencies:   JSON M2XStreamClient-JMF WNCInterface mbed-rtos mbed

Fork of WNCInterface_HTTP_example by Avnet

Committer:
JMF
Date:
Wed Sep 21 15:33:53 2016 +0000
Revision:
0:50979ffa39b6
Child:
1:7634d07c4310
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew 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 0:50979ffa39b6 46 printf("STARTING WNCInterface & Socket Test" CRLF);
JMF 0:50979ffa39b6 47
JMF 0:50979ffa39b6 48 ret = wnc.init();
JMF 0:50979ffa39b6 49 printf("WNC Module %s initialized (%02X)." CRLF, ret?"IS":"IS NOT", ret);
JMF 0:50979ffa39b6 50 if( !ret ) {
JMF 0:50979ffa39b6 51 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 52 while(1);
JMF 0:50979ffa39b6 53 }
JMF 0:50979ffa39b6 54
JMF 0:50979ffa39b6 55 ret = wnc.connect();
JMF 0:50979ffa39b6 56 printf("IP Address: %s " CRLF CRLF, wnc.getIPAddress());
JMF 0:50979ffa39b6 57 //
JMF 0:50979ffa39b6 58 //demonstrate a TCP connection
JMF 0:50979ffa39b6 59 //
JMF 0:50979ffa39b6 60 TCPSocketConnection tsock;
JMF 0:50979ffa39b6 61 printf("\n\n\n\n------------------------------------" CRLF "starting TCP Socket Test..." CRLF);
JMF 0:50979ffa39b6 62
JMF 0:50979ffa39b6 63 tsock.connect("mbed.org", 80);
JMF 0:50979ffa39b6 64 printf("Connected!" CRLF );
JMF 0:50979ffa39b6 65 char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
JMF 0:50979ffa39b6 66 ret=tsock.send_all(http_cmd, sizeof(http_cmd)-1);
JMF 0:50979ffa39b6 67 printf("TCP Socket sent %d bytes.\r\n",ret);
JMF 0:50979ffa39b6 68 char buffer[1500];
JMF 0:50979ffa39b6 69
JMF 0:50979ffa39b6 70 tsock.set_blocking (false, 1000);
JMF 0:50979ffa39b6 71 while (ret>0) {
JMF 0:50979ffa39b6 72 ret = tsock.receive(buffer, sizeof(buffer)-1);
JMF 0:50979ffa39b6 73 buffer[ret] = '\0';
JMF 0:50979ffa39b6 74 if( ret > 0 )
JMF 0:50979ffa39b6 75 printf("Received %d chars from server:" CRLF "<----->" CRLF "%s" CRLF
JMF 0:50979ffa39b6 76 "<----->" CRLF, ret, buffer);
JMF 0:50979ffa39b6 77 }
JMF 0:50979ffa39b6 78 tsock.close();
JMF 0:50979ffa39b6 79
JMF 0:50979ffa39b6 80 //
JMF 0:50979ffa39b6 81 //demonstrate a UDP connection
JMF 0:50979ffa39b6 82 //
JMF 0:50979ffa39b6 83 printf("\n\n\n\n-------------------------------------" CRLF);
JMF 0:50979ffa39b6 84 printf( "starting UDP Socket Test..." CRLF);
JMF 0:50979ffa39b6 85
JMF 0:50979ffa39b6 86 //#define UDP_URL "utcnist.colorado.edu"
JMF 0:50979ffa39b6 87 //#define UDP_PORT 37
JMF 0:50979ffa39b6 88 #define UDP_URL "pool.ntp.org"
JMF 0:50979ffa39b6 89 #define UDP_PORT 123
JMF 0:50979ffa39b6 90
JMF 0:50979ffa39b6 91 UDPSocket usock;
JMF 0:50979ffa39b6 92 usock.init();
JMF 0:50979ffa39b6 93 Endpoint nist;
JMF 0:50979ffa39b6 94
JMF 0:50979ffa39b6 95 nist.set_address(UDP_URL, UDP_PORT);
JMF 0:50979ffa39b6 96 printf("Endpoing set to '%s'/%s/%d, try sending data." CRLF,
JMF 0:50979ffa39b6 97 UDP_URL, nist.get_address(), nist.get_port());
JMF 0:50979ffa39b6 98
JMF 0:50979ffa39b6 99 // char out_buffer[] = "plop"; // Does not matter
JMF 0:50979ffa39b6 100
JMF 0:50979ffa39b6 101 char out_buffer[48];
JMF 0:50979ffa39b6 102 out_buffer[0] = 0xE3; // LI, Version, Mode
JMF 0:50979ffa39b6 103 out_buffer[1] = 0; // Stratum, or type of clock
JMF 0:50979ffa39b6 104 out_buffer[2] = 6; // Polling Interval
JMF 0:50979ffa39b6 105 out_buffer[3] = 0xEC; // Peer Clock Precision
JMF 0:50979ffa39b6 106 // 8 bytes of zero for Root Delay & Root Dispersion
JMF 0:50979ffa39b6 107 out_buffer[12] = 49;
JMF 0:50979ffa39b6 108 out_buffer[13] = 0x4E;
JMF 0:50979ffa39b6 109 out_buffer[14] = 49;
JMF 0:50979ffa39b6 110 out_buffer[15] = 52;
JMF 0:50979ffa39b6 111
JMF 0:50979ffa39b6 112 ret = usock.sendTo(nist, out_buffer, sizeof(out_buffer));
JMF 0:50979ffa39b6 113 printf("sent buffer to UDP IP/Port (ret=%d)" CRLF "Now try receiving." CRLF,ret);
JMF 0:50979ffa39b6 114
JMF 0:50979ffa39b6 115
JMF 0:50979ffa39b6 116 usock.set_blocking (true, 1000);
JMF 0:50979ffa39b6 117 char in_buffer[50];
JMF 0:50979ffa39b6 118 int n = usock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
JMF 0:50979ffa39b6 119
JMF 0:50979ffa39b6 120 printf("Received %d bytes via UDP IP address %s on port %d." CRLF,
JMF 0:50979ffa39b6 121 n, nist.get_address(), nist.get_port());
JMF 0:50979ffa39b6 122
JMF 0:50979ffa39b6 123 usock.close();
JMF 0:50979ffa39b6 124
JMF 0:50979ffa39b6 125 //
JMF 0:50979ffa39b6 126 //demonstrate HTTPClient operations
JMF 0:50979ffa39b6 127 //
JMF 0:50979ffa39b6 128 HTTPClient http;
JMF 0:50979ffa39b6 129 char str[512];
JMF 0:50979ffa39b6 130
JMF 0:50979ffa39b6 131 printf("\n\n\n\n-------------------------------------" CRLF);
JMF 0:50979ffa39b6 132 printf( "starting HTTPClient Socket Test..." CRLF);
JMF 0:50979ffa39b6 133
JMF 0:50979ffa39b6 134 //GET data
JMF 0:50979ffa39b6 135 printf(">>>>Fetch a page..." CRLF);
JMF 0:50979ffa39b6 136
JMF 0:50979ffa39b6 137 ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, strlen(str));
JMF 0:50979ffa39b6 138 if (!ret)
JMF 0:50979ffa39b6 139 {
JMF 0:50979ffa39b6 140 printf("Page fetched successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 141 printf("<----->" CRLF "Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 142 }
JMF 0:50979ffa39b6 143 else
JMF 0:50979ffa39b6 144 {
JMF 0:50979ffa39b6 145 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 146 }
JMF 0:50979ffa39b6 147
JMF 0:50979ffa39b6 148 //POST data
JMF 0:50979ffa39b6 149 HTTPMap map;
JMF 0:50979ffa39b6 150 HTTPText inText(str, 512);
JMF 0:50979ffa39b6 151 map.put("Hello", "World");
JMF 0:50979ffa39b6 152 map.put("test", "1234");
JMF 0:50979ffa39b6 153
JMF 0:50979ffa39b6 154 printf(CRLF CRLF ">>>>Post data..." CRLF);
JMF 0:50979ffa39b6 155 ret = http.post("http://httpbin.org/post", map, &inText);
JMF 0:50979ffa39b6 156 if (!ret)
JMF 0:50979ffa39b6 157 {
JMF 0:50979ffa39b6 158 printf("Executed POST successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 159 printf("<----->" CRLF );
JMF 0:50979ffa39b6 160 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 161 }
JMF 0:50979ffa39b6 162 else
JMF 0:50979ffa39b6 163 {
JMF 0:50979ffa39b6 164 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 165 }
JMF 0:50979ffa39b6 166
JMF 0:50979ffa39b6 167 //PUT data
JMF 0:50979ffa39b6 168 strcpy(str, "This is a PUT test!");
JMF 0:50979ffa39b6 169 HTTPText outText(str);
JMF 0:50979ffa39b6 170 printf(CRLF CRLF ">>>>Put data..." CRLF);
JMF 0:50979ffa39b6 171 ret = http.put("http://httpbin.org/put", outText, &inText);
JMF 0:50979ffa39b6 172 if (!ret)
JMF 0:50979ffa39b6 173 {
JMF 0:50979ffa39b6 174 printf("Executed PUT successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 175 printf("<----->" CRLF );
JMF 0:50979ffa39b6 176 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 177 }
JMF 0:50979ffa39b6 178 else
JMF 0:50979ffa39b6 179 {
JMF 0:50979ffa39b6 180 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 181 }
JMF 0:50979ffa39b6 182
JMF 0:50979ffa39b6 183 //DELETE data
JMF 0:50979ffa39b6 184 printf(CRLF CRLF ">>>>Delete data..." CRLF);
JMF 0:50979ffa39b6 185 ret = http.del("http://httpbin.org/delete", &inText);
JMF 0:50979ffa39b6 186 if (!ret)
JMF 0:50979ffa39b6 187 {
JMF 0:50979ffa39b6 188 printf("Executed DELETE successfully - read %d characters" CRLF, strlen(str));
JMF 0:50979ffa39b6 189 printf("<----->" CRLF );
JMF 0:50979ffa39b6 190 printf("Result: %s" CRLF "<----->" CRLF, str);
JMF 0:50979ffa39b6 191 }
JMF 0:50979ffa39b6 192 else
JMF 0:50979ffa39b6 193 {
JMF 0:50979ffa39b6 194 printf("Error - ret = %d - HTTP return code = %d" CRLF, ret, http.getHTTPResponseCode());
JMF 0:50979ffa39b6 195 }
JMF 0:50979ffa39b6 196
JMF 0:50979ffa39b6 197 wnc.disconnect();
JMF 0:50979ffa39b6 198 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:50979ffa39b6 199 while(1) {}
JMF 0:50979ffa39b6 200 }
JMF 0:50979ffa39b6 201