Martin Kojtal / Mbed 2 deprecated cc3000_simple_socket_demo

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "cc3000.h"
00018 #include "main.h"
00019 #include "TCPSocketConnection.h"
00020 #include "TCPSocketServer.h"
00021 
00022 #define STRINGIFY(x) #x
00023 #define TO_STRING(x) STRINGIFY(x)
00024 
00025 using namespace mbed_cc3000;
00026 
00027 tUserFS user_info;
00028 
00029 /* cc3000 module declaration specific for user's board. Check also init() */
00030 #if (MY_BOARD == WIGO)
00031 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5));
00032 Serial pc(USBTX, USBRX);
00033 #elif (MY_BOARD == WIFI_DIPCORTEX)
00034 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
00035 Serial pc(UART_TX, UART_RX);
00036 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
00037 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7));
00038 Serial pc(USBTX, USBRX);
00039 #else
00040 
00041 #endif
00042 
00043 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
00044   const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
00045 #else
00046   const uint8_t smartconfigkey = 0;
00047 #endif
00048 
00049 /**
00050  *  \brief Print cc3000 information
00051  *  \param none
00052  *  \return none
00053  */
00054 void print_cc3000_info() {
00055     uint8_t myMAC[8];
00056 
00057     printf("MAC address + cc3000 info \r\n");
00058     wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
00059     wifi.get_mac_address(myMAC);
00060     printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
00061 
00062     printf(" FTC        %i \r\n",user_info.FTC);
00063     printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]);
00064     printf(" SERV_PACK  %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
00065     printf(" DRV_VER    %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
00066     printf(" FW_VER     %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
00067 }
00068 
00069 /**
00070  *  \brief Connect to SSID with a timeout
00071  *  \param ssid     Name of SSID
00072  *  \param key      Password
00073  *  \param sec_mode Security mode
00074  *  \return none
00075  */
00076 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
00077     printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid);
00078     if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
00079         printf(" Connected. \r\n");
00080     } else {
00081         printf(" Connection timed-out (error). Please restart. \r\n");
00082         while(1);
00083   }
00084 }
00085 
00086 /**
00087  *  \brief Connect to SSID without security
00088  *  \param ssid Name of SSID
00089  *  \return none
00090  */
00091 void connect_to_ssid(char *ssid) {
00092     wifi.connect_open((uint8_t *)ssid);
00093 }
00094 
00095 /**
00096  *  \brief First time configuration
00097  *  \param none
00098  *  \return none
00099  */
00100 void do_FTC(void) {
00101     printf("Running First Time Configuration \r\n");
00102     wifi.start_smart_config(smartconfigkey);
00103     while (wifi.is_dhcp_configured() == false) {
00104          wait_ms(500);
00105          printf("Waiting for dhcp to be set. \r\n");
00106     }
00107     user_info.FTC = 1;
00108     wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
00109     wifi._wlan.stop();
00110     printf("FTC finished. \r\n");
00111 }
00112 
00113 /**
00114  *  \brief Start smart config
00115  *  \param none
00116  *  \return none
00117  */
00118 void start_smart_config() {
00119     wifi.start_smart_config(smartconfigkey);
00120 }
00121 
00122 /**
00123  *  \brief Simple socket demo
00124  *  \param none
00125  *  \return int
00126  */
00127 int main() {
00128     init(); /* board dependent init */
00129     pc.baud(115200);
00130 
00131     wifi.start(0);
00132     printf("cc3000 simple socket demo. \r\n");
00133     print_cc3000_info();
00134 
00135     printf("User's AP setup: SSID: %s, Password: %s, Security: %s \r\n", TO_STRING(SSID), TO_STRING(AP_KEY), TO_STRING(AP_SECURITY));
00136 
00137     printf("\n<0> Normal run. SmartConfig will \r\n    start if no valid connection exists. \r\n");
00138     printf("<1> Connect using fixed SSID without AP_KEY: %s \r\n", SSID);
00139     printf("<2> Connect using fixed SSID with AP_KEY: %s \r\n", SSID);
00140     printf("<8> Erase all stored profiles.\r\n");
00141     printf("<9> SmartConfig. \r\n");
00142 
00143     signed char c = getchar();
00144     switch (c)
00145     {
00146         case '0':
00147             if (!user_info.FTC) {
00148                 do_FTC();
00149                 wifi._wlan.stop();
00150             } else {
00151                 wifi._wlan.ioctl_set_connection_policy(0, 1, 1);
00152             }
00153             break;
00154         case '1':
00155             printf("Attempting SSID Connection. \r\n");
00156 
00157             wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
00158             connect_to_ssid(SSID);
00159             break;
00160         case '2':
00161             printf("Attempting SSID Connection. \r\n");
00162 
00163             wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
00164 #ifndef CC3000_TINY_DRIVER
00165             connect_to_ssid(SSID, AP_KEY, AP_SECURITY);
00166 #else
00167             connect_to_ssid(SSID);
00168 #endif
00169             break;
00170         case '8':
00171             printf("Erasing all wireless profiles. \r\n");
00172             wifi.delete_profiles();
00173             wifi.stop();
00174             printf("Done - press the reset button on your board... \r\n");
00175             while(1);
00176 
00177         case '9':
00178             printf("Starting Smart Config configuration. \r\n");
00179             if (!user_info.FTC) {
00180                 printf("First Time Configuration was not run. \r\nSwitching to First Time Configuration. \r\n");
00181                 do_FTC();
00182                 wifi._wlan.stop();
00183             } else {
00184                 start_smart_config();
00185             }
00186             while (wifi.is_dhcp_configured() == false)
00187             {
00188                 wait_ms(500);
00189                 printf("Waiting for dhcp to be set. \r\n");
00190             }
00191             printf("Press the reset button on your board and select 0... \r\n");
00192             while(1);
00193         default:
00194             printf("Wrong selection. \r\n");
00195             printf("Reset the board and try again. \r\n");
00196             break;
00197     }
00198 
00199     printf("DHCP request \r\n");
00200     while (wifi.is_dhcp_configured() == false) {
00201          wait_ms(500);
00202          printf("  Waiting for dhcp to be set. \r\n");
00203     }
00204 
00205     tNetappIpconfigRetArgs ipinfo2;
00206     wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
00207     printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
00208 
00209     char python_msg[] = "Hello Python\n";
00210     TCPSocketServer server;
00211     TCPSocketConnection client;
00212 
00213     server.bind(15000);
00214     server.listen();
00215 
00216     while (1) {
00217         int32_t status = server.accept(client);
00218         if (status >= 0) {
00219             client.set_blocking(false, 1500); // Timeout after (1.5)s
00220             printf("Connection from: %s \r\n", client.get_address());
00221             char buffer[256];
00222             int stat = client.receive(buffer, sizeof(buffer));
00223             if (stat >=0) {
00224                 printf("Received: %s \r\n",buffer);
00225                 printf("Sending the message to the server. \r\n");
00226                 client.send_all(python_msg, sizeof(python_msg));
00227                 client.close();
00228             }
00229         }
00230     }
00231 }