test_wifi_client

Dependencies:   DISCO_L475VG_IOT01A_wifi AX12

Files at this revision

API Documentation at this revision

Comitter:
ajuton
Date:
Fri Nov 15 08:14:08 2019 +0000
Parent:
3:1f8ef0fd4347
Commit message:
test_wifi_client

Changed in this revision

AX12.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r 1f8ef0fd4347 -r 6624d4af08d1 AX12.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AX12.lib	Fri Nov 15 08:14:08 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jplunkett/code/AX12/#5b9ce05ad2e0
diff -r 1f8ef0fd4347 -r 6624d4af08d1 main.cpp
--- a/main.cpp	Thu Aug 17 16:37:16 2017 +0000
+++ b/main.cpp	Fri Nov 15 08:14:08 2019 +0000
@@ -1,14 +1,15 @@
 #include "mbed.h"
 #include "wifi.h"
+#include "AX12.h"
 /*------------------------------------------------------------------------------
 Hyperterminal settings: 115200 bauds, 8-bit data, no parity
 
-This example 
+This example
   - connects to a wifi network (SSID & PWD to set in mbed_app.json)
   - 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), 
+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)
 ------------------------------------------------------------------------------*/
 
@@ -21,21 +22,55 @@
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
 Serial pc(SERIAL_TX, SERIAL_RX);
-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]; 
-uint8_t  IP_Addr[4]; 
+AX12 myax12 (PC_4, PC_5, 1,1000000);
+
+uint32_t wifi_init(void);
 
 int main()
 {
     int32_t Socket = -1;
-    uint16_t Datalen;
+    uint16_t Datalen,lg_nombre;
+    uint16_t angle_servo,i,exposants[3]= {1,10,100};
+    uint8_t RxData [500], TxData[100] = "Bonjour Cachan";
+    pc.baud(115200);
+    Socket = wifi_init();
+
+    while(1) {
+        if(Socket != -1) {
+            if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK) 
+            {
+                if(Datalen > 0) 
+                {
+                    printf("%s",RxData);
+                    printf("Datalen %d\n",Datalen);
+                    angle_servo=0;
+                    lg_nombre = Datalen-2;
+                    if( lg_nombre>3 ) lg_nombre = 3;
+                    for(i=0; ((i<=lg_nombre-1)); i++) {
+                        angle_servo += (RxData[i]-'0') * exposants[lg_nombre-i-1];
+                        printf("char %d %d\n",i,angle_servo);
+                    }
+                    if (angle_servo>300) angle_servo=300;
+
+                    myax12.SetGoal(angle_servo);    // go to 0 degrees
+                    sprintf((char*)TxData,"valeur recue : %d",angle_servo);
+                    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");
+            }
+        }
+    }
+}
+
+
+uint32_t wifi_init(void)
+{
     uint16_t Trials = CONNECTION_TRIAL_MAX;
-
-    pc.baud(115200);
+    int32_t Socket = -1;
+    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  MAC_Addr[6];
+    uint8_t  IP_Addr[4];
 
     printf("\n");
     printf("************************************************************\n");
@@ -46,42 +81,42 @@
     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("*** 3- Get the Network Name or IP Address of your phone from the step 2.\n\n");
     printf("************************************************************\n");
 
     /*Initialize  WIFI module */
     if(WIFI_Init() ==  WIFI_STATUS_OK) {
-        printf("> WIFI Module Initialized.\n");  
+        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",     
+            printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",
                    MAC_Addr[0],
                    MAC_Addr[1],
                    MAC_Addr[2],
                    MAC_Addr[3],
                    MAC_Addr[4],
-                   MAC_Addr[5]);   
+                   MAC_Addr[5]);
         } else {
             printf("> ERROR : CANNOT get MAC address\n");
         }
-    
+
         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",     
+                printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",
                        IP_Addr[0],
                        IP_Addr[1],
                        IP_Addr[2],
-                       IP_Addr[3]); 
-        
-                printf("> Trying to connect to Server: %d.%d.%d.%d:8002 ...\n",     
+                       IP_Addr[3]);
+
+                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"); 
+
+                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;
                     }
                 }
@@ -95,20 +130,7 @@
             printf("> ERROR : es-wifi module NOT connected\n");
         }
     } else {
-        printf("> ERROR : WIFI Module cannot be initialized.\n"); 
+        printf("> ERROR : WIFI Module cannot be initialized.\n");
     }
-  
-    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");  
-            }
-        }
-    }
+    return Socket;
 }
diff -r 1f8ef0fd4347 -r 6624d4af08d1 mbed_app.json
--- a/mbed_app.json	Thu Aug 17 16:37:16 2017 +0000
+++ b/mbed_app.json	Fri Nov 15 08:14:08 2019 +0000
@@ -2,27 +2,27 @@
     "config": {
         "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"SSID\""
+            "value": "\"HP-JUTON\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"PASSWORD\""
+            "value": "\"Um0wEmvx\""
         },
         "server-ip-1": {
             "help": "TCP server IP address 1st value",
-            "value": "192"
+            "value": "10"
         },
         "server-ip-2": {
             "help": "TCP server IP address 2nd value",
-            "value": "168"
+            "value": "42"
         },
         "server-ip-3": {
             "help": "TCP server IP address 3rd value",
-            "value": "1"
+            "value": "0"
         },
         "server-ip-4": {
             "help": "TCP server IP address 4th value",
-            "value": "101"
+            "value": "161"
         }
     }
 }
\ No newline at end of file