Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Mon Sep 26 18:02:16 2016 +0000
Revision:
38:532a0d929756
Parent:
37:ee01f752524a
Child:
39:3bbb3dbb531b
latest

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);
JMF 0:9d5134074d84 48 SerialBuffered mdm(PTD3, PTD2, 128);
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 }
JMF 0:9d5134074d84 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);
fkellermavnet 6:713b4cbf1a7d 327 if (cmd && n > 0) {
JMF 2:0e2ef866af95 328 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 329 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 2:0e2ef866af95 330 }
fkellermavnet 6:713b4cbf1a7d 331 while (n--) {
fkellermavnet 6:713b4cbf1a7d 332 mdm.putc(*cmd++);
fkellermavnet 6:713b4cbf1a7d 333 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 334 };
fkellermavnet 6:713b4cbf1a7d 335 mdm.putc('\r');
fkellermavnet 6:713b4cbf1a7d 336 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 337 mdm.putc('\n');
fkellermavnet 6:713b4cbf1a7d 338 wait_ms(1);
JMF 2:0e2ef866af95 339 }
JMF 2:0e2ef866af95 340
JMF 2:0e2ef866af95 341 if (rsp_list) {
JMF 2:0e2ef866af95 342 rsp->erase(); // Clean up from prior cmd response
JMF 2:0e2ef866af95 343 *len = 0;
JMF 2:0e2ef866af95 344 Timer timer;
JMF 2:0e2ef866af95 345 timer.start();
JMF 2:0e2ef866af95 346 while (timer.read_ms() < timeout_ms) {
JMF 2:0e2ef866af95 347 int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms());
JMF 2:0e2ef866af95 348
JMF 2:0e2ef866af95 349 if (lenCmd == 0)
JMF 2:0e2ef866af95 350 continue;
JMF 2:0e2ef866af95 351
JMF 2:0e2ef866af95 352 if (lenCmd < 0)
JMF 2:0e2ef866af95 353 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 354 else {
JMF 2:0e2ef866af95 355 *len += lenCmd;
JMF 2:0e2ef866af95 356 *rsp += cmd_buf;
JMF 2:0e2ef866af95 357 }
JMF 2:0e2ef866af95 358
JMF 2:0e2ef866af95 359 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 360 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf);
JMF 2:0e2ef866af95 361 }
JMF 2:0e2ef866af95 362
JMF 2:0e2ef866af95 363 int rsp_idx = 0;
JMF 2:0e2ef866af95 364 while (rsp_list[rsp_idx]) {
JMF 2:0e2ef866af95 365 if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) {
JMF 2:0e2ef866af95 366 return rsp_idx;
JMF 2:0e2ef866af95 367 }
JMF 2:0e2ef866af95 368 rsp_idx++;
JMF 2:0e2ef866af95 369 }
JMF 2:0e2ef866af95 370 }
JMF 2:0e2ef866af95 371 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 372 }
JMF 2:0e2ef866af95 373 pc.printf("D %s",rsp);
JMF 2:0e2ef866af95 374 return MDM_OK;
JMF 2:0e2ef866af95 375 }
elmkom 36:f8d96ff1dd1b 376 void system_reset()
elmkom 36:f8d96ff1dd1b 377 {
elmkom 36:f8d96ff1dd1b 378 printf(RED "\n\rSystem resetting..." DEF "\n");
elmkom 36:f8d96ff1dd1b 379 NVIC_SystemReset();
elmkom 36:f8d96ff1dd1b 380 }
JMF 2:0e2ef866af95 381 void reinitialize_mdm(void)
JMF 2:0e2ef866af95 382 {
elmkom 36:f8d96ff1dd1b 383 system_reset();
elmkom 36:f8d96ff1dd1b 384 /*
JMF 2:0e2ef866af95 385 // Initialize the modem
JMF 2:0e2ef866af95 386 printf(GRN "Modem RE-initializing..." DEF "\r\n");
JMF 2:0e2ef866af95 387 if (!mdm_init()) {
elmkom 36:f8d96ff1dd1b 388
elmkom 36:f8d96ff1dd1b 389 system_reset();
JMF 2:0e2ef866af95 390 }
JMF 2:0e2ef866af95 391 printf("\r\n");
elmkom 36:f8d96ff1dd1b 392 */
JMF 2:0e2ef866af95 393 }
JMF 2:0e2ef866af95 394 // These are built on the fly
JMF 2:0e2ef866af95 395 string MyServerIpAddress;
JMF 2:0e2ef866af95 396 string MySocketData;
JMF 2:0e2ef866af95 397
JMF 2:0e2ef866af95 398 // These are to be built on the fly
JMF 2:0e2ef866af95 399 string my_temp;
JMF 2:0e2ef866af95 400 string my_humidity;
JMF 2:0e2ef866af95 401
JMF 0:9d5134074d84 402 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 403
stefanrousseau 3:26b3cc155f39 404 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 405 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 406 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 407 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 408 {
stefanrousseau 12:7c94ec5069dc 409 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 410 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 411 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 412 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 413 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 414 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 415 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 416 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 417 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 418 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 419 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 420 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 421 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 422 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 423 };
stefanrousseau 12:7c94ec5069dc 424
stefanrousseau 3:26b3cc155f39 425 void GenerateModemString(char * modem_string)
stefanrousseau 3:26b3cc155f39 426 {
stefanrousseau 12:7c94ec5069dc 427 switch(iSensorsToReport)
stefanrousseau 12:7c94ec5069dc 428 {
elmkom 38:532a0d929756 429 case PROXIMITY_ONLY:
elmkom 35:2e864bae3af0 430 {
elmkom 38:532a0d929756 431 char* data = proximityStrip.getDataStr();
elmkom 38:532a0d929756 432 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 433 break;
elmkom 35:2e864bae3af0 434 }
stefanrousseau 12:7c94ec5069dc 435 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 436 {
stefanrousseau 12:7c94ec5069dc 437 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 438 break;
stefanrousseau 12:7c94ec5069dc 439 }
stefanrousseau 12:7c94ec5069dc 440 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 441 {
stefanrousseau 12:7c94ec5069dc 442 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 443 break;
stefanrousseau 12:7c94ec5069dc 444 }
stefanrousseau 12:7c94ec5069dc 445 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 446 {
stefanrousseau 12:7c94ec5069dc 447 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 448 break;
stefanrousseau 12:7c94ec5069dc 449 }
stefanrousseau 12:7c94ec5069dc 450 default:
stefanrousseau 12:7c94ec5069dc 451 {
stefanrousseau 12:7c94ec5069dc 452 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 453 break;
stefanrousseau 12:7c94ec5069dc 454 }
stefanrousseau 16:17c5916f2d12 455 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 456 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 457
stefanrousseau 3:26b3cc155f39 458
stefanrousseau 16:17c5916f2d12 459 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 460 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 461 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 462 bool extract_JSON(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 463 {
stefanrousseau 16:17c5916f2d12 464 char* beginquote;
stefanrousseau 16:17c5916f2d12 465 char* endquote;
stefanrousseau 16:17c5916f2d12 466 beginquote = strchr(search_field, '{'); //start of JSON
stefanrousseau 16:17c5916f2d12 467 endquote = strchr(search_field, '}'); //end of JSON
stefanrousseau 16:17c5916f2d12 468 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 469 {
stefanrousseau 16:17c5916f2d12 470 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 471 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 472 {
stefanrousseau 16:17c5916f2d12 473 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 474 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 475 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 476 return true;
stefanrousseau 16:17c5916f2d12 477 }
stefanrousseau 16:17c5916f2d12 478 else
stefanrousseau 16:17c5916f2d12 479 {
stefanrousseau 16:17c5916f2d12 480 endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing
stefanrousseau 16:17c5916f2d12 481 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 482 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 483 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 484 return false;
stefanrousseau 16:17c5916f2d12 485 }
stefanrousseau 16:17c5916f2d12 486 }
stefanrousseau 16:17c5916f2d12 487 else
stefanrousseau 16:17c5916f2d12 488 {
stefanrousseau 16:17c5916f2d12 489 return false;
stefanrousseau 16:17c5916f2d12 490 }
stefanrousseau 16:17c5916f2d12 491 } //extract_JSON
stefanrousseau 16:17c5916f2d12 492
elmkom 38:532a0d929756 493 void setPowerSave(bool on)
elmkom 38:532a0d929756 494 {
elmkom 38:532a0d929756 495 string * pRespStr;
elmkom 38:532a0d929756 496 if(on)
elmkom 38:532a0d929756 497 {
elmkom 38:532a0d929756 498 powerSave = true;
elmkom 38:532a0d929756 499 send_wnc_cmd("AT+CPSMS=1", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 500 }
elmkom 38:532a0d929756 501 else
elmkom 38:532a0d929756 502 {
elmkom 38:532a0d929756 503 powerSave = false;
elmkom 38:532a0d929756 504 send_wnc_cmd("AT+CPSMS=0", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 505 }
elmkom 38:532a0d929756 506 }
elmkom 38:532a0d929756 507
elmkom 38:532a0d929756 508 void parse_JSON_PSM(char* json_string)
stefanrousseau 16:17c5916f2d12 509 {
stefanrousseau 16:17c5916f2d12 510 char* beginquote;
elmkom 38:532a0d929756 511 char token[] = "\"PSM\":\"";
stefanrousseau 16:17c5916f2d12 512 beginquote = strstr(json_string, token );
stefanrousseau 16:17c5916f2d12 513 if ((beginquote != 0))
stefanrousseau 16:17c5916f2d12 514 {
elmkom 38:532a0d929756 515 char mode = beginquote[strlen(token)];
elmkom 38:532a0d929756 516 printf(GRN "PSM Found : %c" DEF "\r\n", mode);
elmkom 38:532a0d929756 517 switch(mode)
stefanrousseau 16:17c5916f2d12 518 {
elmkom 38:532a0d929756 519 case 'T':
elmkom 38:532a0d929756 520 { setPowerSave(true);
stefanrousseau 16:17c5916f2d12 521 break;
elmkom 38:532a0d929756 522 }
elmkom 38:532a0d929756 523 case 'F':
elmkom 38:532a0d929756 524 { setPowerSave(false);
stefanrousseau 16:17c5916f2d12 525 break;
elmkom 38:532a0d929756 526 }
elmkom 38:532a0d929756 527 }
elmkom 38:532a0d929756 528 }
elmkom 38:532a0d929756 529 }
elmkom 38:532a0d929756 530
elmkom 38:532a0d929756 531
elmkom 38:532a0d929756 532 int secToTau(int time)
elmkom 38:532a0d929756 533 {
elmkom 38:532a0d929756 534 /*
elmkom 38:532a0d929756 535 0 - value is incremented in multiples of 10 minutes
elmkom 38:532a0d929756 536 1 - value is incremented in multiples of 1 hour
elmkom 38:532a0d929756 537 2 - value is incremented in multiples of 10 hours
elmkom 38:532a0d929756 538 3 - value is incremented in multiples of 2 seconds
elmkom 38:532a0d929756 539 4 - value is incremented in multiples of 30 seconds
elmkom 38:532a0d929756 540 5 - value is incremented in multiples of 1 minute
elmkom 38:532a0d929756 541 */
elmkom 38:532a0d929756 542 if(time/2 < 32)
elmkom 38:532a0d929756 543 {
elmkom 38:532a0d929756 544 return (0x3<<5)+time/2;
elmkom 38:532a0d929756 545 }
elmkom 38:532a0d929756 546 else if(time/30 < 32)
elmkom 38:532a0d929756 547 {
elmkom 38:532a0d929756 548 return (0x4<<5)+time/30;
elmkom 38:532a0d929756 549 }
elmkom 38:532a0d929756 550 else if(time/60 < 32)
elmkom 38:532a0d929756 551 {
elmkom 38:532a0d929756 552 return (0x5<<5)+time/60;
elmkom 38:532a0d929756 553 }
elmkom 38:532a0d929756 554 else if(time/3600 < 32)
elmkom 38:532a0d929756 555 {
elmkom 38:532a0d929756 556 return (0x1<<5)+time/3600;
elmkom 38:532a0d929756 557 }
elmkom 38:532a0d929756 558 else if(time/36000 < 32)
elmkom 38:532a0d929756 559 {
elmkom 38:532a0d929756 560 return (0x2<<5)+time/36000;
stefanrousseau 16:17c5916f2d12 561 }
stefanrousseau 16:17c5916f2d12 562 else
elmkom 38:532a0d929756 563 return (0x7<<5);
elmkom 38:532a0d929756 564
elmkom 38:532a0d929756 565
elmkom 38:532a0d929756 566 }
elmkom 38:532a0d929756 567 int secToActivity(int time)
elmkom 38:532a0d929756 568 {
elmkom 38:532a0d929756 569 /*
elmkom 38:532a0d929756 570 0 - value is incremented in multiples of 2 seconds
elmkom 38:532a0d929756 571 1 - value is incremented in multiples of 1 minute
elmkom 38:532a0d929756 572 2 - value is incremented in multiples of decihours
elmkom 38:532a0d929756 573 7 - value indicates that the timer is deactivated.
elmkom 38:532a0d929756 574 */
elmkom 38:532a0d929756 575 if(time/2 < 32)
stefanrousseau 16:17c5916f2d12 576 {
elmkom 38:532a0d929756 577 return (0x0<<5)+time/2;
elmkom 38:532a0d929756 578 }
elmkom 38:532a0d929756 579 else if(time/60 < 32)
elmkom 38:532a0d929756 580 {
elmkom 38:532a0d929756 581 return (0x1<<5)+time/60;
elmkom 38:532a0d929756 582 }
elmkom 38:532a0d929756 583 else if(time/36000 < 32)
elmkom 38:532a0d929756 584 {
elmkom 38:532a0d929756 585 return (0x2<<5)+time/36000;
stefanrousseau 16:17c5916f2d12 586 }
elmkom 38:532a0d929756 587 else
elmkom 38:532a0d929756 588 return (0x7<<5);
elmkom 38:532a0d929756 589
elmkom 38:532a0d929756 590 }
elmkom 38:532a0d929756 591 void setTauTimer(int time)
elmkom 38:532a0d929756 592 {
elmkom 38:532a0d929756 593 string * pRespStr;
elmkom 38:532a0d929756 594 string cmd_str("AT%SETACFG=\"ecm.Mtc.PsmPTAU_T3412\",\"");
elmkom 38:532a0d929756 595 char str[15];
elmkom 38:532a0d929756 596 sprintf(str, "%d", secToTau(time));
elmkom 38:532a0d929756 597 pc.printf("TAU %d = %d",time,secToTau(time));
elmkom 38:532a0d929756 598 cmd_str += str;
elmkom 38:532a0d929756 599 cmd_str += "\"";
elmkom 38:532a0d929756 600 send_wnc_cmd("AT%SETCFG=\"EXT_TAU_CAP_EN\",\"1\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 601 send_wnc_cmd("AT%SETACFG=\"ecm.Mtc.PsmCpsmsEn\",\"true\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 602 send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 603 }
elmkom 38:532a0d929756 604
elmkom 38:532a0d929756 605 void setActivityTimer(int time)
elmkom 38:532a0d929756 606 {
elmkom 38:532a0d929756 607 string * pRespStr;
elmkom 38:532a0d929756 608 string cmd_str("AT%SETACFG=\"ecm.Mtc.PsmActTime_T3324\",\"");
elmkom 38:532a0d929756 609 char str[15];
elmkom 38:532a0d929756 610 sprintf(str, "%d", secToActivity(time));
elmkom 38:532a0d929756 611 pc.printf("Activity %d = %d",time,secToActivity(time));
elmkom 38:532a0d929756 612 cmd_str += str;
elmkom 38:532a0d929756 613 cmd_str += "\"";
elmkom 38:532a0d929756 614 send_wnc_cmd("AT%SETCFG=\"PSM_CAP_EN\",\"1\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 615 send_wnc_cmd("AT%SETACFG=\"ecm.Mtc.PsmCpsmsEn\",\"true\"", &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 616 send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
elmkom 38:532a0d929756 617 }
elmkom 38:532a0d929756 618
elmkom 38:532a0d929756 619
stefanrousseau 16:17c5916f2d12 620
JMF 0:9d5134074d84 621 int main() {
elmkom 36:f8d96ff1dd1b 622
JMF 2:0e2ef866af95 623 int i;
elmkom 37:ee01f752524a 624 int sendAttemps = 0;
elmkom 36:f8d96ff1dd1b 625
JMF 0:9d5134074d84 626 HTS221 hts221;
JMF 0:9d5134074d84 627 pc.baud(115200);
elmkom 38:532a0d929756 628
elmkom 36:f8d96ff1dd1b 629
JMF 0:9d5134074d84 630
JMF 0:9d5134074d84 631 void hts221_init(void);
JMF 0:9d5134074d84 632
fkellermavnet 20:27a4f27254d0 633 // Set LED to RED until init finishes
fkellermavnet 20:27a4f27254d0 634 SetLedColor(0x1);
fkellermavnet 20:27a4f27254d0 635
JMF 1:af7a42f7d465 636 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 637 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 638
JMF 0:9d5134074d84 639 i = hts221.begin();
JMF 0:9d5134074d84 640 if( i )
JMF 0:9d5134074d84 641 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 642 else
JMF 0:9d5134074d84 643 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 644
JMF 0:9d5134074d84 645 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 646 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
elmkom 35:2e864bae3af0 647
stefanrousseau 11:e6602513730f 648
JMF 0:9d5134074d84 649 // Initialize the modem
JMF 0:9d5134074d84 650 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 651 do {
fkellermavnet 14:0c353e212296 652 i=mdm_init();
fkellermavnet 14:0c353e212296 653 if (!i) {
fkellermavnet 14:0c353e212296 654 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 655 }
fkellermavnet 14:0c353e212296 656 } while (!i);
JMF 0:9d5134074d84 657
elmkom 36:f8d96ff1dd1b 658
elmkom 36:f8d96ff1dd1b 659
JMF 2:0e2ef866af95 660 //Software init
JMF 2:0e2ef866af95 661 software_init_mdm();
fkellermavnet 19:f89baed3bd6f 662
JMF 2:0e2ef866af95 663 // Resolve URL to IP address to connect to
JMF 2:0e2ef866af95 664 resolve_mdm();
JMF 0:9d5134074d84 665
fkellermavnet 25:e7996d22a7e6 666 sockopen_mdm();
stefanrousseau 3:26b3cc155f39 667
fkellermavnet 20:27a4f27254d0 668 // Set LED BLUE for partial init
fkellermavnet 20:27a4f27254d0 669 SetLedColor(0x4);
elmkom 38:532a0d929756 670
elmkom 38:532a0d929756 671 setTauTimer(12*60*60);
elmkom 38:532a0d929756 672 setActivityTimer(20);
elmkom 36:f8d96ff1dd1b 673
elmkom 38:532a0d929756 674 proximityStrip.init();
elmkom 38:532a0d929756 675
elmkom 38:532a0d929756 676 proximityStrip.on();
elmkom 38:532a0d929756 677
elmkom 38:532a0d929756 678 int count = 0;
JMF 2:0e2ef866af95 679 // Send and receive data perpetually
JMF 2:0e2ef866af95 680 while(1) {
elmkom 38:532a0d929756 681
elmkom 38:532a0d929756 682
elmkom 38:532a0d929756 683 //sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
elmkom 38:532a0d929756 684 //sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
elmkom 38:532a0d929756 685 // read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
elmkom 36:f8d96ff1dd1b 686
elmkom 38:532a0d929756 687 SetLedColor(0x2); //green
elmkom 38:532a0d929756 688
elmkom 38:532a0d929756 689 proximityStrip.scan();
elmkom 38:532a0d929756 690
elmkom 38:532a0d929756 691 SetLedColor(0); //off
fkellermavnet 20:27a4f27254d0 692
elmkom 38:532a0d929756 693 if(count >= 5*60 ||proximityStrip.changed(50))
elmkom 38:532a0d929756 694 {
elmkom 38:532a0d929756 695 count = 0;
elmkom 38:532a0d929756 696 SetLedColor(0x04); //blue
elmkom 36:f8d96ff1dd1b 697
elmkom 38:532a0d929756 698 char modem_string[512];
elmkom 38:532a0d929756 699 GenerateModemString(&modem_string[0]);
elmkom 38:532a0d929756 700 printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
elmkom 38:532a0d929756 701 wakeModem();
elmkom 38:532a0d929756 702 sockwrite_mdm(modem_string);
elmkom 38:532a0d929756 703 sockread_mdm(&MySocketData, 1024, 20);
fkellermavnet 20:27a4f27254d0 704
elmkom 38:532a0d929756 705 // If any non-zero response from server, make it GREEN one-time
elmkom 38:532a0d929756 706 // then the actual FLOW responses will set the color.
elmkom 38:532a0d929756 707 if (MySocketData.length() > 0)
elmkom 38:532a0d929756 708 {
elmkom 37:ee01f752524a 709
elmkom 38:532a0d929756 710 SetLedColor(0x2); // green
elmkom 38:532a0d929756 711 //only copy on sucessful send
elmkom 38:532a0d929756 712
elmkom 38:532a0d929756 713 printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]);
elmkom 38:532a0d929756 714 char myJsonResponse[512];
elmkom 38:532a0d929756 715 if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
elmkom 38:532a0d929756 716 {
elmkom 38:532a0d929756 717 printf(GRN "JSON : %s" DEF "\r\n", &myJsonResponse[0]);
elmkom 38:532a0d929756 718 parse_JSON_PSM(&myJsonResponse[0]);
elmkom 36:f8d96ff1dd1b 719 }
elmkom 38:532a0d929756 720 SetLedColor(0); // off
elmkom 38:532a0d929756 721 }
elmkom 38:532a0d929756 722 else
elmkom 38:532a0d929756 723 {
elmkom 38:532a0d929756 724 SetLedColor(0x1); //red
elmkom 38:532a0d929756 725 // reset socket if read fails
elmkom 38:532a0d929756 726 if(sendAttemps < 2)
elmkom 36:f8d96ff1dd1b 727 {
elmkom 38:532a0d929756 728 sendAttemps++;
elmkom 38:532a0d929756 729 sockclose_mdm();
elmkom 38:532a0d929756 730 sockopen_mdm();
elmkom 38:532a0d929756 731 }
elmkom 38:532a0d929756 732 else // give up and do full reset
elmkom 38:532a0d929756 733 {
elmkom 38:532a0d929756 734 if(!rebootModem())
elmkom 37:ee01f752524a 735 system_reset();
elmkom 38:532a0d929756 736 }
elmkom 36:f8d96ff1dd1b 737
elmkom 38:532a0d929756 738 }
elmkom 38:532a0d929756 739 }
elmkom 38:532a0d929756 740 count++;
elmkom 38:532a0d929756 741 wait(0.2);
stefanrousseau 3:26b3cc155f39 742 } //forever loop
JMF 0:9d5134074d84 743 }