Abdul Hannan / Mbed OS Project

Dependencies:   DISCO_L475VG_IOT01A_wifi VL53L0X 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 // Sensors drivers present in the BSP library
00004 #include "stm32l475e_iot01_tsensor.h"
00005 #include "stm32l475e_iot01_hsensor.h"
00006 #include "VL53L0X.h"
00007 
00008 #define WIFI_WRITE_TIMEOUT 10000
00009 #define WIFI_READ_TIMEOUT  10000
00010 #define CONNECTION_TRIAL_MAX          10
00011 
00012 Serial pc(SERIAL_TX, SERIAL_RX);
00013 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};
00014 uint8_t RxData [500];
00015 char* modulename;
00016 uint16_t RxLen;
00017 uint8_t  MAC_Addr[6]; 
00018 uint8_t  IP_Addr[4]; 
00019 DigitalOut led(LED1);
00020 
00021 static DevI2C devI2c(PB_11,PB_10);
00022 
00023 
00024 static DigitalOut shutdown_pin(PC_6);
00025 static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
00026 
00027 
00028 int main()
00029 {
00030     int32_t Socket = -1;
00031     uint16_t Datalen;
00032     uint16_t Trials = CONNECTION_TRIAL_MAX;
00033 
00034     pc.baud(115200);
00035     
00036     /*Initialize  WIFI module */
00037     if(WIFI_Init() ==  WIFI_STATUS_OK) {
00038         printf("> WIFI Module Initialized.\n");  
00039         if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
00040             printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",     
00041                    MAC_Addr[0],
00042                    MAC_Addr[1],
00043                    MAC_Addr[2],
00044                    MAC_Addr[3],
00045                    MAC_Addr[4],
00046                    MAC_Addr[5]);   
00047         } else {
00048             printf("> ERROR : CANNOT get MAC address\n");
00049         }
00050     
00051         if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
00052             printf("> es-wifi module connected \n");
00053             if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
00054                 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",     
00055                        IP_Addr[0],
00056                        IP_Addr[1],
00057                        IP_Addr[2],
00058                        IP_Addr[3]); 
00059         
00060                 printf("> Trying to connect to Server: %d.%d.%d.%d:27015 ...\n",     
00061                        RemoteIP[0],
00062                        RemoteIP[1],
00063                        RemoteIP[2],
00064                        RemoteIP[3]);
00065         
00066                 while (Trials--){ 
00067                     if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 27015, 0) == WIFI_STATUS_OK){
00068                         printf("> TCP Connection opened successfully.\n"); 
00069                         Socket = 0;
00070                     }
00071                 }
00072                 if(!Trials) {
00073                     printf("> ERROR : Cannot open Connection\n");
00074                 }
00075             } else {
00076                 printf("> ERROR : es-wifi module CANNOT get IP address\n");
00077             }
00078         } else {
00079             printf("> ERROR : es-wifi module NOT connected\n");
00080         }
00081     } else {
00082         printf("> ERROR : WIFI Module cannot be initialized.\n"); 
00083     }
00084     
00085     
00086     float sensor_valuet;
00087     float sensor_valueh;
00088 
00089     printf("Start sensor init\n");
00090 
00091     BSP_TSENSOR_Init();
00092     BSP_HSENSOR_Init();
00093     
00094     range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
00095 
00096     int i = 0;
00097     
00098     while(1) {
00099         printf("\nNew loop, LED1 should blink during sensor read\n");
00100 
00101         led = 1;
00102         
00103         uint32_t distance;
00104         int status = range.get_distance(&distance);
00105         if (status == VL53L0X_ERROR_NONE) {
00106             printf("VL53L0X [mm]:            %6u\r\n", distance);
00107         } else {
00108             printf("VL53L0X [mm]:                --\r\n");
00109             distance = 0;
00110         }   
00111         
00112         int timer = 0;
00113         
00114         while (distance < 150 && distance > 0)
00115         {
00116             int status = range.get_distance(&distance);
00117             if (status == VL53L0X_ERROR_NONE) {
00118                 printf("VL53L0X [mm]:            %6u\r\n", distance);
00119             } else {
00120                 printf("VL53L0X [mm]:                --\r\n");
00121                 distance = 0;
00122             } 
00123             
00124             timer++;
00125             i++;
00126             ThisThread::sleep_for(1000);
00127         }
00128         
00129         if (timer >= 10)
00130         {
00131             uint8_t data[1024] = "";
00132             uint8_t  temp[50] = "";
00133             
00134             sprintf((char *)temp, "%d", timer);
00135             strcat((char *)data, (char *)temp);
00136             
00137             if(Socket != -1) {
00138                 if(WIFI_SendData(Socket, data, sizeof(data), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
00139                     printf("Desu\n");   
00140                 }
00141             }  
00142         }  
00143         
00144         if (i%60 == 0)
00145         {
00146             uint8_t data[1024] = "";
00147             uint8_t  temp[50] = "";
00148             sensor_valuet = BSP_TSENSOR_ReadTemp();
00149             sensor_valueh = BSP_HSENSOR_ReadHumidity();
00150             printf("\nTEMPERATURE = %.2f degC\n", sensor_valuet);
00151             printf("HUMIDITY    = %.2f %%\n", sensor_valueh);
00152         
00153         
00154             sprintf((char *)temp, "%f", sensor_valuet);
00155             strcat((char *)data, (char *)temp);
00156             strcat((char *)data, (char *)",");
00157             sprintf((char *)temp, "%f", sensor_valueh);
00158             strcat((char *)data, (char *)temp);
00159         
00160             printf("%s\n", data);
00161         
00162             if(Socket != -1) {
00163                 if(WIFI_SendData(Socket, data, sizeof(data), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
00164                     printf("Desu\n");   
00165                 }
00166             }  
00167         }
00168 
00169         led = 0;
00170         
00171         ThisThread::sleep_for(1000);
00172         i++;
00173 
00174     }
00175 }