Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* WiFi Example 00002 * Copyright (c) 2016 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "TCPSocket.h" 00019 #include "MXCHIPInterface.h" 00020 00021 MXCHIPInterface wifi(D10,D2); 00022 Serial pc(STDIO_UART_TX,STDIO_UART_RX,115200); 00023 00024 void http_demo(NetworkInterface *net) 00025 { 00026 TCPSocket socket; 00027 nsapi_error_t response; 00028 00029 printf("Sending HTTP request to www.arm.com...\r\n"); 00030 00031 // Open a socket on the network interface, and create a TCP connection to www.arm.com 00032 socket.open(net); 00033 response = socket.connect("www.arm.com", 80); 00034 if(0 != response) { 00035 printf("Error connecting: %d\r\n", response); 00036 socket.close(); 00037 return; 00038 } 00039 00040 // Send a simple http request 00041 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; 00042 nsapi_size_t size = sizeof sbuffer; 00043 response = 0; 00044 while(size) 00045 { 00046 response = socket.send(sbuffer+response, size); 00047 if (response < 0) { 00048 printf("Error sending data: %d\r\n", response); 00049 socket.close(); 00050 return; 00051 } else { 00052 size -= response; 00053 // Check if entire message was sent or not 00054 printf("sent %d [%.*s]\r\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); 00055 } 00056 } 00057 00058 // Recieve a simple http response and print out the response line 00059 char rbuffer[64]; 00060 response = socket.recv(rbuffer, sizeof rbuffer); 00061 if (response < 0) { 00062 printf("Error receiving data: %d\r\n", response); 00063 } else { 00064 printf("recv %d [%.*s]\r\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); 00065 } 00066 00067 // Close the socket to return its memory and bring down the network interface 00068 socket.close(); 00069 } 00070 00071 int main() 00072 { 00073 printf("WiFi example\r\n\r\n"); 00074 00075 printf("\r\nConnecting...\r\n"); 00076 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00077 if (ret != 0) { 00078 printf("\r\nConnection error\r\n"); 00079 return -1; 00080 } 00081 00082 printf("Success\r\n\r\n"); 00083 printf("MAC: %s\r\n", wifi.get_mac_address()); 00084 printf("IP: %s\r\n", wifi.get_ip_address()); 00085 printf("Netmask: %s\r\n", wifi.get_netmask()); 00086 printf("Gateway: %s\r\n", wifi.get_gateway()); 00087 printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); 00088 00089 http_demo(&wifi); 00090 00091 wifi.disconnect(); 00092 00093 printf("\r\nDone\r\n"); 00094 }
Generated on Fri Jul 15 2022 07:58:40 by
1.7.2