a

Dependencies:   stm32l475e_iot01_gps BSP_B-L475E-IOT01

Revision:
100:84aa1516022b
Parent:
91:dab9882e2b49
Child:
101:8d2cd7e4f3c1
--- a/main.cpp	Tue Oct 08 14:00:11 2019 +0100
+++ b/main.cpp	Thu Jan 14 23:36:14 2021 +0000
@@ -1,5 +1,5 @@
 /* WiFi Example
- * Copyright (c) 2016 ARM Limited
+ * Copyright (c) 2018 ARM Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,9 +14,31 @@
  * limitations under the License.
  */
 
+
+
+#include "ThisThread.h"
 #include "mbed.h"
+#include "TCPSocket.h"
+//#include "TCPServer.h"
+#include "stm32l475e_iot01_accelero.h"
+
+#define WIFI_IDW0XX1    2
 
-WiFiInterface *wifi;
+#if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
+#include "ISM43362Interface.h"
+ISM43362Interface wifi( false);
+
+#else // External WiFi modules
+
+#if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
+#include "SpwfSAInterface.h"
+SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
+#endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
+
+#endif
+
+#define SCALE_MULTIPLIER    0.004
+
 
 const char *sec2str(nsapi_security_t sec)
 {
@@ -44,69 +66,266 @@
     printf("Scan:\n");
 
     int count = wifi->scan(NULL,0);
-
-    if (count <= 0) {
-        printf("scan() failed with return value: %d\n", count);
-        return 0;
-    }
+    printf("%d networks available.\n", count);
 
     /* Limit number of network arbitrary to 15 */
     count = count < 15 ? count : 15;
 
     ap = new WiFiAccessPoint[count];
     count = wifi->scan(ap, count);
-
-    if (count <= 0) {
-        printf("scan() failed with return value: %d\n", count);
-        return 0;
-    }
-
-    for (int i = 0; i < count; i++) {
+    for (int i = 0; i < count; i++)
+    {
         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
     }
-    printf("%d networks available.\n", count);
 
     delete[] ap;
     return count;
 }
 
+void acc_server(NetworkInterface *net)
+{
+    /* 
+    TCPServer socket;
+    TCPSocket* client;*/
+    TCPSocket socket;
+    SocketAddress addr("192.168.43.199",65431);
+    nsapi_error_t response;
+
+    int16_t pDataXYZ[3] = {0};
+    char recv_buffer[9];
+    char acc_json[64];
+    int sample_num = 0;
+    
+    //analyze
+    float bufX[80], bufY[80], bufZ[80];
+    int saX[80]={0}, saY[80]={0}, saZ[80]={0};
+    int fuzX[3]={80,0,0}, fuzY[3]={80,0,0}, fuzZ[3]={80,0,0};
+    int stX=0, stY=0, stZ=0;
+    int ptr=0;
+    int i=0;
+    /*
+    for(i=0;i<80;i++){
+        saX[i]=0;
+        saY[i]=0;
+        saZ[i]=0;
+    }
+    */
+
+    // Open a socket on the network interface, and create a TCP connection to addr
+    response = socket.open(net);
+    if (0 != response){
+        printf("Error opening: %d\n", response);
+    }
+    response = socket.connect(addr);
+    
+    if (0 != response){
+        printf("Error connecting: %d\n", response);
+    }
+
+
+    socket.set_blocking(1);
+    while (1){
+        ptr=sample_num%80;
+        ++sample_num;
+        BSP_ACCELERO_AccGetXYZ(pDataXYZ);
+        float x = pDataXYZ[0]*SCALE_MULTIPLIER, y = pDataXYZ[1]*SCALE_MULTIPLIER, z = pDataXYZ[2]*SCALE_MULTIPLIER;
+        int len;
+        
+        bufX[ptr]=x;
+        bufY[ptr]=y;
+        bufZ[ptr]=z;
+        
+        fuzX[saX[ptr]]-=1;
+        if(x>1.5){
+            saX[ptr]=2;
+            fuzX[2]+=1;
+        }else if(x>-1.5){
+            saX[ptr]=1;
+            fuzX[1]+=1;
+        }else{
+            saX[ptr]=0;
+            fuzX[0]+=1;
+        }
+        fuzY[saX[ptr]]-=1;
+        if(y>1.5){
+            saY[ptr]=2;
+            fuzY[2]+=1;
+        }else if(y>-1.5){
+            saY[ptr]=1;
+            fuzY[1]+=1;
+        }else{
+            saY[ptr]=0;
+            fuzY[0]+=1;
+        }
+        fuzZ[saX[ptr]]-=1;
+        if(z>5.5){
+            saZ[ptr]=2;
+            fuzZ[2]+=1;
+        }else if(z>2.5){
+            saZ[ptr]=1;
+            fuzZ[1]+=1;
+        }else{
+            saZ[ptr]=0;
+            fuzZ[0]+=1;
+        }
+        
+        //fuzzy
+        //X
+        if(fuzX[2]>64 || fuzX[0]>64){
+            if(stX!=3){
+                len = sprintf(acc_json,"x3\n");
+                socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stX=3;
+            }
+        }else if((fuzX[2]>32 || fuzX[0]>32) && fuzX[1]>32){
+            if(stX!=2){
+                len = sprintf(acc_json,"x2\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stX=2;
+            }
+        }else if(fuzX[2]>32 && fuzX[0]>32){
+            if(stX!=1){
+                len = sprintf(acc_json,"x1\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stX=1;
+            }
+        }else if(fuzX[1]>64){
+            stX=0;
+        }
+        //Y
+        if(fuzY[2]>64 || fuzY[0]>64){
+            if(stY!=3){
+                len = sprintf(acc_json,"y3\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stY=3;
+            }
+        }else if((fuzY[2]>32 || fuzY[0]>32) && fuzY[1]>32){
+            if(stY!=2){
+                len = sprintf(acc_json,"y2\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stY=2;
+            }
+        }else if(fuzY[2]>32 && fuzY[0]>32){
+            if(stY!=1){
+                len = sprintf(acc_json,"y1\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stY=1;
+            }
+        }else if(fuzY[1]>64){
+            stY=0;
+        }
+        //Z
+        if(fuzZ[2]>64 || fuzZ[0]>64){
+            if(stZ!=3){
+                len = sprintf(acc_json,"z3\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stZ=3;
+            }
+        }else if((fuzZ[2]>32 || fuzZ[0]>32) && fuzZ[1]>32){
+            if(stZ!=2){
+                len = sprintf(acc_json,"z2\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stZ=2;
+            }
+        }else if(fuzZ[2]>32 && fuzZ[0]>32){
+            if(stZ!=1){
+                len = sprintf(acc_json,"z1\n");
+                response = socket.send(acc_json,len);
+                for(i=1;i<81;i++){
+                    len=sprintf(acc_json,"%f,%f,%f\n",(float)((int)(bufX[(ptr+i)%80]*10000))/10000,
+                                (float)((int)(bufY[(ptr+i)%80]*10000))/10000, (float)((int)(bufZ[(ptr+i)%80]*10000))/10000);
+                    socket.send(acc_json,len);
+                }
+                stZ=1;
+            }
+        }else if(fuzZ[1]>64){
+            stZ=0;
+        }
+        
+        //int len = sprintf(acc_json,"%f,%f,%f,%d,%d\n",(float)((int)(x*10000))/10000,
+        //                                (float)((int)(y*10000))/10000, (float)((int)(z*10000))/10000, sample_num, ptr);
+
+            
+        //response = socket.send(acc_json,len);
+        if (0 >= response){
+            printf("Error seding: %d\n", response);
+        }
+        ThisThread::sleep_for(0.1);
+    
+
+    }
+
+ 
+    socket.close();
+}
+
 int main()
 {
-    printf("WiFi example\n");
-
-#ifdef MBED_MAJOR_VERSION
-    printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
-#endif
-
-    wifi = WiFiInterface::get_default_instance();
-    if (!wifi) {
-        printf("ERROR: No WiFiInterface found.\n");
-        return -1;
-    }
-
-    int count = scan_demo(wifi);
-    if (count == 0) {
-        printf("No WIFI APs found - can't continue further.\n");
-        return -1;
-    }
 
     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
-    int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
+    //wifi.set_network("192.168.130.105","255.255.255.0","192.168.130.254");
+    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
     if (ret != 0) {
-        printf("\nConnection error: %d\n", ret);
+        printf("\nConnection error\n");
         return -1;
     }
 
     printf("Success\n\n");
-    printf("MAC: %s\n", wifi->get_mac_address());
-    printf("IP: %s\n", wifi->get_ip_address());
-    printf("Netmask: %s\n", wifi->get_netmask());
-    printf("Gateway: %s\n", wifi->get_gateway());
-    printf("RSSI: %d\n\n", wifi->get_rssi());
+    printf("MAC: %s\n", wifi.get_mac_address());
+    printf("IP: %s\n", wifi.get_ip_address());
+    printf("Netmask: %s\n", wifi.get_netmask());
+    printf("Gateway: %s\n", wifi.get_gateway());
+    printf("RSSI: %d\n\n", wifi.get_rssi());
+
+
 
-    wifi->disconnect();
+    BSP_ACCELERO_Init();    
+
 
-    printf("\nDone\n");
-}
+    acc_server(&wifi);
+
+
+
+}
\ No newline at end of file