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

Dependencies:   NNN50_WIFI_API

Committer:
tsungta
Date:
Wed Sep 13 07:13:41 2017 +0000
Revision:
2:a9c2d2fdd48f
Parent:
1:4d09372e86ed
add .mbedignore to fix compile error issue

Who changed what in which revision?

UserRevisionLine numberNew 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 1:4d09372e86ed 18 const char* SERVER_ADDRESS = "google.com";
tsungta 1:4d09372e86ed 19 const int SERVER_PORT = 443;
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 1:4d09372e86ed 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 1:4d09372e86ed 50 TCPSocketConnection ssl_socket;
tsungta 1:4d09372e86ed 51
tsungta 1:4d09372e86ed 52 if (wifi.is_AP_connected() && ssl_socket.connect(SERVER_ADDRESS, SERVER_PORT, true) == 0) { //set true to enable ssl socket connection
tsungta 1:4d09372e86ed 53 printf("Connected to Server \n");
tsungta 1:4d09372e86ed 54
tsungta 1:4d09372e86ed 55 ssl_socket.set_blocking(false, 3500);
tsungta 1:4d09372e86ed 56
tsungta 1:4d09372e86ed 57 // Send message to server
tsungta 1:4d09372e86ed 58 char buf_out[] = "GET / HTTP/1.1\r\n\r\n";
tsungta 1:4d09372e86ed 59 printf("Sending message to Server : '%s' \n",buf_out);
tsungta 1:4d09372e86ed 60 ssl_socket.send(buf_out, sizeof(buf_out));
tsungta 1:4d09372e86ed 61
tsungta 1:4d09372e86ed 62 // Receive message from server
tsungta 1:4d09372e86ed 63 char buf_in[1400]; //set receive buffer size to maximum of 1400 bytes
tsungta 1:4d09372e86ed 64
tsungta 1:4d09372e86ed 65 int n = ssl_socket.receive(buf_in, 1400);
tsungta 1:4d09372e86ed 66 printf("Received %i bytes from server: \n %s \n", n, buf_in);
tsungta 1:4d09372e86ed 67 } else
tsungta 1:4d09372e86ed 68 printf("Unable to connect to server\n");
tsungta 0:3e902eda4f71 69
tsungta 1:4d09372e86ed 70 // Clean up
tsungta 1:4d09372e86ed 71 ssl_socket.close();
tsungta 1:4d09372e86ed 72 eth.disconnect();
tsungta 1:4d09372e86ed 73 wifi.sleep();
tsungta 1:4d09372e86ed 74
tsungta 0:3e902eda4f71 75 while(1) {
tsungta 0:3e902eda4f71 76 }
tsungta 0:3e902eda4f71 77 }
tsungta 0:3e902eda4f71 78