Reads DISCO-L475VG-IOT01A sensor data and sends it over WIFI

Dependencies:   mbed DISCO_L475VG_IOT01A_wifi BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "wifi.h"
00003 #include "stm32l475e_iot01_tsensor.h"
00004 #include "stm32l475e_iot01_hsensor.h"
00005 #include "stm32l475e_iot01_psensor.h"
00006 #include "stm32l475e_iot01_magneto.h"
00007 #include "stm32l475e_iot01_gyro.h"
00008 #include "stm32l475e_iot01_accelero.h"
00009 
00010 #define WIFI_WRITE_TIMEOUT 10000
00011 #define WIFI_READ_TIMEOUT  10000
00012 #define PORT           80
00013 #define MBED_RAM_SIZE 0xCE30
00014 
00015 
00016 /* Private typedef------------------------------------------------------------*/
00017 typedef enum
00018 {
00019   WS_IDLE = 0,
00020   WS_CONNECTED,
00021   WS_DISCONNECTED,
00022   WS_ERROR,
00023 } WebServerState_t;
00024 
00025 /* Private macro -------------------------------------------------------------*/
00026 static int wifi_sample_run(void);
00027 static void WebServerProcess(void);
00028 /*Declaring variables*/
00029 float Tsensor_value = 0;
00030 float Hsensor_value = 0;
00031 float Psensor_value = 0;
00032 int16_t ACCELERO_DataXYZ[3] = {0};
00033 int16_t MAGNETO_DataXYZ[3] = {0};
00034 float pGyroDataXYZ[3] = {0};
00035 
00036 static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float Tsensor_value, float Hsensor_value, float Psensor_value, int16_t ACCELERO_DataXYZ[3], int16_t MAGNETO_DataXYZ[3], float pGyroDataXYZ[3]);
00037 /* Private variables ---------------------------------------------------------*/
00038 Serial pc(USBTX, USBRX);
00039 static   uint8_t http[1024];
00040 static   uint8_t resp[1024];
00041 uint16_t respLen;
00042 uint8_t  IP_Addr[4]; 
00043 uint8_t  MAC_Addr[6]; 
00044 int32_t Socket = -1;
00045 static   WebServerState_t  State = WS_ERROR;
00046 char     ModuleName[32];
00047 
00048 DigitalOut Led_WIFI(LED2);
00049 AnalogIn adc_temp(ADC_TEMP); /*to work on---*/
00050 
00051 DigitalOut led(LED1);
00052 
00053 int main()
00054 {
00055 
00056     BSP_TSENSOR_Init();
00057     BSP_HSENSOR_Init();
00058     BSP_PSENSOR_Init();
00059 
00060     BSP_MAGNETO_Init();
00061     BSP_GYRO_Init();
00062     BSP_ACCELERO_Init();
00063 
00064     while(1) {
00065 
00066         led = 1;
00067 
00068         Tsensor_value = BSP_TSENSOR_ReadTemp();
00069         pc.printf("\nTEMPERATURE = %.2f degC\n", Tsensor_value);
00070 
00071         Hsensor_value = BSP_HSENSOR_ReadHumidity();
00072         pc.printf("HUMIDITY    = %.2f %%\n", Hsensor_value);
00073 
00074         Psensor_value = BSP_PSENSOR_ReadPressure();
00075         pc.printf("PRESSURE is = %.2f mBar\n", Psensor_value);
00076 
00077         Led_WIFI = 0;
00078 
00079         wait(1);
00080 
00081         led = 1;
00082 
00083         BSP_MAGNETO_GetXYZ(MAGNETO_DataXYZ);
00084         pc.printf("\nMAGNETO_X = %d\n", MAGNETO_DataXYZ[0]);
00085         pc.printf("MAGNETO_Y = %d\n", MAGNETO_DataXYZ[1]);
00086         printf("MAGNETO_Z = %d\n", MAGNETO_DataXYZ[2]);
00087 
00088         BSP_GYRO_GetXYZ(pGyroDataXYZ);
00089         pc.printf("\nGYRO_X = %.2f\n", pGyroDataXYZ[0]);
00090         pc.printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]);
00091         pc.printf("GYRO_Z = %.2f\n", pGyroDataXYZ[2]);
00092 
00093         BSP_ACCELERO_AccGetXYZ(ACCELERO_DataXYZ);
00094         pc.printf("\nACCELERO_X = %d\n", ACCELERO_DataXYZ[0]);
00095         pc.printf("ACCELERO_Y = %d\n", ACCELERO_DataXYZ[1]);
00096         pc.printf("ACCELERO_Z = %d\n", ACCELERO_DataXYZ[2]);
00097 
00098         led = 0;
00099         int ret = 0;
00100         led = 0;
00101         pc.baud(115200);
00102         pc.printf("\n");
00103         pc.printf("************************************************************\n\r");
00104         pc.printf("***                WIFI Web Server                     ***\n\r");
00105         pc.printf("************************************************************\n\r");
00106         
00107         /* Working application */
00108         ret = wifi_sample_run();
00109         
00110         if (ret != 0) {
00111             return -1;
00112         }
00113     
00114         
00115         while(1) {
00116             WebServerProcess ();
00117         }
00118 
00119         wait(1);
00120 
00121     }
00122 }
00123 
00124 int wifi_sample_run(void)
00125 {
00126   
00127     /*Initialisation module WIFI */
00128     if(WIFI_Init() ==  WIFI_STATUS_OK) {
00129         pc.printf("WIFI Initialise.\n\r");
00130     
00131         if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {       
00132             pc.printf("> adresse MAC: %X:%X:%X:%X:%X:%X\n\r",     
00133                    MAC_Addr[0],
00134                    MAC_Addr[1],
00135                    MAC_Addr[2],
00136                    MAC_Addr[3],
00137                    MAC_Addr[4],
00138                    MAC_Addr[5]);   
00139         } else {
00140             pc.printf("> ERREUR : CANNOT get MAC address\n\r");
00141         }
00142     
00143         if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
00144             pc.printf("> module WIFI connecté \n\r");
00145       
00146             if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
00147                 pc.printf("> IP Address : %d.%d.%d.%d\n\r",     
00148                        IP_Addr[0],
00149                        IP_Addr[1],
00150                        IP_Addr[2],
00151                        IP_Addr[3]); 
00152         
00153                 pc.printf(">Serveur HTTP ... \n\r");
00154                 State = WS_IDLE;
00155             } else {    
00156                 pc.printf("> ERROR :module CANNOT get IP address\n\r");
00157                 return -1;
00158             }
00159         } else {
00160             pc.printf("> ERROR : module NOT connected\n\r");
00161             return -1;
00162         }
00163     } else {
00164         pc.printf("> ERROR : WIFI Module cannot be initialized.\n"); 
00165         return -1;
00166     }
00167     return 0;
00168 }
00169 
00170 
00171 static void WebServerProcess(void)
00172 {
00173   uint8_t LedState = 0;
00174   float temp;
00175   switch(State)
00176   {
00177   case WS_IDLE:
00178     Socket = 0;
00179     WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT);
00180     
00181     if(Socket != -1)
00182     {
00183       pc.printf("> HTTP Server Started \n");  
00184       State = WS_CONNECTED;
00185     }
00186     else
00187     {
00188       pc.printf("> ERROR : Connection cannot be established.\n"); 
00189       State = WS_ERROR;
00190     }    
00191     break;
00192     
00193   case WS_CONNECTED:
00194     
00195     WIFI_ReceiveData(Socket, resp, 1200, &respLen, WIFI_READ_TIMEOUT);
00196     
00197     if( respLen > 0)
00198     {
00199       if(strstr((char *)resp, "GET")) /* GET: put web page */
00200       {
00201         temp = (adc_temp.read()*100);
00202         if(SendWebPage(LedState, Tsensor_value, Hsensor_value, Psensor_value, ACCELERO_DataXYZ, MAGNETO_DataXYZ, pGyroDataXYZ) != WIFI_STATUS_OK)
00203         {
00204           pc.printf("> ERROR : Cannot send web page\n");
00205           State = WS_ERROR;
00206         }
00207       }
00208       else if(strstr((char *)resp, "POST"))/* POST: received info */
00209       {
00210           if(strstr((char *)resp, "radio"))
00211           {          
00212             if(strstr((char *)resp, "radio=0"))
00213             {
00214               LedState = 0;
00215               Led_WIFI = 0;
00216             }
00217             else if(strstr((char *)resp, "radio=1"))
00218             {
00219               LedState = 1;
00220               Led_WIFI = 1;
00221             } 
00222             
00223            temp = (adc_temp.read()*100);
00224             if(SendWebPage(LedState, Tsensor_value, Hsensor_value, Psensor_value, ACCELERO_DataXYZ, MAGNETO_DataXYZ, pGyroDataXYZ) != WIFI_STATUS_OK)
00225             {
00226               pc.printf("> ERROR : Cannot send web page\n");
00227               State = WS_ERROR;
00228           }
00229         }
00230       }
00231     }
00232     if(WIFI_StopServer(Socket) == WIFI_STATUS_OK)
00233     {
00234       WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT);
00235     }
00236     else
00237     {
00238       State = WS_ERROR;  
00239     }
00240     break;
00241   case WS_ERROR:   
00242   default:
00243     break;
00244   }
00245 }
00246 
00247 
00248 /**
00249   * @brief  Send HTML page
00250   * @param  None
00251   * @retval None
00252   */
00253 static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float Tsensor_value, float Hsensor_value, float Psensor_value, int16_t ACCELERO_DataXYZ[3], int16_t MAGNETO_DataXYZ[3], float pGyroDataXYZ[3])
00254 {
00255   uint8_t  temp[50];
00256   uint16_t SentDataLength;
00257   WIFI_Status_t ret;
00258   
00259   /* construct web page content */
00260   strcpy((char *)http, (char *)"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
00261   strcat((char *)http, (char *)"<html>\r\n<body>\r\n");
00262   strcat((char *)http, (char *)"<title>STM32 Web Server</title>\r\n");
00263   strcat((char *)http, (char *)"<h2>Server WIFI</h2>\r\n");
00264   strcat((char *)http, (char *)"<br /><hr>\r\n");
00265   
00266   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Pression: <input type=\"text\" size=6 value=\"");
00267   sprintf((char *)temp, "%f", Psensor_value);
00268   strcat((char *)http, (char *)temp);
00269   strcat((char *)http, (char *)"\"> mBar");
00270   
00271   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Temperature: <input type=\"text\" size=2 value=\"");
00272   sprintf((char *)temp, "%f", Tsensor_value);
00273   strcat((char *)http, (char *)temp);
00274   strcat((char *)http, (char *)"\"> <sup>O</sup>C");
00275   
00276   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Humidity: <input type=\"text\" size=2 value=\"");
00277   sprintf((char *)temp, "%f", Hsensor_value);
00278   strcat((char *)http, (char *)temp);
00279   strcat((char *)http, (char *)"\">%");
00280   
00281   /*ACCELERO_DataXYZ*/
00282   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>ACCELERO_X: <input type=\"text\" size=4 value=\"");
00283   sprintf((char *)temp, "%d", ACCELERO_DataXYZ[0]);
00284   strcat((char *)http, (char *)temp);
00285   strcat((char *)http, (char *)"\"> ");
00286   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>ACCELERO_Y: <input type=\"text\" size=4 value=\"");
00287   sprintf((char *)temp, "%d", ACCELERO_DataXYZ[1]);
00288   strcat((char *)http, (char *)temp);
00289   strcat((char *)http, (char *)"\"> ");
00290   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>ACCELERO_Z: <input type=\"text\" size=4 value=\"");
00291   sprintf((char *)temp, "%d", ACCELERO_DataXYZ[2]);
00292   strcat((char *)http, (char *)temp);
00293   strcat((char *)http, (char *)"\">");
00294   
00295   /*MAGNETO_DataXYZ*/
00296   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>MAGNETO_X: <input type=\"text\" size=4 value=\"");
00297   sprintf((char *)temp, "%d", MAGNETO_DataXYZ[0]);
00298   strcat((char *)http, (char *)temp);
00299   strcat((char *)http, (char *)"\">");
00300   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>MAGNETO_Y: <input type=\"text\" size=4 value=\"");
00301   sprintf((char *)temp, "%d", MAGNETO_DataXYZ[1]);
00302   strcat((char *)http, (char *)temp);
00303   strcat((char *)http, (char *)"\">");
00304   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>MAGNETO_Z: <input type=\"text\" size=4 value=\"");
00305   sprintf((char *)temp, "%d", MAGNETO_DataXYZ[2]);
00306   strcat((char *)http, (char *)temp);
00307   strcat((char *)http, (char *)"\">");
00308  
00309   /*Gyro_Data*/
00310   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Gyro_X: <input type=\"text\" size=4 value=\"");
00311   sprintf((char *)temp, "%.2f", pGyroDataXYZ[0]);
00312   strcat((char *)http, (char *)temp);
00313   strcat((char *)http, (char *)"\">");
00314   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Gyro_Y: <input type=\"text\" size=4 value=\"");
00315   sprintf((char *)temp, "%.2f", pGyroDataXYZ[1]);
00316   strcat((char *)http, (char *)temp);
00317   strcat((char *)http, (char *)"\">");
00318   strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Gyro_Z: <input type=\"text\" size=4 value=\"");
00319   sprintf((char *)temp, "%.2f", pGyroDataXYZ[2]);
00320   strcat((char *)http, (char *)temp);
00321   strcat((char *)http, (char *)"\">");
00322   
00323   
00324   
00325   ret = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, WIFI_WRITE_TIMEOUT); 
00326   
00327   if((ret == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http)))
00328   {
00329     ret = WIFI_STATUS_ERROR;
00330   }
00331     
00332   return ret;
00333 }