Final project Network enabled hardware development

Dependencies:   DISCO_L475VG_IOT01A_wifi BSP_B-L475E-IOT01

Revision:
1:d7452fd9f05b
Parent:
0:e7a19c004965
--- a/main.cpp	Wed Feb 19 14:23:28 2020 +0000
+++ b/main.cpp	Wed Apr 22 12:01:03 2020 +0000
@@ -1,20 +1,23 @@
 #include "mbed.h"
 #include "wifi.h"
+#include "stm32l475e_iot01_tsensor.h"//temperature sensor
+#include "stm32l475e_iot01_hsensor.h"//humidity sensor
+#include <sstream>
 /*------------------------------------------------------------------------------
 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)
 ------------------------------------------------------------------------------*/
 
 /* Private defines -----------------------------------------------------------*/
-#define WIFI_WRITE_TIMEOUT 10000
-#define WIFI_READ_TIMEOUT  10000
+#define WIFI_WRITE_TIMEOUT 100
+#define WIFI_READ_TIMEOUT  100
 #define CONNECTION_TRIAL_MAX          10
 
 /* Private typedef------------------------------------------------------------*/
@@ -24,10 +27,10 @@
 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 : Im sending a ping \n";
+uint8_t * TxData;
 uint16_t RxLen;
-uint8_t  MAC_Addr[6]; 
-uint8_t  IP_Addr[4]; 
+uint8_t  MAC_Addr[6];
+uint8_t  IP_Addr[4];
 
 int main()
 {
@@ -45,43 +48,43 @@
     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("***    with port(7331).\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:7331 ...\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, 7331, 0) == WIFI_STATUS_OK) {
+                        printf("> TCP Connection opened successfully.\n");
                         Socket = 0;
                     }
                 }
@@ -95,21 +98,54 @@
             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){
+
+    printf("Initializing sensors...\n");
+    BSP_TSENSOR_Init(); //initilize the temperature sensor
+    BSP_HSENSOR_Init(); //iitilize the humidity sensor
+    float sensor_value_temp = 0;
+    float sensor_value_humid = 0;
+
+
+    while(1) {
+        //ThisThread::sleep_for(1000);
+        printf("Resting...\n");
+        //wait(2);
+        //Get readings
+        printf("Reading Sensors...\n");
+        sensor_value_temp = BSP_TSENSOR_ReadTemp();
+        sensor_value_humid = BSP_HSENSOR_ReadHumidity();
+
+        //Format readings
+        stringstream ss;
+        ss << "{ \"temp\":" << sensor_value_temp << ",\"humid\":"<<sensor_value_humid<<"}";
+        std::uint8_t readingLength = ss.str().length() + 1;
+        char * rawData = (char *) malloc(sizeof(char) * readingLength);
+        strcpy(rawData,ss.str().c_str());
+        TxData = new uint8_t[readingLength];
+        for (int i = 0; i < readingLength; i++) {
+            TxData[i] = (uint8_t) rawData[i];
+        }
+        printf("String length: %d", readingLength);
+        printf(ss.str().c_str());
+        printf("\n");
+
+        //Send readings
         if(Socket != -1) {
-            if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK){
-                printf("Received data\n");
-                if(Datalen > 0) {
-                    if(WIFI_SendData(Socket, TxData, sizeof(TxData), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
-                        printf("> ERROR : Failed to send Data.\n");   
-                    } 
+            printf("Waiting for data...\n");
+            if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK) {
+                printf("Received Data. Sending...\n");
+                if(Datalen > 0 || true) {
+                    printf("Sending data...\n");
+                    if(WIFI_SendData(Socket, (std::uint8_t*)TxData, readingLength, &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
+                        printf("> ERROR : Failed to send Data.\n");
+                    } else {
+                        printf("OK");
+                    }
                 }
-            } else {
-                printf("> ERROR : Failed to Receive Data.\n");  
             }
         }
+
     }
 }