Austin Blackstone / Mbed 2 deprecated murataDemo

Dependencies:   SNICInterface mbed-rtos mbed

Fork of SNIC-xively-jumpstart-demo by muRata

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
00002  *  muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 #include "mbed.h"
00020 #include "SNIC_WifiInterface.h"
00021 
00022 Serial pc(USBTX, USBRX);
00023 
00024 
00025 #define WIFI_SSID                  "demossid"
00026 #define WIFI_SECURITY_TYPE         e_SEC_OPEN
00027 #define WIFI_SECUTIRY_KEY          "WPA2_PASSPHRASE"
00028 #define WIFI_SECUTIRY_KEY_LEN      15
00029 
00030 //#define WIFI_SSID                  "AP_SSID"
00031 //#define WIFI_SECURITY_TYPE         e_SEC_WPA2_AES
00032 //#define WIFI_SECUTIRY_KEY          "WPA2_PASSPHRASE"
00033 //#define WIFI_SECUTIRY_KEY_LEN      15
00034 
00035 
00036 C_SNIC_WifiInterface     wifi( D1, D0, NC, NC, D3 );
00037 
00038 // callback function used by scan()
00039 // this function will be called on every network scanned
00040 void scanCallbackFn(tagSCAN_RESULT_T *scan_result)
00041 {
00042     printf("\r\n");
00043     printf("channel = %d \r\n"      ,scan_result->channel);
00044     printf("rssi = %d \r\n"         ,scan_result->rssi);
00045     printf("security = %d \r\n"     ,scan_result->security);
00046     printf("bssid = %x%x%x%x%x%x\r\n",scan_result->bssid[0],scan_result->bssid[1],scan_result->bssid[2],scan_result->bssid[3],scan_result->bssid[4],scan_result->bssid[5]);
00047     printf("network_type = %d \r\n" ,scan_result->network_type);
00048     printf("max_rate = %d \r\n"     ,scan_result->max_rate);
00049     printf("ssid = %s \r\n"         ,scan_result->ssid);
00050 }
00051 
00052 // main loop
00053 int main()
00054 {
00055     // for built in debug printouts
00056 //    pc.baud( 115200 );
00057 
00058     int check = 0;
00059 
00060     // Initialize Wi-Fi interface
00061     check = wifi.init();
00062     if( check != 0 ) {
00063         printf( "Wifi could not be initialized, halting.\r\n" );
00064         return -1;
00065     } else {
00066         printf("wifi initialized successfully!\r\n");
00067     }
00068     wait(0.5);
00069 
00070     // good form to make sure you are disconnected from all AP's
00071     check = wifi.disconnect();
00072     if( check != 0 ) {
00073         printf( "disconnect failed\r\n" );
00074         return -1;
00075     } else {
00076         printf("disconnection successful!\r\n");
00077     }
00078     wait(0.3);
00079 
00080     // Connect AP
00081     check = wifi.connect( WIFI_SSID
00082                           , strlen(WIFI_SSID)
00083                           , WIFI_SECURITY_TYPE
00084                           , WIFI_SECUTIRY_KEY
00085                           , WIFI_SECUTIRY_KEY_LEN );
00086     if( check != 0) {
00087         printf("Connect Failed\r\n");
00088     } else {
00089         printf("connected successfully!\r\n");
00090     }
00091     wait(0.5);
00092 
00093     // Get DHCP IP
00094     check = wifi.setIPConfig( true ); // get IP as DHCP IP
00095     if(check != 0) {
00096         printf("SetIPConfig failed \r\n");
00097     } else {
00098         printf("SetIPConfig successful \r\n");
00099     }
00100 
00101     // Get RSSI
00102     signed char temp = 0;
00103     check = wifi.getRssi(&temp);
00104     if(check != 0) {
00105         printf("getRssi failed. \r\n");
00106     } else {
00107         printf("getRssi success: %d \r\n",temp);
00108     }
00109 
00110     // check IP Address
00111     char * ip ;
00112     ip = wifi.getIPAddress();
00113     if(ip == 0) {
00114         printf("getIP failed. \r\n");
00115     } else {
00116         printf("getIP success: %s \r\n",ip);
00117     }
00118 
00119     // get wifi status
00120     tagWIFI_STATUS_T status;
00121     check = wifi.getWifiStatus(&status);
00122     if(check != 0) {
00123         printf("getWifiStatus failed \r\n");
00124     } else {
00125         // Status 0=WifiOff, 1=No Network, 2=Connected to AP, 3=Started AP mode
00126         printf("getWifiStatus success: status =%d, MAC = %x%x%x%x%x%x, SSID = %s  \r\n",
00127                status.status,
00128                status.mac_address[0], status.mac_address[1],
00129                status.mac_address[2], status.mac_address[3],
00130                status.mac_address[4], status.mac_address[5],
00131                status.ssid );
00132     }
00133 
00134     // scan for wifi, results will be called in callback function
00135     check = wifi.scan(NULL,NULL,scanCallbackFn);
00136     if(check != 0) {
00137         printf("scan failed! \r\n");
00138     } else {
00139         printf("Scan Success! \r\n");
00140 
00141     }
00142 
00143 //    if(check != 0){
00144 //        printf(" \r\n");
00145 //    }else{
00146 //        printf(" \r\n");
00147 //    }
00148 
00149     wait( 1.0 );
00150 }