WIFU

Dependencies:   DISCO_L475VG_IOT01A_wifi

Revision:
2:9a54ede37c7e
Parent:
0:c301ccc87f5b
Child:
4:5faac3e1e099
diff -r a544e9eba8fb -r 9a54ede37c7e main.cpp
--- a/main.cpp	Thu Aug 17 15:38:01 2017 +0000
+++ b/main.cpp	Thu Aug 17 16:35:36 2017 +0000
@@ -5,9 +5,8 @@
 
 This example 
   - connects to a wifi network (SSID & PWD to set in mbed_app.json)
-  - displays the IP address and creates a web page
-  - then connect on its IP address on the same wifi network with another device
-  - Now able to change the led status and read the temperature
+  - Connects to a TCP server (set the address in RemoteIP)
+  - Sends "Hello" to the server when data is received
 
 This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13), 
 wifi_dataready pin (PE_1), wifi reset pin (PE_8)
@@ -16,71 +15,44 @@
 /* Private defines -----------------------------------------------------------*/
 #define WIFI_WRITE_TIMEOUT 10000
 #define WIFI_READ_TIMEOUT  10000
-#define PORT           80
+#define CONNECTION_TRIAL_MAX          10
 
 /* Private typedef------------------------------------------------------------*/
-typedef enum
-{
-  WS_IDLE = 0,
-  WS_CONNECTED,
-  WS_DISCONNECTED,
-  WS_ERROR,
-} WebServerState_t;
-
 /* Private macro -------------------------------------------------------------*/
-static int wifi_sample_run(void);
-static void WebServerProcess(void);
-static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float temperature);
 /* Private variables ---------------------------------------------------------*/
 Serial pc(SERIAL_TX, SERIAL_RX);
-static   uint8_t http[1024];
-static   uint8_t resp[1024];
-uint16_t respLen;
-uint8_t  IP_Addr[4]; 
+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};
+uint8_t RxData [500];
+char* modulename;
+uint8_t TxData[] = "STM32 : Hello!\n";
+uint16_t RxLen;
 uint8_t  MAC_Addr[6]; 
-int32_t Socket = -1;
-static   WebServerState_t  State = WS_ERROR;
-char     ModuleName[32];
-DigitalOut led(LED2);
-AnalogIn adc_temp(ADC_TEMP);
+uint8_t  IP_Addr[4]; 
 
 int main()
 {
-    int ret = 0;
-    led = 0;
+    int32_t Socket = -1;
+    uint16_t Datalen;
+    uint16_t Trials = CONNECTION_TRIAL_MAX;
+
     pc.baud(115200);
+
     printf("\n");
     printf("************************************************************\n");
     printf("***   STM32 IoT Discovery kit for STM32L475 MCU          ***\n");
-    printf("***         WIFI Web Server demonstration                ***\n\n");
-    printf("*** Copy the IP address on another device connected      ***\n");
-    printf("*** to the wifi network                                  ***\n");
-    printf("*** Read the temperature and update the LED status       ***\n");
+    printf("***      WIFI Module in TCP Client mode demonstration    ***\n\n");
+    printf("*** TCP Client Instructions :\n");
+    printf("*** 1- Make sure your Phone is connected to the same network that\n");
+    printf("***    you configured using the Configuration Access Point.\n");
+    printf("*** 2- Create a server by using the android application TCP Server\n");
+    printf("***    with port(8002).\n");
+    printf("*** 3- Get the Network Name or IP Address of your phone from the step 2.\n\n"); 
     printf("************************************************************\n");
-    
-    /* Working application */
-    ret = wifi_sample_run();
-    
-    if (ret != 0) {
-        return -1;
-    }
 
-    
-    while(1) {
-        WebServerProcess ();
-    }
-
-}
-
-
-int wifi_sample_run(void)
-{
-  
-    /*Initialize and use WIFI module */
+    /*Initialize  WIFI module */
     if(WIFI_Init() ==  WIFI_STATUS_OK) {
-        printf("ES-WIFI Initialized.\n");
-    
-        if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {       
+        printf("> WIFI Module Initialized.\n");  
+        if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
             printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",     
                    MAC_Addr[0],
                    MAC_Addr[1],
@@ -94,7 +66,6 @@
     
         if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
             printf("> es-wifi module connected \n");
-      
             if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
                 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",     
                        IP_Addr[0],
@@ -102,148 +73,42 @@
                        IP_Addr[2],
                        IP_Addr[3]); 
         
-                printf(">Start HTTP Server... \n");
-                printf(">Wait for connection...  \n");
-                State = WS_IDLE;
-            } else {    
+                printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\n",     
+                       RemoteIP[0],
+                       RemoteIP[1],
+                       RemoteIP[2],
+                       RemoteIP[3]);
+        
+                while (Trials--){ 
+                    if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 8002, 0) == WIFI_STATUS_OK){
+                        printf("> TCP Connection opened successfully.\n"); 
+                        Socket = 0;
+                    }
+                }
+                if(!Trials) {
+                    printf("> ERROR : Cannot open Connection\n");
+                }
+            } else {
                 printf("> ERROR : es-wifi module CANNOT get IP address\n");
-                return -1;
             }
         } else {
             printf("> ERROR : es-wifi module NOT connected\n");
-            return -1;
         }
     } else {
         printf("> ERROR : WIFI Module cannot be initialized.\n"); 
-        return -1;
     }
