This example test the SSL socket connection ability with google.com@443

Dependencies:   NNN50_WIFI_API

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. ***************
00002 *
00003 * File Name : main.cpp
00004 * Authors   : Tsungta Wu - CPBG (tsungta.wu@deltaww.com)
00005 * Version   : V.1.0.0
00006 * Date      : 2016/Nov/24
00007 *
00008 * This example only show the most basic WiFi operation include AP scan and connect 
00009 * The usage of TCP/UDP socket please refer to the mbed Handbook from the link below
00010 * https://developer.mbed.org/handbook/Socket
00011 *
00012 *******************************************************************************/
00013 
00014 #include "mbed.h"
00015 #include "EthernetInterface.h"
00016 #include "WIFIDevice.h"
00017 
00018 const char* SERVER_ADDRESS = "google.com";
00019 const int SERVER_PORT = 443;
00020 
00021 void scanCallback(tstrM2mWifiscanResult result)
00022 {
00023     printf("SSID: %s \n", result.au8SSID);
00024     printf("RSSI: %i \n", result.s8rssi);
00025 }
00026 
00027 int main() {
00028 
00029     EthernetInterface eth;
00030     WIFIDevice wifi;
00031 
00032     eth.init();
00033 
00034     wifi.apScan(scanCallback);
00035              
00036     wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TWCYNPC0209_Mac_mini", "mayday55555"); 
00037     
00038     eth.connect();    
00039 
00040     if(wifi.is_AP_connected())
00041         printf("Connect Success! \n");
00042     else
00043         printf("Connect Fail! \n");    
00044 
00045     printf("MAC: %s\n", eth.getMACAddress());            
00046     printf("IP: %s\n", eth.getIPAddress());
00047     printf("Gateway: %s\n", eth.getGateway());
00048     printf("NetworkMask: %s\n", eth.getNetworkMask()); 
00049 
00050     TCPSocketConnection ssl_socket;
00051 
00052     if (wifi.is_AP_connected() && ssl_socket.connect(SERVER_ADDRESS, SERVER_PORT, true) == 0) { //set true to enable ssl socket connection
00053         printf("Connected to Server \n");
00054                     
00055         ssl_socket.set_blocking(false, 3500);
00056                     
00057         // Send message to server
00058         char buf_out[] = "GET / HTTP/1.1\r\n\r\n";
00059         printf("Sending  message to Server : '%s' \n",buf_out);
00060         ssl_socket.send(buf_out, sizeof(buf_out));
00061                     
00062         // Receive message from server
00063         char buf_in[1400];  //set receive buffer size to maximum of 1400 bytes
00064 
00065         int n = ssl_socket.receive(buf_in, 1400);
00066         printf("Received %i bytes from server: \n %s \n", n, buf_in);                
00067     } else
00068         printf("Unable to connect to server\n");    
00069     
00070     // Clean up
00071     ssl_socket.close();
00072     eth.disconnect();
00073     wifi.sleep();     
00074            
00075     while(1) {
00076     }
00077 }
00078