WIFU
Dependencies: DISCO_L475VG_IOT01A_wifi
main.cpp@2:9a54ede37c7e, 2017-08-17 (annotated)
- Committer:
- adustm
- Date:
- Thu Aug 17 16:35:36 2017 +0000
- Revision:
- 2:9a54ede37c7e
- Parent:
- 0:c301ccc87f5b
- Child:
- 4:5faac3e1e099
WIFI CLIENT Example for DISCO_L475VG_IOT1A platform
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adustm | 0:c301ccc87f5b | 1 | #include "mbed.h" |
adustm | 0:c301ccc87f5b | 2 | #include "wifi.h" |
adustm | 0:c301ccc87f5b | 3 | /*------------------------------------------------------------------------------ |
adustm | 0:c301ccc87f5b | 4 | Hyperterminal settings: 115200 bauds, 8-bit data, no parity |
adustm | 0:c301ccc87f5b | 5 | |
adustm | 0:c301ccc87f5b | 6 | This example |
adustm | 0:c301ccc87f5b | 7 | - connects to a wifi network (SSID & PWD to set in mbed_app.json) |
adustm | 2:9a54ede37c7e | 8 | - Connects to a TCP server (set the address in RemoteIP) |
adustm | 2:9a54ede37c7e | 9 | - Sends "Hello" to the server when data is received |
adustm | 0:c301ccc87f5b | 10 | |
adustm | 0:c301ccc87f5b | 11 | This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13), |
adustm | 0:c301ccc87f5b | 12 | wifi_dataready pin (PE_1), wifi reset pin (PE_8) |
adustm | 0:c301ccc87f5b | 13 | ------------------------------------------------------------------------------*/ |
adustm | 0:c301ccc87f5b | 14 | |
adustm | 0:c301ccc87f5b | 15 | /* Private defines -----------------------------------------------------------*/ |
adustm | 0:c301ccc87f5b | 16 | #define WIFI_WRITE_TIMEOUT 10000 |
adustm | 0:c301ccc87f5b | 17 | #define WIFI_READ_TIMEOUT 10000 |
adustm | 2:9a54ede37c7e | 18 | #define CONNECTION_TRIAL_MAX 10 |
adustm | 0:c301ccc87f5b | 19 | |
adustm | 0:c301ccc87f5b | 20 | /* Private typedef------------------------------------------------------------*/ |
adustm | 0:c301ccc87f5b | 21 | /* Private macro -------------------------------------------------------------*/ |
adustm | 0:c301ccc87f5b | 22 | /* Private variables ---------------------------------------------------------*/ |
adustm | 0:c301ccc87f5b | 23 | Serial pc(SERIAL_TX, SERIAL_RX); |
adustm | 2:9a54ede37c7e | 24 | 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}; |
adustm | 2:9a54ede37c7e | 25 | uint8_t RxData [500]; |
adustm | 2:9a54ede37c7e | 26 | char* modulename; |
adustm | 2:9a54ede37c7e | 27 | uint8_t TxData[] = "STM32 : Hello!\n"; |
adustm | 2:9a54ede37c7e | 28 | uint16_t RxLen; |
adustm | 0:c301ccc87f5b | 29 | uint8_t MAC_Addr[6]; |
adustm | 2:9a54ede37c7e | 30 | uint8_t IP_Addr[4]; |
adustm | 0:c301ccc87f5b | 31 | |
adustm | 0:c301ccc87f5b | 32 | int main() |
adustm | 0:c301ccc87f5b | 33 | { |
adustm | 2:9a54ede37c7e | 34 | int32_t Socket = -1; |
adustm | 2:9a54ede37c7e | 35 | uint16_t Datalen; |
adustm | 2:9a54ede37c7e | 36 | uint16_t Trials = CONNECTION_TRIAL_MAX; |
adustm | 2:9a54ede37c7e | 37 | |
adustm | 0:c301ccc87f5b | 38 | pc.baud(115200); |
adustm | 2:9a54ede37c7e | 39 | |
adustm | 0:c301ccc87f5b | 40 | printf("\n"); |
adustm | 0:c301ccc87f5b | 41 | printf("************************************************************\n"); |
adustm | 0:c301ccc87f5b | 42 | printf("*** STM32 IoT Discovery kit for STM32L475 MCU ***\n"); |
adustm | 2:9a54ede37c7e | 43 | printf("*** WIFI Module in TCP Client mode demonstration ***\n\n"); |
adustm | 2:9a54ede37c7e | 44 | printf("*** TCP Client Instructions :\n"); |
adustm | 2:9a54ede37c7e | 45 | printf("*** 1- Make sure your Phone is connected to the same network that\n"); |
adustm | 2:9a54ede37c7e | 46 | printf("*** you configured using the Configuration Access Point.\n"); |
adustm | 2:9a54ede37c7e | 47 | printf("*** 2- Create a server by using the android application TCP Server\n"); |
adustm | 2:9a54ede37c7e | 48 | printf("*** with port(8002).\n"); |
adustm | 2:9a54ede37c7e | 49 | printf("*** 3- Get the Network Name or IP Address of your phone from the step 2.\n\n"); |
adustm | 0:c301ccc87f5b | 50 | printf("************************************************************\n"); |
adustm | 0:c301ccc87f5b | 51 | |
adustm | 2:9a54ede37c7e | 52 | /*Initialize WIFI module */ |
adustm | 0:c301ccc87f5b | 53 | if(WIFI_Init() == WIFI_STATUS_OK) { |
adustm | 2:9a54ede37c7e | 54 | printf("> WIFI Module Initialized.\n"); |
adustm | 2:9a54ede37c7e | 55 | if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { |
adustm | 0:c301ccc87f5b | 56 | printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", |
adustm | 0:c301ccc87f5b | 57 | MAC_Addr[0], |
adustm | 0:c301ccc87f5b | 58 | MAC_Addr[1], |
adustm | 0:c301ccc87f5b | 59 | MAC_Addr[2], |
adustm | 0:c301ccc87f5b | 60 | MAC_Addr[3], |
adustm | 0:c301ccc87f5b | 61 | MAC_Addr[4], |
adustm | 0:c301ccc87f5b | 62 | MAC_Addr[5]); |
adustm | 0:c301ccc87f5b | 63 | } else { |
adustm | 0:c301ccc87f5b | 64 | printf("> ERROR : CANNOT get MAC address\n"); |
adustm | 0:c301ccc87f5b | 65 | } |
adustm | 0:c301ccc87f5b | 66 | |
adustm | 0:c301ccc87f5b | 67 | if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { |
adustm | 0:c301ccc87f5b | 68 | printf("> es-wifi module connected \n"); |
adustm | 0:c301ccc87f5b | 69 | if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { |
adustm | 0:c301ccc87f5b | 70 | printf("> es-wifi module got IP Address : %d.%d.%d.%d\n", |
adustm | 0:c301ccc87f5b | 71 | IP_Addr[0], |
adustm | 0:c301ccc87f5b | 72 | IP_Addr[1], |
adustm | 0:c301ccc87f5b | 73 | IP_Addr[2], |
adustm | 0:c301ccc87f5b | 74 | IP_Addr[3]); |
adustm | 0:c301ccc87f5b | 75 | |
adustm | 2:9a54ede37c7e | 76 | printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\n", |
adustm | 2:9a54ede37c7e | 77 | RemoteIP[0], |
adustm | 2:9a54ede37c7e | 78 | RemoteIP[1], |
adustm | 2:9a54ede37c7e | 79 | RemoteIP[2], |
adustm | 2:9a54ede37c7e | 80 | RemoteIP[3]); |
adustm | 2:9a54ede37c7e | 81 | |
adustm | 2:9a54ede37c7e | 82 | while (Trials--){ |
adustm | 2:9a54ede37c7e | 83 | if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 8002, 0) == WIFI_STATUS_OK){ |
adustm | 2:9a54ede37c7e | 84 | printf("> TCP Connection opened successfully.\n"); |
adustm | 2:9a54ede37c7e | 85 | Socket = 0; |
adustm | 2:9a54ede37c7e | 86 | } |
adustm | 2:9a54ede37c7e | 87 | } |
adustm | 2:9a54ede37c7e | 88 | if(!Trials) { |
adustm | 2:9a54ede37c7e | 89 | printf("> ERROR : Cannot open Connection\n"); |
adustm | 2:9a54ede37c7e | 90 | } |
adustm | 2:9a54ede37c7e | 91 | } else { |
adustm | 0:c301ccc87f5b | 92 | printf("> ERROR : es-wifi module CANNOT get IP address\n"); |
adustm | 0:c301ccc87f5b | 93 | } |
adustm | 0:c301ccc87f5b | 94 | } else { |
adustm | 0:c301ccc87f5b | 95 | printf("> ERROR : es-wifi module NOT connected\n"); |
adustm | 0:c301ccc87f5b | 96 | } |
adustm | 0:c301ccc87f5b | 97 | } else { |
adustm | 0:c301ccc87f5b | 98 | printf("> ERROR : WIFI Module cannot be initialized.\n"); |
adustm | 0:c301ccc87f5b | 99 | } |
adustm | 2:9a54ede37c7e | 100 | |
adustm | 2:9a54ede37c7e | 101 | while(1){ |
adustm | 2:9a54ede37c7e | 102 | if(Socket != -1) { |
adustm | 2:9a54ede37c7e | 103 | if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK){ |
adustm | 2:9a54ede37c7e | 104 | if(Datalen > 0) { |
adustm | 2:9a54ede37c7e | 105 | if(WIFI_SendData(Socket, TxData, sizeof(TxData), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) { |
adustm | 2:9a54ede37c7e | 106 | printf("> ERROR : Failed to send Data.\n"); |
adustm | 2:9a54ede37c7e | 107 | } |
adustm | 2:9a54ede37c7e | 108 | } |
adustm | 2:9a54ede37c7e | 109 | } else { |
adustm | 2:9a54ede37c7e | 110 | printf("> ERROR : Failed to Receive Data.\n"); |
adustm | 0:c301ccc87f5b | 111 | } |
adustm | 0:c301ccc87f5b | 112 | } |
adustm | 0:c301ccc87f5b | 113 | } |
adustm | 0:c301ccc87f5b | 114 | } |