-    return 0;
-}
-
-/**
-  * @brief  Send HTML page
-  * @param  None
-  * @retval None
-  */
-static void WebServerProcess(void)
-{
-  uint8_t LedState = 0;
-  float temp;
-  switch(State)
-  {
-  case WS_IDLE:
-    Socket = 0;
-    WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT);
-    
-    if(Socket != -1)
-    {
-      printf("> HTTP Server Started \n");  
-      State = WS_CONNECTED;
-    }
-    else
-    {
-      printf("> ERROR : Connection cannot be established.\n"); 
-      State = WS_ERROR;
-    }    
-    break;
-    
-  case WS_CONNECTED:
-    
-    WIFI_ReceiveData(Socket, resp, 1200, &respLen, WIFI_READ_TIMEOUT);
-    
-    if( respLen > 0)
-    {
-      if(strstr((char *)resp, "GET")) /* GET: put web page */
-      {
-        temp = (adc_temp.read()*100);
-        if(SendWebPage(LedState, temp) != WIFI_STATUS_OK)
-        {
-          printf("> ERROR : Cannot send web page\n");
-          State = WS_ERROR;
-        }
-      }
-      else if(strstr((char *)resp, "POST"))/* POST: received info */
-      {
-          if(strstr((char *)resp, "radio"))
-          {          
-            if(strstr((char *)resp, "radio=0"))
-            {
-              LedState = 0;
-              led = 0;
+  
+    while(1){
+        if(Socket != -1) {
+            if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK){
+                if(Datalen > 0) {
+                    if(WIFI_SendData(Socket, TxData, sizeof(TxData), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
+                        printf("> ERROR : Failed to send Data.\n");   
+                    } 
+                }
+            } else {
+                printf("> ERROR : Failed to Receive Data.\n");  
             }
-            else if(strstr((char *)resp, "radio=1"))
-            {
-              LedState = 1;
-              led = 1;
-            } 
-            
-           temp = (adc_temp.read()*100);
-            if(SendWebPage(LedState, temp) != WIFI_STATUS_OK)
-            {
-              printf("> ERROR : Cannot send web page\n");
-              State = WS_ERROR;
-          }
         }
-      }
     }
-    if(WIFI_StopServer(Socket) == WIFI_STATUS_OK)
-    {
-      WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT);
-    }
-    else
-    {
-      State = WS_ERROR;  
-    }
-    break;
-  case WS_ERROR:   
-  default:
-    break;
-  }
 }
-
-
-/**
-  * @brief  Send HTML page
-  * @param  None
-  * @retval None
-  */
-static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float temperature)
-{
-  uint8_t  temp[50];
-  uint16_t SentDataLength;
-  WIFI_Status_t ret;
-  
-  /* construct web page content */
-  strcpy((char *)http, (char *)"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
-  strcat((char *)http, (char *)"<html>\r\n<body>\r\n");
-  strcat((char *)http, (char *)"<title>STM32 Web Server</title>\r\n");
-  strcat((char *)http, (char *)"<h2>InventekSys : Web Server using Es-Wifi with STM32</h2>\r\n");
-  strcat((char *)http, (char *)"<br /><hr>\r\n");
-  strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Temp: <input type=\"text\" size=2 value=\"");
-  sprintf((char *)temp, "%f", temperature);
-  strcat((char *)http, (char *)temp);
-  strcat((char *)http, (char *)"\"> <sup>O</sup>C");
-  
-  if (ledIsOn) 
-  {
-    strcat((char *)http, (char *)"<p><input type=\"radio\" name=\"radio\" value=\"0\" >LED off");
-    strcat((char *)http, (char *)"<br><input type=\"radio\" name=\"radio\" value=\"1\" checked>LED on");
-  } 
-  else 
-  {
-    strcat((char *)http, (char *)"<p><input type=\"radio\" name=\"radio\" value=\"0\" checked>LED off");
-    strcat((char *)http, (char *)"<br><input type=\"radio\" name=\"radio\" value=\"1\" >LED on");
-  }
-
-  strcat((char *)http, (char *)"</strong><p><input type=\"submit\"></form></span>");
-  strcat((char *)http, (char *)"</body>\r\n</html>\r\n");
-  
-  ret = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, WIFI_WRITE_TIMEOUT); 
-  
-  if((ret == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http)))
-  {
-    ret = WIFI_STATUS_ERROR;
-  }
-    
-  return ret;
-}