Austin Blackstone / Mbed 2 deprecated murataTCPDemo

Dependencies:   SNICInterface mbed-rtos mbed

Fork of murataDemo by Austin Blackstone

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 #include "TCPSocketServer.h"
00022 
00023 Serial pc(USBTX, USBRX);
00024 
00025 
00026 #define WIFI_SSID                  "demossid"
00027 #define WIFI_SECURITY_TYPE         e_SEC_OPEN
00028 #define WIFI_SECUTIRY_KEY          "WPA2_PASSPHRASE"
00029 #define WIFI_SECUTIRY_KEY_LEN      15
00030 
00031 //#define WIFI_SSID                  "AP_SSID"
00032 //#define WIFI_SECURITY_TYPE         e_SEC_WPA2_AES
00033 //#define WIFI_SECUTIRY_KEY          "WPA2_PASSPHRASE"
00034 //#define WIFI_SECUTIRY_KEY_LEN      15
00035 
00036 
00037 C_SNIC_WifiInterface     wifi( D1, D0, NC, NC, D3 );
00038 
00039 int wifiInit()
00040 {
00041     int check = 0;
00042 
00043     // Initialize Wi-Fi interface
00044     check = wifi.init();
00045     if( check != 0 ) {
00046         printf( "Wifi could not be initialized, halting.\r\n" );
00047         return -1;
00048     } else {
00049         printf("wifi initialized successfully!\r\n");
00050     }
00051     wait(0.5);
00052 
00053     // good form to make sure you are disconnected from all AP's
00054     check = wifi.disconnect();
00055     if( check != 0 ) {
00056         printf( "disconnect failed\r\n" );
00057         return -1;
00058     } else {
00059         printf("disconnection successful!\r\n");
00060     }
00061     wait(0.3);
00062 
00063     // Connect AP
00064     check = wifi.connect( WIFI_SSID
00065                           , strlen(WIFI_SSID)
00066                           , WIFI_SECURITY_TYPE
00067                           , WIFI_SECUTIRY_KEY
00068                           , WIFI_SECUTIRY_KEY_LEN );
00069     if( check != 0) {
00070         printf("Connect Failed\r\n");
00071     } else {
00072         printf("connected successfully!\r\n");
00073     }
00074     wait(0.5);
00075 
00076     // Get DHCP IP
00077     check = wifi.setIPConfig( true ); // get IP as DHCP IP
00078     if(check != 0) {
00079         printf("SetIPConfig failed \r\n");
00080     } else {
00081         printf("SetIPConfig successful \r\n");
00082     }
00083 
00084     // check IP Address
00085     char * ip ;
00086     ip = wifi.getIPAddress();
00087     if(ip == 0) {
00088         printf("getIP failed. \r\n");
00089     } else {
00090         printf("getIP success: %s \r\n",ip);
00091     }
00092 }
00093 
00094 //
00095 // main loop
00096 //
00097 int main()
00098 {
00099 
00100     // Initialize Wifi
00101     wifiInit();
00102 
00103     // TCP Schenanigans!
00104     const char* ECHO_SERVER_ADDRESS = "192.168.11.3";
00105     const int ECHO_SERVER_PORT = 7;
00106 
00107     // connect to server
00108     TCPSocketConnection socket;
00109     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
00110         printf("Unable to connect to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00111         wait(1);
00112     }
00113 
00114     // send messages back and forth as long as we are connected
00115     int x = 0;
00116     while(socket.is_connected()) {
00117         // Setup & send message to server
00118         char message[64] = {};
00119         sprintf(message,"%s%d","HelloFromMicrocontroller: ",x++);
00120         socket.send_all(message, sizeof(message) - 1);
00121 
00122         // Receive & print message from server
00123         char buf[256];
00124         int n = socket.receive_all(buf, 256);
00125         buf[n] = '\0';
00126         printf("TCP data from server: '%s'", buf);
00127 
00128         // not needed, here to slow the data down so you can see it whip past.
00129         wait(0.5);
00130     }
00131 
00132     socket.close();
00133     wifi.disconnect();
00134 
00135     while(true) {}
00136 
00137 }