This is a NetworkSocketAPI example for LWIPBP3595Interface_STA library. LWIPBP3595Interface_STA library only works with GR-PEACH. The base example is HelloLWIPInterface.
Dependencies: LWIPBP3595Interface_STA LWIPInterface NetworkSocketAPI mbed-rtos mbed
Fork of HelloLWIPBP3595Interface by
main.cpp@6:0679a8be2368, 2017-06-29 (annotated)
- Committer:
- MACRUM
- Date:
- Thu Jun 29 10:08:08 2017 +0000
- Revision:
- 6:0679a8be2368
- Parent:
- 5:f2f7d44c366d
Add wifi.connect parameter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tousaki | 0:8d78a24b262f | 1 | /* main.cpp in HelloLWIPBP3595Interface */ |
tousaki | 0:8d78a24b262f | 2 | /* Copyright (C) 2016 Grape Systems, Inc. */ |
tousaki | 0:8d78a24b262f | 3 | /* The base file is main.cpp in HelloLWIPInterface. */ |
tousaki | 0:8d78a24b262f | 4 | |
tousaki | 0:8d78a24b262f | 5 | /* main.cpp in HelloLWIPInterface */ |
tousaki | 0:8d78a24b262f | 6 | /* NetworkSocketAPI Example Program |
tousaki | 0:8d78a24b262f | 7 | * Copyright (c) 2015 ARM Limited |
tousaki | 0:8d78a24b262f | 8 | * |
tousaki | 0:8d78a24b262f | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
tousaki | 0:8d78a24b262f | 10 | * you may not use this file except in compliance with the License. |
tousaki | 0:8d78a24b262f | 11 | * You may obtain a copy of the License at |
tousaki | 0:8d78a24b262f | 12 | * |
tousaki | 0:8d78a24b262f | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
tousaki | 0:8d78a24b262f | 14 | * |
tousaki | 0:8d78a24b262f | 15 | * Unless required by applicable law or agreed to in writing, software |
tousaki | 0:8d78a24b262f | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
tousaki | 0:8d78a24b262f | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
tousaki | 0:8d78a24b262f | 18 | * See the License for the specific language governing permissions and |
tousaki | 0:8d78a24b262f | 19 | * limitations under the License. |
tousaki | 0:8d78a24b262f | 20 | */ |
tousaki | 0:8d78a24b262f | 21 | |
tousaki | 5:f2f7d44c366d | 22 | /* This is a NetworkSocketAPI example for LWIPBP3595Interface_STA library. */ |
tousaki | 5:f2f7d44c366d | 23 | /* LWIPBP3595Interface_STA library only works with GR-PEACH. */ |
tousaki | 5:f2f7d44c366d | 24 | /* The base example is HelloLWIPInterface. */ |
tousaki | 5:f2f7d44c366d | 25 | |
tousaki | 5:f2f7d44c366d | 26 | /* |
tousaki | 5:f2f7d44c366d | 27 | Warning! |
tousaki | 5:f2f7d44c366d | 28 | When exporting and using it, increase the following stack size. |
tousaki | 5:f2f7d44c366d | 29 | |
tousaki | 5:f2f7d44c366d | 30 | [LWIPInterface/lwip/lwipopts.h]------------- |
tousaki | 5:f2f7d44c366d | 31 | #define TCPIP_THREAD_STACKSIZE 1024 |
tousaki | 5:f2f7d44c366d | 32 | -> |
tousaki | 5:f2f7d44c366d | 33 | #define TCPIP_THREAD_STACKSIZE 2048 |
tousaki | 5:f2f7d44c366d | 34 | -------------------------------------------- |
tousaki | 5:f2f7d44c366d | 35 | */ |
tousaki | 5:f2f7d44c366d | 36 | |
tousaki | 2:144dc7ff85e5 | 37 | /* |
tousaki | 2:144dc7ff85e5 | 38 | This program works with the following library. |
tousaki | 3:a4b863fd65b8 | 39 | mbed-rtos : revision 115 |
tousaki | 2:144dc7ff85e5 | 40 | */ |
tousaki | 2:144dc7ff85e5 | 41 | |
tousaki | 0:8d78a24b262f | 42 | #include "mbed.h" |
tousaki | 0:8d78a24b262f | 43 | #include "LWIPBP3595Interface.h" |
tousaki | 0:8d78a24b262f | 44 | #include "TCPSocket.h" |
tousaki | 0:8d78a24b262f | 45 | |
tousaki | 0:8d78a24b262f | 46 | LWIPBP3595Interface wifi; |
tousaki | 0:8d78a24b262f | 47 | |
tousaki | 0:8d78a24b262f | 48 | DigitalOut led(LED_GREEN); |
tousaki | 0:8d78a24b262f | 49 | void blink() |
tousaki | 0:8d78a24b262f | 50 | { |
tousaki | 0:8d78a24b262f | 51 | led = !led; |
tousaki | 0:8d78a24b262f | 52 | } |
tousaki | 0:8d78a24b262f | 53 | |
tousaki | 0:8d78a24b262f | 54 | int main() |
tousaki | 0:8d78a24b262f | 55 | { |
tousaki | 0:8d78a24b262f | 56 | Ticker blinky; |
tousaki | 0:8d78a24b262f | 57 | blinky.attach(blink, 0.4f); |
tousaki | 0:8d78a24b262f | 58 | |
tousaki | 0:8d78a24b262f | 59 | printf("NetworkSocketAPI Example\r\n"); |
tousaki | 0:8d78a24b262f | 60 | |
MACRUM | 6:0679a8be2368 | 61 | wifi.connect("ssid", "password", NSAPI_SECURITY_WPA2); |
tousaki | 0:8d78a24b262f | 62 | const char *ip = wifi.get_ip_address(); |
tousaki | 0:8d78a24b262f | 63 | const char *mac = wifi.get_mac_address(); |
tousaki | 0:8d78a24b262f | 64 | printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
tousaki | 0:8d78a24b262f | 65 | printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
tousaki | 0:8d78a24b262f | 66 | |
tousaki | 0:8d78a24b262f | 67 | SocketAddress addr(&wifi, "mbed.org"); |
tousaki | 0:8d78a24b262f | 68 | printf("mbed.org resolved to: %s\r\n", addr.get_ip_address()); |
tousaki | 0:8d78a24b262f | 69 | |
tousaki | 0:8d78a24b262f | 70 | TCPSocket socket(&wifi); |
tousaki | 0:8d78a24b262f | 71 | socket.connect("4.ifcfg.me", 23); |
tousaki | 0:8d78a24b262f | 72 | |
tousaki | 0:8d78a24b262f | 73 | char buffer[64]; |
tousaki | 0:8d78a24b262f | 74 | int count = socket.recv(buffer, sizeof buffer); |
tousaki | 0:8d78a24b262f | 75 | printf("public IP address is: %.15s\r\n", &buffer[15]); |
tousaki | 0:8d78a24b262f | 76 | |
tousaki | 0:8d78a24b262f | 77 | socket.close(); |
tousaki | 0:8d78a24b262f | 78 | wifi.disconnect(); |
tousaki | 0:8d78a24b262f | 79 | |
tousaki | 0:8d78a24b262f | 80 | printf("Done\r\n"); |
tousaki | 0:8d78a24b262f | 81 | } |