Final project Network enabled hardware development
Dependencies: DISCO_L475VG_IOT01A_wifi BSP_B-L475E-IOT01
main.cpp
00001 #include "mbed.h" 00002 #include "wifi.h" 00003 #include "stm32l475e_iot01_tsensor.h"//temperature sensor 00004 #include "stm32l475e_iot01_hsensor.h"//humidity sensor 00005 #include <sstream> 00006 /*------------------------------------------------------------------------------ 00007 Hyperterminal settings: 115200 bauds, 8-bit data, no parity 00008 00009 This example 00010 - connects to a wifi network (SSID & PWD to set in mbed_app.json) 00011 - Connects to a TCP server (set the address in RemoteIP) 00012 - Sends "Hello" to the server when data is received 00013 00014 This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13), 00015 wifi_dataready pin (PE_1), wifi reset pin (PE_8) 00016 ------------------------------------------------------------------------------*/ 00017 00018 /* Private defines -----------------------------------------------------------*/ 00019 #define WIFI_WRITE_TIMEOUT 100 00020 #define WIFI_READ_TIMEOUT 100 00021 #define CONNECTION_TRIAL_MAX 10 00022 00023 /* Private typedef------------------------------------------------------------*/ 00024 /* Private macro -------------------------------------------------------------*/ 00025 /* Private variables ---------------------------------------------------------*/ 00026 Serial pc(SERIAL_TX, SERIAL_RX); 00027 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}; 00028 uint8_t RxData [500]; 00029 char* modulename; 00030 uint8_t * TxData; 00031 uint16_t RxLen; 00032 uint8_t MAC_Addr[6]; 00033 uint8_t IP_Addr[4]; 00034 00035 int main() 00036 { 00037 int32_t Socket = -1; 00038 uint16_t Datalen; 00039 uint16_t Trials = CONNECTION_TRIAL_MAX; 00040 00041 pc.baud(9600); 00042 00043 printf("\n"); 00044 printf("************************************************************\n"); 00045 printf("*** STM32 IoT Discovery kit for STM32L475 MCU ***\n"); 00046 printf("*** WIFI Module in TCP Client mode demonstration ***\n\n"); 00047 printf("*** TCP Client Instructions :\n"); 00048 printf("*** 1- Make sure your Phone is connected to the same network that\n"); 00049 printf("*** you configured using the Configuration Access Point.\n"); 00050 printf("*** 2- Create a server by using the android application TCP Server\n"); 00051 printf("*** with port(7331).\n"); 00052 printf("*** 3- Get the Network Name or IP Address of your phone from the step 2.\n\n"); 00053 printf("************************************************************\n"); 00054 00055 /*Initialize WIFI module */ 00056 if(WIFI_Init() == WIFI_STATUS_OK) { 00057 printf("> WIFI Module Initialized.\n"); 00058 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { 00059 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", 00060 MAC_Addr[0], 00061 MAC_Addr[1], 00062 MAC_Addr[2], 00063 MAC_Addr[3], 00064 MAC_Addr[4], 00065 MAC_Addr[5]); 00066 } else { 00067 printf("> ERROR : CANNOT get MAC address\n"); 00068 } 00069 00070 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { 00071 printf("> es-wifi module connected \n"); 00072 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { 00073 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n", 00074 IP_Addr[0], 00075 IP_Addr[1], 00076 IP_Addr[2], 00077 IP_Addr[3]); 00078 00079 printf("> Trying to connect to Server: %d.%d.%d.%d:7331 ...\n", 00080 RemoteIP[0], 00081 RemoteIP[1], 00082 RemoteIP[2], 00083 RemoteIP[3]); 00084 00085 while (Trials--) { 00086 if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 7331, 0) == WIFI_STATUS_OK) { 00087 printf("> TCP Connection opened successfully.\n"); 00088 Socket = 0; 00089 } 00090 } 00091 if(!Trials) { 00092 printf("> ERROR : Cannot open Connection\n"); 00093 } 00094 } else { 00095 printf("> ERROR : es-wifi module CANNOT get IP address\n"); 00096 } 00097 } else { 00098 printf("> ERROR : es-wifi module NOT connected\n"); 00099 } 00100 } else { 00101 printf("> ERROR : WIFI Module cannot be initialized.\n"); 00102 } 00103 00104 printf("Initializing sensors...\n"); 00105 BSP_TSENSOR_Init(); //initilize the temperature sensor 00106 BSP_HSENSOR_Init(); //iitilize the humidity sensor 00107 float sensor_value_temp = 0; 00108 float sensor_value_humid = 0; 00109 00110 00111 while(1) { 00112 //ThisThread::sleep_for(1000); 00113 printf("Resting...\n"); 00114 //wait(2); 00115 //Get readings 00116 printf("Reading Sensors...\n"); 00117 sensor_value_temp = BSP_TSENSOR_ReadTemp(); 00118 sensor_value_humid = BSP_HSENSOR_ReadHumidity(); 00119 00120 //Format readings 00121 stringstream ss; 00122 ss << "{ \"temp\":" << sensor_value_temp << ",\"humid\":"<<sensor_value_humid<<"}"; 00123 std::uint8_t readingLength = ss.str().length() + 1; 00124 char * rawData = (char *) malloc(sizeof(char) * readingLength); 00125 strcpy(rawData,ss.str().c_str()); 00126 TxData = new uint8_t[readingLength]; 00127 for (int i = 0; i < readingLength; i++) { 00128 TxData[i] = (uint8_t) rawData[i]; 00129 } 00130 printf("String length: %d", readingLength); 00131 printf(ss.str().c_str()); 00132 printf("\n"); 00133 00134 //Send readings 00135 if(Socket != -1) { 00136 printf("Waiting for data...\n"); 00137 if(WIFI_ReceiveData(Socket, RxData, sizeof(RxData), &Datalen, WIFI_READ_TIMEOUT) == WIFI_STATUS_OK) { 00138 printf("Received Data. Sending...\n"); 00139 if(Datalen > 0 || true) { 00140 printf("Sending data...\n"); 00141 if(WIFI_SendData(Socket, (std::uint8_t*)TxData, readingLength, &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) { 00142 printf("> ERROR : Failed to send Data.\n"); 00143 } else { 00144 printf("OK"); 00145 } 00146 } 00147 } 00148 } 00149 00150 } 00151 }
Generated on Wed Nov 2 2022 09:48:21 by
1.7.2