WiFi DipCortex / CC3000 Demo - Contains a menu driven set of tests to initalise and control the CC3000 radio. Also allowing you to test various TCP and UDP connections.

Dependencies:   NTPClient WebSocketClient cc3000_hostdriver_mbedsocket mbed HTTPClient

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  
00002 #include "mbed.h"
00003 #include "cc3000.h"
00004 #include "wifi.h"
00005 
00006 
00007 #include "UDPSocket.h"
00008 
00009 #include "NTPClient.h"
00010 #include "tcpTests.h"
00011 #include "main.h"
00012 
00013 using namespace mbed_cc3000;
00014 
00015 // TODO : List internal varibles & CC3000 buffers
00016 // AP point scanning
00017 // List open sockets
00018 // Overkill mode 2 TCP 2 UDP echo ports?
00019 
00020 Serial pc(p19, p20);
00021 //Serial pc(USBTX, USBRX);
00022 
00023 const int ECHO_SERVER_PORT_UDP = 81;
00024 uint8_t *HostToPing = (uint8_t *)"google.com";
00025 tNetappIpconfigRetArgs ipinfo;
00026 
00027 MENU_LEVEL currentMenu = MENU_TOP;
00028 
00029 // ------------------------------------------------------------------------------------------------------------
00030 /*!
00031     @brief Resolve a hostname and ping it
00032 */
00033 // ------------------------------------------------------------------------------------------------------------
00034 void PingTest ( void )
00035 {
00036 uint32_t ip;
00037 int32_t resolveRetCode = 0;
00038     
00039     printf("Get an IP address for %s\r\n",HostToPing);
00040     resolveRetCode = wifi._socket.gethostbyname(HostToPing,strlen((const char *)HostToPing), &ip);
00041     printf("gethostbyname Returned code : %i \r\n", resolveRetCode);
00042     
00043     if (resolveRetCode > -1) 
00044     {
00045         uint8_t add0 = (ip >> 24);
00046         uint8_t add1 = (ip >> 16);
00047         uint8_t add2 = (ip >> 8);
00048         uint8_t add3 = (ip >> 0);
00049         printf("IP address of %s: %d.%d.%d.%d \r\n", HostToPing, add0, add1, add2, add3);
00050         
00051         printf("Sending ping\r\n");
00052         uint32_t reply_count = wifi.ping(ip, 5, 500, 32);
00053         printf("Received %d replies\r\n", reply_count);
00054         printf("Ping complete.\r\n");
00055     } 
00056     else 
00057     {
00058         printf("Failed to resolve\r\n");
00059     }
00060 }
00061 
00062 // ------------------------------------------------------------------------------------------------------------
00063 /*!
00064     @brief Test the NTP library
00065 */
00066 // ------------------------------------------------------------------------------------------------------------
00067 void NtpTest ( void )
00068 {
00069 NTPClient ntp;
00070 
00071     if (ntp.setTime("0.pool.ntp.org",123,10000) == 0)
00072     {
00073         printf("Set time successfully\r\n");
00074         time_t ctTime;
00075         ctTime = time(NULL);
00076         printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
00077     }
00078     else
00079     {
00080         printf("NTP Update Failed\r\n");
00081     } 
00082 }
00083 
00084 // ------------------------------------------------------------------------------------------------------------
00085 /*!
00086     @brief Send a UDP Packet, wait for response
00087 */
00088 // ------------------------------------------------------------------------------------------------------------
00089 void UdpClientTest ( void )
00090 {
00091 UDPSocket socket;
00092 char buf[256];
00093 int n = 0;
00094 Endpoint outEndpoint;
00095 Endpoint inEndpoint;
00096 
00097     if (0 == socket.bind(ECHO_SERVER_PORT_UDP) )
00098     {
00099         
00100         // 2 second timeout
00101         socket.set_blocking(false, 2000);
00102         
00103         printf("\r\n!! Press any key to stop !!\r\n\r\n");
00104     
00105         while (1)
00106         {
00107             if( outEndpoint.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_UDP) < 0 )
00108             {
00109                 printf("Failed to set endpoint address.\r\n");
00110                 break;
00111             }
00112             else if ( socket.sendTo( outEndpoint, hello, sizeof(hello) ) < 0 )
00113             {
00114                 printf("Failed to send the packet.\r\n");
00115             }
00116             else
00117             {
00118                 // Message sent, recv reply
00119                 printf("UDP Socket Sent : %s \r\n", hello);   
00120                 n = socket.receiveFrom( inEndpoint, buf, sizeof(buf) );
00121                 if ( n < 0 )
00122                 {
00123                     printf("Failed to recv the UDP packet.\r\n");
00124                 }
00125                 else
00126                 {
00127                     buf[n] = '\0';
00128                     printf("UDP Socket Recv'd : %s \r\n", buf);    
00129                 }
00130             }
00131     
00132             // Should we stop?
00133             if ( pc.readable() )
00134             {
00135                 pc.getc();
00136                 break;
00137             }
00138         }
00139 
00140         if ( wifi.is_connected() )
00141         {        
00142             socket.close();
00143         }
00144     }
00145     else
00146     {
00147         // Failed to bind to the socket
00148     }
00149 }
00150 
00151 // ------------------------------------------------------------------------------------------------------------
00152 /*!
00153     @brief Listen on a UDP port for messages
00154 */
00155 // ------------------------------------------------------------------------------------------------------------
00156 void UdpServerTest ( void )
00157 {
00158 UDPSocket socket;
00159 Endpoint client;
00160 char buffer[256];
00161 int n = 0;
00162 
00163     if (0 == socket.bind(ECHO_SERVER_PORT_UDP) )
00164     {   
00165         printf("\r\n!! Press any key to stop listening !!\r\n\r\n");
00166         
00167         while (true) 
00168         {
00169             printf("Waiting for packet...\r\n");
00170             n = socket.receiveFrom(client, buffer, sizeof(buffer));
00171         
00172             printf("Received packet from: %s\n", client.get_address());
00173             socket.sendTo(client, buffer, n);
00174         
00175             // Should we stop?
00176             if ( pc.readable() )
00177             {
00178                 pc.getc();
00179                 break;
00180             }
00181         }
00182     }
00183 }
00184 
00185 // ------------------------------------------------------------------------------------------------------------
00186 /*!
00187     @brief Print menu header
00188 */
00189 // ------------------------------------------------------------------------------------------------------------
00190 void Menu_PrintHeader ( void )
00191 {
00192     if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() ))
00193     {
00194         wifi.get_ip_config(&ipinfo);
00195     }
00196     
00197     printf("\r\n");
00198     printf("+-------------------------------------------+\r\n");
00199     printf("|   WiFi DipCortex / CC3000 Kitchen Sink    |\r\n");
00200     printf("+-------------------------------------------+\r\n");
00201     if (! wifi.is_enabled() )
00202     {
00203         printf("|   CC3000 Disabled                         |\r\n"); 
00204     }
00205     else if ( wifi.is_dhcp_configured() ) 
00206     {
00207         printf("|   SSID : %-33s|\r\n", ipinfo.uaSSID);
00208         printf("|   IP : %-35s|\r\n", wifi.getIPAddress());   
00209     }
00210     else if ( wifi.is_connected() )
00211     {
00212         printf("|   Connecting, waiting for DHCP            |\r\n"); 
00213     }
00214     else
00215     {
00216         printf("|   Not Connected                           |\r\n");   
00217     }
00218     printf("+-------------------------------------------+\r\n");
00219     printf("\r\n");
00220 }
00221 
00222 // ------------------------------------------------------------------------------------------------------------
00223 /*!
00224     @brief Control the wifi connection
00225 */
00226 // ------------------------------------------------------------------------------------------------------------
00227 char WaitForSerialCommand ( void )
00228 {
00229 char charIn = 0;
00230 char prevCharIn;
00231 
00232     while (1)
00233     {
00234         prevCharIn = charIn;
00235         charIn = pc.getc();
00236         printf("%c", charIn);
00237         if ((charIn == '\n') || (charIn == '\r'))
00238         {
00239             break;
00240         }
00241     }
00242     
00243     return ( prevCharIn );
00244 }
00245 
00246 // ------------------------------------------------------------------------------------------------------------
00247 /*!
00248     @brief Configure the module for smart connect mode allow configuration over the air
00249 */
00250 // ------------------------------------------------------------------------------------------------------------
00251 void SmartConfig ( void )
00252 {
00253     // We dont want to auto reconnect to an access point
00254     wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
00255     
00256     // start smart config will disconnect, set the prefix
00257     // wait for a message via a SmartConfig app, store it to the profile list
00258     // finally it will reenable auto connection, triggering the module to connect to the new access point
00259     wifi.start_smart_config(0);
00260 }
00261 
00262 // ------------------------------------------------------------------------------------------------------------
00263 /*!
00264     @brief Control the wifi connection
00265 */
00266 // ------------------------------------------------------------------------------------------------------------
00267 void Menu_ConnectionControl ( void )
00268 {
00269 uint32_t ip = 0;
00270     Menu_PrintHeader();
00271     printf(" 1 - Enable auto connect to any previous access point\r\n");
00272     printf(" 2 - Disable auto connect \r\n");
00273     printf(" 3 - Connect to %s \r\n", SSID);
00274     printf(" 4 - Disconnect \r\n");
00275     printf(" 5 - Start SmartConfig \r\n");  
00276     printf(" 6 - Erase profiles \r\n");  
00277     printf(" 7 - Get Status \r\n");
00278     printf(" x - Top Menu \r\n");
00279     
00280     printf("\r\n");
00281     printf("Enter command character : ");
00282         
00283     switch(WaitForSerialCommand()) 
00284     {
00285         case '1':      
00286             wifi._wlan.ioctl_set_connection_policy(0, 1, 1);
00287         break;
00288         
00289         case '2':      
00290             wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
00291         break;
00292         
00293         case '3':     
00294         
00295             //wifi.start(0);
00296             
00297             // Enable DHCP
00298             wifi._netapp.dhcp(&ip, &ip, &ip, &ip);
00299             
00300             if ( AP_SECURITY == NONE )
00301             {
00302                 wifi.connect_non_blocking((uint8_t *)SSID, 0, AP_SECURITY);
00303             }
00304             else
00305             {
00306                 printf("\r\n Connecting to : %s key : %s", SSID, AP_KEY );
00307                 wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY);
00308                 
00309             }      
00310         break;
00311         
00312         case '4' :
00313             // Stop the module re-connecting
00314             wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
00315             // Then disconnect
00316             wifi.disconnect();
00317         break;
00318         
00319         case '5' :
00320             SmartConfig();
00321         break;
00322         
00323         case '6' :
00324             wifi._wlan.ioctl_del_profile(255);
00325         break;
00326         
00327         case '7' :
00328             print_cc3000_info();
00329         break;
00330         
00331         case '8':
00332             //wifi._spi.manualIrqCheck();
00333         break;
00334         
00335         case 'x':      
00336             currentMenu = MENU_TOP;
00337         break;
00338     }
00339 }
00340 
00341 // ------------------------------------------------------------------------------------------------------------
00342 /*!
00343     @brief 
00344 */
00345 // ------------------------------------------------------------------------------------------------------------
00346 void Menu_UdpControl ( void )
00347 {
00348     Menu_PrintHeader();
00349 
00350     printf(" 1 - UDP Client, Connect to %s:%d\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_UDP);
00351     printf(" 2 - UDP Server \r\n");
00352     printf(" 3 - NTP Client \r\n");
00353     printf(" x - Exit to top menu \r\n");
00354     
00355     printf("\r\n");
00356     printf("Enter command character : ");
00357     
00358     switch(WaitForSerialCommand())  
00359     {
00360         case '1':      
00361             UdpClientTest();
00362         break;
00363         case '2':      
00364             UdpServerTest();
00365         break;
00366         case '3':      
00367             NtpTest();
00368         break;
00369         case 'x':      
00370             currentMenu = MENU_TOP;
00371         break;
00372     }
00373 }
00374 
00375 // ------------------------------------------------------------------------------------------------------------
00376 /*!
00377     @brief 
00378 */
00379 // ------------------------------------------------------------------------------------------------------------
00380 void Menu_TcpControl ( void )
00381 {
00382     Menu_PrintHeader();
00383 
00384     printf(" 1 - TCP Client, Connect to %s:%d\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP);
00385     printf(" 2 - TCP Server, listen on %d.%d.%d.%d:%d\r\n", ipinfo.aucIP[3], ipinfo.aucIP[2], ipinfo.aucIP[1], ipinfo.aucIP[0], ECHO_SERVER_PORT_TCP);
00386     printf(" 3 - Web Socket Write \r\n");
00387     printf(" 4 - Web Socket Read \r\n");
00388     printf(" 5 - HTTP Client \r\n");
00389     printf(" x - Exit to top menu ");
00390     printf("\r\n");
00391     printf("Enter command character : ");
00392     
00393     switch(WaitForSerialCommand()) 
00394     {
00395         case '1':      
00396             TcpClientTest();
00397         break;
00398         case '2':      
00399             TcpServerTest();
00400         break;
00401         case '3':      
00402             WebSocketTest();
00403         break;
00404         case '4':
00405         
00406         break;
00407         case '5':
00408             HttpClientTest();
00409         break;
00410         case 'x':      
00411             currentMenu = MENU_TOP;
00412         break;
00413     }
00414 }
00415 
00416 
00417 // ------------------------------------------------------------------------------------------------------------
00418 /*!
00419     @brief Display a menu to the user
00420 */
00421 // ------------------------------------------------------------------------------------------------------------
00422 void Menu_Top ( void )
00423 {
00424     Menu_PrintHeader();
00425     
00426     printf(" 1 - Connection control menu \r\n" );
00427     printf(" 2 - TCP test menu \r\n" );
00428     printf(" 3 - UDP test menu \r\n" );
00429     printf(" 4 - Connection status \r\n");
00430     printf(" 5 - Ping - %s \r\n", HostToPing);
00431     printf(" x - Top Menu \r\n");
00432     
00433     printf("\r\n");
00434     printf("Enter command character : ");
00435  
00436     switch(WaitForSerialCommand()) 
00437     {
00438         case '1':      
00439             currentMenu = MENU_CONNECTION;
00440         break;
00441         case '2':      
00442             currentMenu = MENU_TCP;
00443         break;
00444         case '3':      
00445             currentMenu = MENU_UDP;
00446         break;
00447         case '4':      
00448             print_cc3000_info();
00449         break;
00450         case '5':      
00451             PingTest();
00452         break;
00453         case 'x':      
00454             currentMenu = MENU_TOP;
00455         break;
00456     }
00457 }
00458 
00459 // ------------------------------------------------------------------------------------------------------------
00460 /*!
00461     @brief Pick which menu to display
00462 */
00463 // ------------------------------------------------------------------------------------------------------------
00464 void MenuSwitch ( void )
00465 {
00466 bool connected = false;
00467 
00468     if ( wifi.is_dhcp_configured() )
00469     {
00470         connected = true;
00471     }
00472     
00473     switch ( currentMenu )
00474     {
00475         case MENU_TOP :
00476             Menu_Top();
00477         break;
00478         
00479         case MENU_CONNECTION :
00480             Menu_ConnectionControl();
00481         break;
00482         
00483         case MENU_TCP :
00484             if (connected) 
00485             {
00486                 Menu_TcpControl();
00487             }
00488             else
00489             {
00490                 currentMenu = MENU_TOP;
00491             }
00492         break;
00493         
00494         case MENU_UDP :
00495             if (connected) 
00496             {
00497                 Menu_UdpControl();
00498             }
00499             else
00500             {
00501                 currentMenu = MENU_TOP;
00502             }
00503         break;
00504         
00505         default :
00506             printf("Unknown command\r\n");
00507         break;
00508     }
00509 }
00510 
00511 // ------------------------------------------------------------------------------------------------------------
00512 /*!
00513     @brief main loop
00514 */
00515 // ------------------------------------------------------------------------------------------------------------
00516 int main( void ) 
00517 {   
00518     // Initalise the WiFi Module
00519     init(); 
00520     
00521     pc.baud(SERIAL_BAUD_RATE);
00522     
00523     wait(1);
00524     wifi.start(0);
00525     
00526     wait_ms(750);
00527 
00528     while (1)
00529     {
00530         MenuSwitch();
00531     }
00532 }