![](/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@42:d8ea089e5606, 2016-02-25 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Feb 25 04:19:15 2016 -0600
- Revision:
- 42:d8ea089e5606
- Parent:
- 41:83cf71e499ee
- Parent:
- 21:979b1db5d7da
- Child:
- 43:932c5ddd7860
Merged api-changes
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" |
sarahmarshy | 16:bb0a5b830dc5 | 18 | #include "ESP8266Interface.h" |
Christopher Haster |
30:f80540b6e2db | 19 | #include "TCPSocket.h" |
sam_grove | 0:1984a177ff56 | 20 | |
Christopher Haster |
32:28a909a3748e | 21 | DigitalOut myled(LED_GREEN); |
Christopher Haster |
30:f80540b6e2db | 22 | void flash(){ myled = !myled; } |
sam_grove | 0:1984a177ff56 | 23 | |
sarahmarshy | 16:bb0a5b830dc5 | 24 | ESP8266Interface wifi(D1, D0); |
Christopher Haster |
30:f80540b6e2db | 25 | TCPSocket socket(&wifi); |
sam_grove | 2:7283ce112304 | 26 | |
sam_grove | 0:1984a177ff56 | 27 | int main() |
sam_grove | 20:4cb9ef3b0cc9 | 28 | { |
sam_grove | 20:4cb9ef3b0cc9 | 29 | Ticker t; |
sam_grove | 24:471a07e886ae | 30 | t.attach(flash, 0.4f); |
bridadan | 14:c47437f5dae8 | 31 | printf("NetworkSocketAPI Example\r\n"); |
sam_grove | 4:cb8a17dd6746 | 32 | |
Christopher Haster |
33:b80621db4a68 | 33 | wifi.connect("ssid", "password"); |
bridadan | 14:c47437f5dae8 | 34 | |
Christopher Haster |
30:f80540b6e2db | 35 | const char *ip = wifi.getIPAddress(); |
Christopher Haster |
30:f80540b6e2db | 36 | const char *mac = wifi.getMACAddress(); |
Christopher Haster |
30:f80540b6e2db | 37 | printf("IP Address is: %s\r\n", (ip) ? ip : "No IP"); |
Christopher Haster |
30:f80540b6e2db | 38 | printf("MAC Address is: %s\r\n", (mac) ? mac : "No MAC"); |
sarahmarshy | 22:1d355289fc18 | 39 | |
Christopher Haster |
30:f80540b6e2db | 40 | socket.open("time-a.nist.gov", 37); |
Christopher Haster |
30:f80540b6e2db | 41 | printf("time-a.nist.gov resolved to: %s\r\n", socket.getIPAddress()); |
sarahmarshy | 21:979b1db5d7da | 42 | |
sam_grove | 24:471a07e886ae | 43 | char recieved[100] = {0}; |
Christopher Haster |
30:f80540b6e2db | 44 | int32_t size = 0; |
Christopher Haster |
30:f80540b6e2db | 45 | size = socket.recv(recieved, sizeof(recieved)); |
Christopher Haster |
30:f80540b6e2db | 46 | |
Christopher Haster |
30:f80540b6e2db | 47 | socket.close(); |
sam_grove | 2:7283ce112304 | 48 | wifi.disconnect(); |
Christopher Haster |
30:f80540b6e2db | 49 | |
Christopher Haster |
30:f80540b6e2db | 50 | printf("Recieved: %ld bytes, %02x%02x%02x%02x\r\n", size, |
Christopher Haster |
30:f80540b6e2db | 51 | recieved[0], recieved[1], recieved[2], recieved[3]); |
sam_grove | 24:471a07e886ae | 52 | printf("NetworkSocketAPI Example Finished\r\n"); |
sam_grove | 0:1984a177ff56 | 53 | } |