WiFi DipCortex USB CDC
Dependencies: HTTPClient NTPClient USBDevice WebSocketClient cc3000_hostdriver_mbedsocket mbed
Fork of WiFiDip-UsbKitchenSink by
main.cpp
00001 00002 #include "mbed.h" 00003 #include "cc3000.h" 00004 #include "wifi.h" 00005 #include "UDPSocket.h" 00006 #include "tcpTests.h" 00007 #include "main.h" 00008 #include "USBSerial.h" 00009 #include "NTPClient.h" 00010 #include <time.h> 00011 00012 using namespace mbed_cc3000; 00013 00014 /* cc3000 module declaration specific for user's board. Check also init() */ 00015 #if (MY_BOARD == WIGO) 00016 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn); 00017 #elif (MY_BOARD == WIFI_DIPCORTEX) 00018 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); 00019 Serial uart(p19, p20); 00020 USBSerial pc; // USB CDC serial port 00021 #else 00022 00023 #endif 00024 00025 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG 00026 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; 00027 #else 00028 const uint8_t smartconfigkey = 0; 00029 #endif 00030 00031 const int ECHO_SERVER_PORT_UDP = 81; 00032 uint8_t *HostToPing = (uint8_t *)"google.com"; 00033 tNetappIpconfigRetArgs ipinfo; 00034 extern char tmpBuffer[512]; 00035 00036 MENU_LEVEL currentMenu = MENU_TOP; 00037 00038 bool Connected = false; 00039 bool UsingSmartConfig = false; 00040 char _deviceName[] = "CC3000"; 00041 00042 // ------------------------------------------------------------------------------------------------------------ 00043 /*! 00044 @brief Resolve a hostname and ping it 00045 */ 00046 // ------------------------------------------------------------------------------------------------------------ 00047 void PingTest ( void ) 00048 { 00049 uint32_t ip; 00050 int32_t resolveRetCode = 0; 00051 00052 pc.printf("Get an IP address for %s\r\n",HostToPing); 00053 resolveRetCode = wifi._socket.gethostbyname(HostToPing,strlen((const char *)HostToPing), &ip); 00054 pc.printf("gethostbyname Returned code : %i \r\n", resolveRetCode); 00055 00056 if (resolveRetCode > -1) 00057 { 00058 uint8_t add0 = (ip >> 24); 00059 uint8_t add1 = (ip >> 16); 00060 uint8_t add2 = (ip >> 8); 00061 uint8_t add3 = (ip >> 0); 00062 pc.printf("IP address of %s: %d.%d.%d.%d \r\n", HostToPing, add0, add1, add2, add3); 00063 00064 pc.printf("Sending ping\r\n"); 00065 uint32_t reply_count = wifi.ping(ip, 5, 500, 32); 00066 pc.printf("Received %d replies\r\n", reply_count); 00067 pc.printf("Ping complete.\r\n"); 00068 } 00069 else 00070 { 00071 pc.printf("Failed to resolve\r\n"); 00072 } 00073 } 00074 00075 // ------------------------------------------------------------------------------------------------------------ 00076 /*! 00077 @brief Test the NTP library 00078 */ 00079 // ------------------------------------------------------------------------------------------------------------ 00080 void NtpTest ( void ) 00081 { 00082 NTPClient ntp; 00083 struct tm *currentTime; 00084 if (ntp.setTime("0.pool.ntp.org",123,10000) == 0) 00085 { 00086 pc.printf("\r\nGot the time successfully\r\n"); 00087 currentTime = localtime(&ntp.NTPLastResult); 00088 pc.printf("NTP Response : %s\r\n", asctime(currentTime)); 00089 } 00090 else 00091 { 00092 pc.printf("NTP Update Failed\r\n"); 00093 } 00094 } 00095 00096 // ------------------------------------------------------------------------------------------------------------ 00097 /*! 00098 @brief Send a UDP Packet, wait for response 00099 */ 00100 // ------------------------------------------------------------------------------------------------------------ 00101 void UdpClientTest ( void ) 00102 { 00103 UDPSocket socket; 00104 int n = 0; 00105 Endpoint outEndpoint; 00106 Endpoint inEndpoint; 00107 00108 if (0 == socket.bind(ECHO_SERVER_PORT_UDP) ) 00109 { 00110 00111 // 2 second timeout 00112 socket.set_blocking(false, 2000); 00113 00114 pc.printf("\r\n!! Press any key to stop !!\r\n\r\n"); 00115 00116 while (1) 00117 { 00118 if( outEndpoint.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_UDP) < 0 ) 00119 { 00120 pc.printf("Failed to set endpoint address.\r\n"); 00121 break; 00122 } 00123 else 00124 { 00125 if ( socket.sendTo( outEndpoint, hello, sizeof(hello) ) < 0 ) 00126 { 00127 pc.printf("Failed to send the packet.\r\n"); 00128 } 00129 else 00130 { 00131 // Message sent, recv reply 00132 pc.printf("UDP Socket Sent : %s \r\n", hello); 00133 n = socket.receiveFrom( inEndpoint, tmpBuffer, sizeof(tmpBuffer) ); 00134 if ( n < 0 ) 00135 { 00136 pc.printf("Failed to recv the UDP packet.\r\n"); 00137 } 00138 else 00139 { 00140 tmpBuffer[n] = '\0'; 00141 pc.printf("UDP Socket Recv'd : %s \r\n", tmpBuffer); 00142 } 00143 } 00144 } 00145 00146 // Should we stop? 00147 if ( pc.readable() ) 00148 { 00149 pc.getc(); 00150 break; 00151 } 00152 } 00153 00154 if ( wifi.is_connected() ) 00155 { 00156 socket.close(); 00157 } 00158 } 00159 else 00160 { 00161 // Failed to bind to the socket 00162 } 00163 } 00164 00165 // ------------------------------------------------------------------------------------------------------------ 00166 /*! 00167 @brief Listen on a UDP port for messages 00168 */ 00169 // ------------------------------------------------------------------------------------------------------------ 00170 void UdpServerTest ( void ) 00171 { 00172 UDPSocket socket; 00173 Endpoint client; 00174 int n = 0; 00175 00176 if (0 == socket.bind(ECHO_SERVER_PORT_UDP) ) 00177 { 00178 pc.printf("\r\n!! Press any key to stop listening !!\r\n\r\n"); 00179 00180 while (true) 00181 { 00182 pc.printf("Waiting for packet...\r\n"); 00183 n = socket.receiveFrom(client, tmpBuffer, sizeof(tmpBuffer)); 00184 00185 pc.printf("Received packet from: %s\n", client.get_address()); 00186 socket.sendTo(client, tmpBuffer, n); 00187 00188 // Should we stop? 00189 if ( pc.readable() ) 00190 { 00191 pc.getc(); 00192 break; 00193 } 00194 } 00195 } 00196 } 00197 00198 // ------------------------------------------------------------------------------------------------------------ 00199 /*! 00200 @brief Print menu header 00201 */ 00202 // ------------------------------------------------------------------------------------------------------------ 00203 void Menu_PrintHeader ( void ) 00204 { 00205 if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) 00206 { 00207 wifi.get_ip_config(&ipinfo); 00208 } 00209 00210 pc.printf("\r\n"); 00211 pc.printf("+-------------------------------------------+\r\n"); 00212 pc.printf("| WiFi DipCortex / CC3000 Kitchen Sink |\r\n"); 00213 pc.printf("+-------------------------------------------+\r\n"); 00214 if (! wifi.is_enabled() ) 00215 { 00216 pc.printf("| CC3000 Disabled |\r\n"); 00217 } 00218 else if ( wifi.is_dhcp_configured() ) 00219 { 00220 pc.printf("| SSID : %-33s|\r\n", ipinfo.uaSSID); 00221 pc.printf("| IP : %-35s|\r\n", wifi.getIPAddress()); 00222 } 00223 else if ( wifi.is_connected() ) 00224 { 00225 pc.printf("| Connecting, waiting for DHCP |\r\n"); 00226 } 00227 else 00228 { 00229 pc.printf("| Not Connected |\r\n"); 00230 } 00231 pc.printf("+-------------------------------------------+\r\n"); 00232 pc.printf("\r\n"); 00233 } 00234 00235 // ------------------------------------------------------------------------------------------------------------ 00236 /*! 00237 @brief Control the wifi connection 00238 */ 00239 // ------------------------------------------------------------------------------------------------------------ 00240 char WaitForSerialCommand ( void ) 00241 { 00242 char charIn = 0; 00243 char prevCharIn; 00244 00245 pc.printf("Enter command character : "); 00246 00247 while (1) 00248 { 00249 prevCharIn = charIn; 00250 charIn = pc.getc(); 00251 00252 pc.printf("%c", charIn); 00253 if ((charIn == '\n') || (charIn == '\r')) 00254 { 00255 break; 00256 } 00257 } 00258 00259 return ( prevCharIn ); 00260 } 00261 00262 // ------------------------------------------------------------------------------------------------------------ 00263 /*! 00264 @brief Configure the module for smart connect mode allow configuration over the air 00265 */ 00266 // ------------------------------------------------------------------------------------------------------------ 00267 void SmartConfig ( void ) 00268 { 00269 pc.printf("\r\nStarting Smart config, waiting for message from smartphone app ....\r\n"); 00270 // We dont want to auto reconnect to an access point 00271 wifi._wlan.ioctl_set_connection_policy(0, 0, 0); 00272 00273 // start smart config will disconnect, set the prefix 00274 // wait for a message via a SmartConfig app, store it to the profile list 00275 // finally it will reenable auto connection, triggering the module to connect to the new access point 00276 wifi.start_smart_config(0); 00277 00278 UsingSmartConfig = true; 00279 } 00280 00281 // ------------------------------------------------------------------------------------------------------------ 00282 /*! 00283 @brief Print cc3000 information 00284 */ 00285 // ------------------------------------------------------------------------------------------------------------ 00286 void print_cc3000_info() { 00287 uint8_t myMAC[8]; 00288 uint8_t buffer[2]; 00289 int32_t status = 0; 00290 tNetappIpconfigRetArgs ipinfo2; 00291 tUserFS cc_user_info; 00292 const char * WIFI_STATUS[] = {"Disconnected", "Scanning", "Connecting", "Connected"}; 00293 00294 wifi.get_user_file_info((uint8_t *)&cc_user_info, sizeof(cc_user_info)); 00295 wifi.get_mac_address(myMAC); 00296 pc.printf(" MAC address : %02x:%02x:%02x:%02x:%02x:%02x\r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); 00297 00298 if (! wifi._nvmem.read_sp_version( (unsigned char*)&buffer ) ) 00299 { 00300 pc.printf(" CC3000 Firmware Version : %u.%u \r\n", buffer[0], buffer[1]); 00301 } 00302 else 00303 { 00304 pc.printf(" CC3000 Read nvmem failed!"); 00305 } 00306 00307 status = wifi._wlan.ioctl_statusget(); 00308 if (( status > -1 ) && ( status < 4 )) 00309 { 00310 pc.printf(" Wifi Status : %s\r\n", WIFI_STATUS[status]); 00311 } 00312 else 00313 { 00314 pc.printf(" Wifi Status : %d\r\n", status); 00315 } 00316 00317 if ( wifi.is_dhcp_configured() ) 00318 { 00319 wifi.get_ip_config(&ipinfo2); 00320 pc.printf(" Connected to : %s \r\n", ipinfo2.uaSSID); 00321 pc.printf(" IP : %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]); 00322 pc.printf(" Gateway : %d.%d.%d.%d \r\n", ipinfo2.aucDefaultGateway[3], ipinfo2.aucDefaultGateway[2], ipinfo2.aucDefaultGateway[1], ipinfo2.aucDefaultGateway[0]); 00323 pc.printf(" Subnet : %d.%d.%d.%d \r\n", ipinfo2.aucSubnetMask[3], ipinfo2.aucSubnetMask[2], ipinfo2.aucSubnetMask[1], ipinfo2.aucSubnetMask[0]); 00324 pc.printf(" DNS : %d.%d.%d.%d \r\n", ipinfo2.aucDNSServer[3], ipinfo2.aucDNSServer[2], ipinfo2.aucDNSServer[1], ipinfo2.aucDNSServer[0]); 00325 00326 pc.printf(" Cached IP : %s \r\n", wifi.getIPAddress()); 00327 pc.printf(" Cached Gateway : %s \r\n", wifi.getGateway()); 00328 pc.printf(" Cached Subnet : %s \r\n", wifi.getNetworkMask()); 00329 00330 } 00331 else 00332 { 00333 pc.printf(" Not connected \r\n"); 00334 } 00335 } 00336 00337 00338 // ------------------------------------------------------------------------------------------------------------ 00339 /*! 00340 @brief Control the wifi connection 00341 */ 00342 // ------------------------------------------------------------------------------------------------------------ 00343 void Menu_ConnectionControl ( void ) 00344 { 00345 uint32_t ip = 0; 00346 Menu_PrintHeader(); 00347 pc.printf(" 1 - Enable auto connect to any previous access point\r\n"); 00348 pc.printf(" 2 - Disable auto connect \r\n"); 00349 pc.printf(" 3 - Connect to %s \r\n", SSID); 00350 pc.printf(" 4 - Disconnect \r\n"); 00351 pc.printf(" 5 - Start SmartConfig \r\n"); 00352 pc.printf(" 6 - Erase profiles \r\n"); 00353 pc.printf(" 7 - Get Status \r\n"); 00354 pc.printf(" x - Top Menu \r\n"); 00355 00356 pc.printf("\r\n"); 00357 00358 switch(WaitForSerialCommand()) 00359 { 00360 case '1': 00361 UsingSmartConfig = false; 00362 wifi._wlan.ioctl_set_connection_policy(0, 0, 1); 00363 break; 00364 00365 case '2': 00366 wifi._wlan.ioctl_set_connection_policy(0, 0, 0); 00367 break; 00368 00369 case '3': 00370 00371 UsingSmartConfig = false; 00372 00373 // Enable DHCP 00374 wifi._netapp.dhcp(&ip, &ip, &ip, &ip); 00375 00376 if ( AP_SECURITY == NONE ) 00377 { 00378 wifi.connect_non_blocking((uint8_t *)SSID, 0, AP_SECURITY); 00379 } 00380 else 00381 { 00382 pc.printf("\r\n Connecting to : %s key : %s", SSID, AP_KEY ); 00383 wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY); 00384 00385 } 00386 break; 00387 00388 case '4' : 00389 // Stop the module re-connecting 00390 wifi._wlan.ioctl_set_connection_policy(0, 0, 0); 00391 // Then disconnect 00392 wifi.disconnect(); 00393 break; 00394 00395 case '5' : 00396 SmartConfig(); 00397 break; 00398 00399 case '6' : 00400 wifi._wlan.ioctl_del_profile(255); 00401 break; 00402 00403 case '7' : 00404 print_cc3000_info(); 00405 break; 00406 00407 case '8': 00408 //wifi._spi.manualIrqCheck(); 00409 break; 00410 case '0': 00411 case 'x': 00412 currentMenu = MENU_TOP; 00413 break; 00414 } 00415 } 00416 00417 // ------------------------------------------------------------------------------------------------------------ 00418 /*! 00419 @brief 00420 */ 00421 // ------------------------------------------------------------------------------------------------------------ 00422 void Menu_UdpControl ( void ) 00423 { 00424 Menu_PrintHeader(); 00425 00426 pc.printf(" 1 - UDP Client, Connect to %s:%d\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_UDP); 00427 pc.printf(" 2 - UDP Server \r\n"); 00428 pc.printf(" 3 - NTP Client \r\n"); 00429 pc.printf(" x - Exit to top menu \r\n"); 00430 00431 pc.printf("\r\n"); 00432 00433 switch(WaitForSerialCommand()) 00434 { 00435 case '1': 00436 UdpClientTest(); 00437 break; 00438 case '2': 00439 UdpServerTest(); 00440 break; 00441 case '3': 00442 NtpTest(); 00443 break; 00444 case '0': 00445 case 'x': 00446 currentMenu = MENU_TOP; 00447 break; 00448 } 00449 } 00450 00451 // ------------------------------------------------------------------------------------------------------------ 00452 /*! 00453 @brief 00454 */ 00455 // ------------------------------------------------------------------------------------------------------------ 00456 void Menu_TcpControl ( void ) 00457 { 00458 Menu_PrintHeader(); 00459 00460 pc.printf(" 1 - TCP Client, Connect to %s:%d\r\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT_TCP); 00461 pc.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); 00462 pc.printf(" 3 - Web Socket Write \r\n"); 00463 pc.printf(" 4 - Web Socket Read \r\n"); 00464 pc.printf(" 5 - Web Socket Write ADCs \r\n"); 00465 pc.printf(" 6 - HTTP Client \r\n"); 00466 pc.printf(" 7 - Xively Post ADC's \r\n"); 00467 pc.printf(" x - Exit to top menu "); 00468 pc.printf("\r\n"); 00469 00470 switch(WaitForSerialCommand()) 00471 { 00472 case '1': 00473 TcpClientTest(); 00474 break; 00475 case '2': 00476 TcpServerTest(); 00477 break; 00478 case '3': 00479 WebSocketTest(); 00480 break; 00481 case '4': 00482 WebSocketReadTest(); 00483 break; 00484 case '5': 00485 WebSocketAdcStream(); 00486 break; 00487 case '6': 00488 HttpClientTest(); 00489 break; 00490 case '7': 00491 XivelySimpleTest(); 00492 break; 00493 case '0': 00494 case 'x': 00495 currentMenu = MENU_TOP; 00496 break; 00497 } 00498 } 00499 00500 00501 // ------------------------------------------------------------------------------------------------------------ 00502 /*! 00503 @brief Display a menu to the user 00504 */ 00505 // ------------------------------------------------------------------------------------------------------------ 00506 void Menu_Top ( void ) 00507 { 00508 Menu_PrintHeader(); 00509 00510 pc.printf(" 1 - Connection control menu \r\n" ); 00511 pc.printf(" 2 - TCP test menu \r\n" ); 00512 pc.printf(" 3 - UDP test menu \r\n" ); 00513 pc.printf(" 4 - Connection status \r\n"); 00514 pc.printf(" 5 - Ping - %s \r\n", HostToPing); 00515 pc.printf(" x - Top Menu \r\n"); 00516 00517 pc.printf("\r\n"); 00518 00519 switch(WaitForSerialCommand()) 00520 { 00521 case '1': 00522 currentMenu = MENU_CONNECTION; 00523 break; 00524 case '2': 00525 currentMenu = MENU_TCP; 00526 break; 00527 case '3': 00528 currentMenu = MENU_UDP; 00529 break; 00530 case '4': 00531 print_cc3000_info(); 00532 break; 00533 case '5': 00534 PingTest(); 00535 break; 00536 case '0': 00537 case 'x': 00538 currentMenu = MENU_TOP; 00539 break; 00540 } 00541 } 00542 00543 // ------------------------------------------------------------------------------------------------------------ 00544 /*! 00545 @brief Pick which menu to display 00546 */ 00547 // ------------------------------------------------------------------------------------------------------------ 00548 void MenuSwitch ( void ) 00549 { 00550 //bool connected = false; 00551 00552 if ( wifi.is_dhcp_configured() ) 00553 { 00554 if (!Connected) 00555 { 00556 // We have just connected 00557 Connected = true; 00558 00559 if ( UsingSmartConfig ) 00560 { 00561 // Start the mdns service, this tells any smart config apps listening we have succeeded 00562 wifi._socket.mdns_advertiser(1, (uint8_t *)_deviceName, strlen(_deviceName)); 00563 00564 UsingSmartConfig = false; 00565 } 00566 } 00567 } 00568 else 00569 { 00570 Connected = false; 00571 00572 } 00573 00574 switch ( currentMenu ) 00575 { 00576 case MENU_TOP : 00577 Menu_Top(); 00578 break; 00579 00580 case MENU_CONNECTION : 00581 Menu_ConnectionControl(); 00582 break; 00583 00584 case MENU_TCP : 00585 if (Connected) 00586 { 00587 Menu_TcpControl(); 00588 } 00589 else 00590 { 00591 currentMenu = MENU_TOP; 00592 } 00593 break; 00594 00595 case MENU_UDP : 00596 if (Connected) 00597 { 00598 Menu_UdpControl(); 00599 } 00600 else 00601 { 00602 currentMenu = MENU_TOP; 00603 } 00604 break; 00605 00606 default : 00607 pc.printf("Unknown command\r\n"); 00608 break; 00609 } 00610 } 00611 00612 // ------------------------------------------------------------------------------------------------------------ 00613 /*! 00614 @brief Init 00615 */ 00616 // ------------------------------------------------------------------------------------------------------------ 00617 void init() 00618 { 00619 NVIC_SetPriority(SSP1_IRQn, 0x0); 00620 NVIC_SetPriority(PIN_INT0_IRQn, 0x1); 00621 00622 // SysTick set to lower priority than Wi-Fi SPI bus interrupt 00623 NVIC_SetPriority(SysTick_IRQn, 0x2); 00624 00625 00626 // Enable RAM1 00627 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); 00628 } 00629 00630 // ------------------------------------------------------------------------------------------------------------ 00631 /*! 00632 @brief main loop 00633 */ 00634 // ------------------------------------------------------------------------------------------------------------ 00635 int main( void ) 00636 { 00637 // Initalise the WiFi Module 00638 init(); 00639 00640 uart.baud(SERIAL_BAUD_RATE); 00641 00642 wait(1); 00643 wifi.start(0); 00644 00645 wait_ms(750); 00646 00647 while (1) 00648 { 00649 MenuSwitch(); 00650 } 00651 }
Generated on Wed Jul 13 2022 03:02:38 by 1.7.2