![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Non-blocking example of the LWIPInterface
Dependencies: LWIPInterface NetworkSocketAPI mbed-rtos mbed
Non-blocking example for the NetworkSocketAPI. Attempts to get the device's public facing IP address from ifcfg.me using the Happy Eyeballs algorithm to fetch an IPv4 or IPv6 address.
main.cpp@9:da5a230fa4f4, 2015-06-17 (annotated)
- Committer:
- sam_grove
- Date:
- Wed Jun 17 23:38:33 2015 +0000
- Revision:
- 9:da5a230fa4f4
- Parent:
- 7:6787b920b496
- Child:
- 11:b7e531a4d661
update example program adding sockets access
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:1984a177ff56 | 1 | /* NetworkSocketAPI Example Program |
sam_grove | 0:1984a177ff56 | 2 | * Copyright (c) 2015 ARM Limited |
sam_grove | 0:1984a177ff56 | 3 | * |
sam_grove | 0:1984a177ff56 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 0:1984a177ff56 | 5 | * you may not use this file except in compliance with the License. |
sam_grove | 0:1984a177ff56 | 6 | * You may obtain a copy of the License at |
sam_grove | 0:1984a177ff56 | 7 | * |
sam_grove | 0:1984a177ff56 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 0:1984a177ff56 | 9 | * |
sam_grove | 0:1984a177ff56 | 10 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 0:1984a177ff56 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 0:1984a177ff56 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 0:1984a177ff56 | 13 | * See the License for the specific language governing permissions and |
sam_grove | 0:1984a177ff56 | 14 | * limitations under the License. |
sam_grove | 0:1984a177ff56 | 15 | */ |
sam_grove | 0:1984a177ff56 | 16 | |
sam_grove | 0:1984a177ff56 | 17 | #include "mbed.h" |
sam_grove | 7:6787b920b496 | 18 | #include "WiFiRadioInterface.h" |
sam_grove | 0:1984a177ff56 | 19 | |
sam_grove | 0:1984a177ff56 | 20 | DigitalOut myled(LED1); |
sam_grove | 0:1984a177ff56 | 21 | |
sam_grove | 7:6787b920b496 | 22 | WiFiRadioInterface wifi; |
sam_grove | 2:7283ce112304 | 23 | // or |
sam_grove | 2:7283ce112304 | 24 | //NetworkInterface *wifi = new WiFiInterface; |
sam_grove | 2:7283ce112304 | 25 | |
sam_grove | 4:cb8a17dd6746 | 26 | //TCPSocket tcp(wifi.getInstance()); |
sam_grove | 2:7283ce112304 | 27 | // or |
sam_grove | 2:7283ce112304 | 28 | //SocketInterface tcp = new TCPSocket(wifi->getInstance(); |
sam_grove | 2:7283ce112304 | 29 | |
sam_grove | 4:cb8a17dd6746 | 30 | //UDPSocket udp(wifi.getInstance()); |
sam_grove | 2:7283ce112304 | 31 | // or |
sam_grove | 2:7283ce112304 | 32 | //SocketInterface udp = new UDPSocket(wifi->getInstance(); |
sam_grove | 2:7283ce112304 | 33 | |
sam_grove | 0:1984a177ff56 | 34 | int main() |
sam_grove | 0:1984a177ff56 | 35 | { |
sam_grove | 2:7283ce112304 | 36 | puts("NetworkSocketAPI Example"); |
sam_grove | 4:cb8a17dd6746 | 37 | |
sam_grove | 2:7283ce112304 | 38 | wifi.init(); |
sam_grove | 6:751245e265f6 | 39 | wifi.connect("apname", "phrase", WI_WPA2); |
sam_grove | 6:751245e265f6 | 40 | |
sam_grove | 4:cb8a17dd6746 | 41 | printf("Wifi is %s\n", (wifi.isConnected() > 0) ? "Connected" : "Disconnected"); |
sam_grove | 4:cb8a17dd6746 | 42 | |
sam_grove | 2:7283ce112304 | 43 | printf("IP: %s\n", wifi.getIPAddress()); |
sam_grove | 2:7283ce112304 | 44 | printf("Gateway: %s\n", wifi.getGateway()); |
sam_grove | 2:7283ce112304 | 45 | printf("Mask: %s\n", wifi.getNetworkMask()); |
sam_grove | 2:7283ce112304 | 46 | printf("MAC: %s\n", wifi.getMACAddress()); |
sam_grove | 9:da5a230fa4f4 | 47 | |
sam_grove | 9:da5a230fa4f4 | 48 | //Endpoint google; |
sam_grove | 9:da5a230fa4f4 | 49 | wifi.set_address(wifi.get_host_by_name("www.google.com")); |
sam_grove | 9:da5a230fa4f4 | 50 | wifi.set_address("216.58.192.46"); |
sam_grove | 9:da5a230fa4f4 | 51 | wifi.set_port(80); |
sam_grove | 9:da5a230fa4f4 | 52 | |
sam_grove | 9:da5a230fa4f4 | 53 | //Socket socket; |
sam_grove | 9:da5a230fa4f4 | 54 | char buf[1024]; |
sam_grove | 9:da5a230fa4f4 | 55 | wifi.connect(*wifi.endpoint); |
sam_grove | 9:da5a230fa4f4 | 56 | wifi.recv(buf, 1024); |
sam_grove | 9:da5a230fa4f4 | 57 | wifi.send(buf, sizeof(buf)); |
sam_grove | 9:da5a230fa4f4 | 58 | wifi.close(*wifi.endpoint); |
sam_grove | 2:7283ce112304 | 59 | |
sam_grove | 2:7283ce112304 | 60 | wifi.disconnect(); |
sam_grove | 4:cb8a17dd6746 | 61 | |
sam_grove | 0:1984a177ff56 | 62 | while(1) { |
sam_grove | 2:7283ce112304 | 63 | myled = !myled; |
sam_grove | 0:1984a177ff56 | 64 | } |
sam_grove | 0:1984a177ff56 | 65 | } |