Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
JMF
Date:
Wed Jul 13 15:48:37 2016 +0000
Revision:
17:38a8cc0c6ba5
Parent:
16:17c5916f2d12
Child:
18:b08471eb1d1c
Updated power-up sequence per WNC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:9d5134074d84 1 #include "mbed.h"
JMF 0:9d5134074d84 2 #include <cctype>
JMF 0:9d5134074d84 3 #include <string>
JMF 0:9d5134074d84 4 #include "SerialBuffered.h"
JMF 0:9d5134074d84 5 #include "HTS221.h"
JMF 2:0e2ef866af95 6 #include "config_me.h"
JMF 2:0e2ef866af95 7 #include "wnc_control.h"
stefanrousseau 4:f83bedd9cab4 8 #include "sensors.h"
JMF 0:9d5134074d84 9
stefanrousseau 11:e6602513730f 10 #include "hardware.h"
stefanrousseau 11:e6602513730f 11 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
stefanrousseau 11:e6602513730f 12
JMF 0:9d5134074d84 13 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 14 #define USE_COLOR
JMF 0:9d5134074d84 15 #ifdef USE_COLOR
JMF 0:9d5134074d84 16 #define BLK "\033[30m"
JMF 0:9d5134074d84 17 #define RED "\033[31m"
JMF 0:9d5134074d84 18 #define GRN "\033[32m"
JMF 0:9d5134074d84 19 #define YEL "\033[33m"
JMF 0:9d5134074d84 20 #define BLU "\033[34m"
JMF 0:9d5134074d84 21 #define MAG "\033[35m"
JMF 0:9d5134074d84 22 #define CYN "\033[36m"
JMF 0:9d5134074d84 23 #define WHT "\033[37m"
JMF 0:9d5134074d84 24 #define DEF "\033[39m"
JMF 0:9d5134074d84 25 #else
JMF 0:9d5134074d84 26 #define BLK
JMF 0:9d5134074d84 27 #define RED
JMF 0:9d5134074d84 28 #define GRN
JMF 0:9d5134074d84 29 #define YEL
JMF 0:9d5134074d84 30 #define BLU
JMF 0:9d5134074d84 31 #define MAG
JMF 0:9d5134074d84 32 #define CYN
JMF 0:9d5134074d84 33 #define WHT
JMF 0:9d5134074d84 34 #define DEF
JMF 0:9d5134074d84 35 #endif
JMF 0:9d5134074d84 36
JMF 0:9d5134074d84 37 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 38 #define MDM_DBG_AT_CMDS (1 << 0)
JMF 0:9d5134074d84 39 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 40
JMF 0:9d5134074d84 41 Serial pc(USBTX, USBRX);
JMF 0:9d5134074d84 42 SerialBuffered mdm(PTD3, PTD2, 128);
stefanrousseau 16:17c5916f2d12 43 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 44 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 45 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 46
JMF 0:9d5134074d84 47 DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot
JMF 0:9d5134074d84 48 DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem)
JMF 0:9d5134074d84 49 DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield
JMF 0:9d5134074d84 50
fkellermavnet 14:0c353e212296 51 DigitalOut mdm_reset(PTC12); // active high
fkellermavnet 14:0c353e212296 52
JMF 0:9d5134074d84 53 DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
JMF 0:9d5134074d84 54 DigitalOut mdm_uart1_cts(PTD0);
JMF 0:9d5134074d84 55
JMF 0:9d5134074d84 56 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 57
JMF 0:9d5134074d84 58 const char ok_str[] = "OK";
JMF 0:9d5134074d84 59 const char error_str[] = "ERROR";
JMF 0:9d5134074d84 60
JMF 0:9d5134074d84 61 #define MDM_OK 0
JMF 0:9d5134074d84 62 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 63
JMF 0:9d5134074d84 64 #define MAX_AT_RSP_LEN 255
JMF 0:9d5134074d84 65
JMF 0:9d5134074d84 66 ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) {
JMF 0:9d5134074d84 67 int cin = -1;
JMF 0:9d5134074d84 68 int cin_last;
JMF 0:9d5134074d84 69
JMF 0:9d5134074d84 70 if (NULL == buff || size == 0) {
JMF 0:9d5134074d84 71 return -1;
JMF 0:9d5134074d84 72 }
JMF 0:9d5134074d84 73
JMF 0:9d5134074d84 74 size_t len = 0;
JMF 0:9d5134074d84 75 Timer timer;
JMF 0:9d5134074d84 76 timer.start();
JMF 0:9d5134074d84 77 while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) {
JMF 0:9d5134074d84 78 if (mdm.readable()) {
JMF 0:9d5134074d84 79 cin_last = cin;
JMF 0:9d5134074d84 80 cin = mdm.getc();
JMF 0:9d5134074d84 81 if (isprint(cin)) {
JMF 0:9d5134074d84 82 buff[len++] = (char)cin;
JMF 0:9d5134074d84 83 continue;
JMF 0:9d5134074d84 84 } else if (('\r' == cin_last) && ('\n' == cin)) {
JMF 0:9d5134074d84 85 break;
JMF 0:9d5134074d84 86 }
JMF 0:9d5134074d84 87 }
JMF 0:9d5134074d84 88 wait_ms(1);
JMF 0:9d5134074d84 89 }
JMF 2:0e2ef866af95 90 buff[len] = (char)NULL;
JMF 0:9d5134074d84 91
JMF 0:9d5134074d84 92 return len;
JMF 0:9d5134074d84 93 }
JMF 0:9d5134074d84 94
JMF 0:9d5134074d84 95 int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) {
JMF 0:9d5134074d84 96 if (cmd && strlen(cmd) > 0) {
JMF 0:9d5134074d84 97 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 98 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 0:9d5134074d84 99 }
JMF 0:9d5134074d84 100 mdm.printf("%s\r\n", cmd);
JMF 0:9d5134074d84 101 }
JMF 0:9d5134074d84 102
JMF 0:9d5134074d84 103 if (rsp_list) {
JMF 0:9d5134074d84 104 Timer timer;
JMF 0:9d5134074d84 105 char rsp[MAX_AT_RSP_LEN+1];
JMF 0:9d5134074d84 106 int len;
JMF 0:9d5134074d84 107
JMF 0:9d5134074d84 108 timer.start();
JMF 0:9d5134074d84 109 while (timer.read_ms() < timeout_ms) {
JMF 0:9d5134074d84 110 len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms());
JMF 0:9d5134074d84 111
JMF 0:9d5134074d84 112 if (len < 0)
JMF 0:9d5134074d84 113 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 114
JMF 0:9d5134074d84 115 if (len == 0)
JMF 0:9d5134074d84 116 continue;
JMF 0:9d5134074d84 117
JMF 0:9d5134074d84 118 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 119 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp);
JMF 0:9d5134074d84 120 }
JMF 0:9d5134074d84 121
JMF 0:9d5134074d84 122 if (rsp_list) {
JMF 0:9d5134074d84 123 int rsp_idx = 0;
JMF 0:9d5134074d84 124 while (rsp_list[rsp_idx]) {
JMF 0:9d5134074d84 125 if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) {
JMF 0:9d5134074d84 126 return rsp_idx;
JMF 0:9d5134074d84 127 }
JMF 0:9d5134074d84 128 rsp_idx++;
JMF 0:9d5134074d84 129 }
JMF 0:9d5134074d84 130 }
JMF 0:9d5134074d84 131 }
JMF 0:9d5134074d84 132 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 133 }
JMF 0:9d5134074d84 134 return MDM_OK;
JMF 0:9d5134074d84 135 }
JMF 0:9d5134074d84 136
JMF 0:9d5134074d84 137 int mdm_init(void) {
fkellermavnet 14:0c353e212296 138 // Hard reset the modem (doesn't go through
fkellermavnet 14:0c353e212296 139 // the signal level translator)
fkellermavnet 14:0c353e212296 140 mdm_reset = 0;
JMF 17:38a8cc0c6ba5 141
JMF 17:38a8cc0c6ba5 142 // disable signal level translator (necessary
JMF 17:38a8cc0c6ba5 143 // for the modem to boot properly). All signals
JMF 17:38a8cc0c6ba5 144 // except mdm_reset go through the level translator
JMF 17:38a8cc0c6ba5 145 // and have internal pull-up/down in the module. While
JMF 17:38a8cc0c6ba5 146 // the level translator is disabled, these pins will
JMF 17:38a8cc0c6ba5 147 // be in the correct state.
JMF 17:38a8cc0c6ba5 148 shield_3v3_1v8_sig_trans_ena = 0;
JMF 17:38a8cc0c6ba5 149
JMF 17:38a8cc0c6ba5 150 // While the level translator is disabled and ouptut pins
JMF 17:38a8cc0c6ba5 151 // are tristated, make sure the inputs are in the same state
JMF 17:38a8cc0c6ba5 152 // as the WNC Module pins so that when the level translator is
JMF 17:38a8cc0c6ba5 153 // enabled, there are no differences.
JMF 17:38a8cc0c6ba5 154 mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high
JMF 17:38a8cc0c6ba5 155 mdm_power_on = 0; // powr_on should be low
JMF 17:38a8cc0c6ba5 156 mdm_wakeup_in = 1; // wake-up should be high
JMF 17:38a8cc0c6ba5 157 mdm_uart1_cts = 0; // indicate that it is ok to send
JMF 17:38a8cc0c6ba5 158
JMF 17:38a8cc0c6ba5 159 // Now, wait for the WNC Module to perform its initial boot correctly
fkellermavnet 14:0c353e212296 160 wait(1.0);
JMF 17:38a8cc0c6ba5 161
JMF 17:38a8cc0c6ba5 162 // The WNC module initializes comms at 115200 8N1 so set it up
JMF 17:38a8cc0c6ba5 163 mdm.baud(115200);
JMF 0:9d5134074d84 164
JMF 17:38a8cc0c6ba5 165 //Now, enable the level translator, the input pins should now be the
JMF 17:38a8cc0c6ba5 166 //same as how the M14A module is driving them with internal pull ups/downs.
JMF 17:38a8cc0c6ba5 167 //When enabled, there will be no changes in these 4 pins...
JMF 17:38a8cc0c6ba5 168 shield_3v3_1v8_sig_trans_ena = 1;
JMF 2:0e2ef866af95 169
JMF 17:38a8cc0c6ba5 170 // Now, give the modem 60 secons to start responding by
JMF 0:9d5134074d84 171 // sending simple 'AT' commands to modem once per second.
JMF 0:9d5134074d84 172 Timer timer;
JMF 0:9d5134074d84 173 timer.start();
JMF 0:9d5134074d84 174 while (timer.read() < 60) {
JMF 0:9d5134074d84 175 const char * rsp_lst[] = { ok_str, error_str, NULL };
JMF 0:9d5134074d84 176 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
JMF 0:9d5134074d84 177 if (rc == 0)
fkellermavnet 14:0c353e212296 178 return true; //timer.read();
JMF 0:9d5134074d84 179 wait_ms(1000 - (timer.read_ms() % 1000));
JMF 0:9d5134074d84 180 pc.printf("\r%d",timer.read_ms()/1000);
JMF 0:9d5134074d84 181 }
JMF 0:9d5134074d84 182 return false;
JMF 0:9d5134074d84 183 }
JMF 0:9d5134074d84 184
JMF 2:0e2ef866af95 185 int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) {
JMF 2:0e2ef866af95 186 static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars)
fkellermavnet 6:713b4cbf1a7d 187 size_t n = strlen(cmd);
fkellermavnet 6:713b4cbf1a7d 188 if (cmd && n > 0) {
JMF 2:0e2ef866af95 189 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 190 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 2:0e2ef866af95 191 }
fkellermavnet 6:713b4cbf1a7d 192 while (n--) {
fkellermavnet 6:713b4cbf1a7d 193 mdm.putc(*cmd++);
fkellermavnet 6:713b4cbf1a7d 194 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 195 };
fkellermavnet 6:713b4cbf1a7d 196 mdm.putc('\r');
fkellermavnet 6:713b4cbf1a7d 197 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 198 mdm.putc('\n');
fkellermavnet 6:713b4cbf1a7d 199 wait_ms(1);
JMF 2:0e2ef866af95 200 }
JMF 2:0e2ef866af95 201
JMF 2:0e2ef866af95 202 if (rsp_list) {
JMF 2:0e2ef866af95 203 rsp->erase(); // Clean up from prior cmd response
JMF 2:0e2ef866af95 204 *len = 0;
JMF 2:0e2ef866af95 205 Timer timer;
JMF 2:0e2ef866af95 206 timer.start();
JMF 2:0e2ef866af95 207 while (timer.read_ms() < timeout_ms) {
JMF 2:0e2ef866af95 208 int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms());
JMF 2:0e2ef866af95 209
JMF 2:0e2ef866af95 210 if (lenCmd == 0)
JMF 2:0e2ef866af95 211 continue;
JMF 2:0e2ef866af95 212
JMF 2:0e2ef866af95 213 if (lenCmd < 0)
JMF 2:0e2ef866af95 214 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 215 else {
JMF 2:0e2ef866af95 216 *len += lenCmd;
JMF 2:0e2ef866af95 217 *rsp += cmd_buf;
JMF 2:0e2ef866af95 218 }
JMF 2:0e2ef866af95 219
JMF 2:0e2ef866af95 220 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 221 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf);
JMF 2:0e2ef866af95 222 }
JMF 2:0e2ef866af95 223
JMF 2:0e2ef866af95 224 int rsp_idx = 0;
JMF 2:0e2ef866af95 225 while (rsp_list[rsp_idx]) {
JMF 2:0e2ef866af95 226 if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) {
JMF 2:0e2ef866af95 227 return rsp_idx;
JMF 2:0e2ef866af95 228 }
JMF 2:0e2ef866af95 229 rsp_idx++;
JMF 2:0e2ef866af95 230 }
JMF 2:0e2ef866af95 231 }
JMF 2:0e2ef866af95 232 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 233 }
JMF 2:0e2ef866af95 234 pc.printf("D %s",rsp);
JMF 2:0e2ef866af95 235 return MDM_OK;
JMF 2:0e2ef866af95 236 }
JMF 2:0e2ef866af95 237
JMF 2:0e2ef866af95 238 void reinitialize_mdm(void)
JMF 2:0e2ef866af95 239 {
JMF 2:0e2ef866af95 240 // Initialize the modem
JMF 2:0e2ef866af95 241 printf(GRN "Modem RE-initializing..." DEF "\r\n");
JMF 2:0e2ef866af95 242 if (!mdm_init()) {
JMF 2:0e2ef866af95 243 printf(RED "\n\rModem RE-initialization failed!" DEF "\n");
JMF 2:0e2ef866af95 244 }
JMF 2:0e2ef866af95 245 printf("\r\n");
JMF 2:0e2ef866af95 246 }
JMF 2:0e2ef866af95 247 // These are built on the fly
JMF 2:0e2ef866af95 248 string MyServerIpAddress;
JMF 2:0e2ef866af95 249 string MySocketData;
JMF 2:0e2ef866af95 250
JMF 2:0e2ef866af95 251 // These are to be built on the fly
JMF 2:0e2ef866af95 252 string my_temp;
JMF 2:0e2ef866af95 253 string my_humidity;
JMF 2:0e2ef866af95 254
JMF 0:9d5134074d84 255 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 256
stefanrousseau 3:26b3cc155f39 257 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 258 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 259 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 260 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 261 {
stefanrousseau 12:7c94ec5069dc 262 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 263 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 264 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 265 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 266 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 267 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 268 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 269 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 270 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 271 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 272 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 273 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 274 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 275 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 276 };
stefanrousseau 12:7c94ec5069dc 277
stefanrousseau 3:26b3cc155f39 278 void GenerateModemString(char * modem_string)
stefanrousseau 3:26b3cc155f39 279 {
stefanrousseau 12:7c94ec5069dc 280 switch(iSensorsToReport)
stefanrousseau 12:7c94ec5069dc 281 {
stefanrousseau 12:7c94ec5069dc 282 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 283 {
stefanrousseau 12:7c94ec5069dc 284 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 285 break;
stefanrousseau 12:7c94ec5069dc 286 }
stefanrousseau 12:7c94ec5069dc 287 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 288 {
stefanrousseau 12:7c94ec5069dc 289 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 290 break;
stefanrousseau 12:7c94ec5069dc 291 }
stefanrousseau 12:7c94ec5069dc 292 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 293 {
stefanrousseau 12:7c94ec5069dc 294 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 295 break;
stefanrousseau 12:7c94ec5069dc 296 }
stefanrousseau 12:7c94ec5069dc 297 default:
stefanrousseau 12:7c94ec5069dc 298 {
stefanrousseau 12:7c94ec5069dc 299 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 300 break;
stefanrousseau 12:7c94ec5069dc 301 }
stefanrousseau 16:17c5916f2d12 302 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 303 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 304
stefanrousseau 3:26b3cc155f39 305
stefanrousseau 3:26b3cc155f39 306 //Periodic timer
stefanrousseau 3:26b3cc155f39 307 Ticker OneMsTicker;
stefanrousseau 3:26b3cc155f39 308 volatile bool bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 309 int OneMsTicks = 0;
stefanrousseau 3:26b3cc155f39 310 int iTimer1Interval_ms = 1000;
stefanrousseau 3:26b3cc155f39 311 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 312 //* Periodic 1ms timer tick
stefanrousseau 3:26b3cc155f39 313 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 314 void OneMsFunction()
stefanrousseau 3:26b3cc155f39 315 {
stefanrousseau 3:26b3cc155f39 316 OneMsTicks++;
stefanrousseau 3:26b3cc155f39 317 if ((OneMsTicks % iTimer1Interval_ms) == 0)
stefanrousseau 3:26b3cc155f39 318 {
stefanrousseau 3:26b3cc155f39 319 bTimerExpiredFlag = true;
stefanrousseau 3:26b3cc155f39 320 }
stefanrousseau 3:26b3cc155f39 321 } //OneMsFunction()
stefanrousseau 3:26b3cc155f39 322
stefanrousseau 16:17c5916f2d12 323 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 324 //* Set the RGB LED's Color
stefanrousseau 16:17c5916f2d12 325 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
stefanrousseau 16:17c5916f2d12 326 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 327 void SetLedColor(unsigned char ucColor)
stefanrousseau 16:17c5916f2d12 328 {
stefanrousseau 16:17c5916f2d12 329 //Note that when an LED is on, you write a 0 to it:
stefanrousseau 16:17c5916f2d12 330 led_red = !(ucColor & 0x1); //bit 0
stefanrousseau 16:17c5916f2d12 331 led_green = !(ucColor & 0x2); //bit 1
stefanrousseau 16:17c5916f2d12 332 led_blue = !(ucColor & 0x4); //bit 2
stefanrousseau 16:17c5916f2d12 333 } //SetLedColor()
stefanrousseau 16:17c5916f2d12 334
stefanrousseau 16:17c5916f2d12 335 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 336 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 337 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 338 bool extract_JSON(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 339 {
stefanrousseau 16:17c5916f2d12 340 char* beginquote;
stefanrousseau 16:17c5916f2d12 341 char* endquote;
stefanrousseau 16:17c5916f2d12 342 beginquote = strchr(search_field, '{'); //start of JSON
stefanrousseau 16:17c5916f2d12 343 endquote = strchr(search_field, '}'); //end of JSON
stefanrousseau 16:17c5916f2d12 344 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 345 {
stefanrousseau 16:17c5916f2d12 346 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 347 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 348 {
stefanrousseau 16:17c5916f2d12 349 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 350 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 351 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 352 return true;
stefanrousseau 16:17c5916f2d12 353 }
stefanrousseau 16:17c5916f2d12 354 else
stefanrousseau 16:17c5916f2d12 355 {
stefanrousseau 16:17c5916f2d12 356 endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing
stefanrousseau 16:17c5916f2d12 357 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 358 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 359 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 360 return false;
stefanrousseau 16:17c5916f2d12 361 }
stefanrousseau 16:17c5916f2d12 362 }
stefanrousseau 16:17c5916f2d12 363 else
stefanrousseau 16:17c5916f2d12 364 {
stefanrousseau 16:17c5916f2d12 365 return false;
stefanrousseau 16:17c5916f2d12 366 }
stefanrousseau 16:17c5916f2d12 367 } //extract_JSON
stefanrousseau 16:17c5916f2d12 368
stefanrousseau 16:17c5916f2d12 369 bool parse_JSON(char* json_string)
stefanrousseau 16:17c5916f2d12 370 {
stefanrousseau 16:17c5916f2d12 371 char* beginquote;
stefanrousseau 16:17c5916f2d12 372 char token[] = "\"LED\":\"";
stefanrousseau 16:17c5916f2d12 373 beginquote = strstr(json_string, token );
stefanrousseau 16:17c5916f2d12 374 if ((beginquote != 0))
stefanrousseau 16:17c5916f2d12 375 {
stefanrousseau 16:17c5916f2d12 376 char cLedColor = beginquote[strlen(token)];
stefanrousseau 16:17c5916f2d12 377 printf(GRN "LED Found : %c" DEF "\r\n", cLedColor);
stefanrousseau 16:17c5916f2d12 378 switch(cLedColor)
stefanrousseau 16:17c5916f2d12 379 {
stefanrousseau 16:17c5916f2d12 380 case 'O':
stefanrousseau 16:17c5916f2d12 381 { //Off
stefanrousseau 16:17c5916f2d12 382 SetLedColor(0);
stefanrousseau 16:17c5916f2d12 383 break;
stefanrousseau 16:17c5916f2d12 384 }
stefanrousseau 16:17c5916f2d12 385 case 'R':
stefanrousseau 16:17c5916f2d12 386 { //Red
stefanrousseau 16:17c5916f2d12 387 SetLedColor(1);
stefanrousseau 16:17c5916f2d12 388 break;
stefanrousseau 16:17c5916f2d12 389 }
stefanrousseau 16:17c5916f2d12 390 case 'G':
stefanrousseau 16:17c5916f2d12 391 { //Green
stefanrousseau 16:17c5916f2d12 392 SetLedColor(2);
stefanrousseau 16:17c5916f2d12 393 break;
stefanrousseau 16:17c5916f2d12 394 }
stefanrousseau 16:17c5916f2d12 395 case 'Y':
stefanrousseau 16:17c5916f2d12 396 { //Yellow
stefanrousseau 16:17c5916f2d12 397 SetLedColor(3);
stefanrousseau 16:17c5916f2d12 398 break;
stefanrousseau 16:17c5916f2d12 399 }
stefanrousseau 16:17c5916f2d12 400 case 'B':
stefanrousseau 16:17c5916f2d12 401 { //Blue
stefanrousseau 16:17c5916f2d12 402 SetLedColor(4);
stefanrousseau 16:17c5916f2d12 403 break;
stefanrousseau 16:17c5916f2d12 404 }
stefanrousseau 16:17c5916f2d12 405 case 'M':
stefanrousseau 16:17c5916f2d12 406 { //Magenta
stefanrousseau 16:17c5916f2d12 407 SetLedColor(5);
stefanrousseau 16:17c5916f2d12 408 break;
stefanrousseau 16:17c5916f2d12 409 }
stefanrousseau 16:17c5916f2d12 410 case 'T':
stefanrousseau 16:17c5916f2d12 411 { //Turquoise
stefanrousseau 16:17c5916f2d12 412 SetLedColor(6);
stefanrousseau 16:17c5916f2d12 413 break;
stefanrousseau 16:17c5916f2d12 414 }
stefanrousseau 16:17c5916f2d12 415 case 'W':
stefanrousseau 16:17c5916f2d12 416 { //White
stefanrousseau 16:17c5916f2d12 417 SetLedColor(7);
stefanrousseau 16:17c5916f2d12 418 break;
stefanrousseau 16:17c5916f2d12 419 }
stefanrousseau 16:17c5916f2d12 420 default:
stefanrousseau 16:17c5916f2d12 421 {
stefanrousseau 16:17c5916f2d12 422 break;
stefanrousseau 16:17c5916f2d12 423 }
stefanrousseau 16:17c5916f2d12 424 } //switch(cLedColor)
stefanrousseau 16:17c5916f2d12 425 return true;
stefanrousseau 16:17c5916f2d12 426 }
stefanrousseau 16:17c5916f2d12 427 else
stefanrousseau 16:17c5916f2d12 428 {
stefanrousseau 16:17c5916f2d12 429 return false;
stefanrousseau 16:17c5916f2d12 430 }
stefanrousseau 16:17c5916f2d12 431 } //parse_JSON
stefanrousseau 16:17c5916f2d12 432
JMF 0:9d5134074d84 433 int main() {
JMF 2:0e2ef866af95 434 int i;
JMF 0:9d5134074d84 435 HTS221 hts221;
JMF 0:9d5134074d84 436 pc.baud(115200);
JMF 0:9d5134074d84 437
JMF 0:9d5134074d84 438 void hts221_init(void);
JMF 0:9d5134074d84 439
JMF 1:af7a42f7d465 440 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 441 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 442
JMF 0:9d5134074d84 443 i = hts221.begin();
JMF 0:9d5134074d84 444 if( i )
JMF 0:9d5134074d84 445 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 446 else
JMF 0:9d5134074d84 447 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 448
JMF 0:9d5134074d84 449 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 450 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
JMF 0:9d5134074d84 451
stefanrousseau 16:17c5916f2d12 452 SetLedColor(0); //Off
stefanrousseau 11:e6602513730f 453 sensors_init();
stefanrousseau 12:7c94ec5069dc 454 read_sensors();
stefanrousseau 11:e6602513730f 455
JMF 0:9d5134074d84 456 // Initialize the modem
JMF 0:9d5134074d84 457 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 458 do {
fkellermavnet 14:0c353e212296 459 i=mdm_init();
fkellermavnet 14:0c353e212296 460 if (!i) {
fkellermavnet 14:0c353e212296 461 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 462 }
fkellermavnet 14:0c353e212296 463 } while (!i);
JMF 0:9d5134074d84 464
JMF 2:0e2ef866af95 465 //Software init
JMF 2:0e2ef866af95 466 software_init_mdm();
JMF 2:0e2ef866af95 467
JMF 2:0e2ef866af95 468 // Resolve URL to IP address to connect to
JMF 2:0e2ef866af95 469 resolve_mdm();
JMF 0:9d5134074d84 470
stefanrousseau 3:26b3cc155f39 471 //Create a 1ms timer tick function:
stefanrousseau 3:26b3cc155f39 472 OneMsTicker.attach(OneMsFunction, 0.001f) ;
stefanrousseau 3:26b3cc155f39 473 iTimer1Interval_ms = 5000; //5 seconds
stefanrousseau 3:26b3cc155f39 474
JMF 2:0e2ef866af95 475 // Send and receive data perpetually
JMF 2:0e2ef866af95 476 while(1) {
stefanrousseau 3:26b3cc155f39 477 if (bTimerExpiredFlag)
stefanrousseau 3:26b3cc155f39 478 {
stefanrousseau 3:26b3cc155f39 479 bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 480 sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
stefanrousseau 3:26b3cc155f39 481 sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
stefanrousseau 4:f83bedd9cab4 482 read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
stefanrousseau 3:26b3cc155f39 483 sockopen_mdm();
stefanrousseau 3:26b3cc155f39 484 char modem_string[512];
stefanrousseau 3:26b3cc155f39 485 GenerateModemString(&modem_string[0]);
stefanrousseau 16:17c5916f2d12 486 printf(BLU "Sending to modem : %s" DEF "\n", modem_string);
stefanrousseau 3:26b3cc155f39 487 sockwrite_mdm(modem_string);
stefanrousseau 3:26b3cc155f39 488 sockread_mdm(&MySocketData, 1024, 20);
stefanrousseau 16:17c5916f2d12 489 printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]);
stefanrousseau 16:17c5916f2d12 490 char * myJsonResponse;
stefanrousseau 16:17c5916f2d12 491 if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
stefanrousseau 16:17c5916f2d12 492 {
stefanrousseau 16:17c5916f2d12 493 printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]);
stefanrousseau 16:17c5916f2d12 494 parse_JSON(&myJsonResponse[0]);
stefanrousseau 16:17c5916f2d12 495 }
stefanrousseau 16:17c5916f2d12 496 else
stefanrousseau 16:17c5916f2d12 497 {
stefanrousseau 16:17c5916f2d12 498 printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string
stefanrousseau 16:17c5916f2d12 499 parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
stefanrousseau 16:17c5916f2d12 500 }
stefanrousseau 3:26b3cc155f39 501 sockclose_mdm();
stefanrousseau 3:26b3cc155f39 502 } //bTimerExpiredFlag
stefanrousseau 3:26b3cc155f39 503 } //forever loop
JMF 2:0e2ef866af95 504
stefanrousseau 12:7c94ec5069dc 505 #if (0)
JMF 2:0e2ef866af95 506 string * pStr;
JMF 2:0e2ef866af95 507 while (1)
JMF 2:0e2ef866af95 508 {
JMF 2:0e2ef866af95 509 send_wnc_cmd("AT", &pStr, WNC_TIMEOUT_MS);
JMF 0:9d5134074d84 510 }
stefanrousseau 12:7c94ec5069dc 511 #endif
JMF 0:9d5134074d84 512 }