This program simply connects to a HTS221 I2C device to proximity sensor

Dependencies:   FXOS8700CQ mbed

Committer:
elmkom
Date:
Thu Sep 29 14:19:53 2016 +0000
Revision:
40:45e4636d20cc
Parent:
39:3bbb3dbb531b
Child:
41:85a736a9b900
Remove read delays

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"
elmkom 38:532a0d929756 9 #include "Proximity.h"
JMF 0:9d5134074d84 10
stefanrousseau 11:e6602513730f 11 #include "hardware.h"
stefanrousseau 11:e6602513730f 12 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
elmkom 38:532a0d929756 13 Proximity proximityStrip;
elmkom 36:f8d96ff1dd1b 14
JMF 0:9d5134074d84 15 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 16 #define USE_COLOR
JMF 0:9d5134074d84 17 #ifdef USE_COLOR
JMF 0:9d5134074d84 18 #define BLK "\033[30m"
JMF 0:9d5134074d84 19 #define RED "\033[31m"
JMF 0:9d5134074d84 20 #define GRN "\033[32m"
JMF 0:9d5134074d84 21 #define YEL "\033[33m"
JMF 0:9d5134074d84 22 #define BLU "\033[34m"
JMF 0:9d5134074d84 23 #define MAG "\033[35m"
JMF 0:9d5134074d84 24 #define CYN "\033[36m"
JMF 0:9d5134074d84 25 #define WHT "\033[37m"
JMF 0:9d5134074d84 26 #define DEF "\033[39m"
JMF 0:9d5134074d84 27 #else
JMF 0:9d5134074d84 28 #define BLK
JMF 0:9d5134074d84 29 #define RED
JMF 0:9d5134074d84 30 #define GRN
JMF 0:9d5134074d84 31 #define YEL
JMF 0:9d5134074d84 32 #define BLU
JMF 0:9d5134074d84 33 #define MAG
JMF 0:9d5134074d84 34 #define CYN
JMF 0:9d5134074d84 35 #define WHT
JMF 0:9d5134074d84 36 #define DEF
JMF 0:9d5134074d84 37 #endif
JMF 0:9d5134074d84 38
JMF 0:9d5134074d84 39 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 40 #define MDM_DBG_AT_CMDS (1 << 0)
elmkom 36:f8d96ff1dd1b 41
elmkom 36:f8d96ff1dd1b 42 #define MUXADDRESS 0x70
elmkom 36:f8d96ff1dd1b 43 #define PROXIMITYADDRESS 0x39
elmkom 36:f8d96ff1dd1b 44
JMF 0:9d5134074d84 45 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 46
JMF 0:9d5134074d84 47 Serial pc(USBTX, USBRX);
elmkom 40:45e4636d20cc 48 SerialBuffered mdm(PTD3, PTD2, 512);
stefanrousseau 16:17c5916f2d12 49 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 50 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 51 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 52
JMF 0:9d5134074d84 53 DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot
JMF 0:9d5134074d84 54 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 55 DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield
JMF 0:9d5134074d84 56
fkellermavnet 14:0c353e212296 57 DigitalOut mdm_reset(PTC12); // active high
fkellermavnet 14:0c353e212296 58
JMF 0:9d5134074d84 59 DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
JMF 0:9d5134074d84 60 DigitalOut mdm_uart1_cts(PTD0);
JMF 0:9d5134074d84 61
JMF 0:9d5134074d84 62 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 63
JMF 0:9d5134074d84 64 const char ok_str[] = "OK";
JMF 0:9d5134074d84 65 const char error_str[] = "ERROR";
JMF 0:9d5134074d84 66
JMF 0:9d5134074d84 67 #define MDM_OK 0
JMF 0:9d5134074d84 68 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 69
JMF 0:9d5134074d84 70 #define MAX_AT_RSP_LEN 255
elmkom 38:532a0d929756 71
elmkom 36:f8d96ff1dd1b 72 bool proximityChange = false;
elmkom 38:532a0d929756 73 bool powerSave = false;
elmkom 36:f8d96ff1dd1b 74
elmkom 36:f8d96ff1dd1b 75
elmkom 38:532a0d929756 76
elmkom 35:2e864bae3af0 77
elmkom 35:2e864bae3af0 78
elmkom 36:f8d96ff1dd1b 79
elmkom 36:f8d96ff1dd1b 80
elmkom 36:f8d96ff1dd1b 81
elmkom 36:f8d96ff1dd1b 82 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 83 //* Set the RGB LED's Color
elmkom 36:f8d96ff1dd1b 84 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
elmkom 36:f8d96ff1dd1b 85 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 86 void SetLedColor(unsigned char ucColor)
elmkom 35:2e864bae3af0 87 {
elmkom 38:532a0d929756 88 if(powerSave)
elmkom 38:532a0d929756 89 {
elmkom 38:532a0d929756 90 led_red = !0;
elmkom 38:532a0d929756 91 led_green = !0;
elmkom 38:532a0d929756 92 led_blue = !0;
elmkom 38:532a0d929756 93 }
elmkom 38:532a0d929756 94 else
elmkom 38:532a0d929756 95 {
elmkom 38:532a0d929756 96 //Note that when an LED is on, you write a 0 to it:
elmkom 38:532a0d929756 97 led_red = !(ucColor & 0x1); //bit 0
elmkom 38:532a0d929756 98 led_green = !(ucColor & 0x2); //bit 1
elmkom 38:532a0d929756 99 led_blue = !(ucColor & 0x4); //bit 2
elmkom 38:532a0d929756 100 }
elmkom 36:f8d96ff1dd1b 101 } //SetLedColor()
elmkom 35:2e864bae3af0 102
JMF 0:9d5134074d84 103 ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) {
JMF 0:9d5134074d84 104 int cin = -1;
JMF 0:9d5134074d84 105 int cin_last;
JMF 0:9d5134074d84 106
JMF 0:9d5134074d84 107 if (NULL == buff || size == 0) {
JMF 0:9d5134074d84 108 return -1;
JMF 0:9d5134074d84 109 }
JMF 0:9d5134074d84 110
JMF 0:9d5134074d84 111 size_t len = 0;
JMF 0:9d5134074d84 112 Timer timer;
JMF 0:9d5134074d84 113 timer.start();
JMF 0:9d5134074d84 114 while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) {
JMF 0:9d5134074d84 115 if (mdm.readable()) {
JMF 0:9d5134074d84 116 cin_last = cin;
JMF 0:9d5134074d84 117 cin = mdm.getc();
JMF 0:9d5134074d84 118 if (isprint(cin)) {
JMF 0:9d5134074d84 119 buff[len++] = (char)cin;
elmkom 37:ee01f752524a 120
JMF 0:9d5134074d84 121 continue;
JMF 0:9d5134074d84 122 } else if (('\r' == cin_last) && ('\n' == cin)) {
JMF 0:9d5134074d84 123 break;
JMF 0:9d5134074d84 124 }
JMF 0:9d5134074d84 125 }
elmkom 40:45e4636d20cc 126 //wait_ms(1);
JMF 0:9d5134074d84 127 }
JMF 2:0e2ef866af95 128 buff[len] = (char)NULL;
JMF 0:9d5134074d84 129
JMF 0:9d5134074d84 130 return len;
JMF 0:9d5134074d84 131 }
JMF 0:9d5134074d84 132
JMF 0:9d5134074d84 133 int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) {
JMF 0:9d5134074d84 134 if (cmd && strlen(cmd) > 0) {
JMF 0:9d5134074d84 135 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 136 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 0:9d5134074d84 137 }
JMF 0:9d5134074d84 138 mdm.printf("%s\r\n", cmd);
JMF 0:9d5134074d84 139 }
JMF 0:9d5134074d84 140
JMF 0:9d5134074d84 141 if (rsp_list) {
JMF 0:9d5134074d84 142 Timer timer;
JMF 0:9d5134074d84 143 char rsp[MAX_AT_RSP_LEN+1];
JMF 0:9d5134074d84 144 int len;
JMF 0:9d5134074d84 145
JMF 0:9d5134074d84 146 timer.start();
JMF 0:9d5134074d84 147 while (timer.read_ms() < timeout_ms) {
JMF 0:9d5134074d84 148 len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms());
JMF 0:9d5134074d84 149
JMF 0:9d5134074d84 150 if (len < 0)
JMF 0:9d5134074d84 151 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 152
JMF 0:9d5134074d84 153 if (len == 0)
JMF 0:9d5134074d84 154 continue;
JMF 0:9d5134074d84 155
JMF 0:9d5134074d84 156 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 157 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp);
JMF 0:9d5134074d84 158 }
JMF 0:9d5134074d84 159
JMF 0:9d5134074d84 160 if (rsp_list) {
JMF 0:9d5134074d84 161 int rsp_idx = 0;
JMF 0:9d5134074d84 162 while (rsp_list[rsp_idx]) {
JMF 0:9d5134074d84 163 if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) {
JMF 0:9d5134074d84 164 return rsp_idx;
JMF 0:9d5134074d84 165 }
JMF 0:9d5134074d84 166 rsp_idx++;
JMF 0:9d5134074d84 167 }
JMF 0:9d5134074d84 168 }
JMF 0:9d5134074d84 169 }
JMF 0:9d5134074d84 170 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 171 }
JMF 0:9d5134074d84 172 return MDM_OK;
JMF 0:9d5134074d84 173 }
elmkom 35:2e864bae3af0 174 int mdm_init(void) {
elmkom 35:2e864bae3af0 175 // disable signal level translator (necessary
elmkom 35:2e864bae3af0 176 // for the modem to boot properly)
elmkom 35:2e864bae3af0 177 shield_3v3_1v8_sig_trans_ena = 0;
JMF 0:9d5134074d84 178
elmkom 35:2e864bae3af0 179 // Hard reset the modem (doesn't go through
elmkom 35:2e864bae3af0 180 // the signal level translator)
elmkom 35:2e864bae3af0 181 mdm_reset = 1;
elmkom 35:2e864bae3af0 182
elmkom 35:2e864bae3af0 183 // wait a moment for the modem to react
elmkom 35:2e864bae3af0 184 wait_ms(10);
elmkom 35:2e864bae3af0 185
elmkom 35:2e864bae3af0 186 // Let modem boot
elmkom 35:2e864bae3af0 187 mdm_reset = 0;
elmkom 35:2e864bae3af0 188
elmkom 35:2e864bae3af0 189 // wait a moment for the modem to react
elmkom 35:2e864bae3af0 190 wait(1.0);
elmkom 35:2e864bae3af0 191
elmkom 35:2e864bae3af0 192 // power modem on //off
elmkom 35:2e864bae3af0 193 mdm_power_on = 0; //1;
elmkom 35:2e864bae3af0 194
elmkom 35:2e864bae3af0 195 // insure modem boots into normal operating mode
elmkom 35:2e864bae3af0 196 // and does not go to sleep when powered on
elmkom 35:2e864bae3af0 197 mdm_uart2_rx_boot_mode_sel = 1;
elmkom 35:2e864bae3af0 198 mdm_wakeup_in = 1;
elmkom 35:2e864bae3af0 199
elmkom 35:2e864bae3af0 200 // initialze comm with the modem
elmkom 35:2e864bae3af0 201 mdm.baud(115200);
elmkom 35:2e864bae3af0 202 // clear out potential garbage
elmkom 35:2e864bae3af0 203 while (mdm.readable())
elmkom 35:2e864bae3af0 204 mdm.getc();
elmkom 35:2e864bae3af0 205
elmkom 35:2e864bae3af0 206 mdm_uart1_cts = 0;
elmkom 35:2e864bae3af0 207
elmkom 35:2e864bae3af0 208 // wait a moment for the modem to react to signal
elmkom 35:2e864bae3af0 209 // conditions while the level translator is disabled
elmkom 35:2e864bae3af0 210 // (sorry, don't have enough information to know
elmkom 35:2e864bae3af0 211 // what exactly the modem is doing with the current
elmkom 35:2e864bae3af0 212 // pin settings)
elmkom 35:2e864bae3af0 213 wait(1.0);
elmkom 35:2e864bae3af0 214
elmkom 35:2e864bae3af0 215 // enable the signal level translator to start
elmkom 35:2e864bae3af0 216 // modem reset process (modem will be powered down)
elmkom 35:2e864bae3af0 217 shield_3v3_1v8_sig_trans_ena = 1;
elmkom 35:2e864bae3af0 218
elmkom 35:2e864bae3af0 219 // Give the modem 60 secons to start responding by
elmkom 35:2e864bae3af0 220 // sending simple 'AT' commands to modem once per second.
elmkom 35:2e864bae3af0 221 Timer timer;
elmkom 35:2e864bae3af0 222 timer.start();
elmkom 35:2e864bae3af0 223 while (timer.read() < 60) {
elmkom 36:f8d96ff1dd1b 224 SetLedColor(0x1); //red
elmkom 35:2e864bae3af0 225 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 35:2e864bae3af0 226 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 35:2e864bae3af0 227 if (rc == 0)
elmkom 35:2e864bae3af0 228 return true; //timer.read();
elmkom 36:f8d96ff1dd1b 229 SetLedColor(0); //off
elmkom 36:f8d96ff1dd1b 230 wait_ms(1000 - (timer.read_ms() % 1000));
elmkom 36:f8d96ff1dd1b 231 pc.printf("\r%d",timer.read_ms()/1000);
elmkom 36:f8d96ff1dd1b 232
elmkom 36:f8d96ff1dd1b 233 }
elmkom 36:f8d96ff1dd1b 234 return false;
elmkom 36:f8d96ff1dd1b 235 }
elmkom 36:f8d96ff1dd1b 236
elmkom 36:f8d96ff1dd1b 237 bool oldwakeModem()
elmkom 36:f8d96ff1dd1b 238 {
elmkom 36:f8d96ff1dd1b 239 Timer timer;
elmkom 36:f8d96ff1dd1b 240 timer.start();
elmkom 36:f8d96ff1dd1b 241 while (timer.read() < 60) {
elmkom 36:f8d96ff1dd1b 242 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 36:f8d96ff1dd1b 243 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 36:f8d96ff1dd1b 244 if (rc == 0)
elmkom 36:f8d96ff1dd1b 245 return true;
elmkom 35:2e864bae3af0 246 wait_ms(1000 - (timer.read_ms() % 1000));
elmkom 35:2e864bae3af0 247 pc.printf("\r%d",timer.read_ms()/1000);
elmkom 35:2e864bae3af0 248 }
elmkom 36:f8d96ff1dd1b 249 return false;
elmkom 35:2e864bae3af0 250 }
elmkom 38:532a0d929756 251 /*
elmkom 36:f8d96ff1dd1b 252 bool wakeModem()
elmkom 36:f8d96ff1dd1b 253 {
elmkom 36:f8d96ff1dd1b 254 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 36:f8d96ff1dd1b 255 int tries = 60;
elmkom 36:f8d96ff1dd1b 256 pc.printf("wake ");
elmkom 36:f8d96ff1dd1b 257 while (tries > 0) {
elmkom 36:f8d96ff1dd1b 258 tries--;
elmkom 36:f8d96ff1dd1b 259 pc.printf(".");
elmkom 36:f8d96ff1dd1b 260 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 36:f8d96ff1dd1b 261 if (rc == 0)
elmkom 36:f8d96ff1dd1b 262 {
elmkom 36:f8d96ff1dd1b 263 pc.printf("\r\n");
elmkom 36:f8d96ff1dd1b 264 return true;
elmkom 36:f8d96ff1dd1b 265 }
elmkom 36:f8d96ff1dd1b 266 wait(1.0);
elmkom 36:f8d96ff1dd1b 267 }
elmkom 36:f8d96ff1dd1b 268 return false;
elmkom 36:f8d96ff1dd1b 269 }
elmkom 38:532a0d929756 270 bool rebootModem()
elmkom 38:532a0d929756 271 {
elmkom 38:532a0d929756 272 mdm.printf("ATZ\r\n");
elmkom 38:532a0d929756 273 return wakeModem();
elmkom 38:532a0d929756 274 }
elmkom 38:532a0d929756 275 */
elmkom 35:2e864bae3af0 276 int oldmdm_init(void) {
fkellermavnet 14:0c353e212296 277 // Hard reset the modem (doesn't go through
fkellermavnet 14:0c353e212296 278 // the signal level translator)
fkellermavnet 14:0c353e212296 279 mdm_reset = 0;
JMF 17:38a8cc0c6ba5 280
JMF 17:38a8cc0c6ba5 281 // disable signal level translator (necessary
JMF 17:38a8cc0c6ba5 282 // for the modem to boot properly). All signals
JMF 17:38a8cc0c6ba5 283 // except mdm_reset go through the level translator
JMF 17:38a8cc0c6ba5 284 // and have internal pull-up/down in the module. While
JMF 17:38a8cc0c6ba5 285 // the level translator is disabled, these pins will
JMF 17:38a8cc0c6ba5 286 // be in the correct state.
JMF 17:38a8cc0c6ba5 287 shield_3v3_1v8_sig_trans_ena = 0;
JMF 17:38a8cc0c6ba5 288
JMF 17:38a8cc0c6ba5 289 // While the level translator is disabled and ouptut pins
JMF 17:38a8cc0c6ba5 290 // are tristated, make sure the inputs are in the same state
JMF 17:38a8cc0c6ba5 291 // as the WNC Module pins so that when the level translator is
JMF 17:38a8cc0c6ba5 292 // enabled, there are no differences.
JMF 17:38a8cc0c6ba5 293 mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high
JMF 17:38a8cc0c6ba5 294 mdm_power_on = 0; // powr_on should be low
JMF 17:38a8cc0c6ba5 295 mdm_wakeup_in = 1; // wake-up should be high
JMF 17:38a8cc0c6ba5 296 mdm_uart1_cts = 0; // indicate that it is ok to send
JMF 17:38a8cc0c6ba5 297
JMF 17:38a8cc0c6ba5 298 // Now, wait for the WNC Module to perform its initial boot correctly
fkellermavnet 14:0c353e212296 299 wait(1.0);
JMF 17:38a8cc0c6ba5 300
JMF 17:38a8cc0c6ba5 301 // The WNC module initializes comms at 115200 8N1 so set it up
JMF 17:38a8cc0c6ba5 302 mdm.baud(115200);
JMF 0:9d5134074d84 303
JMF 17:38a8cc0c6ba5 304 //Now, enable the level translator, the input pins should now be the
JMF 17:38a8cc0c6ba5 305 //same as how the M14A module is driving them with internal pull ups/downs.
JMF 17:38a8cc0c6ba5 306 //When enabled, there will be no changes in these 4 pins...
JMF 17:38a8cc0c6ba5 307 shield_3v3_1v8_sig_trans_ena = 1;
JMF 2:0e2ef866af95 308
JMF 17:38a8cc0c6ba5 309 // Now, give the modem 60 secons to start responding by
JMF 0:9d5134074d84 310 // sending simple 'AT' commands to modem once per second.
JMF 0:9d5134074d84 311 Timer timer;
JMF 0:9d5134074d84 312 timer.start();
JMF 0:9d5134074d84 313 while (timer.read() < 60) {
JMF 0:9d5134074d84 314 const char * rsp_lst[] = { ok_str, error_str, NULL };
JMF 0:9d5134074d84 315 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
JMF 0:9d5134074d84 316 if (rc == 0)
fkellermavnet 14:0c353e212296 317 return true; //timer.read();
JMF 0:9d5134074d84 318 wait_ms(1000 - (timer.read_ms() % 1000));
JMF 0:9d5134074d84 319 pc.printf("\r%d",timer.read_ms()/1000);
JMF 0:9d5134074d84 320 }
JMF 0:9d5134074d84 321 return false;
JMF 0:9d5134074d84 322 }
JMF 0:9d5134074d84 323
JMF 2:0e2ef866af95 324 int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) {
JMF 2:0e2ef866af95 325 static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars)
fkellermavnet 6:713b4cbf1a7d 326 size_t n = strlen(cmd);
elmkom 40:45e4636d20cc 327
elmkom 40:45e4636d20cc 328
elmkom 40:45e4636d20cc 329 if (cmd && n > 0) {
elmkom 40:45e4636d20cc 330 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
elmkom 40:45e4636d20cc 331 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
elmkom 40:45e4636d20cc 332 }
elmkom 40:45e4636d20cc 333 while (n--) {
elmkom 40:45e4636d20cc 334 // pc.printf("%c",*cmd);
elmkom 40:45e4636d20cc 335 mdm.putc(*cmd++);
elmkom 40:45e4636d20cc 336
elmkom 40:45e4636d20cc 337 //wait_ms(1);
elmkom 40:45e4636d20cc 338
elmkom 40:45e4636d20cc 339 };
elmkom 40:45e4636d20cc 340 mdm.putc('\r');
elmkom 40:45e4636d20cc 341
elmkom 40:45e4636d20cc 342 // wait_ms(1);
elmkom 40:45e4636d20cc 343 mdm.putc('\n');
elmkom 40:45e4636d20cc 344 // wait_ms(1);
elmkom 40:45e4636d20cc 345 }
elmkom 40:45e4636d20cc 346
elmkom 40:45e4636d20cc 347 if (rsp_list) {
elmkom 40:45e4636d20cc 348 rsp->erase(); // Clean up from prior cmd response
elmkom 40:45e4636d20cc 349 *len = 0;
elmkom 40:45e4636d20cc 350 Timer timer;
elmkom 40:45e4636d20cc 351 timer.start();
elmkom 40:45e4636d20cc 352 while (timer.read_ms() < timeout_ms) {
elmkom 40:45e4636d20cc 353 int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms());
elmkom 40:45e4636d20cc 354
elmkom 40:45e4636d20cc 355 if (lenCmd == 0)
elmkom 40:45e4636d20cc 356 continue;
elmkom 40:45e4636d20cc 357
elmkom 40:45e4636d20cc 358 if (lenCmd < 0)
elmkom 40:45e4636d20cc 359 return MDM_ERR_TIMEOUT;
elmkom 40:45e4636d20cc 360 else {
elmkom 40:45e4636d20cc 361 *len += lenCmd;
elmkom 40:45e4636d20cc 362 *rsp += cmd_buf;
elmkom 40:45e4636d20cc 363 }
elmkom 40:45e4636d20cc 364
elmkom 40:45e4636d20cc 365 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
elmkom 40:45e4636d20cc 366 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf);
elmkom 40:45e4636d20cc 367 }
elmkom 40:45e4636d20cc 368
elmkom 40:45e4636d20cc 369 int rsp_idx = 0;
elmkom 40:45e4636d20cc 370 while (rsp_list[rsp_idx]) {
elmkom 40:45e4636d20cc 371 if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) {
elmkom 40:45e4636d20cc 372 return rsp_idx;
elmkom 40:45e4636d20cc 373 }
elmkom 40:45e4636d20cc 374 rsp_idx++;
elmkom 40:45e4636d20cc 375 }
elmkom 40:45e4636d20cc 376 }
elmkom 40:45e4636d20cc 377 return MDM_ERR_TIMEOUT;
elmkom 40:45e4636d20cc 378 }
elmkom 40:45e4636d20cc 379 pc.printf("D %s",rsp);
elmkom 40:45e4636d20cc 380 return MDM_OK;
elmkom 40:45e4636d20cc 381 }
elmkom 40:45e4636d20cc 382
elmkom 40:45e4636d20cc 383 int oldmdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) {
elmkom 40:45e4636d20cc 384 static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars)
elmkom 40:45e4636d20cc 385 size_t n = strlen(cmd);
fkellermavnet 6:713b4cbf1a7d 386 if (cmd && n > 0) {
JMF 2:0e2ef866af95 387 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 388 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 2:0e2ef866af95 389 }
fkellermavnet 6:713b4cbf1a7d 390 while (n--) {
fkellermavnet 6:713b4cbf1a7d 391 mdm.putc(*cmd++);
fkellermavnet 6:713b4cbf1a7d 392 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 393 };
fkellermavnet 6:713b4cbf1a7d 394 mdm.putc('\r');
fkellermavnet 6:713b4cbf1a7d 395 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 396 mdm.putc('\n');
fkellermavnet 6:713b4cbf1a7d 397 wait_ms(1);
JMF 2:0e2ef866af95 398 }
JMF 2:0e2ef866af95 399
JMF 2:0e2ef866af95 400 if (rsp_list) {
JMF 2:0e2ef866af95 401 rsp->erase(); // Clean up from prior cmd response
JMF 2:0e2ef866af95 402 *len = 0;
JMF 2:0e2ef866af95 403 Timer timer;
JMF 2:0e2ef866af95 404 timer.start();
JMF 2:0e2ef866af95 405 while (timer.read_ms() < timeout_ms) {
JMF 2:0e2ef866af95 406 int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms());
JMF 2:0e2ef866af95 407
JMF 2:0e2ef866af95 408 if (lenCmd == 0)
JMF 2:0e2ef866af95 409 continue;
JMF 2:0e2ef866af95 410
JMF 2:0e2ef866af95 411 if (lenCmd < 0)
JMF 2:0e2ef866af95 412 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 413 else {
JMF 2:0e2ef866af95 414 *len += lenCmd;
JMF 2:0e2ef866af95 415 *rsp += cmd_buf;
JMF 2:0e2ef866af95 416 }
JMF 2:0e2ef866af95 417
JMF 2:0e2ef866af95 418 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 419 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf);
JMF 2:0e2ef866af95 420 }
JMF 2:0e2ef866af95 421
JMF 2:0e2ef866af95 422 int rsp_idx = 0;
JMF 2:0e2ef866af95 423 while (rsp_list[rsp_idx]) {
JMF 2:0e2ef866af95 424 if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) {
JMF 2:0e2ef866af95 425 return rsp_idx;
JMF 2:0e2ef866af95 426 }
JMF 2:0e2ef866af95 427 rsp_idx++;
JMF 2:0e2ef866af95 428 }
JMF 2:0e2ef866af95 429 }
JMF 2:0e2ef866af95 430 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 431 }
JMF 2:0e2ef866af95 432 pc.printf("D %s",rsp);
JMF 2:0e2ef866af95 433 return MDM_OK;
JMF 2:0e2ef866af95 434 }
elmkom 36:f8d96ff1dd1b 435 void system_reset()
elmkom 36:f8d96ff1dd1b 436 {
elmkom 36:f8d96ff1dd1b 437 printf(RED "\n\rSystem resetting..." DEF "\n");
elmkom 36:f8d96ff1dd1b 438 NVIC_SystemReset();
elmkom 36:f8d96ff1dd1b 439 }
JMF 2:0e2ef866af95 440 void reinitialize_mdm(void)
JMF 2:0e2ef866af95 441 {
elmkom 36:f8d96ff1dd1b 442 system_reset();
elmkom 36:f8d96ff1dd1b 443 /*
JMF 2:0e2ef866af95 444 // Initialize the modem
JMF 2:0e2ef866af95 445 printf(GRN "Modem RE-initializing..." DEF "\r\n");
JMF 2:0e2ef866af95 446 if (!mdm_init()) {
elmkom 36:f8d96ff1dd1b 447
elmkom 36:f8d96ff1dd1b 448 system_reset();
JMF 2:0e2ef866af95 449 }
JMF 2:0e2ef866af95 450 printf("\r\n");
elmkom 36:f8d96ff1dd1b 451 */
JMF 2:0e2ef866af95 452 }
JMF 2:0e2ef866af95 453 // These are built on the fly
JMF 2:0e2ef866af95 454 string MyServerIpAddress;
JMF 2:0e2ef866af95 455 string MySocketData;
JMF 2:0e2ef866af95 456
JMF 2:0e2ef866af95 457 // These are to be built on the fly
JMF 2:0e2ef866af95 458 string my_temp;
JMF 2:0e2ef866af95 459 string my_humidity;
JMF 2:0e2ef866af95 460
JMF 0:9d5134074d84 461 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 462
stefanrousseau 3:26b3cc155f39 463 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 464 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 465 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 466 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 467 {
stefanrousseau 12:7c94ec5069dc 468 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 469 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 470 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 471 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 472 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 473 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 474 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 475 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 476 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 477 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 478 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 479 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 480 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 481 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 482 };
stefanrousseau 12:7c94ec5069dc 483
stefanrousseau 3:26b3cc155f39 484 void GenerateModemString(char * modem_string)
stefanrousseau 3:26b3cc155f39 485 {
stefanrousseau 12:7c94ec5069dc 486 switch(iSensorsToReport)
stefanrousseau 12:7c94ec5069dc 487 {
elmkom 38:532a0d929756 488 case PROXIMITY_ONLY:
elmkom 35:2e864bae3af0 489 {
elmkom 38:532a0d929756 490 char* data = proximityStrip.getDataStr();
elmkom 39:3bbb3dbb531b 491 sprintf(modem_string, "GET %s%s?serial=%s&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, data, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 35:2e864bae3af0 492 break;
elmkom 35:2e864bae3af0 493 }
stefanrousseau 12:7c94ec5069dc 494 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 495 {
stefanrousseau 12:7c94ec5069dc 496 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 497 break;
stefanrousseau 12:7c94ec5069dc 498 }
stefanrousseau 12:7c94ec5069dc 499 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 500 {
stefanrousseau 12:7c94ec5069dc 501 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 502 break;
stefanrousseau 12:7c94ec5069dc 503 }
stefanrousseau 12:7c94ec5069dc 504 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 505 {
stefanrousseau 12:7c94ec5069dc 506 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 507 break;
stefanrousseau 12:7c94ec5069dc 508 }
stefanrousseau 12:7c94ec5069dc 509 default:
stefanrousseau 12:7c94ec5069dc 510 {
stefanrousseau 12:7c94ec5069dc 511 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 512 break;
stefanrousseau 12:7c94ec5069dc 513 }
stefanrousseau 16:17c5916f2d12 514 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 515 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 516
stefanrousseau 3:26b3cc155f39 517
stefanrousseau 16:17c5916f2d12 518 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 519 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 520 //********************************************************************************************************************************************
elmkom 40:45e4636d20cc 521 bool extract_reply(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 522 {
stefanrousseau 16:17c5916f2d12 523 char* beginquote;
stefanrousseau 16:17c5916f2d12 524 char* endquote;
elmkom 40:45e4636d20cc 525 beginquote = strstr(search_field, "\r\n\r\n"); //start of data
elmkom 40:45e4636d20cc 526 endquote = strchr(search_field, '\0');
stefanrousseau 16:17c5916f2d12 527 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 528 {
stefanrousseau 16:17c5916f2d12 529 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 530 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 531 {
stefanrousseau 16:17c5916f2d12 532 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 533 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 534 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 535 return true;
stefanrousseau 16:17c5916f2d12 536 }
stefanrousseau 16:17c5916f2d12 537 }
elmkom 40:45e4636d20cc 538
elmkom 40:45e4636d20cc 539 return false;
elmkom 40:45e4636d20cc 540
elmkom 40:45e4636d20cc 541 } //extract_reply
stefanrousseau 16:17c5916f2d12 542
elmkom 38:532a0d929756 543 void setPowerSave(bool on)
elmkom 38:532a0d929756 544 {
elmkom 38:532a0d929756 545 string * pRespStr;
elmkom 38:532a0d929756 546 if(on)
elmkom 38:532a0d929756 547 {
elmkom 38:532a0d929756 548 powerSave = true;
elmkom 38:532a0d929756 549 send_wnc_cmd("AT+CPSMS=1", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 550 }
elmkom 38:532a0d929756 551 else
elmkom 38:532a0d929756 552 {
elmkom 38:532a0d929756 553 powerSave = false;
elmkom 38:532a0d929756 554 send_wnc_cmd("AT+CPSMS=0", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 555 }
elmkom 38:532a0d929756 556 }
elmkom 38:532a0d929756 557
elmkom 40:45e4636d20cc 558
elmkom 38:532a0d929756 559
elmkom 38:532a0d929756 560
elmkom 38:532a0d929756 561 int secToTau(int time)
elmkom 38:532a0d929756 562 {
elmkom 38:532a0d929756 563 /*
elmkom 38:532a0d929756 564 0 - value is incremented in multiples of 10 minutes
elmkom 38:532a0d929756 565 1 - value is incremented in multiples of 1 hour
elmkom 38:532a0d929756 566 2 - value is incremented in multiples of 10 hours
elmkom 38:532a0d929756 567 3 - value is incremented in multiples of 2 seconds
elmkom 38:532a0d929756 568 4 - value is incremented in multiples of 30 seconds
elmkom 38:532a0d929756 569 5 - value is incremented in multiples of 1 minute
elmkom 38:532a0d929756 570 */
elmkom 38:532a0d929756 571 if(time/2 < 32)
elmkom 38:532a0d929756 572 {
elmkom 38:532a0d929756 573 return (0x3<<5)+time/2;
elmkom 38:532a0d929756 574 }
elmkom 38:532a0d929756 575 else if(time/30 < 32)
elmkom 38:532a0d929756 576 {
elmkom 38:532a0d929756 577 return (0x4<<5)+time/30;
elmkom 38:532a0d929756 578 }
elmkom 38:532a0d929756 579 else if(time/60 < 32)
elmkom 38:532a0d929756 580 {
elmkom 38:532a0d929756 581 return (0x5<<5)+time/60;
elmkom 38:532a0d929756 582 }
elmkom 38:532a0d929756 583 else if(time/3600 < 32)
elmkom 38:532a0d929756 584 {
elmkom 38:532a0d929756 585 return (0x1<<5)+time/3600;
elmkom 38:532a0d929756 586 }
elmkom 38:532a0d929756 587 else if(time/36000 < 32)
elmkom 38:532a0d929756 588 {
elmkom 38:532a0d929756 589 return (0x2<<5)+time/36000;
stefanrousseau 16:17c5916f2d12 590 }
stefanrousseau 16:17c5916f2d12 591 else
elmkom 38:532a0d929756 592 return (0x7<<5);
elmkom 38:532a0d929756 593
elmkom 38:532a0d929756 594
elmkom 38:532a0d929756 595 }
elmkom 38:532a0d929756 596 int secToActivity(int time)
elmkom 38:532a0d929756 597 {
elmkom 38:532a0d929756 598 /*
elmkom 38:532a0d929756 599 0 - value is incremented in multiples of 2 seconds
elmkom 38:532a0d929756 600 1 - value is incremented in multiples of 1 minute
elmkom 38:532a0d929756 601 2 - value is incremented in multiples of decihours
elmkom 38:532a0d929756 602 7 - value indicates that the timer is deactivated.
elmkom 38:532a0d929756 603 */
elmkom 38:532a0d929756 604 if(time/2 < 32)
stefanrousseau 16:17c5916f2d12 605 {
elmkom 38:532a0d929756 606 return (0x0<<5)+time/2;
elmkom 38:532a0d929756 607 }
elmkom 38:532a0d929756 608 else if(time/60 < 32)
elmkom 38:532a0d929756 609 {
elmkom 38:532a0d929756 610 return (0x1<<5)+time/60;
elmkom 38:532a0d929756 611 }
elmkom 38:532a0d929756 612 else if(time/36000 < 32)
elmkom 38:532a0d929756 613 {
elmkom 38:532a0d929756 614 return (0x2<<5)+time/36000;
stefanrousseau 16:17c5916f2d12 615 }
elmkom 38:532a0d929756 616 else
elmkom 38:532a0d929756 617 return (0x7<<5);
elmkom 38:532a0d929756 618
elmkom 38:532a0d929756 619 }
elmkom 38:532a0d929756 620 void setTauTimer(int time)
elmkom 38:532a0d929756 621 {
elmkom 38:532a0d929756 622 string * pRespStr;
elmkom 38:532a0d929756 623 string cmd_str("AT%SETACFG=\"ecm.Mtc.PsmPTAU_T3412\",\"");
elmkom 38:532a0d929756 624 char str[15];
elmkom 38:532a0d929756 625 sprintf(str, "%d", secToTau(time));
elmkom 38:532a0d929756 626 pc.printf("TAU %d = %d",time,secToTau(time));
elmkom 38:532a0d929756 627 cmd_str += str;
elmkom 38:532a0d929756 628 cmd_str += "\"";
elmkom 38:532a0d929756 629 send_wnc_cmd("AT%SETCFG=\"EXT_TAU_CAP_EN\",\"1\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 630 send_wnc_cmd("AT%SETACFG=\"ecm.Mtc.PsmCpsmsEn\",\"true\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 631 send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 632 }
elmkom 38:532a0d929756 633
elmkom 38:532a0d929756 634 void setActivityTimer(int time)
elmkom 38:532a0d929756 635 {
elmkom 38:532a0d929756 636 string * pRespStr;
elmkom 38:532a0d929756 637 string cmd_str("AT%SETACFG=\"ecm.Mtc.PsmActTime_T3324\",\"");
elmkom 38:532a0d929756 638 char str[15];
elmkom 38:532a0d929756 639 sprintf(str, "%d", secToActivity(time));
elmkom 38:532a0d929756 640 pc.printf("Activity %d = %d",time,secToActivity(time));
elmkom 38:532a0d929756 641 cmd_str += str;
elmkom 38:532a0d929756 642 cmd_str += "\"";
elmkom 38:532a0d929756 643 send_wnc_cmd("AT%SETCFG=\"PSM_CAP_EN\",\"1\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 644 send_wnc_cmd("AT%SETACFG=\"ecm.Mtc.PsmCpsmsEn\",\"true\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 645 send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 646 }
elmkom 38:532a0d929756 647
elmkom 40:45e4636d20cc 648 void checkSocket()
elmkom 40:45e4636d20cc 649 {
elmkom 40:45e4636d20cc 650 string * pRespStr;
elmkom 38:532a0d929756 651
elmkom 40:45e4636d20cc 652 send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);
elmkom 40:45e4636d20cc 653 pc.printf(pRespStr->c_str());
elmkom 40:45e4636d20cc 654
elmkom 40:45e4636d20cc 655 }
elmkom 40:45e4636d20cc 656
elmkom 40:45e4636d20cc 657 void parse_reply(char* reply)
elmkom 40:45e4636d20cc 658 {
elmkom 40:45e4636d20cc 659 char *tokens[5];
elmkom 40:45e4636d20cc 660 int index = 0;
elmkom 40:45e4636d20cc 661 tokens[index++] = strtok(reply," ");
elmkom 40:45e4636d20cc 662 while( index < 5 )
elmkom 40:45e4636d20cc 663 {
elmkom 40:45e4636d20cc 664 char* token = strtok(NULL, " ");
elmkom 40:45e4636d20cc 665 if(token == NULL) break;
elmkom 40:45e4636d20cc 666 tokens[index++] = token;
elmkom 40:45e4636d20cc 667 }
elmkom 40:45e4636d20cc 668 if(strcmp("PSM",tokens[1]) == 0)
elmkom 40:45e4636d20cc 669 {
elmkom 40:45e4636d20cc 670
elmkom 40:45e4636d20cc 671 int t3412 = atoi(tokens[3]);
elmkom 40:45e4636d20cc 672 int t3324 = atoi(tokens[4]);
elmkom 40:45e4636d20cc 673 pc.printf("t3412 %d t3324 %d\r\n",t3412,t3324);
elmkom 40:45e4636d20cc 674 setTauTimer(t3412);
elmkom 40:45e4636d20cc 675 setActivityTimer(t3324);
elmkom 40:45e4636d20cc 676
elmkom 40:45e4636d20cc 677 if(strcmp("true",tokens[2]) == 0)
elmkom 40:45e4636d20cc 678 {
elmkom 40:45e4636d20cc 679 pc.printf("PSM ON\r\n");
elmkom 40:45e4636d20cc 680 setPowerSave(true);
elmkom 40:45e4636d20cc 681 }
elmkom 40:45e4636d20cc 682 else
elmkom 40:45e4636d20cc 683 {
elmkom 40:45e4636d20cc 684 pc.printf("PSM OFF\r\n");
elmkom 40:45e4636d20cc 685 setPowerSave(false);
elmkom 40:45e4636d20cc 686 }
elmkom 40:45e4636d20cc 687 }
elmkom 40:45e4636d20cc 688 }
stefanrousseau 16:17c5916f2d12 689
JMF 0:9d5134074d84 690 int main() {
elmkom 36:f8d96ff1dd1b 691
JMF 2:0e2ef866af95 692 int i;
elmkom 37:ee01f752524a 693 int sendAttemps = 0;
elmkom 36:f8d96ff1dd1b 694
JMF 0:9d5134074d84 695 HTS221 hts221;
JMF 0:9d5134074d84 696 pc.baud(115200);
elmkom 38:532a0d929756 697
elmkom 36:f8d96ff1dd1b 698
JMF 0:9d5134074d84 699
JMF 0:9d5134074d84 700 void hts221_init(void);
JMF 0:9d5134074d84 701
fkellermavnet 20:27a4f27254d0 702 // Set LED to RED until init finishes
fkellermavnet 20:27a4f27254d0 703 SetLedColor(0x1);
fkellermavnet 20:27a4f27254d0 704
JMF 1:af7a42f7d465 705 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 706 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 707
JMF 0:9d5134074d84 708 i = hts221.begin();
JMF 0:9d5134074d84 709 if( i )
JMF 0:9d5134074d84 710 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 711 else
JMF 0:9d5134074d84 712 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 713
JMF 0:9d5134074d84 714 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 715 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
elmkom 35:2e864bae3af0 716
stefanrousseau 11:e6602513730f 717
JMF 0:9d5134074d84 718 // Initialize the modem
JMF 0:9d5134074d84 719 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 720 do {
fkellermavnet 14:0c353e212296 721 i=mdm_init();
fkellermavnet 14:0c353e212296 722 if (!i) {
fkellermavnet 14:0c353e212296 723 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 724 }
fkellermavnet 14:0c353e212296 725 } while (!i);
JMF 0:9d5134074d84 726
elmkom 36:f8d96ff1dd1b 727
elmkom 36:f8d96ff1dd1b 728
JMF 2:0e2ef866af95 729 //Software init
JMF 2:0e2ef866af95 730 software_init_mdm();
elmkom 40:45e4636d20cc 731
fkellermavnet 19:f89baed3bd6f 732
JMF 2:0e2ef866af95 733 // Resolve URL to IP address to connect to
JMF 2:0e2ef866af95 734 resolve_mdm();
JMF 0:9d5134074d84 735
fkellermavnet 25:e7996d22a7e6 736 sockopen_mdm();
stefanrousseau 3:26b3cc155f39 737
fkellermavnet 20:27a4f27254d0 738 // Set LED BLUE for partial init
fkellermavnet 20:27a4f27254d0 739 SetLedColor(0x4);
elmkom 38:532a0d929756 740
elmkom 38:532a0d929756 741 setTauTimer(12*60*60);
elmkom 38:532a0d929756 742 setActivityTimer(20);
elmkom 40:45e4636d20cc 743 setPowerSave(false);
elmkom 36:f8d96ff1dd1b 744
elmkom 38:532a0d929756 745 proximityStrip.init();
elmkom 38:532a0d929756 746
elmkom 38:532a0d929756 747 proximityStrip.on();
elmkom 38:532a0d929756 748
elmkom 38:532a0d929756 749 int count = 0;
JMF 2:0e2ef866af95 750 // Send and receive data perpetually
JMF 2:0e2ef866af95 751 while(1) {
elmkom 38:532a0d929756 752
elmkom 38:532a0d929756 753
elmkom 38:532a0d929756 754 //sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
elmkom 38:532a0d929756 755 //sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
elmkom 38:532a0d929756 756 // read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
elmkom 36:f8d96ff1dd1b 757
elmkom 38:532a0d929756 758 SetLedColor(0x2); //green
elmkom 38:532a0d929756 759
elmkom 38:532a0d929756 760 proximityStrip.scan();
elmkom 38:532a0d929756 761
elmkom 38:532a0d929756 762 SetLedColor(0); //off
fkellermavnet 20:27a4f27254d0 763
elmkom 38:532a0d929756 764 if(count >= 5*60 ||proximityStrip.changed(50))
elmkom 38:532a0d929756 765 {
elmkom 38:532a0d929756 766 count = 0;
elmkom 38:532a0d929756 767 SetLedColor(0x04); //blue
elmkom 36:f8d96ff1dd1b 768
elmkom 38:532a0d929756 769 char modem_string[512];
elmkom 38:532a0d929756 770 GenerateModemString(&modem_string[0]);
elmkom 38:532a0d929756 771 printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
elmkom 38:532a0d929756 772 wakeModem();
elmkom 40:45e4636d20cc 773 checkSocket();
elmkom 38:532a0d929756 774 sockwrite_mdm(modem_string);
elmkom 40:45e4636d20cc 775 wakeModem();
elmkom 38:532a0d929756 776 sockread_mdm(&MySocketData, 1024, 20);
fkellermavnet 20:27a4f27254d0 777
elmkom 38:532a0d929756 778 // If any non-zero response from server, make it GREEN one-time
elmkom 38:532a0d929756 779 // then the actual FLOW responses will set the color.
elmkom 38:532a0d929756 780 if (MySocketData.length() > 0)
elmkom 38:532a0d929756 781 {
elmkom 37:ee01f752524a 782
elmkom 38:532a0d929756 783 SetLedColor(0x2); // green
elmkom 38:532a0d929756 784 //only copy on sucessful send
elmkom 38:532a0d929756 785
elmkom 38:532a0d929756 786 printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]);
elmkom 40:45e4636d20cc 787 char reply[512];
elmkom 40:45e4636d20cc 788 if (extract_reply(&MySocketData[0], &reply[0]))
elmkom 38:532a0d929756 789 {
elmkom 40:45e4636d20cc 790 printf(GRN "JSON : %s" DEF "\r\n", &reply[0]);
elmkom 40:45e4636d20cc 791 parse_reply(&reply[0]);
elmkom 36:f8d96ff1dd1b 792 }
elmkom 38:532a0d929756 793 SetLedColor(0); // off
elmkom 38:532a0d929756 794 }
elmkom 38:532a0d929756 795 else
elmkom 38:532a0d929756 796 {
elmkom 38:532a0d929756 797 SetLedColor(0x1); //red
elmkom 38:532a0d929756 798 // reset socket if read fails
elmkom 38:532a0d929756 799 if(sendAttemps < 2)
elmkom 36:f8d96ff1dd1b 800 {
elmkom 38:532a0d929756 801 sendAttemps++;
elmkom 38:532a0d929756 802 sockclose_mdm();
elmkom 38:532a0d929756 803 sockopen_mdm();
elmkom 38:532a0d929756 804 }
elmkom 38:532a0d929756 805 else // give up and do full reset
elmkom 38:532a0d929756 806 {
elmkom 38:532a0d929756 807 if(!rebootModem())
elmkom 37:ee01f752524a 808 system_reset();
elmkom 38:532a0d929756 809 }
elmkom 36:f8d96ff1dd1b 810
elmkom 38:532a0d929756 811 }
elmkom 38:532a0d929756 812 }
elmkom 38:532a0d929756 813 count++;
elmkom 38:532a0d929756 814 wait(0.2);
stefanrousseau 3:26b3cc155f39 815 } //forever loop
JMF 0:9d5134074d84 816 }