Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Tue Oct 04 20:29:31 2016 +0000
Revision:
41:85a736a9b900
Parent:
40:45e4636d20cc
Child:
42:8500f0cb2ea5
Make proximity optional

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