This example test the SSL socket connection ability with google.com@443
Dependencies: NNN50_WIFI_API
main.cpp@0:3e902eda4f71, 2017-09-12 (annotated)
- Committer:
- tsungta
- Date:
- Tue Sep 12 02:06:32 2017 +0000
- Revision:
- 0:3e902eda4f71
- Child:
- 1:4d09372e86ed
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsungta | 0:3e902eda4f71 | 1 | /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** |
tsungta | 0:3e902eda4f71 | 2 | * |
tsungta | 0:3e902eda4f71 | 3 | * File Name : main.cpp |
tsungta | 0:3e902eda4f71 | 4 | * Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) |
tsungta | 0:3e902eda4f71 | 5 | * Version : V.1.0.0 |
tsungta | 0:3e902eda4f71 | 6 | * Date : 2016/Nov/24 |
tsungta | 0:3e902eda4f71 | 7 | * |
tsungta | 0:3e902eda4f71 | 8 | * This example only show the most basic WiFi operation include AP scan and connect |
tsungta | 0:3e902eda4f71 | 9 | * The usage of TCP/UDP socket please refer to the mbed Handbook from the link below |
tsungta | 0:3e902eda4f71 | 10 | * https://developer.mbed.org/handbook/Socket |
tsungta | 0:3e902eda4f71 | 11 | * |
tsungta | 0:3e902eda4f71 | 12 | *******************************************************************************/ |
tsungta | 0:3e902eda4f71 | 13 | |
tsungta | 0:3e902eda4f71 | 14 | #include "mbed.h" |
tsungta | 0:3e902eda4f71 | 15 | #include "EthernetInterface.h" |
tsungta | 0:3e902eda4f71 | 16 | #include "WIFIDevice.h" |
tsungta | 0:3e902eda4f71 | 17 | |
tsungta | 0:3e902eda4f71 | 18 | const char* ECHO_SERVER_ADDRESS = "192.168.2.13"; |
tsungta | 0:3e902eda4f71 | 19 | const int ECHO_SERVER_PORT = 1030; |
tsungta | 0:3e902eda4f71 | 20 | |
tsungta | 0:3e902eda4f71 | 21 | void scanCallback(tstrM2mWifiscanResult result) |
tsungta | 0:3e902eda4f71 | 22 | { |
tsungta | 0:3e902eda4f71 | 23 | printf("SSID: %s \n", result.au8SSID); |
tsungta | 0:3e902eda4f71 | 24 | printf("RSSI: %i \n", result.s8rssi); |
tsungta | 0:3e902eda4f71 | 25 | } |
tsungta | 0:3e902eda4f71 | 26 | |
tsungta | 0:3e902eda4f71 | 27 | int main() { |
tsungta | 0:3e902eda4f71 | 28 | |
tsungta | 0:3e902eda4f71 | 29 | EthernetInterface eth; |
tsungta | 0:3e902eda4f71 | 30 | WIFIDevice wifi; |
tsungta | 0:3e902eda4f71 | 31 | |
tsungta | 0:3e902eda4f71 | 32 | eth.init(); |
tsungta | 0:3e902eda4f71 | 33 | |
tsungta | 0:3e902eda4f71 | 34 | wifi.apScan(scanCallback); |
tsungta | 0:3e902eda4f71 | 35 | |
tsungta | 0:3e902eda4f71 | 36 | wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TWCYNPC0209_Mac mini", "mayday55555"); |
tsungta | 0:3e902eda4f71 | 37 | |
tsungta | 0:3e902eda4f71 | 38 | eth.connect(); |
tsungta | 0:3e902eda4f71 | 39 | |
tsungta | 0:3e902eda4f71 | 40 | if(wifi.is_AP_connected()) |
tsungta | 0:3e902eda4f71 | 41 | printf("Connect Success! \n"); |
tsungta | 0:3e902eda4f71 | 42 | else |
tsungta | 0:3e902eda4f71 | 43 | printf("Connect Fail! \n"); |
tsungta | 0:3e902eda4f71 | 44 | |
tsungta | 0:3e902eda4f71 | 45 | printf("MAC: %s\n", eth.getMACAddress()); |
tsungta | 0:3e902eda4f71 | 46 | printf("IP: %s\n", eth.getIPAddress()); |
tsungta | 0:3e902eda4f71 | 47 | printf("Gateway: %s\n", eth.getGateway()); |
tsungta | 0:3e902eda4f71 | 48 | printf("NetworkMask: %s\n", eth.getNetworkMask()); |
tsungta | 0:3e902eda4f71 | 49 | |
tsungta | 0:3e902eda4f71 | 50 | UDPSocket sock; |
tsungta | 0:3e902eda4f71 | 51 | sock.init(); |
tsungta | 0:3e902eda4f71 | 52 | |
tsungta | 0:3e902eda4f71 | 53 | Endpoint echo_server; |
tsungta | 0:3e902eda4f71 | 54 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
tsungta | 0:3e902eda4f71 | 55 | |
tsungta | 0:3e902eda4f71 | 56 | char out_buffer[] = "Hello World"; |
tsungta | 0:3e902eda4f71 | 57 | printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); |
tsungta | 0:3e902eda4f71 | 58 | sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); |
tsungta | 0:3e902eda4f71 | 59 | |
tsungta | 0:3e902eda4f71 | 60 | char in_buffer[256]; //IMPORTANT, array size MUST >= the actual received data size or set to maximum of 1400 if there is uncertainty |
tsungta | 0:3e902eda4f71 | 61 | int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); |
tsungta | 0:3e902eda4f71 | 62 | |
tsungta | 0:3e902eda4f71 | 63 | if(n <0) |
tsungta | 0:3e902eda4f71 | 64 | in_buffer[0] = '\0';//IMPORTANT, in case n = -1 when set_bloacking is timeout, prevent the illegal array in_buffer[-1] access |
tsungta | 0:3e902eda4f71 | 65 | else |
tsungta | 0:3e902eda4f71 | 66 | in_buffer[n] = '\0'; |
tsungta | 0:3e902eda4f71 | 67 | |
tsungta | 0:3e902eda4f71 | 68 | printf("Received message from server: '%s'\n", in_buffer); |
tsungta | 0:3e902eda4f71 | 69 | |
tsungta | 0:3e902eda4f71 | 70 | sock.close(); |
tsungta | 0:3e902eda4f71 | 71 | |
tsungta | 0:3e902eda4f71 | 72 | eth.disconnect(); |
tsungta | 0:3e902eda4f71 | 73 | |
tsungta | 0:3e902eda4f71 | 74 | wifi.sleep(); |
tsungta | 0:3e902eda4f71 | 75 | |
tsungta | 0:3e902eda4f71 | 76 | while(1) { |
tsungta | 0:3e902eda4f71 | 77 | } |
tsungta | 0:3e902eda4f71 | 78 | } |
tsungta | 0:3e902eda4f71 | 79 |