Example program for the C027Interface
Dependencies: C027Interface NetworkSocketAPI mbed
main.cpp@57:f3faa28dc87e, 2016-04-21 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Apr 21 06:53:53 2016 -0500
- Revision:
- 57:f3faa28dc87e
- Parent:
- 56:62bb3fbd1b7e
Updated to match changes to the NSAPI
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" |
geky | 56:62bb3fbd1b7e | 18 | #include "C027Interface.h" |
Christopher Haster |
30:f80540b6e2db | 19 | #include "TCPSocket.h" |
sam_grove | 0:1984a177ff56 | 20 | |
Christopher Haster |
57:f3faa28dc87e | 21 | C027Interface cell; |
Christopher Haster |
57:f3faa28dc87e | 22 | |
Christopher Haster |
57:f3faa28dc87e | 23 | DigitalOut led(LED1); |
Christopher Haster |
57:f3faa28dc87e | 24 | void blink() |
sam_grove | 50:a9926b8b21fe | 25 | { |
Christopher Haster |
57:f3faa28dc87e | 26 | led = !led; |
sam_grove | 50:a9926b8b21fe | 27 | } |
sam_grove | 0:1984a177ff56 | 28 | |
sam_grove | 0:1984a177ff56 | 29 | int main() |
sam_grove | 20:4cb9ef3b0cc9 | 30 | { |
Christopher Haster |
57:f3faa28dc87e | 31 | Ticker blinky; |
Christopher Haster |
57:f3faa28dc87e | 32 | blinky.attach(blink, 0.4f); |
Christopher Haster |
57:f3faa28dc87e | 33 | |
bridadan | 14:c47437f5dae8 | 34 | printf("NetworkSocketAPI Example\r\n"); |
sam_grove | 4:cb8a17dd6746 | 35 | |
geky | 56:62bb3fbd1b7e | 36 | cell.connect(); |
Christopher Haster |
57:f3faa28dc87e | 37 | const char *ip = cell.get_ip_address(); |
Christopher Haster |
57:f3faa28dc87e | 38 | const char *mac = cell.get_mac_address(); |
Christopher Haster |
57:f3faa28dc87e | 39 | printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
Christopher Haster |
57:f3faa28dc87e | 40 | printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
Christopher Haster |
57:f3faa28dc87e | 41 | |
Christopher Haster |
57:f3faa28dc87e | 42 | SocketAddress addr(&cell, "mbed.org"); |
Christopher Haster |
57:f3faa28dc87e | 43 | printf("mbed.org resolved to: %s\r\n", addr.get_ip_address()); |
sarahmarshy | 22:1d355289fc18 | 44 | |
Christopher Haster |
57:f3faa28dc87e | 45 | TCPSocket socket(&cell); |
Christopher Haster |
57:f3faa28dc87e | 46 | socket.connect("4.ifcfg.me", 23); |
sarahmarshy | 21:979b1db5d7da | 47 | |
Christopher Haster |
57:f3faa28dc87e | 48 | char buffer[64]; |
Christopher Haster |
57:f3faa28dc87e | 49 | int count = socket.recv(buffer, sizeof buffer); |
Christopher Haster |
57:f3faa28dc87e | 50 | printf("public IP address is: %.15s\r\n", &buffer[15]); |
Christopher Haster |
57:f3faa28dc87e | 51 | |
Christopher Haster |
30:f80540b6e2db | 52 | socket.close(); |
geky | 56:62bb3fbd1b7e | 53 | cell.disconnect(); |
Christopher Haster |
30:f80540b6e2db | 54 | |
Christopher Haster |
57:f3faa28dc87e | 55 | printf("Done\r\n"); |
sam_grove | 0:1984a177ff56 | 56 | } |