Network Enabled Hardware Development

Dependencies:   DISCO_L475VG_IOT01A_wifi VL53L0X BSP_B-L475E-IOT01

Committer:
abdulh
Date:
Sun Apr 19 00:29:14 2020 +0000
Revision:
0:98a1923c15fb
Project for Class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abdulh 0:98a1923c15fb 1 #include "mbed.h"
abdulh 0:98a1923c15fb 2 #include "wifi.h"
abdulh 0:98a1923c15fb 3 // Sensors drivers present in the BSP library
abdulh 0:98a1923c15fb 4 #include "stm32l475e_iot01_tsensor.h"
abdulh 0:98a1923c15fb 5 #include "stm32l475e_iot01_hsensor.h"
abdulh 0:98a1923c15fb 6 #include "VL53L0X.h"
abdulh 0:98a1923c15fb 7
abdulh 0:98a1923c15fb 8 #define WIFI_WRITE_TIMEOUT 10000
abdulh 0:98a1923c15fb 9 #define WIFI_READ_TIMEOUT 10000
abdulh 0:98a1923c15fb 10 #define CONNECTION_TRIAL_MAX 10
abdulh 0:98a1923c15fb 11
abdulh 0:98a1923c15fb 12 Serial pc(SERIAL_TX, SERIAL_RX);
abdulh 0:98a1923c15fb 13 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};
abdulh 0:98a1923c15fb 14 uint8_t RxData [500];
abdulh 0:98a1923c15fb 15 char* modulename;
abdulh 0:98a1923c15fb 16 uint16_t RxLen;
abdulh 0:98a1923c15fb 17 uint8_t MAC_Addr[6];
abdulh 0:98a1923c15fb 18 uint8_t IP_Addr[4];
abdulh 0:98a1923c15fb 19 DigitalOut led(LED1);
abdulh 0:98a1923c15fb 20
abdulh 0:98a1923c15fb 21 static DevI2C devI2c(PB_11,PB_10);
abdulh 0:98a1923c15fb 22
abdulh 0:98a1923c15fb 23
abdulh 0:98a1923c15fb 24 static DigitalOut shutdown_pin(PC_6);
abdulh 0:98a1923c15fb 25 static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
abdulh 0:98a1923c15fb 26
abdulh 0:98a1923c15fb 27
abdulh 0:98a1923c15fb 28 int main()
abdulh 0:98a1923c15fb 29 {
abdulh 0:98a1923c15fb 30 int32_t Socket = -1;
abdulh 0:98a1923c15fb 31 uint16_t Datalen;
abdulh 0:98a1923c15fb 32 uint16_t Trials = CONNECTION_TRIAL_MAX;
abdulh 0:98a1923c15fb 33
abdulh 0:98a1923c15fb 34 pc.baud(115200);
abdulh 0:98a1923c15fb 35
abdulh 0:98a1923c15fb 36 /*Initialize WIFI module */
abdulh 0:98a1923c15fb 37 if(WIFI_Init() == WIFI_STATUS_OK) {
abdulh 0:98a1923c15fb 38 printf("> WIFI Module Initialized.\n");
abdulh 0:98a1923c15fb 39 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
abdulh 0:98a1923c15fb 40 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",
abdulh 0:98a1923c15fb 41 MAC_Addr[0],
abdulh 0:98a1923c15fb 42 MAC_Addr[1],
abdulh 0:98a1923c15fb 43 MAC_Addr[2],
abdulh 0:98a1923c15fb 44 MAC_Addr[3],
abdulh 0:98a1923c15fb 45 MAC_Addr[4],
abdulh 0:98a1923c15fb 46 MAC_Addr[5]);
abdulh 0:98a1923c15fb 47 } else {
abdulh 0:98a1923c15fb 48 printf("> ERROR : CANNOT get MAC address\n");
abdulh 0:98a1923c15fb 49 }
abdulh 0:98a1923c15fb 50
abdulh 0:98a1923c15fb 51 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
abdulh 0:98a1923c15fb 52 printf("> es-wifi module connected \n");
abdulh 0:98a1923c15fb 53 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
abdulh 0:98a1923c15fb 54 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",
abdulh 0:98a1923c15fb 55 IP_Addr[0],
abdulh 0:98a1923c15fb 56 IP_Addr[1],
abdulh 0:98a1923c15fb 57 IP_Addr[2],
abdulh 0:98a1923c15fb 58 IP_Addr[3]);
abdulh 0:98a1923c15fb 59
abdulh 0:98a1923c15fb 60 printf("> Trying to connect to Server: %d.%d.%d.%d:27015 ...\n",
abdulh 0:98a1923c15fb 61 RemoteIP[0],
abdulh 0:98a1923c15fb 62 RemoteIP[1],
abdulh 0:98a1923c15fb 63 RemoteIP[2],
abdulh 0:98a1923c15fb 64 RemoteIP[3]);
abdulh 0:98a1923c15fb 65
abdulh 0:98a1923c15fb 66 while (Trials--){
abdulh 0:98a1923c15fb 67 if( WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", RemoteIP, 27015, 0) == WIFI_STATUS_OK){
abdulh 0:98a1923c15fb 68 printf("> TCP Connection opened successfully.\n");
abdulh 0:98a1923c15fb 69 Socket = 0;
abdulh 0:98a1923c15fb 70 }
abdulh 0:98a1923c15fb 71 }
abdulh 0:98a1923c15fb 72 if(!Trials) {
abdulh 0:98a1923c15fb 73 printf("> ERROR : Cannot open Connection\n");
abdulh 0:98a1923c15fb 74 }
abdulh 0:98a1923c15fb 75 } else {
abdulh 0:98a1923c15fb 76 printf("> ERROR : es-wifi module CANNOT get IP address\n");
abdulh 0:98a1923c15fb 77 }
abdulh 0:98a1923c15fb 78 } else {
abdulh 0:98a1923c15fb 79 printf("> ERROR : es-wifi module NOT connected\n");
abdulh 0:98a1923c15fb 80 }
abdulh 0:98a1923c15fb 81 } else {
abdulh 0:98a1923c15fb 82 printf("> ERROR : WIFI Module cannot be initialized.\n");
abdulh 0:98a1923c15fb 83 }
abdulh 0:98a1923c15fb 84
abdulh 0:98a1923c15fb 85
abdulh 0:98a1923c15fb 86 float sensor_valuet;
abdulh 0:98a1923c15fb 87 float sensor_valueh;
abdulh 0:98a1923c15fb 88
abdulh 0:98a1923c15fb 89 printf("Start sensor init\n");
abdulh 0:98a1923c15fb 90
abdulh 0:98a1923c15fb 91 BSP_TSENSOR_Init();
abdulh 0:98a1923c15fb 92 BSP_HSENSOR_Init();
abdulh 0:98a1923c15fb 93
abdulh 0:98a1923c15fb 94 range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
abdulh 0:98a1923c15fb 95
abdulh 0:98a1923c15fb 96 int i = 0;
abdulh 0:98a1923c15fb 97
abdulh 0:98a1923c15fb 98 while(1) {
abdulh 0:98a1923c15fb 99 printf("\nNew loop, LED1 should blink during sensor read\n");
abdulh 0:98a1923c15fb 100
abdulh 0:98a1923c15fb 101 led = 1;
abdulh 0:98a1923c15fb 102
abdulh 0:98a1923c15fb 103 uint32_t distance;
abdulh 0:98a1923c15fb 104 int status = range.get_distance(&distance);
abdulh 0:98a1923c15fb 105 if (status == VL53L0X_ERROR_NONE) {
abdulh 0:98a1923c15fb 106 printf("VL53L0X [mm]: %6u\r\n", distance);
abdulh 0:98a1923c15fb 107 } else {
abdulh 0:98a1923c15fb 108 printf("VL53L0X [mm]: --\r\n");
abdulh 0:98a1923c15fb 109 distance = 0;
abdulh 0:98a1923c15fb 110 }
abdulh 0:98a1923c15fb 111
abdulh 0:98a1923c15fb 112 int timer = 0;
abdulh 0:98a1923c15fb 113
abdulh 0:98a1923c15fb 114 while (distance < 150 && distance > 0)
abdulh 0:98a1923c15fb 115 {
abdulh 0:98a1923c15fb 116 int status = range.get_distance(&distance);
abdulh 0:98a1923c15fb 117 if (status == VL53L0X_ERROR_NONE) {
abdulh 0:98a1923c15fb 118 printf("VL53L0X [mm]: %6u\r\n", distance);
abdulh 0:98a1923c15fb 119 } else {
abdulh 0:98a1923c15fb 120 printf("VL53L0X [mm]: --\r\n");
abdulh 0:98a1923c15fb 121 distance = 0;
abdulh 0:98a1923c15fb 122 }
abdulh 0:98a1923c15fb 123
abdulh 0:98a1923c15fb 124 timer++;
abdulh 0:98a1923c15fb 125 i++;
abdulh 0:98a1923c15fb 126 ThisThread::sleep_for(1000);
abdulh 0:98a1923c15fb 127 }
abdulh 0:98a1923c15fb 128
abdulh 0:98a1923c15fb 129 if (timer >= 10)
abdulh 0:98a1923c15fb 130 {
abdulh 0:98a1923c15fb 131 uint8_t data[1024] = "";
abdulh 0:98a1923c15fb 132 uint8_t temp[50] = "";
abdulh 0:98a1923c15fb 133
abdulh 0:98a1923c15fb 134 sprintf((char *)temp, "%d", timer);
abdulh 0:98a1923c15fb 135 strcat((char *)data, (char *)temp);
abdulh 0:98a1923c15fb 136
abdulh 0:98a1923c15fb 137 if(Socket != -1) {
abdulh 0:98a1923c15fb 138 if(WIFI_SendData(Socket, data, sizeof(data), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
abdulh 0:98a1923c15fb 139 printf("Desu\n");
abdulh 0:98a1923c15fb 140 }
abdulh 0:98a1923c15fb 141 }
abdulh 0:98a1923c15fb 142 }
abdulh 0:98a1923c15fb 143
abdulh 0:98a1923c15fb 144 if (i%60 == 0)
abdulh 0:98a1923c15fb 145 {
abdulh 0:98a1923c15fb 146 uint8_t data[1024] = "";
abdulh 0:98a1923c15fb 147 uint8_t temp[50] = "";
abdulh 0:98a1923c15fb 148 sensor_valuet = BSP_TSENSOR_ReadTemp();
abdulh 0:98a1923c15fb 149 sensor_valueh = BSP_HSENSOR_ReadHumidity();
abdulh 0:98a1923c15fb 150 printf("\nTEMPERATURE = %.2f degC\n", sensor_valuet);
abdulh 0:98a1923c15fb 151 printf("HUMIDITY = %.2f %%\n", sensor_valueh);
abdulh 0:98a1923c15fb 152
abdulh 0:98a1923c15fb 153
abdulh 0:98a1923c15fb 154 sprintf((char *)temp, "%f", sensor_valuet);
abdulh 0:98a1923c15fb 155 strcat((char *)data, (char *)temp);
abdulh 0:98a1923c15fb 156 strcat((char *)data, (char *)",");
abdulh 0:98a1923c15fb 157 sprintf((char *)temp, "%f", sensor_valueh);
abdulh 0:98a1923c15fb 158 strcat((char *)data, (char *)temp);
abdulh 0:98a1923c15fb 159
abdulh 0:98a1923c15fb 160 printf("%s\n", data);
abdulh 0:98a1923c15fb 161
abdulh 0:98a1923c15fb 162 if(Socket != -1) {
abdulh 0:98a1923c15fb 163 if(WIFI_SendData(Socket, data, sizeof(data), &Datalen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
abdulh 0:98a1923c15fb 164 printf("Desu\n");
abdulh 0:98a1923c15fb 165 }
abdulh 0:98a1923c15fb 166 }
abdulh 0:98a1923c15fb 167 }
abdulh 0:98a1923c15fb 168
abdulh 0:98a1923c15fb 169 led = 0;
abdulh 0:98a1923c15fb 170
abdulh 0:98a1923c15fb 171 ThisThread::sleep_for(1000);
abdulh 0:98a1923c15fb 172 i++;
abdulh 0:98a1923c15fb 173
abdulh 0:98a1923c15fb 174 }
abdulh 0:98a1923c15fb 175 }