WIFI Client example for DISCO_L475VG_IOT1A platform
Dependencies: DISCO_L475VG_IOT01A_wifi
Fork of DISCO_L475VG_IOT01-wifi_http_server by
main.cpp
00001 #include "mbed.h" 00002 #include "wifi.h" 00003 /*------------------------------------------------------------------------------ 00004 Hyperterminal settings: 115200 bauds, 8-bit data, no parity 00005 00006 This example 00007 - connects to a wifi network (SSID & PWD to set in mbed_app.json) 00008 - Connects to a TCP server (set the address in RemoteIP) 00009 - Sends "Hello" to the server when data is received 00010 00011 This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13), 00012 wifi_dataready pin (PE_1), wifi reset pin (PE_8) 00013 ------------------------------------------------------------------------------*/ 00014 00015 /* Private defines -----------------------------------------------------------*/ 00016 #define WIFI_WRITE_TIMEOUT 10000 00017 #define WIFI_READ_TIMEOUT 10000 00018 #define CONNECTION_TRIAL_MAX 10 00019 00020 /* Private typedef------------------------------------------------------------*/ 00021 /* Private macro -------------------------------------------------------------*/ 00022 /* Private variables ---------------------------------------------------------*/ 00023 Serial pc(SERIAL_TX, SERIAL_RX); 00024 uint8_t RemoteIP[] = {MBED_CONF_APP_SERVER_IP_1,MBED_CONF_APP_SERVER_IP_2,MBED_CONF_APP_SERVER_IP_3, MBED_CONF_APP_SERVER_IP_4}; 00025 uint8_t RxData [500]; 00026 char* modulename; 00027 uint8_t TxData[] = "STM32 : Hello!\n"; 00028 uint16_t RxLen; 00029 uint8_t MAC_Addr[6]; 00030 uint8_t IP_Addr[4]; 00031 00032 int main() 00033 { 00034 int32_t Socket = -1; 00035 uint16_t Datalen; 00036 uint16_t Trials = CONNECTION_TRIAL_MAX; 00037 00038 pc.baud(115200); 00039 00040 printf("\n"); 00041 printf("************************************************************\n"); 00042 printf("*** STM32 IoT Discovery kit for STM32L475 MCU ***\n"); 00043 printf("*** WIFI Module in TCP Client mode demonstration ***\n\n"); 00044 printf("*** TCP Client Instructions :\n"); 00045 printf("*** 1- Make sure your Phone is connected to the same network that\n"); 00046 printf("*** you configured using the Configuration Access Point.\n"); 00047 printf("*** 2- Create a server by using the android application TCP Server\n"); 00048 printf("*** with port(8002).\n"); 00049 printf("*** 3- Get the Network Name or IP Address of your phone from the step 2.\n\n"); 00050 printf("************************************************************\n"); 00051 00052 /*Initialize WIFI module */ 00053 if(WIFI_Init() == WIFI_STATUS_OK) { 00054 printf("> WIFI Module Initialized.\n"); 00055 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { 00056 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", 00057 MAC_Addr[0], 00058 MAC_Addr[1], 00059 MAC_Addr[2], 00060 MAC_Addr[3], 00061 MAC_Addr[4], 00062 MAC_Addr[5]); 00063 } else { 00064 printf("> ERROR : CANNOT get MAC address\n"); 00065 } 00066 00067 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { 00068 printf("> es-wifi module connected \n"); 00069 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { 00070 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n", 00071 IP_Addr[0], 00072 IP_Addr[1], 00073 IP_Addr[2], 00074 IP_Addr[3]); 00075 00076 printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\n", 00077 RemoteIP[0], 00078 RemoteIP[1], 00079 RemoteIP[2], 00080 RemoteIP[3]); 00081 00082 while (Trials--){ 00083 if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 8002, 0) == WIFI_STATUS_OK){ 00084 printf("> TCP Connection opened successfully.\n"); 00085 Socket = 0; 00086 } 00087 } 00088 if(!Trials) { 00089 printf("> ERROR : Cannot open Connection\n"); 00090 } 00091 } else { 00092 printf("> ERROR : es-wifi module CANNOT get IP address\n"); 00093 } 00094 } else { 00095 printf("> ERROR : es-wifi module NOT connected\n"); 00096 } 00097 } else { 00098 printf("> ERROR : WIFI Module cannot be initialized.\n"); 00099 } 00100 00101 while(1){ 00102 if(Socket != -1) { 00103 if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK){ 00104 if(Datalen > 0) { 00105 if(WIFI_SendData(Socket, TxData, sizeof(TxData), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) { 00106 printf("> ERROR : Failed to send Data.\n"); 00107 } 00108 } 00109 } else { 00110 printf("> ERROR : Failed to Receive Data.\n"); 00111 } 00112 } 00113 } 00114 }
Generated on Thu Jul 14 2022 22:15:29 by 1.7.2