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:
Mon Jan 29 14:41:02 2018 +0000
Revision:
14:0c8967e20f93
Parent:
13:f8e84b12665c
scrapped wifi credentials in preparation for publishing

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 13:f8e84b12665c 10 - add RTC sync and time display --> done
jaafaryn 13:f8e84b12665c 11 - add light sensor to monitor illuminance --> low priority
jaafaryn 11:1e0b9a529ee0 12 - improve reading by either: reduce power on time of WiFi module (batch logging)
jaafaryn 13:f8e84b12665c 13 or by compensating for board own temperature --> WiP
jaafaryn 14:0c8967e20f93 14 - use a local toolchain to compile mbed projects (with trueStudio) --> WiP
jaafaryn 11:1e0b9a529ee0 15 - add option to choose location from a list and update location tag
jaafaryn 11:1e0b9a529ee0 16 - incrementally convert the project to electric energy monitoring:
jaafaryn 11:1e0b9a529ee0 17 - read 2x simultaneous adc channels using DMA
jaafaryn 11:1e0b9a529ee0 18 - save daily power readings to QSP mem for batch logging and recovey?
jaafaryn 11:1e0b9a529ee0 19 - add screen to display current power drawing, daily and monthly cumulatives
jaafaryn 11:1e0b9a529ee0 20 ------------------------------------------------------------------------------*/
jaafaryn 11:1e0b9a529ee0 21
jaafaryn 11:1e0b9a529ee0 22 #include "main.h"
jaafaryn 11:1e0b9a529ee0 23
jaafaryn 11:1e0b9a529ee0 24 /* ---------- SETUP ------------- */
jaafaryn 11:1e0b9a529ee0 25 void setup()
jaafaryn 11:1e0b9a529ee0 26 {
jaafaryn 11:1e0b9a529ee0 27 printf("\n***************************************************************\n");
jaafaryn 11:1e0b9a529ee0 28 printf("*** Home Env on STM32 IoT Discovery ***\n");
jaafaryn 11:1e0b9a529ee0 29 printf("***************************************************************\n");
jaafaryn 11:1e0b9a529ee0 30
jaafaryn 11:1e0b9a529ee0 31 printf("> Initializing sensors and WiFi... \n");
jaafaryn 11:1e0b9a529ee0 32
jaafaryn 11:1e0b9a529ee0 33 int range_status;
jaafaryn 11:1e0b9a529ee0 34
jaafaryn 11:1e0b9a529ee0 35 /* Init all sensors with default params */
jaafaryn 11:1e0b9a529ee0 36 hum_temp.init(NULL);
jaafaryn 11:1e0b9a529ee0 37 press_temp.init(NULL);
jaafaryn 11:1e0b9a529ee0 38 magnetometer.init(NULL);
jaafaryn 11:1e0b9a529ee0 39 acc_gyro.init(NULL);
jaafaryn 11:1e0b9a529ee0 40 range_status = range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
nikapov 0:6a97487a0ab3 41
jaafaryn 14:0c8967e20f93 42 /* enable sensors */
jaafaryn 11:1e0b9a529ee0 43 hum_temp.enable();
jaafaryn 11:1e0b9a529ee0 44 press_temp.enable();
jaafaryn 14:0c8967e20f93 45 // acc_gyro.enable_x();
jaafaryn 14:0c8967e20f93 46 // acc_gyro.enable_g();
jaafaryn 11:1e0b9a529ee0 47
jaafaryn 11:1e0b9a529ee0 48 uint8_t id;
jaafaryn 11:1e0b9a529ee0 49 hum_temp.read_id(&id);
jaafaryn 11:1e0b9a529ee0 50 printf("HTS221 humidity & temperature id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 51 press_temp.read_id(&id);
jaafaryn 11:1e0b9a529ee0 52 printf("LPS22HB pressure & temperature id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 53 magnetometer.read_id(&id);
jaafaryn 11:1e0b9a529ee0 54 printf("LIS3MDL magnetometer id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 55 acc_gyro.read_id(&id);
jaafaryn 11:1e0b9a529ee0 56 printf("LSM6DSL accelerometer&gyroscope id = 0x%X\r\n", id);
jaafaryn 11:1e0b9a529ee0 57 printf("VL53L0x status = %d \r\n", range_status);
nikapov 0:6a97487a0ab3 58
jaafaryn 11:1e0b9a529ee0 59 sense_enabled = true;
nikapov 8:39ecd15538f9 60
jaafaryn 11:1e0b9a529ee0 61 /* Setup display */
jaafaryn 14:0c8967e20f93 62 WAKE_UP_DISPLAY;
jaafaryn 11:1e0b9a529ee0 63 WIFI_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 64 UPLOAD_ICON_OFF;
jaafaryn 13:f8e84b12665c 65 NO_WAKEUP_ON_SERIALIN;// no wakeup on serial in
jaafaryn 13:f8e84b12665c 66 SLEEP_ON_NOTOUCH_30S;// sleep after 30sec no touch
jaafaryn 13:f8e84b12665c 67 WAKEUP_ON_TOUCH;// wake up on touch
jaafaryn 11:1e0b9a529ee0 68
nikapov 8:39ecd15538f9 69
jaafaryn 11:1e0b9a529ee0 70 /*Initialize WIFI module */
jaafaryn 11:1e0b9a529ee0 71 WiFi_on = connectWiFi();
jaafaryn 11:1e0b9a529ee0 72 if (WiFi_on) {
jaafaryn 11:1e0b9a529ee0 73 led3 = 1;
jaafaryn 11:1e0b9a529ee0 74 //WiFi_led_ticker.attach(&toggle_led3_cb, 0.25);
jaafaryn 12:058b012dbebe 75 rtc_synced = sync_rtc();
jaafaryn 14:0c8967e20f93 76 // enter power save mode
jaafaryn 14:0c8967e20f93 77 /*if (WIFI_SetPowerSaveMode(1, (UPLOAD_PERIOD_S - 1)*1000) == WIFI_STATUS_OK) {
jaafaryn 14:0c8967e20f93 78 printf("> es-wifi entered power save mode\n");
jaafaryn 14:0c8967e20f93 79 } else {
jaafaryn 14:0c8967e20f93 80 printf("> ERROR: es-wifi did not enter power save mode\n");
jaafaryn 14:0c8967e20f93 81 }*/
jaafaryn 11:1e0b9a529ee0 82 }
nikapov 8:39ecd15538f9 83
jaafaryn 11:1e0b9a529ee0 84 // start event tickers
jaafaryn 11:1e0b9a529ee0 85 getMeasurementsTicker.attach(&time_to_sense_cb, SENSE_PERIOD_S);
jaafaryn 13:f8e84b12665c 86 sendMeasurementsTicker.attach(&time_to_send_cb, UPLOAD_PERIOD_S);
jaafaryn 11:1e0b9a529ee0 87 checkProximityTicker.attach(&time_to_check_distance_cb, CHECK_PROXIMITY_PERIOD_S);
nikapov 8:39ecd15538f9 88
jaafaryn 11:1e0b9a529ee0 89 } // Setup
nikapov 0:6a97487a0ab3 90
nikapov 0:6a97487a0ab3 91
jaafaryn 11:1e0b9a529ee0 92 /* ---------- LOOP ------------- */
jaafaryn 11:1e0b9a529ee0 93 void loop() {
jaafaryn 13:f8e84b12665c 94 time_t rawtime;
jaafaryn 13:f8e84b12665c 95 tm *pTime;
jaafaryn 13:f8e84b12665c 96
jaafaryn 11:1e0b9a529ee0 97 if (take_measurements) {
jaafaryn 11:1e0b9a529ee0 98 // Environment data
jaafaryn 11:1e0b9a529ee0 99 hum_temp.get_temperature(&tempC_val);
jaafaryn 11:1e0b9a529ee0 100 tempF_val = tempC_val*(9.0/5.0) + 32;
jaafaryn 11:1e0b9a529ee0 101 hum_temp.get_humidity(&RH_val);
jaafaryn 11:1e0b9a529ee0 102
jaafaryn 11:1e0b9a529ee0 103 press_temp.get_pressure(&Patm_val);
jaafaryn 11:1e0b9a529ee0 104 press_temp.get_temperature(&tempC_val2);
jaafaryn 12:058b012dbebe 105
jaafaryn 11:1e0b9a529ee0 106
jaafaryn 11:1e0b9a529ee0 107 // Inertial data
jaafaryn 11:1e0b9a529ee0 108 /*
jaafaryn 11:1e0b9a529ee0 109 magnetometer.get_m_axes(axes);
jaafaryn 11:1e0b9a529ee0 110 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
jaafaryn 11:1e0b9a529ee0 111
jaafaryn 11:1e0b9a529ee0 112 acc_gyro.get_x_axes(axes);
jaafaryn 11:1e0b9a529ee0 113 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
jaafaryn 11:1e0b9a529ee0 114
jaafaryn 11:1e0b9a529ee0 115 acc_gyro.get_g_axes(axes);
jaafaryn 11:1e0b9a529ee0 116 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
jaafaryn 11:1e0b9a529ee0 117 */
jaafaryn 11:1e0b9a529ee0 118
jaafaryn 11:1e0b9a529ee0 119 // print results to terminal
jaafaryn 14:0c8967e20f93 120 /*
jaafaryn 11:1e0b9a529ee0 121 printf("HTS221: temp= %.2f C, hum= %.2f%%\r\n", tempC_val, RH_val);
jaafaryn 11:1e0b9a529ee0 122 printf(" temp= %.2f F\n", tempF_val);
jaafaryn 11:1e0b9a529ee0 123 printf("LPS22HB: patm= %.2f mbar, temp= %.2f C\r\n", Patm_val, tempC_val2);
jaafaryn 12:058b012dbebe 124 printf("VL53L0X [mm] = %6ld\r\n", distance_val);
jaafaryn 14:0c8967e20f93 125 */
jaafaryn 11:1e0b9a529ee0 126 // refresh screen with updated measurements
jaafaryn 11:1e0b9a529ee0 127 nextion.printf("valC.txt=\"%.1f\"\xff\xff\xff", tempC_val);
jaafaryn 11:1e0b9a529ee0 128 nextion.printf("valF.txt=\"%.1f\"\xff\xff\xff", tempF_val);
jaafaryn 11:1e0b9a529ee0 129 nextion.printf("valRH.txt=\"%.1f\"\xff\xff\xff", RH_val);
jaafaryn 11:1e0b9a529ee0 130 nextion.printf("valAtm.val=%.0f\xff\xff\xff", Patm_val);
jaafaryn 12:058b012dbebe 131 nextion.printf("valRange.val=%d\xff\xff\xff", distance_val);
jaafaryn 11:1e0b9a529ee0 132
jaafaryn 11:1e0b9a529ee0 133 take_measurements = false;
jaafaryn 14:0c8967e20f93 134 }
jaafaryn 11:1e0b9a529ee0 135
jaafaryn 11:1e0b9a529ee0 136 if (send_measurements) {
jaafaryn 11:1e0b9a529ee0 137
jaafaryn 14:0c8967e20f93 138 printf("HTS221: temp= %.2f C, hum= %.2f%%\r\n", tempC_val, RH_val);
nikapov 0:6a97487a0ab3 139
jaafaryn 11:1e0b9a529ee0 140 // body of the request
jaafaryn 11:1e0b9a529ee0 141 char request_body[256];
jaafaryn 14:0c8967e20f93 142 char *InfluxServerUrl = INFLUX_SERVER_URL;
jaafaryn 11:1e0b9a529ee0 143 sprintf(request_body, "disco_iot,loc=%s temperature=%.2f,humidity=%.1f,pressure=%.1f \n","playroom", tempC_val, RH_val, Patm_val);
jaafaryn 11:1e0b9a529ee0 144
jaafaryn 11:1e0b9a529ee0 145 // build header of the request
jaafaryn 14:0c8967e20f93 146 sprintf((char *)http_request, "POST %s HTTP/1.1\r\nHost: %s \r\n", INFLUX_WRITE_EP, InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 147 strcat((char *)http_request, "Accept: */*\r\n");
jaafaryn 11:1e0b9a529ee0 148 strcat((char *)http_request, "User-agent: ES-WIFI TcpClient\r\n");
jaafaryn 11:1e0b9a529ee0 149 strcat((char *)http_request, "Connection: Close\r\n"); //"Connection: Keep-Alive\r\n"
jaafaryn 11:1e0b9a529ee0 150 char buffer[64];
jaafaryn 11:1e0b9a529ee0 151 sprintf(buffer, "Content-Length: %d \r\n\r\n", strlen(request_body));
jaafaryn 11:1e0b9a529ee0 152 strcat((char *)http_request, buffer);
jaafaryn 11:1e0b9a529ee0 153
jaafaryn 11:1e0b9a529ee0 154 // append body to the header of the request
jaafaryn 11:1e0b9a529ee0 155 strcat((char *)http_request, request_body);
jaafaryn 11:1e0b9a529ee0 156 reqLen = strlen((char *)http_request);
nikapov 0:6a97487a0ab3 157
jaafaryn 11:1e0b9a529ee0 158 // Establish a connection to DB server
jaafaryn 11:1e0b9a529ee0 159 uint8_t socketid = 1;
jaafaryn 11:1e0b9a529ee0 160 if (checkWiFi()) {
jaafaryn 11:1e0b9a529ee0 161 led3 = 1;
jaafaryn 14:0c8967e20f93 162 if (connectToServer(INFLUX_SERVER_URL, INFLUX_SERVER_PORT, socketid)) {
jaafaryn 11:1e0b9a529ee0 163 ledhttp =1;
jaafaryn 11:1e0b9a529ee0 164 // submit POST request
jaafaryn 14:0c8967e20f93 165 // printf("> Sending a POST request with length=%d including a body length=%d\n", reqLen, strlen(request_body));
jaafaryn 14:0c8967e20f93 166 // printf((char *)http_request);
jaafaryn 11:1e0b9a529ee0 167 uint16_t dataLen;
jaafaryn 11:1e0b9a529ee0 168 if (WIFI_SendData(socketid, http_request, reqLen, &dataLen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 169 printf("> ERROR: Could not send request to %s", InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 170 UPLOAD_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 171 } else {
jaafaryn 11:1e0b9a529ee0 172 request_sent++;
jaafaryn 11:1e0b9a529ee0 173 UPLOAD_ICON_ON;
jaafaryn 11:1e0b9a529ee0 174 }
jaafaryn 11:1e0b9a529ee0 175 // get server response
jaafaryn 11:1e0b9a529ee0 176 // memset(&http_resp[0], 0, sizeof(http_resp));
jaafaryn 11:1e0b9a529ee0 177 WIFI_ReceiveData(socketid, http_resp, sizeof(http_resp), &dataLen, WIFI_READ_TIMEOUT);
jaafaryn 11:1e0b9a529ee0 178 if(dataLen > 0) {
jaafaryn 11:1e0b9a529ee0 179 /*
jaafaryn 11:1e0b9a529ee0 180 printf("> Server response:\n");
jaafaryn 11:1e0b9a529ee0 181 printf((char *)http_resp);
jaafaryn 11:1e0b9a529ee0 182 printf("\n> Response length: %d \n", dataLen);
jaafaryn 11:1e0b9a529ee0 183 */
jaafaryn 11:1e0b9a529ee0 184 memset(&buffer[0], 0, sizeof(buffer));
jaafaryn 11:1e0b9a529ee0 185 // get the first line of the response
jaafaryn 11:1e0b9a529ee0 186 strcpy(buffer, strtok((char *)http_resp, "\r\n"));
jaafaryn 11:1e0b9a529ee0 187 // extract the response code
jaafaryn 11:1e0b9a529ee0 188 int response_code = 0;
jaafaryn 11:1e0b9a529ee0 189 sscanf(buffer, "HTTP/1.1 %d", &response_code);
jaafaryn 14:0c8967e20f93 190 // printf("> Response code: %d \n", response_code);
jaafaryn 14:0c8967e20f93 191 /* common response codes from InfluxDB API:
jaafaryn 11:1e0b9a529ee0 192 HTTP/1.1 204 No Content
jaafaryn 11:1e0b9a529ee0 193 HTTP/1.1 400 Bad Request
jaafaryn 11:1e0b9a529ee0 194 HTTP/1.1 404 Not Found
jaafaryn 11:1e0b9a529ee0 195 HTTP/1.1 500 Internal Server Error
jaafaryn 11:1e0b9a529ee0 196 */
jaafaryn 11:1e0b9a529ee0 197 if (response_code == 204) request_acked++;
jaafaryn 11:1e0b9a529ee0 198 } else {
jaafaryn 11:1e0b9a529ee0 199 UPLOAD_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 200 printf("> Error: No response from the server %s.\n", InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 201 }
jaafaryn 11:1e0b9a529ee0 202 printf("> Requests sent: %d, ack'ed: %d\n", request_sent, request_acked);
jaafaryn 11:1e0b9a529ee0 203 ledhttp = 0;
jaafaryn 11:1e0b9a529ee0 204 WIFI_CloseClientConnection(socketid);
jaafaryn 11:1e0b9a529ee0 205 } else {
jaafaryn 14:0c8967e20f93 206 printf("> ERROR: Could not open connection to %s \n", InfluxServerUrl);
jaafaryn 11:1e0b9a529ee0 207 }
jaafaryn 14:0c8967e20f93 208
jaafaryn 14:0c8967e20f93 209 // enter power save mode for UPLOAD_PERIOD_S - 1 sec, beacon interval = 100ms
jaafaryn 14:0c8967e20f93 210 if (WIFI_SetPowerSaveMode(1, (UPLOAD_PERIOD_S - 1)*1000) == WIFI_STATUS_OK) {
jaafaryn 14:0c8967e20f93 211 printf("> es-wifi entered power save mode\n");
jaafaryn 14:0c8967e20f93 212 } else {
jaafaryn 14:0c8967e20f93 213 printf("> ERROR: es-wifi did not enter power save mode\n");
jaafaryn 14:0c8967e20f93 214 }
jaafaryn 14:0c8967e20f93 215
jaafaryn 11:1e0b9a529ee0 216 } else {
jaafaryn 11:1e0b9a529ee0 217 printf("> ERROR: Could not connect to WiFi \n");
jaafaryn 11:1e0b9a529ee0 218 led3 = 0;
jaafaryn 11:1e0b9a529ee0 219 }
jaafaryn 13:f8e84b12665c 220
jaafaryn 13:f8e84b12665c 221 if (rtc_synced) {
jaafaryn 13:f8e84b12665c 222 time(&rawtime);
jaafaryn 13:f8e84b12665c 223 pTime = localtime(&rawtime);
jaafaryn 14:0c8967e20f93 224 // printf ("Current local time and date: %s", asctime(pTime));
jaafaryn 14:0c8967e20f93 225 // printf("> %d:%d:%d\n", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
jaafaryn 13:f8e84b12665c 226 } else {
jaafaryn 14:0c8967e20f93 227 rtc_synced = sync_rtc();
jaafaryn 13:f8e84b12665c 228 }
jaafaryn 13:f8e84b12665c 229
jaafaryn 11:1e0b9a529ee0 230 send_measurements = false;
jaafaryn 14:0c8967e20f93 231 }
nikapov 8:39ecd15538f9 232
jaafaryn 11:1e0b9a529ee0 233 if (check_proximity) {
jaafaryn 11:1e0b9a529ee0 234 // make sure display is awake when somebody get close to the screen
jaafaryn 11:1e0b9a529ee0 235 // Proximity value
jaafaryn 11:1e0b9a529ee0 236 uint32_t dist_val = 0;
jaafaryn 11:1e0b9a529ee0 237 int status = range.get_distance(&dist_val);
jaafaryn 11:1e0b9a529ee0 238 if (status == VL53L0X_ERROR_NONE) {
jaafaryn 12:058b012dbebe 239 if (dist_val < 500) {
jaafaryn 12:058b012dbebe 240 nextion.printf("sleep=0\xff\xff\xff");// send wake up command
jaafaryn 12:058b012dbebe 241 }
jaafaryn 12:058b012dbebe 242 distance_val = dist_val;
jaafaryn 11:1e0b9a529ee0 243 } else {
jaafaryn 12:058b012dbebe 244 distance_val = 9999;
jaafaryn 11:1e0b9a529ee0 245 }
jaafaryn 13:f8e84b12665c 246
jaafaryn 13:f8e84b12665c 247 // since this is a 1-sec period ticker, we do clock display refresh in here too
jaafaryn 13:f8e84b12665c 248 // get a formatted local time to be displayed
jaafaryn 13:f8e84b12665c 249 if (rtc_synced) {
jaafaryn 13:f8e84b12665c 250 time(&rawtime);
jaafaryn 13:f8e84b12665c 251 pTime = localtime(&rawtime);
jaafaryn 14:0c8967e20f93 252 nextion.printf("hour.txt=\"%02d\"\xff\xff\xff", pTime->tm_hour);
jaafaryn 14:0c8967e20f93 253 nextion.printf("minute.txt=\"%02d\"\xff\xff\xff", pTime->tm_min);
jaafaryn 14:0c8967e20f93 254 nextion.printf("vis sec,1\xff\xff\xff");
jaafaryn 13:f8e84b12665c 255 //nextion.printf("vis sec,%d\xff\xff\xff", pTime->tm_sec % 2 == 0 ? 1 : 0);
jaafaryn 13:f8e84b12665c 256 }
jaafaryn 13:f8e84b12665c 257
jaafaryn 11:1e0b9a529ee0 258 check_proximity = false;
jaafaryn 14:0c8967e20f93 259 }
jaafaryn 11:1e0b9a529ee0 260 } // Loop
nikapov 8:39ecd15538f9 261
nikapov 8:39ecd15538f9 262
jaafaryn 11:1e0b9a529ee0 263 int main() {
jaafaryn 11:1e0b9a529ee0 264 setup();
jaafaryn 11:1e0b9a529ee0 265 while(1) {
jaafaryn 11:1e0b9a529ee0 266 loop();
jaafaryn 11:1e0b9a529ee0 267 }
jaafaryn 11:1e0b9a529ee0 268 }
jaafaryn 11:1e0b9a529ee0 269
jaafaryn 11:1e0b9a529ee0 270
jaafaryn 11:1e0b9a529ee0 271 /* */
jaafaryn 11:1e0b9a529ee0 272 /* Interrupts servicing callback functions */
jaafaryn 11:1e0b9a529ee0 273 /* */
jaafaryn 11:1e0b9a529ee0 274
jaafaryn 11:1e0b9a529ee0 275 // ISR for flashing WiFi LED
jaafaryn 11:1e0b9a529ee0 276 /*
jaafaryn 11:1e0b9a529ee0 277 void toggle_led3_cb()
jaafaryn 11:1e0b9a529ee0 278 {
jaafaryn 11:1e0b9a529ee0 279 led3 = !led3;
jaafaryn 11:1e0b9a529ee0 280 }*/
jaafaryn 11:1e0b9a529ee0 281
jaafaryn 11:1e0b9a529ee0 282 // ISR to enable taking measurements (and refresh display)
jaafaryn 11:1e0b9a529ee0 283 void time_to_sense_cb(void)
jaafaryn 11:1e0b9a529ee0 284 {
jaafaryn 11:1e0b9a529ee0 285 if (sense_enabled) {
jaafaryn 11:1e0b9a529ee0 286 take_measurements = true;
jaafaryn 11:1e0b9a529ee0 287 }
jaafaryn 11:1e0b9a529ee0 288 }
nikapov 0:6a97487a0ab3 289
jaafaryn 11:1e0b9a529ee0 290 // ISR to enable handling sending data
jaafaryn 11:1e0b9a529ee0 291 void time_to_send_cb(void)
jaafaryn 11:1e0b9a529ee0 292 {
jaafaryn 11:1e0b9a529ee0 293 if (sense_enabled) {
jaafaryn 11:1e0b9a529ee0 294 send_measurements = true;
jaafaryn 11:1e0b9a529ee0 295 }
jaafaryn 11:1e0b9a529ee0 296 }
jaafaryn 11:1e0b9a529ee0 297
jaafaryn 11:1e0b9a529ee0 298 // ISR to enable checking distance
jaafaryn 11:1e0b9a529ee0 299 void time_to_check_distance_cb(void)
jaafaryn 11:1e0b9a529ee0 300 {
jaafaryn 11:1e0b9a529ee0 301 check_proximity = true;
jaafaryn 11:1e0b9a529ee0 302 }
jaafaryn 11:1e0b9a529ee0 303
jaafaryn 11:1e0b9a529ee0 304 /**
jaafaryn 11:1e0b9a529ee0 305 * @brief Connect to the WiFi network with credentials in mbed_app.json
jaafaryn 11:1e0b9a529ee0 306 * @param None
jaafaryn 11:1e0b9a529ee0 307 * @retval True for WiFi connected, False if there was an error
jaafaryn 11:1e0b9a529ee0 308 */
jaafaryn 11:1e0b9a529ee0 309 bool connectWiFi(void)
jaafaryn 11:1e0b9a529ee0 310 {
jaafaryn 11:1e0b9a529ee0 311 uint8_t IP_Addr[4];
jaafaryn 11:1e0b9a529ee0 312 uint8_t MAC_Addr[6];
jaafaryn 11:1e0b9a529ee0 313
jaafaryn 11:1e0b9a529ee0 314 if(WIFI_Init() == WIFI_STATUS_OK) {
jaafaryn 14:0c8967e20f93 315 // printf("> WiFi module initialized.\n");
jaafaryn 13:f8e84b12665c 316 char fwVer[32];
jaafaryn 13:f8e84b12665c 317 if(WIFI_GetModuleFwRevision(fwVer) == WIFI_STATUS_OK) {
jaafaryn 13:f8e84b12665c 318 printf("> Firmware version: %s\n", fwVer);
jaafaryn 13:f8e84b12665c 319 }
jaafaryn 14:0c8967e20f93 320 /*
jaafaryn 11:1e0b9a529ee0 321 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 322 printf("> WiFi module MAC Address : %X:%X:%X:%X:%X:%X\n",
jaafaryn 11:1e0b9a529ee0 323 MAC_Addr[0],
jaafaryn 11:1e0b9a529ee0 324 MAC_Addr[1],
jaafaryn 11:1e0b9a529ee0 325 MAC_Addr[2],
jaafaryn 11:1e0b9a529ee0 326 MAC_Addr[3],
jaafaryn 11:1e0b9a529ee0 327 MAC_Addr[4],
jaafaryn 11:1e0b9a529ee0 328 MAC_Addr[5]);
jaafaryn 11:1e0b9a529ee0 329 } else {
jaafaryn 11:1e0b9a529ee0 330 printf("> ERROR : CANNOT get MAC address\n");
jaafaryn 14:0c8967e20f93 331 }*/
jaafaryn 14:0c8967e20f93 332
jaafaryn 11:1e0b9a529ee0 333 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 334 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 335 printf("> IP Address : %d.%d.%d.%d\n",
jaafaryn 11:1e0b9a529ee0 336 IP_Addr[0],
jaafaryn 11:1e0b9a529ee0 337 IP_Addr[1],
jaafaryn 11:1e0b9a529ee0 338 IP_Addr[2],
jaafaryn 11:1e0b9a529ee0 339 IP_Addr[3]);
jaafaryn 12:058b012dbebe 340 WIFI_ICON_ON;
jaafaryn 14:0c8967e20f93 341 UPLOAD_ICON_OFF; // reset db sync status if (re-)connect happened
jaafaryn 11:1e0b9a529ee0 342 return true;
jaafaryn 11:1e0b9a529ee0 343 } else {
jaafaryn 11:1e0b9a529ee0 344 printf("> ERROR : es-wifi module CANNOT get IP address\n");
jaafaryn 11:1e0b9a529ee0 345 }
jaafaryn 11:1e0b9a529ee0 346 } else {
jaafaryn 11:1e0b9a529ee0 347 printf("> ERROR : es-wifi module NOT connected\n");
jaafaryn 11:1e0b9a529ee0 348 }
jaafaryn 11:1e0b9a529ee0 349 } else {
jaafaryn 11:1e0b9a529ee0 350 printf("> ERROR : WIFI Module cannot be initialized.\n");
jaafaryn 11:1e0b9a529ee0 351 }
jaafaryn 12:058b012dbebe 352 WIFI_ICON_OFF;
jaafaryn 14:0c8967e20f93 353 UPLOAD_ICON_OFF;
jaafaryn 11:1e0b9a529ee0 354 return false;
jaafaryn 11:1e0b9a529ee0 355 }
nikapov 0:6a97487a0ab3 356
nikapov 0:6a97487a0ab3 357
jaafaryn 11:1e0b9a529ee0 358 /**
jaafaryn 11:1e0b9a529ee0 359 * @brief check WiFi connection status, retry once if it's offline
jaafaryn 11:1e0b9a529ee0 360 * @param None
jaafaryn 11:1e0b9a529ee0 361 * @retval True for WiFi (re)connected, False if reconnection failed
jaafaryn 11:1e0b9a529ee0 362 */
nikapov 0:6a97487a0ab3 363
jaafaryn 11:1e0b9a529ee0 364 bool checkWiFi(void)
jaafaryn 11:1e0b9a529ee0 365 {
jaafaryn 11:1e0b9a529ee0 366 uint8_t ip_addr[4];
jaafaryn 14:0c8967e20f93 367 if(WIFI_GetIP_Address(ip_addr) == WIFI_STATUS_OK) {
jaafaryn 14:0c8967e20f93 368 WIFI_ICON_ON;
jaafaryn 14:0c8967e20f93 369 return true;
jaafaryn 14:0c8967e20f93 370 } else {
jaafaryn 11:1e0b9a529ee0 371 return connectWiFi();
jaafaryn 11:1e0b9a529ee0 372 }
jaafaryn 11:1e0b9a529ee0 373 }
nikapov 0:6a97487a0ab3 374
jaafaryn 11:1e0b9a529ee0 375 /**
jaafaryn 11:1e0b9a529ee0 376 * @brief create a TCP session on a server
jaafaryn 11:1e0b9a529ee0 377 * @param serverUrl : TCP server URL
jaafaryn 11:1e0b9a529ee0 378 * @param serverPort : port TCP server is listening on
jaafaryn 11:1e0b9a529ee0 379 * @retval true if connection established
jaafaryn 11:1e0b9a529ee0 380 */
jaafaryn 11:1e0b9a529ee0 381 bool connectToServer(char *serverUrl, uint16_t serverPort, uint8_t socket_id)
jaafaryn 11:1e0b9a529ee0 382 {
jaafaryn 11:1e0b9a529ee0 383 uint8_t serverIP[] = {0, 0, 0, 0};
jaafaryn 11:1e0b9a529ee0 384 uint16_t trials = CONNECTION_TRIAL_MAX;
nikapov 0:6a97487a0ab3 385
jaafaryn 11:1e0b9a529ee0 386 while (trials--) {
jaafaryn 11:1e0b9a529ee0 387 // get the server IP through a (local) DNS resolve
jaafaryn 11:1e0b9a529ee0 388 if (WIFI_GetHostAddress(serverUrl, serverIP) == WIFI_STATUS_OK) {
jaafaryn 14:0c8967e20f93 389
jaafaryn 11:1e0b9a529ee0 390 printf("> %s resolved to: %d.%d.%d.%d\n", serverUrl,
jaafaryn 11:1e0b9a529ee0 391 serverIP[0],
jaafaryn 11:1e0b9a529ee0 392 serverIP[1],
jaafaryn 11:1e0b9a529ee0 393 serverIP[2],
jaafaryn 14:0c8967e20f93 394 serverIP[3]);
jaafaryn 11:1e0b9a529ee0 395 // establish TCP connection to server
jaafaryn 12:058b012dbebe 396 if( WIFI_OpenClientConnection(socket_id, WIFI_TCP_PROTOCOL, "TCP_Client", serverIP, serverPort, 0) == WIFI_STATUS_OK) {
jaafaryn 11:1e0b9a529ee0 397 printf("> Connected to %s .\n", serverUrl);
jaafaryn 11:1e0b9a529ee0 398 return true;
jaafaryn 11:1e0b9a529ee0 399 }
jaafaryn 11:1e0b9a529ee0 400 } else {
jaafaryn 11:1e0b9a529ee0 401 printf("> ERROR : Cannot resolve URL: %s\n", serverUrl);
jaafaryn 14:0c8967e20f93 402 printf("> Trials left: %d\n", trials);
jaafaryn 11:1e0b9a529ee0 403 }
nikapov 8:39ecd15538f9 404 }
jaafaryn 14:0c8967e20f93 405
jaafaryn 11:1e0b9a529ee0 406 if(!trials) {
jaafaryn 11:1e0b9a529ee0 407 printf("> ERROR : Cannot establish connection\n");
jaafaryn 11:1e0b9a529ee0 408 }
jaafaryn 11:1e0b9a529ee0 409 return false;
jaafaryn 11:1e0b9a529ee0 410 }
nikapov 8:39ecd15538f9 411
jaafaryn 12:058b012dbebe 412
jaafaryn 12:058b012dbebe 413 /**
jaafaryn 12:058b012dbebe 414 * @brief get current unix timestamp and use it to sync rtc
jaafaryn 12:058b012dbebe 415 * @retval True if successful, False if failed
jaafaryn 12:058b012dbebe 416 */
jaafaryn 12:058b012dbebe 417 bool sync_rtc() {
jaafaryn 12:058b012dbebe 418 // 1- establish connection to time server (socket 2)
jaafaryn 12:058b012dbebe 419 uint8_t socketid = 2;
jaafaryn 12:058b012dbebe 420 uint16_t dataLen;
jaafaryn 12:058b012dbebe 421 if (checkWiFi()) {
jaafaryn 12:058b012dbebe 422 if (connectToServer(TIME_SERVER_URL, TIME_SERVER_PORT, socketid)) {
jaafaryn 14:0c8967e20f93 423 sprintf((char *)http_request, "GET %s HTTP/1.1\r\nHost: %s \r\n", TIME_SERVER_EP, TIME_SERVER_URL);
jaafaryn 12:058b012dbebe 424 strcat((char *)http_request, "Connection: Close\r\n\r\n");
jaafaryn 12:058b012dbebe 425 reqLen = strlen((char *)http_request);
jaafaryn 12:058b012dbebe 426 if (WIFI_SendData(socketid, http_request, reqLen, &dataLen, WIFI_WRITE_TIMEOUT) != WIFI_STATUS_OK) {
jaafaryn 12:058b012dbebe 427 printf("> ERROR: Could not send request to %s\n", TIME_SERVER_URL);
jaafaryn 12:058b012dbebe 428 } else {
jaafaryn 12:058b012dbebe 429 printf("> TS: Sent %d bytes\n", dataLen);
jaafaryn 12:058b012dbebe 430 }
jaafaryn 12:058b012dbebe 431
jaafaryn 12:058b012dbebe 432 // 2- get server response and parse the json
jaafaryn 12:058b012dbebe 433 WIFI_ReceiveData(socketid, http_resp, sizeof(http_resp), &dataLen, WIFI_READ_TIMEOUT);
jaafaryn 12:058b012dbebe 434 if(dataLen > 0) {
jaafaryn 12:058b012dbebe 435 printf("> TS: Received %d bytes\n", dataLen);
jaafaryn 12:058b012dbebe 436 // extract the body (json payload) from the response
jaafaryn 12:058b012dbebe 437 char *resp_body = strstr((char *)http_resp, "\r\n\r\n"); // get the body + "OK"
jaafaryn 12:058b012dbebe 438 resp_body = strtok(resp_body, "\r\n"); // get rid of 'OK'
jaafaryn 12:058b012dbebe 439 picojson::value json_val;
jaafaryn 12:058b012dbebe 440 string err = picojson::parse(json_val, resp_body, resp_body + strlen(resp_body));
jaafaryn 12:058b012dbebe 441 if(err.empty()) {
jaafaryn 12:058b012dbebe 442
jaafaryn 12:058b012dbebe 443 // 3- convert timestamp to local time and sync rtc
jaafaryn 12:058b012dbebe 444 uint32_t timestamp_val = (unsigned)json_val.get("timestamp").get<double>();
jaafaryn 12:058b012dbebe 445 printf("> utc_ts = %u \n",timestamp_val); // print utc
jaafaryn 12:058b012dbebe 446 timestamp_val -= 3600*5; // EST: utc - 5h
jaafaryn 12:058b012dbebe 447 set_time(timestamp_val);
jaafaryn 12:058b012dbebe 448 WIFI_CloseClientConnection(socketid); // close socket on success
jaafaryn 12:058b012dbebe 449 return true;
jaafaryn 12:058b012dbebe 450 } else {
jaafaryn 12:058b012dbebe 451 printf("> ERROR: TS json parsing\n");
jaafaryn 12:058b012dbebe 452 }
jaafaryn 12:058b012dbebe 453 } else {
jaafaryn 12:058b012dbebe 454 printf("> ERROR: Did not receive data from TS server\n");
jaafaryn 12:058b012dbebe 455 }
jaafaryn 12:058b012dbebe 456 WIFI_CloseClientConnection(socketid); // close socket on failure to retrieve time
jaafaryn 12:058b012dbebe 457 } else {
jaafaryn 12:058b012dbebe 458 printf("> ERROR: Cannot establish connection to %s \n", TIME_SERVER_URL);
jaafaryn 12:058b012dbebe 459 }
jaafaryn 12:058b012dbebe 460 }
jaafaryn 12:058b012dbebe 461 return false;
jaafaryn 12:058b012dbebe 462 }
jaafaryn 12:058b012dbebe 463