a sensor hub for monitoring home environment

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X picojson

Fork of HelloWorld_ST_Sensors by ST

Committer:
jaafaryn
Date:
Tue Dec 12 03:01:51 2017 +0000
Revision:
11:1e0b9a529ee0
Parent:
8:39ecd15538f9
Child:
12:058b012dbebe
- using standalone st sensors drivers instead of BSP drivers; - added interface to display on Nextion LCD

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaafaryn 11:1e0b9a529ee0 1 /*------------------------------------------------------------------------------
jaafaryn 11:1e0b9a529ee0 2 Home Environment Monitoring with STM32 Discovery IOT Node
jaafaryn 11:1e0b9a529ee0 3 Disco_HomeEnv
jaafaryn 11:1e0b9a529ee0 4 Features:
jaafaryn 11:1e0b9a529ee0 5 - Monitor temperature, relative humidity and air pressure
jaafaryn 11:1e0b9a529ee0 6 - Connects to a local TSDB server (over http) to log this data
jaafaryn 11:1e0b9a529ee0 7 - Display the data on a Nextion smart display with 30s autosleep and proximity
jaafaryn 11:1e0b9a529ee0 8 or touch detection wake up
jaafaryn 11:1e0b9a529ee0 9 Next:
jaafaryn 11:1e0b9a529ee0 10 - add RTC sync and time display
jaafaryn 11:1e0b9a529ee0 11 - add light sensor to monitor illuminance
jaafaryn 11:1e0b9a529ee0 12 - improve reading by either: reduce power on time of WiFi module (batch logging)
jaafaryn 11:1e0b9a529ee0 13 or by compensating for board own temperature
jaafaryn 11:1e0b9a529ee0 14 - add option to choose location from a list and update location tag
jaafaryn 11:1e0b9a529ee0 15 - incrementally convert the project to electric energy monitoring:
jaafaryn 11:1e0b9a529ee0 16 - read 2x simultaneous adc channels using DMA
jaafaryn 11:1e0b9a529ee0 17 - save daily power readings to QSP mem for batch logging and recovey?
jaafaryn 11:1e0b9a529ee0 18 - add screen to display current power drawing, daily and monthly cumulatives
jaafaryn 11:1e0b9a529ee0 19 ------------------------------------------------------------------------------*/
jaafaryn 11:1e0b9a529ee0 20
jaafaryn 11:1e0b9a529ee0 21 #include "main.h"
jaafaryn 11:1e0b9a529ee0 22
jaafaryn 11:1e0b9a529ee0 23 /* ---------- SETUP ------------- */
jaafaryn 11:1e0b9a529ee0 24 void setup()
jaafaryn 11:1e0b9a529ee0 25 {
jaafaryn 11:1e0b9a529ee0 26 printf("\n***************************************************************\n");
jaafaryn 11:1e0b9a529ee0 27 printf("*** Home Env on STM32 IoT Discovery ***\n");
jaafaryn 11:1e0b9a529ee0 28 printf("***************************************************************\n");
jaafaryn 11:1e0b9a529ee0 29
jaafaryn 11:1e0b9a529ee0 30 printf("> Initializing sensors and WiFi... \n");
jaafaryn 11:1e0b9a529ee0 31
jaafaryn 11:1e0b9a529ee0 32 int range_status;
jaafaryn 11:1e0b9a529ee0 33
jaafaryn 11:1e0b9a529ee0 34 /* Init all sensors with default params */
jaafaryn 11:1e0b9a529ee0 35 hum_temp.init(NULL);
jaafaryn 11:1e0b9a529ee0 36 press_temp.init(NULL);
jaafaryn 11:1e0b9a529ee0 37 magnetometer.init(NULL);
jaafaryn 11:1e0b9a529ee0 38 acc_gyro.init(NULL);
jaafaryn 11:1e0b9a529ee0 39 range_status = range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
nikapov 0:6a97487a0ab3 40
jaafaryn 11:1e0b9a529ee0 41 /* Enable all sensors */
jaafaryn 11:1e0b9a529ee0 42 hum_temp.enable();
jaafaryn 11:1e0b9a529ee0 43 press_temp.enable();
jaafaryn 11:1e0b9a529ee0 44 acc_gyro.enable_x();
jaafaryn 11:1e0b9a529ee0 45 acc_gyro.enable_g();
jaafaryn 11:1e0b9a529ee0 46
jaafaryn 11:1e0b9a529ee0 47 uint8_t id;
jaafaryn 11:1e0b9a529ee0 48 hum_temp.read_id(&id);
jaafaryn 11:1e0b9a529ee0 49 printf("HTS221 humidity & temperature id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 50 press_temp.read_id(&id);
jaafaryn 11:1e0b9a529ee0 51 printf("LPS22HB pressure & temperature id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 52 magnetometer.read_id(&id);
jaafaryn 11:1e0b9a529ee0 53 printf("LIS3MDL magnetometer id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 54 acc_gyro.read_id(&id);
jaafaryn 11:1e0b9a529ee0 55 printf("LSM6DSL accelerometer&gyroscope id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 56 printf("VL53L0x status = %d \r\n", range_status);
nikapov 0:6a97487a0ab3 57
jaafaryn 11:1e0b9a529ee0 58 sense_enabled = true;
nikapov 8:39ecd15538f9 59
jaafaryn 11:1e0b9a529ee0 60 /* Setup display */
jaafaryn 11:1e0b9a529ee0 61 WIFI_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 62 UPLOAD_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 63 nextion.printf("usup=0\xff\xff\xff");// no wakeup on serial in
jaafaryn 11:1e0b9a529ee0 64 nextion.printf("thsp=30\xff\xff\xff");// sleep after 30sec no touch
jaafaryn 11:1e0b9a529ee0 65 nextion.printf("thup=1\xff\xff\xff");// wake up on touch
jaafaryn 11:1e0b9a529ee0 66
nikapov 8:39ecd15538f9 67
jaafaryn 11:1e0b9a529ee0 68 /*Initialize WIFI module */
jaafaryn 11:1e0b9a529ee0 69 WiFi_on = connectWiFi();
jaafaryn 11:1e0b9a529ee0 70 if (WiFi_on) {
jaafaryn 11:1e0b9a529ee0 71 led3 = 1;
jaafaryn 11:1e0b9a529ee0 72 WIFI_ICON_ON;
jaafaryn 11:1e0b9a529ee0 73 //WiFi_led_ticker.attach(&toggle_led3_cb, 0.25);
jaafaryn 11:1e0b9a529ee0 74 // sync_time();
jaafaryn 11:1e0b9a529ee0 75 }
nikapov 8:39ecd15538f9 76
jaafaryn 11:1e0b9a529ee0 77 // start event tickers
jaafaryn 11:1e0b9a529ee0 78 getMeasurementsTicker.attach(&time_to_sense_cb, SENSE_PERIOD_S);
jaafaryn 11:1e0b9a529ee0 79 sendMeasurementsTicker.attach(&time_to_send_cb, UPLOAD_PERIOD_S);
jaafaryn 11:1e0b9a529ee0 80 checkProximityTicker.attach(&time_to_check_distance_cb, CHECK_PROXIMITY_PERIOD_S);
nikapov 8:39ecd15538f9 81
jaafaryn 11:1e0b9a529ee0 82 } // Setup
nikapov 0:6a97487a0ab3 83
nikapov 0:6a97487a0ab3 84
jaafaryn 11:1e0b9a529ee0 85 /* ---------- LOOP ------------- */
jaafaryn 11:1e0b9a529ee0 86 void loop() {
jaafaryn 11:1e0b9a529ee0 87 if (take_measurements) {
jaafaryn 11:1e0b9a529ee0 88 // Environment data
jaafaryn 11:1e0b9a529ee0 89 hum_temp.get_temperature(&tempC_val);
jaafaryn 11:1e0b9a529ee0 90 tempF_val = tempC_val*(9.0/5.0) + 32;
jaafaryn 11:1e0b9a529ee0 91 hum_temp.get_humidity(&RH_val);
jaafaryn 11:1e0b9a529ee0 92
jaafaryn 11:1e0b9a529ee0 93 press_temp.get_pressure(&Patm_val);
jaafaryn 11:1e0b9a529ee0 94 press_temp.get_temperature(&tempC_val2);
jaafaryn 11:1e0b9a529ee0 95
jaafaryn 11:1e0b9a529ee0 96 // Inertial data
jaafaryn 11:1e0b9a529ee0 97 /*
jaafaryn 11:1e0b9a529ee0 98 magnetometer.get_m_axes(axes);
jaafaryn 11:1e0b9a529ee0 99 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
jaafaryn 11:1e0b9a529ee0 100
jaafaryn 11:1e0b9a529ee0 101 acc_gyro.get_x_axes(axes);
jaafaryn 11:1e0b9a529ee0 102 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
jaafaryn 11:1e0b9a529ee0 103
jaafaryn 11:1e0b9a529ee0 104 acc_gyro.get_g_axes(axes);
jaafaryn 11:1e0b9a529ee0 105 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
jaafaryn 11:1e0b9a529ee0 106 */
jaafaryn 11:1e0b9a529ee0 107
jaafaryn 11:1e0b9a529ee0 108 // print results to terminal
jaafaryn 11:1e0b9a529ee0 109 /*
jaafaryn 11:1e0b9a529ee0 110 printf("HTS221: temp= %.2f C, hum= %.2f%%\r\n", tempC_val, RH_val);
jaafaryn 11:1e0b9a529ee0 111 printf(" temp= %.2f F\n", tempF_val);
jaafaryn 11:1e0b9a529ee0 112 printf("LPS22HB: patm= %.2f mbar, temp= %.2f C\r\n", Patm_val, tempC_val2);
jaafaryn 11:1e0b9a529ee0 113 printf("VL53L0X [mm] = %6ld\r\n", distance);
jaafaryn 11:1e0b9a529ee0 114 */
nikapov 0:6a97487a0ab3 115
jaafaryn 11:1e0b9a529ee0 116 // refresh screen with updated measurements
jaafaryn 11:1e0b9a529ee0 117 nextion.printf("valC.txt=\"%.1f\"\xff\xff\xff", tempC_val);
jaafaryn 11:1e0b9a529ee0 118 nextion.printf("valF.txt=\"%.1f\"\xff\xff\xff", tempF_val);
jaafaryn 11:1e0b9a529ee0 119 nextion.printf("valRH.txt=\"%.1f\"\xff\xff\xff", RH_val);
jaafaryn 11:1e0b9a529ee0 120 nextion.printf("valAtm.val=%.0f\xff\xff\xff", Patm_val);
jaafaryn 11:1e0b9a529ee0 121 nextion.printf("valRange.val=%d\xff\xff\xff", distance);
jaafaryn 11:1e0b9a529ee0 122
jaafaryn 11:1e0b9a529ee0 123 take_measurements = false;
jaafaryn 11:1e0b9a529ee0 124 }// take measurements
jaafaryn 11:1e0b9a529ee0 125
jaafaryn 11:1e0b9a529ee0 126 if (send_measurements) {
jaafaryn 11:1e0b9a529ee0 127
jaafaryn 11:1e0b9a529ee0 128 //printf("HTS221: temp= %.2f C, hum= %.2f%%\r\n", tempC_val, RH_val);
nikapov 0:6a97487a0ab3 129
jaafaryn 11:1e0b9a529ee0 130 // body of the request
jaafaryn 11:1e0b9a529ee0 131 char request_body[256];
jaafaryn 11:1e0b9a529ee0 132 sprintf(request_body, "disco_iot,loc=%s temperature=%.2f,humidity=%.1f,pressure=%.1f \n","playroom", tempC_val, RH_val, Patm_val);
jaafaryn 11:1e0b9a529ee0 133
jaafaryn 11:1e0b9a529ee0 134 // build header of the request
jaafaryn 11:1e0b9a529ee0 135 sprintf((char *)http_request, "POST %s HTTP/1.1\r\nHost: %s \r\n", influx_query, InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 136 strcat((char *)http_request, "Accept: */*\r\n");
jaafaryn 11:1e0b9a529ee0 137 strcat((char *)http_request, "User-agent: ES-WIFI TcpClient\r\n");
jaafaryn 11:1e0b9a529ee0 138 strcat((char *)http_request, "Connection: Close\r\n"); //"Connection: Keep-Alive\r\n"
jaafaryn 11:1e0b9a529ee0 139 char buffer[64];
jaafaryn 11:1e0b9a529ee0 140 sprintf(buffer, "Content-Length: %d \r\n\r\n", strlen(request_body));
jaafaryn 11:1e0b9a529ee0 141 strcat((char *)http_request, buffer);
jaafaryn 11:1e0b9a529ee0 142
jaafaryn 11:1e0b9a529ee0 143 // append body to the header of the request
jaafaryn 11:1e0b9a529ee0 144 strcat((char *)http_request, request_body);
jaafaryn 11:1e0b9a529ee0 145 reqLen = strlen((char *)http_request);
nikapov 0:6a97487a0ab3 146
jaafaryn 11:1e0b9a529ee0 147 // Establish a connection to DB server
jaafaryn 11:1e0b9a529ee0 148 uint8_t socketid = 1;
jaafaryn 11:1e0b9a529ee0 149 if (checkWiFi()) {
jaafaryn 11:1e0b9a529ee0 150 led3 = 1;
jaafaryn 11:1e0b9a529ee0 151 WIFI_ICON_ON;
jaafaryn 11:1e0b9a529ee0 152 if (connectToServer(InfluxServerUrl, InfluxServerPort, socketid)) {
jaafaryn 11:1e0b9a529ee0 153 ledhttp =1;
jaafaryn 11:1e0b9a529ee0 154 // submit POST request
jaafaryn 11:1e0b9a529ee0 155 printf("> Sending a POST request with length=%d including a body length=%d\n", reqLen, strlen(request_body));
jaafaryn 11:1e0b9a529ee0 156 printf((char *)http_request);
jaafaryn 11:1e0b9a529ee0 157 uint16_t dataLen;
jaafaryn 11:1e0b9a529ee0 158 if (WIFI_SendData(socketid, http_request, reqLen, &dataLen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 159 printf("> ERROR: Could not send request to %s", InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 160 UPLOAD_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 161 } else {
jaafaryn 11:1e0b9a529ee0 162 request_sent++;
jaafaryn 11:1e0b9a529ee0 163 UPLOAD_ICON_ON;
jaafaryn 11:1e0b9a529ee0 164 }
jaafaryn 11:1e0b9a529ee0 165 // get server response
jaafaryn 11:1e0b9a529ee0 166 // memset(&http_resp[0], 0, sizeof(http_resp));
jaafaryn 11:1e0b9a529ee0 167 WIFI_ReceiveData(socketid, http_resp, sizeof(http_resp), &dataLen, WIFI_READ_TIMEOUT);
jaafaryn 11:1e0b9a529ee0 168 if(dataLen > 0) {
jaafaryn 11:1e0b9a529ee0 169 /*
jaafaryn 11:1e0b9a529ee0 170 printf("> Server response:\n");
jaafaryn 11:1e0b9a529ee0 171 printf((char *)http_resp);
jaafaryn 11:1e0b9a529ee0 172 printf("\n> Response length: %d \n", dataLen);
jaafaryn 11:1e0b9a529ee0 173 */
jaafaryn 11:1e0b9a529ee0 174 memset(&buffer[0], 0, sizeof(buffer));
jaafaryn 11:1e0b9a529ee0 175 // get the first line of the response
jaafaryn 11:1e0b9a529ee0 176 strcpy(buffer, strtok((char *)http_resp, "\r\n"));
jaafaryn 11:1e0b9a529ee0 177 // extract the response code
jaafaryn 11:1e0b9a529ee0 178 int response_code = 0;
jaafaryn 11:1e0b9a529ee0 179 sscanf(buffer, "HTTP/1.1 %d", &response_code);
jaafaryn 11:1e0b9a529ee0 180 printf("> Response code: %d \n", response_code);
jaafaryn 11:1e0b9a529ee0 181 /* c ommon response codes from InfluxDB API:
jaafaryn 11:1e0b9a529ee0 182 HTTP/1.1 204 No Content
jaafaryn 11:1e0b9a529ee0 183 HTTP/1.1 400 Bad Request
jaafaryn 11:1e0b9a529ee0 184 HTTP/1.1 404 Not Found
jaafaryn 11:1e0b9a529ee0 185 HTTP/1.1 500 Internal Server Error
jaafaryn 11:1e0b9a529ee0 186 */
jaafaryn 11:1e0b9a529ee0 187 if (response_code == 204) request_acked++;
jaafaryn 11:1e0b9a529ee0 188 } else {
jaafaryn 11:1e0b9a529ee0 189 UPLOAD_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 190 printf("> Error: No response from the server %s.\n", InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 191 }
jaafaryn 11:1e0b9a529ee0 192 printf("> Requests sent: %d, ack'ed: %d\n", request_sent, request_acked);
jaafaryn 11:1e0b9a529ee0 193 ledhttp = 0;
jaafaryn 11:1e0b9a529ee0 194 WIFI_CloseClientConnection(socketid);
jaafaryn 11:1e0b9a529ee0 195 } else {
jaafaryn 11:1e0b9a529ee0 196 printf("> ERROR: Could not connect to %s \n", InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 197 }
jaafaryn 11:1e0b9a529ee0 198 } else {
jaafaryn 11:1e0b9a529ee0 199 printf("> ERROR: Could not connect to WiFi \n");
jaafaryn 11:1e0b9a529ee0 200 led3 = 0;
jaafaryn 11:1e0b9a529ee0 201 WIFI_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 202 }
jaafaryn 11:1e0b9a529ee0 203 send_measurements = false;
jaafaryn 11:1e0b9a529ee0 204 } // sendMeasurement()
nikapov 8:39ecd15538f9 205
jaafaryn 11:1e0b9a529ee0 206 if (check_proximity) {
jaafaryn 11:1e0b9a529ee0 207 // make sure display is awake when somebody get close to the screen
jaafaryn 11:1e0b9a529ee0 208 // Proximity value
jaafaryn 11:1e0b9a529ee0 209 uint32_t dist_val = 0;
jaafaryn 11:1e0b9a529ee0 210 int status = range.get_distance(&dist_val);
jaafaryn 11:1e0b9a529ee0 211 if (status == VL53L0X_ERROR_NONE) {
jaafaryn 11:1e0b9a529ee0 212 distance = (int)dist_val;
jaafaryn 11:1e0b9a529ee0 213 } else {
jaafaryn 11:1e0b9a529ee0 214 distance = -1;
jaafaryn 11:1e0b9a529ee0 215 }
jaafaryn 11:1e0b9a529ee0 216 if (distance < 400) {
jaafaryn 11:1e0b9a529ee0 217 nextion.printf("sleep=0\xff\xff\xff");// send wake up command
jaafaryn 11:1e0b9a529ee0 218 }
jaafaryn 11:1e0b9a529ee0 219 check_proximity = false;
jaafaryn 11:1e0b9a529ee0 220 } // check proximity
jaafaryn 11:1e0b9a529ee0 221 } // Loop
nikapov 8:39ecd15538f9 222
nikapov 8:39ecd15538f9 223
jaafaryn 11:1e0b9a529ee0 224 int main() {
jaafaryn 11:1e0b9a529ee0 225 setup();
jaafaryn 11:1e0b9a529ee0 226 while(1) {
jaafaryn 11:1e0b9a529ee0 227 loop();
jaafaryn 11:1e0b9a529ee0 228 }
jaafaryn 11:1e0b9a529ee0 229 }
jaafaryn 11:1e0b9a529ee0 230
jaafaryn 11:1e0b9a529ee0 231
jaafaryn 11:1e0b9a529ee0 232 /* */
jaafaryn 11:1e0b9a529ee0 233 /* Interrupts servicing callback functions */
jaafaryn 11:1e0b9a529ee0 234 /* */
jaafaryn 11:1e0b9a529ee0 235
jaafaryn 11:1e0b9a529ee0 236 // ISR for flashing WiFi LED
jaafaryn 11:1e0b9a529ee0 237 /*
jaafaryn 11:1e0b9a529ee0 238 void toggle_led3_cb()
jaafaryn 11:1e0b9a529ee0 239 {
jaafaryn 11:1e0b9a529ee0 240 led3 = !led3;
jaafaryn 11:1e0b9a529ee0 241 }*/
jaafaryn 11:1e0b9a529ee0 242
jaafaryn 11:1e0b9a529ee0 243 // ISR to enable taking measurements (and refresh display)
jaafaryn 11:1e0b9a529ee0 244 void time_to_sense_cb(void)
jaafaryn 11:1e0b9a529ee0 245 {
jaafaryn 11:1e0b9a529ee0 246 if (sense_enabled) {
jaafaryn 11:1e0b9a529ee0 247 take_measurements = true;
jaafaryn 11:1e0b9a529ee0 248 }
jaafaryn 11:1e0b9a529ee0 249 }
nikapov 0:6a97487a0ab3 250
jaafaryn 11:1e0b9a529ee0 251 // ISR to enable handling sending data
jaafaryn 11:1e0b9a529ee0 252 void time_to_send_cb(void)
jaafaryn 11:1e0b9a529ee0 253 {
jaafaryn 11:1e0b9a529ee0 254 if (sense_enabled) {
jaafaryn 11:1e0b9a529ee0 255 send_measurements = true;
jaafaryn 11:1e0b9a529ee0 256 }
jaafaryn 11:1e0b9a529ee0 257 }
jaafaryn 11:1e0b9a529ee0 258
jaafaryn 11:1e0b9a529ee0 259 // ISR to enable checking distance
jaafaryn 11:1e0b9a529ee0 260 void time_to_check_distance_cb(void)
jaafaryn 11:1e0b9a529ee0 261 {
jaafaryn 11:1e0b9a529ee0 262 check_proximity = true;
jaafaryn 11:1e0b9a529ee0 263 }
jaafaryn 11:1e0b9a529ee0 264
jaafaryn 11:1e0b9a529ee0 265 /**
jaafaryn 11:1e0b9a529ee0 266 * @brief Connect to the WiFi network with credentials in mbed_app.json
jaafaryn 11:1e0b9a529ee0 267 * @param None
jaafaryn 11:1e0b9a529ee0 268 * @retval True for WiFi connected, False if there was an error
jaafaryn 11:1e0b9a529ee0 269 */
jaafaryn 11:1e0b9a529ee0 270 bool connectWiFi(void)
jaafaryn 11:1e0b9a529ee0 271 {
jaafaryn 11:1e0b9a529ee0 272 uint8_t IP_Addr[4];
jaafaryn 11:1e0b9a529ee0 273 uint8_t MAC_Addr[6];
jaafaryn 11:1e0b9a529ee0 274
jaafaryn 11:1e0b9a529ee0 275 if(WIFI_Init() == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 276 // printf("> WiFi module initialized.\n");
jaafaryn 11:1e0b9a529ee0 277 //if(WIFI_GetModuleName(moduleName) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 278 // printf("> Module name: %s\n", moduleName);
jaafaryn 11:1e0b9a529ee0 279 //}
jaafaryn 11:1e0b9a529ee0 280 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 281 printf("> WiFi module MAC Address : %X:%X:%X:%X:%X:%X\n",
jaafaryn 11:1e0b9a529ee0 282 MAC_Addr[0],
jaafaryn 11:1e0b9a529ee0 283 MAC_Addr[1],
jaafaryn 11:1e0b9a529ee0 284 MAC_Addr[2],
jaafaryn 11:1e0b9a529ee0 285 MAC_Addr[3],
jaafaryn 11:1e0b9a529ee0 286 MAC_Addr[4],
jaafaryn 11:1e0b9a529ee0 287 MAC_Addr[5]);
jaafaryn 11:1e0b9a529ee0 288 } else {
jaafaryn 11:1e0b9a529ee0 289 printf("> ERROR : CANNOT get MAC address\n");
jaafaryn 11:1e0b9a529ee0 290 }
nikapov 0:6a97487a0ab3 291
jaafaryn 11:1e0b9a529ee0 292 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 293 // printf("> WiFi connected \n");
jaafaryn 11:1e0b9a529ee0 294 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 295 printf("> IP Address : %d.%d.%d.%d\n",
jaafaryn 11:1e0b9a529ee0 296 IP_Addr[0],
jaafaryn 11:1e0b9a529ee0 297 IP_Addr[1],
jaafaryn 11:1e0b9a529ee0 298 IP_Addr[2],
jaafaryn 11:1e0b9a529ee0 299 IP_Addr[3]);
jaafaryn 11:1e0b9a529ee0 300 return true;
jaafaryn 11:1e0b9a529ee0 301 } else {
jaafaryn 11:1e0b9a529ee0 302 printf("> ERROR : es-wifi module CANNOT get IP address\n");
jaafaryn 11:1e0b9a529ee0 303 }
jaafaryn 11:1e0b9a529ee0 304 } else {
jaafaryn 11:1e0b9a529ee0 305 printf("> ERROR : es-wifi module NOT connected\n");
jaafaryn 11:1e0b9a529ee0 306 }
jaafaryn 11:1e0b9a529ee0 307 } else {
jaafaryn 11:1e0b9a529ee0 308 printf("> ERROR : WIFI Module cannot be initialized.\n");
jaafaryn 11:1e0b9a529ee0 309 }
jaafaryn 11:1e0b9a529ee0 310 return false;
jaafaryn 11:1e0b9a529ee0 311 }
nikapov 0:6a97487a0ab3 312
nikapov 0:6a97487a0ab3 313
jaafaryn 11:1e0b9a529ee0 314 /**
jaafaryn 11:1e0b9a529ee0 315 * @brief check WiFi connection status, retry once if it's offline
jaafaryn 11:1e0b9a529ee0 316 * @param None
jaafaryn 11:1e0b9a529ee0 317 * @retval True for WiFi (re)connected, False if reconnection failed
jaafaryn 11:1e0b9a529ee0 318 */
nikapov 0:6a97487a0ab3 319
jaafaryn 11:1e0b9a529ee0 320 bool checkWiFi(void)
jaafaryn 11:1e0b9a529ee0 321 {
jaafaryn 11:1e0b9a529ee0 322 uint8_t ip_addr[4];
jaafaryn 11:1e0b9a529ee0 323 if(WIFI_GetIP_Address(ip_addr) == WIFI_STATUS_OK) return true;
jaafaryn 11:1e0b9a529ee0 324 else {
jaafaryn 11:1e0b9a529ee0 325 return connectWiFi();
jaafaryn 11:1e0b9a529ee0 326 }
jaafaryn 11:1e0b9a529ee0 327 }
nikapov 0:6a97487a0ab3 328
jaafaryn 11:1e0b9a529ee0 329 /**
jaafaryn 11:1e0b9a529ee0 330 * @brief create a TCP session on a server
jaafaryn 11:1e0b9a529ee0 331 * @param serverUrl : TCP server URL
jaafaryn 11:1e0b9a529ee0 332 * @param serverPort : port TCP server is listening on
jaafaryn 11:1e0b9a529ee0 333 * @retval true if connection established
jaafaryn 11:1e0b9a529ee0 334 */
jaafaryn 11:1e0b9a529ee0 335 bool connectToServer(char *serverUrl, uint16_t serverPort, uint8_t socket_id)
jaafaryn 11:1e0b9a529ee0 336 {
jaafaryn 11:1e0b9a529ee0 337 uint8_t serverIP[] = {0, 0, 0, 0};
jaafaryn 11:1e0b9a529ee0 338 uint16_t trials = CONNECTION_TRIAL_MAX;
nikapov 0:6a97487a0ab3 339
jaafaryn 11:1e0b9a529ee0 340 while (trials--) {
jaafaryn 11:1e0b9a529ee0 341 // get the server IP through a (local) DNS resolve
jaafaryn 11:1e0b9a529ee0 342 if (WIFI_GetHostAddress(serverUrl, serverIP) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 343
jaafaryn 11:1e0b9a529ee0 344 printf("> %s resolved to: %d.%d.%d.%d\n", serverUrl,
jaafaryn 11:1e0b9a529ee0 345 serverIP[0],
jaafaryn 11:1e0b9a529ee0 346 serverIP[1],
jaafaryn 11:1e0b9a529ee0 347 serverIP[2],
jaafaryn 11:1e0b9a529ee0 348 serverIP[3]);
jaafaryn 11:1e0b9a529ee0 349 // establish TCP connection to server
jaafaryn 11:1e0b9a529ee0 350 if( WIFI_OpenClientConnection(socket_id, WIFI_TCP_PROTOCOL, "InfluxDB", serverIP, serverPort, 0) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 351 printf("> Connected to %s .\n", serverUrl);
jaafaryn 11:1e0b9a529ee0 352 return true;
jaafaryn 11:1e0b9a529ee0 353 }
jaafaryn 11:1e0b9a529ee0 354 } else {
jaafaryn 11:1e0b9a529ee0 355 printf("> ERROR : Cannot resolve URL: %s\n", serverUrl);
jaafaryn 11:1e0b9a529ee0 356 }
nikapov 8:39ecd15538f9 357 }
jaafaryn 11:1e0b9a529ee0 358 if(!trials) {
jaafaryn 11:1e0b9a529ee0 359 printf("> ERROR : Cannot establish connection\n");
jaafaryn 11:1e0b9a529ee0 360 }
jaafaryn 11:1e0b9a529ee0 361 return false;
jaafaryn 11:1e0b9a529ee0 362 }
nikapov 8:39ecd15538f9 363