Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Mon Sep 19 16:16:17 2016 +0000
Revision:
35:2e864bae3af0
Parent:
34:029e07b67a41
Child:
36:f8d96ff1dd1b
proximity sensors working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:9d5134074d84 1 #include "mbed.h"
JMF 0:9d5134074d84 2 #include <cctype>
JMF 0:9d5134074d84 3 #include <string>
JMF 0:9d5134074d84 4 #include "SerialBuffered.h"
JMF 0:9d5134074d84 5 #include "HTS221.h"
JMF 2:0e2ef866af95 6 #include "config_me.h"
JMF 2:0e2ef866af95 7 #include "wnc_control.h"
stefanrousseau 4:f83bedd9cab4 8 #include "sensors.h"
JMF 0:9d5134074d84 9
stefanrousseau 11:e6602513730f 10 #include "hardware.h"
stefanrousseau 11:e6602513730f 11 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
elmkom 35:2e864bae3af0 12 I2C proximityi2c(PTE25, PTE24);
JMF 0:9d5134074d84 13 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 14 #define USE_COLOR
JMF 0:9d5134074d84 15 #ifdef USE_COLOR
JMF 0:9d5134074d84 16 #define BLK "\033[30m"
JMF 0:9d5134074d84 17 #define RED "\033[31m"
JMF 0:9d5134074d84 18 #define GRN "\033[32m"
JMF 0:9d5134074d84 19 #define YEL "\033[33m"
JMF 0:9d5134074d84 20 #define BLU "\033[34m"
JMF 0:9d5134074d84 21 #define MAG "\033[35m"
JMF 0:9d5134074d84 22 #define CYN "\033[36m"
JMF 0:9d5134074d84 23 #define WHT "\033[37m"
JMF 0:9d5134074d84 24 #define DEF "\033[39m"
JMF 0:9d5134074d84 25 #else
JMF 0:9d5134074d84 26 #define BLK
JMF 0:9d5134074d84 27 #define RED
JMF 0:9d5134074d84 28 #define GRN
JMF 0:9d5134074d84 29 #define YEL
JMF 0:9d5134074d84 30 #define BLU
JMF 0:9d5134074d84 31 #define MAG
JMF 0:9d5134074d84 32 #define CYN
JMF 0:9d5134074d84 33 #define WHT
JMF 0:9d5134074d84 34 #define DEF
JMF 0:9d5134074d84 35 #endif
JMF 0:9d5134074d84 36
JMF 0:9d5134074d84 37 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 38 #define MDM_DBG_AT_CMDS (1 << 0)
JMF 0:9d5134074d84 39 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 40
JMF 0:9d5134074d84 41 Serial pc(USBTX, USBRX);
JMF 0:9d5134074d84 42 SerialBuffered mdm(PTD3, PTD2, 128);
stefanrousseau 16:17c5916f2d12 43 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 44 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 45 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 46
JMF 0:9d5134074d84 47 DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot
JMF 0:9d5134074d84 48 DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem)
JMF 0:9d5134074d84 49 DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield
JMF 0:9d5134074d84 50
fkellermavnet 14:0c353e212296 51 DigitalOut mdm_reset(PTC12); // active high
fkellermavnet 14:0c353e212296 52
JMF 0:9d5134074d84 53 DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
JMF 0:9d5134074d84 54 DigitalOut mdm_uart1_cts(PTD0);
JMF 0:9d5134074d84 55
JMF 0:9d5134074d84 56 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 57
JMF 0:9d5134074d84 58 const char ok_str[] = "OK";
JMF 0:9d5134074d84 59 const char error_str[] = "ERROR";
JMF 0:9d5134074d84 60
JMF 0:9d5134074d84 61 #define MDM_OK 0
JMF 0:9d5134074d84 62 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 63
JMF 0:9d5134074d84 64 #define MAX_AT_RSP_LEN 255
JMF 0:9d5134074d84 65
elmkom 35:2e864bae3af0 66 short proximity = 0;
elmkom 35:2e864bae3af0 67
elmkom 35:2e864bae3af0 68 void prox_write_reg(char address,char reg, char cmd)
elmkom 35:2e864bae3af0 69 {
elmkom 35:2e864bae3af0 70 char txbuffer [2];
elmkom 35:2e864bae3af0 71 txbuffer[0] = reg;
elmkom 35:2e864bae3af0 72 txbuffer[1] = cmd;
elmkom 35:2e864bae3af0 73 proximityi2c.write(address<<1, txbuffer, 2,false );
elmkom 35:2e864bae3af0 74 }
elmkom 35:2e864bae3af0 75
elmkom 35:2e864bae3af0 76 void prox_write(char address, char cmd)
elmkom 35:2e864bae3af0 77 {
elmkom 35:2e864bae3af0 78 char txbuffer [1];
elmkom 35:2e864bae3af0 79 txbuffer[0] = cmd;
elmkom 35:2e864bae3af0 80 proximityi2c.write(address<<1, txbuffer, 1,false );
elmkom 35:2e864bae3af0 81 }
elmkom 35:2e864bae3af0 82
elmkom 35:2e864bae3af0 83 unsigned char prox_read_reg(char address,char reg)
elmkom 35:2e864bae3af0 84 {
elmkom 35:2e864bae3af0 85 char txbuffer [1];
elmkom 35:2e864bae3af0 86 char rxbuffer [1];
elmkom 35:2e864bae3af0 87 rxbuffer[0] = 0;
elmkom 35:2e864bae3af0 88 txbuffer[0] = reg;
elmkom 35:2e864bae3af0 89 proximityi2c.write(address<<1, txbuffer, 1,false );
elmkom 35:2e864bae3af0 90 proximityi2c.read(address<<1, rxbuffer, 1 );
elmkom 35:2e864bae3af0 91 return (unsigned char)rxbuffer[0];
elmkom 35:2e864bae3af0 92 }
elmkom 35:2e864bae3af0 93
elmkom 35:2e864bae3af0 94 void init_proximity_sensor(int sensor)
elmkom 35:2e864bae3af0 95 {
elmkom 35:2e864bae3af0 96 char C25ma = 0x00;
elmkom 35:2e864bae3af0 97 char C50ma = 0x01;
elmkom 35:2e864bae3af0 98 char C100m1 = 0x02;
elmkom 35:2e864bae3af0 99 char C200ma = 0x03;
elmkom 35:2e864bae3af0 100
elmkom 35:2e864bae3af0 101 char GainAls1Ir1 = 0x00<<2;
elmkom 35:2e864bae3af0 102 char GainAls2Ir1 = 0x04<<2;
elmkom 35:2e864bae3af0 103 char GainAls2Ir2 = 0x05<<2;
elmkom 35:2e864bae3af0 104 char GainAls64Ir64 = 0x0A<<2;
elmkom 35:2e864bae3af0 105 char GainAls128Ir64 = 0x0D<<2;
elmkom 35:2e864bae3af0 106 char GainAls128Ir128 = 0x0F<<2;
elmkom 35:2e864bae3af0 107
elmkom 35:2e864bae3af0 108 char Als0Ps0 = 0x00;
elmkom 35:2e864bae3af0 109 char Als0Ps10 = 0x01;
elmkom 35:2e864bae3af0 110 char Als0Ps40 = 0x02;
elmkom 35:2e864bae3af0 111 char Als0Ps100 = 0x03;
elmkom 35:2e864bae3af0 112 char Als0Ps400 = 0x04;
elmkom 35:2e864bae3af0 113
elmkom 35:2e864bae3af0 114 char Als100Ps0 = 0x05;
elmkom 35:2e864bae3af0 115 char Als100Ps100 = 0x06;
elmkom 35:2e864bae3af0 116 char Als100Ps400 = 0x07;
elmkom 35:2e864bae3af0 117
elmkom 35:2e864bae3af0 118 char Als401Ps0 = 0x08;
elmkom 35:2e864bae3af0 119 char Als401Ps100 = 0x09;
elmkom 35:2e864bae3af0 120 char Als400Ps0 = 0x0A;
elmkom 35:2e864bae3af0 121 char Als400Ps400 = 0x0B;
elmkom 35:2e864bae3af0 122
elmkom 35:2e864bae3af0 123 char Als50Ps50 = 0x0C;
elmkom 35:2e864bae3af0 124
elmkom 35:2e864bae3af0 125 char muxaddress = 0x70;
elmkom 35:2e864bae3af0 126 char proxaddress = 0x39;
elmkom 35:2e864bae3af0 127 prox_write(muxaddress,sensor); // sensor 2
elmkom 35:2e864bae3af0 128 prox_write_reg(proxaddress,0x41,Als0Ps400); // initiate ALS: and PS
elmkom 35:2e864bae3af0 129 prox_write_reg(proxaddress,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma
elmkom 35:2e864bae3af0 130 }
elmkom 35:2e864bae3af0 131
elmkom 35:2e864bae3af0 132 short read_proximity(int sensor)
elmkom 35:2e864bae3af0 133 {
elmkom 35:2e864bae3af0 134
elmkom 35:2e864bae3af0 135 char muxaddress = 0x70;
elmkom 35:2e864bae3af0 136 char proxaddress = 0x39;
elmkom 35:2e864bae3af0 137 prox_write(muxaddress,sensor); // sensor 2
elmkom 35:2e864bae3af0 138 unsigned char prox_lsb = prox_read_reg(proxaddress,0x44);
elmkom 35:2e864bae3af0 139 unsigned char prox_msb = prox_read_reg(proxaddress,0x45);
elmkom 35:2e864bae3af0 140 unsigned char ALS_lsb = prox_read_reg(proxaddress,0x46);
elmkom 35:2e864bae3af0 141 unsigned char ALS_msb = prox_read_reg(proxaddress,0x47);
elmkom 35:2e864bae3af0 142 unsigned char IR_lsb = prox_read_reg(proxaddress,0x48);
elmkom 35:2e864bae3af0 143 unsigned char IR_msb = prox_read_reg(proxaddress,0x49);
elmkom 35:2e864bae3af0 144
elmkom 35:2e864bae3af0 145 short proximity = prox_msb*256+prox_lsb;
elmkom 35:2e864bae3af0 146 short ALS = ALS_msb*256+ALS_lsb;
elmkom 35:2e864bae3af0 147 short IR = IR_msb*256+IR_lsb;
elmkom 35:2e864bae3af0 148 pc.printf(GRN "Sensor %d\n\r",sensor);
elmkom 35:2e864bae3af0 149 pc.printf(GRN "Prox %d\n\r",proximity);
elmkom 35:2e864bae3af0 150 pc.printf(GRN "ALS %d\n\r",ALS);
elmkom 35:2e864bae3af0 151 pc.printf(GRN "IR %d\n\r",IR);
elmkom 35:2e864bae3af0 152 return proximity;
elmkom 35:2e864bae3af0 153 }
elmkom 35:2e864bae3af0 154
elmkom 35:2e864bae3af0 155
elmkom 35:2e864bae3af0 156 /*
elmkom 35:2e864bae3af0 157 I2C_w_3 (sfh_address*2, 0x41, 0x08); // initiate ALS: 400ms rep rate, T_int=100ms
elmkom 35:2e864bae3af0 158 I2C_w_3 (sfh_address*2, 0x42, 0x28); // set ALS_VIS=ALS_IR GAIN = 64
elmkom 35:2e864bae3af0 159 I2C_w_2_r_1 (sfh_address*2, 0x46); // read lsb of ALS_VIS, register 0x46
elmkom 35:2e864bae3af0 160 Content1 = Content;
elmkom 35:2e864bae3af0 161 I2C_w_2_r_1 (sfh_address*2, 0x47); // read msb of ALS_VIS, register 0x47
elmkom 35:2e864bae3af0 162 ALS_VIS = (Content * 256 + Content1); // combining LSB+MSB byte to decimal value
elmkom 35:2e864bae3af0 163 I2C_w_2_r_1 (sfh_address*2, 0x48); // read lsb of ALS_IR, register 0x48
elmkom 35:2e864bae3af0 164 Content1 = Content;
elmkom 35:2e864bae3af0 165 I2C_w_2_r_1 (sfh_address*2, 0x49); // read msb of ALS_IR, register 0x49
elmkom 35:2e864bae3af0 166 ALS_IR = (Content * 256 + Content1); // combining LSB+MSB byte to decimal value
elmkom 35:2e864bae3af0 167 // Lux Calculation based on ALS Gain = 64 and ALS_Int_Time = 100 ms
elmkom 35:2e864bae3af0 168 // Lux value in front of sensor, no cover glass
elmkom 35:2e864bae3af0 169 IF ((ALS_IR / ALS_VIS) < 0.109)
elmkom 35:2e864bae3af0 170 {LUX = (1.534 * ALS_VIS / 64 - 3.759 * ALS_IR / 64) * 1};
elmkom 35:2e864bae3af0 171 ELSE IF ((ALS_IR / ALS_VIS) < 0.429)
elmkom 35:2e864bae3af0 172 {LUX = (1.339 * ALS_VIS / 64 – 1.972 * ALS_IR / 64) * 1};
elmkom 35:2e864bae3af0 173 ELSE IF ((ALS_IR/ALS_VIS) < (0.95 * 1.45))
elmkom 35:2e864bae3af0 174 {LUX = (0.701 * ALS_VIS / 64 – 0.483 * ALS_IR / 64) * 1};
elmkom 35:2e864bae3af0 175 ELSE IF ((ALS_IR/ALS_VIS) < (1.5 * 1.45))
elmkom 35:2e864bae3af0 176 {LUX = (2 * 0.701 * ALS_VIS / 64 – 1.18 * 0.483 * ALS_IR / 64) * 1};
elmkom 35:2e864bae3af0 177 ELSE IF ((ALS_IR/ALS_VIS) < (2.5 * 1.45))
elmkom 35:2e864bae3af0 178 {LUX = (4 * 0.701 * ALS_VIS / 64 – 1.33 * 0.483 * ALS_IR / 64) * 1};
elmkom 35:2e864bae3af0 179 Else {LUX = 8 * 0.701 * ALS_VIS / 64};
elmkom 35:2e864bae3af0 180 */
elmkom 35:2e864bae3af0 181 short oldread_proximity( void )
elmkom 35:2e864bae3af0 182 {
elmkom 35:2e864bae3af0 183 char muxaddress = (0x70<<1);
elmkom 35:2e864bae3af0 184 char proxaddress = (0x39<<1);
elmkom 35:2e864bae3af0 185 char reg = 0x40;
elmkom 35:2e864bae3af0 186 short muxvalue = 0;
elmkom 35:2e864bae3af0 187 short value = 0;
elmkom 35:2e864bae3af0 188 char sensor = 0x02;
elmkom 35:2e864bae3af0 189
elmkom 35:2e864bae3af0 190 char txbuffer [1];
elmkom 35:2e864bae3af0 191 char rxbuffer [1];
elmkom 35:2e864bae3af0 192 rxbuffer[0] = 0;
elmkom 35:2e864bae3af0 193 txbuffer[0] = sensor;
elmkom 35:2e864bae3af0 194 proximityi2c.write(muxaddress, txbuffer, 1,false );
elmkom 35:2e864bae3af0 195 proximityi2c.read(muxaddress, rxbuffer, 1 );
elmkom 35:2e864bae3af0 196 muxvalue = (unsigned char)rxbuffer[0];
elmkom 35:2e864bae3af0 197
elmkom 35:2e864bae3af0 198 rxbuffer[0] = 0;
elmkom 35:2e864bae3af0 199 txbuffer[0] = reg;
elmkom 35:2e864bae3af0 200 proximityi2c.write(proxaddress, txbuffer, 1,false );
elmkom 35:2e864bae3af0 201 proximityi2c.read(proxaddress, rxbuffer, 1 );
elmkom 35:2e864bae3af0 202 value = (unsigned char)rxbuffer[0];
elmkom 35:2e864bae3af0 203 pc.printf(GRN "Mux %d\n\r",muxvalue);
elmkom 35:2e864bae3af0 204 pc.printf(GRN "Proximity %d\n\r",value);
elmkom 35:2e864bae3af0 205 return value;
elmkom 35:2e864bae3af0 206
elmkom 35:2e864bae3af0 207 }
elmkom 35:2e864bae3af0 208
JMF 0:9d5134074d84 209 ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) {
JMF 0:9d5134074d84 210 int cin = -1;
JMF 0:9d5134074d84 211 int cin_last;
JMF 0:9d5134074d84 212
JMF 0:9d5134074d84 213 if (NULL == buff || size == 0) {
JMF 0:9d5134074d84 214 return -1;
JMF 0:9d5134074d84 215 }
JMF 0:9d5134074d84 216
JMF 0:9d5134074d84 217 size_t len = 0;
JMF 0:9d5134074d84 218 Timer timer;
JMF 0:9d5134074d84 219 timer.start();
JMF 0:9d5134074d84 220 while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) {
JMF 0:9d5134074d84 221 if (mdm.readable()) {
JMF 0:9d5134074d84 222 cin_last = cin;
JMF 0:9d5134074d84 223 cin = mdm.getc();
JMF 0:9d5134074d84 224 if (isprint(cin)) {
JMF 0:9d5134074d84 225 buff[len++] = (char)cin;
JMF 0:9d5134074d84 226 continue;
JMF 0:9d5134074d84 227 } else if (('\r' == cin_last) && ('\n' == cin)) {
JMF 0:9d5134074d84 228 break;
JMF 0:9d5134074d84 229 }
JMF 0:9d5134074d84 230 }
JMF 0:9d5134074d84 231 wait_ms(1);
JMF 0:9d5134074d84 232 }
JMF 2:0e2ef866af95 233 buff[len] = (char)NULL;
JMF 0:9d5134074d84 234
JMF 0:9d5134074d84 235 return len;
JMF 0:9d5134074d84 236 }
JMF 0:9d5134074d84 237
JMF 0:9d5134074d84 238 int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) {
JMF 0:9d5134074d84 239 if (cmd && strlen(cmd) > 0) {
JMF 0:9d5134074d84 240 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 241 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 0:9d5134074d84 242 }
JMF 0:9d5134074d84 243 mdm.printf("%s\r\n", cmd);
JMF 0:9d5134074d84 244 }
JMF 0:9d5134074d84 245
JMF 0:9d5134074d84 246 if (rsp_list) {
JMF 0:9d5134074d84 247 Timer timer;
JMF 0:9d5134074d84 248 char rsp[MAX_AT_RSP_LEN+1];
JMF 0:9d5134074d84 249 int len;
JMF 0:9d5134074d84 250
JMF 0:9d5134074d84 251 timer.start();
JMF 0:9d5134074d84 252 while (timer.read_ms() < timeout_ms) {
JMF 0:9d5134074d84 253 len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms());
JMF 0:9d5134074d84 254
JMF 0:9d5134074d84 255 if (len < 0)
JMF 0:9d5134074d84 256 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 257
JMF 0:9d5134074d84 258 if (len == 0)
JMF 0:9d5134074d84 259 continue;
JMF 0:9d5134074d84 260
JMF 0:9d5134074d84 261 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 262 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp);
JMF 0:9d5134074d84 263 }
JMF 0:9d5134074d84 264
JMF 0:9d5134074d84 265 if (rsp_list) {
JMF 0:9d5134074d84 266 int rsp_idx = 0;
JMF 0:9d5134074d84 267 while (rsp_list[rsp_idx]) {
JMF 0:9d5134074d84 268 if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) {
JMF 0:9d5134074d84 269 return rsp_idx;
JMF 0:9d5134074d84 270 }
JMF 0:9d5134074d84 271 rsp_idx++;
JMF 0:9d5134074d84 272 }
JMF 0:9d5134074d84 273 }
JMF 0:9d5134074d84 274 }
JMF 0:9d5134074d84 275 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 276 }
JMF 0:9d5134074d84 277 return MDM_OK;
JMF 0:9d5134074d84 278 }
elmkom 35:2e864bae3af0 279 int mdm_init(void) {
elmkom 35:2e864bae3af0 280 // disable signal level translator (necessary
elmkom 35:2e864bae3af0 281 // for the modem to boot properly)
elmkom 35:2e864bae3af0 282 shield_3v3_1v8_sig_trans_ena = 0;
JMF 0:9d5134074d84 283
elmkom 35:2e864bae3af0 284 // Hard reset the modem (doesn't go through
elmkom 35:2e864bae3af0 285 // the signal level translator)
elmkom 35:2e864bae3af0 286 mdm_reset = 1;
elmkom 35:2e864bae3af0 287
elmkom 35:2e864bae3af0 288 // wait a moment for the modem to react
elmkom 35:2e864bae3af0 289 wait_ms(10);
elmkom 35:2e864bae3af0 290
elmkom 35:2e864bae3af0 291 // Let modem boot
elmkom 35:2e864bae3af0 292 mdm_reset = 0;
elmkom 35:2e864bae3af0 293
elmkom 35:2e864bae3af0 294 // wait a moment for the modem to react
elmkom 35:2e864bae3af0 295 wait(1.0);
elmkom 35:2e864bae3af0 296
elmkom 35:2e864bae3af0 297 // power modem on //off
elmkom 35:2e864bae3af0 298 mdm_power_on = 0; //1;
elmkom 35:2e864bae3af0 299
elmkom 35:2e864bae3af0 300 // insure modem boots into normal operating mode
elmkom 35:2e864bae3af0 301 // and does not go to sleep when powered on
elmkom 35:2e864bae3af0 302 mdm_uart2_rx_boot_mode_sel = 1;
elmkom 35:2e864bae3af0 303 mdm_wakeup_in = 1;
elmkom 35:2e864bae3af0 304
elmkom 35:2e864bae3af0 305 // initialze comm with the modem
elmkom 35:2e864bae3af0 306 mdm.baud(115200);
elmkom 35:2e864bae3af0 307 // clear out potential garbage
elmkom 35:2e864bae3af0 308 while (mdm.readable())
elmkom 35:2e864bae3af0 309 mdm.getc();
elmkom 35:2e864bae3af0 310
elmkom 35:2e864bae3af0 311 mdm_uart1_cts = 0;
elmkom 35:2e864bae3af0 312
elmkom 35:2e864bae3af0 313 // wait a moment for the modem to react to signal
elmkom 35:2e864bae3af0 314 // conditions while the level translator is disabled
elmkom 35:2e864bae3af0 315 // (sorry, don't have enough information to know
elmkom 35:2e864bae3af0 316 // what exactly the modem is doing with the current
elmkom 35:2e864bae3af0 317 // pin settings)
elmkom 35:2e864bae3af0 318 wait(1.0);
elmkom 35:2e864bae3af0 319
elmkom 35:2e864bae3af0 320 // enable the signal level translator to start
elmkom 35:2e864bae3af0 321 // modem reset process (modem will be powered down)
elmkom 35:2e864bae3af0 322 shield_3v3_1v8_sig_trans_ena = 1;
elmkom 35:2e864bae3af0 323
elmkom 35:2e864bae3af0 324 // Give the modem 60 secons to start responding by
elmkom 35:2e864bae3af0 325 // sending simple 'AT' commands to modem once per second.
elmkom 35:2e864bae3af0 326 Timer timer;
elmkom 35:2e864bae3af0 327 timer.start();
elmkom 35:2e864bae3af0 328 while (timer.read() < 60) {
elmkom 35:2e864bae3af0 329 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 35:2e864bae3af0 330 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 35:2e864bae3af0 331 if (rc == 0)
elmkom 35:2e864bae3af0 332 return true; //timer.read();
elmkom 35:2e864bae3af0 333 wait_ms(1000 - (timer.read_ms() % 1000));
elmkom 35:2e864bae3af0 334 pc.printf("\r%d",timer.read_ms()/1000);
elmkom 35:2e864bae3af0 335 }
elmkom 35:2e864bae3af0 336 return false;
elmkom 35:2e864bae3af0 337 }
elmkom 35:2e864bae3af0 338 int oldmdm_init(void) {
fkellermavnet 14:0c353e212296 339 // Hard reset the modem (doesn't go through
fkellermavnet 14:0c353e212296 340 // the signal level translator)
fkellermavnet 14:0c353e212296 341 mdm_reset = 0;
JMF 17:38a8cc0c6ba5 342
JMF 17:38a8cc0c6ba5 343 // disable signal level translator (necessary
JMF 17:38a8cc0c6ba5 344 // for the modem to boot properly). All signals
JMF 17:38a8cc0c6ba5 345 // except mdm_reset go through the level translator
JMF 17:38a8cc0c6ba5 346 // and have internal pull-up/down in the module. While
JMF 17:38a8cc0c6ba5 347 // the level translator is disabled, these pins will
JMF 17:38a8cc0c6ba5 348 // be in the correct state.
JMF 17:38a8cc0c6ba5 349 shield_3v3_1v8_sig_trans_ena = 0;
JMF 17:38a8cc0c6ba5 350
JMF 17:38a8cc0c6ba5 351 // While the level translator is disabled and ouptut pins
JMF 17:38a8cc0c6ba5 352 // are tristated, make sure the inputs are in the same state
JMF 17:38a8cc0c6ba5 353 // as the WNC Module pins so that when the level translator is
JMF 17:38a8cc0c6ba5 354 // enabled, there are no differences.
JMF 17:38a8cc0c6ba5 355 mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high
JMF 17:38a8cc0c6ba5 356 mdm_power_on = 0; // powr_on should be low
JMF 17:38a8cc0c6ba5 357 mdm_wakeup_in = 1; // wake-up should be high
JMF 17:38a8cc0c6ba5 358 mdm_uart1_cts = 0; // indicate that it is ok to send
JMF 17:38a8cc0c6ba5 359
JMF 17:38a8cc0c6ba5 360 // Now, wait for the WNC Module to perform its initial boot correctly
fkellermavnet 14:0c353e212296 361 wait(1.0);
JMF 17:38a8cc0c6ba5 362
JMF 17:38a8cc0c6ba5 363 // The WNC module initializes comms at 115200 8N1 so set it up
JMF 17:38a8cc0c6ba5 364 mdm.baud(115200);
JMF 0:9d5134074d84 365
JMF 17:38a8cc0c6ba5 366 //Now, enable the level translator, the input pins should now be the
JMF 17:38a8cc0c6ba5 367 //same as how the M14A module is driving them with internal pull ups/downs.
JMF 17:38a8cc0c6ba5 368 //When enabled, there will be no changes in these 4 pins...
JMF 17:38a8cc0c6ba5 369 shield_3v3_1v8_sig_trans_ena = 1;
JMF 2:0e2ef866af95 370
JMF 17:38a8cc0c6ba5 371 // Now, give the modem 60 secons to start responding by
JMF 0:9d5134074d84 372 // sending simple 'AT' commands to modem once per second.
JMF 0:9d5134074d84 373 Timer timer;
JMF 0:9d5134074d84 374 timer.start();
JMF 0:9d5134074d84 375 while (timer.read() < 60) {
JMF 0:9d5134074d84 376 const char * rsp_lst[] = { ok_str, error_str, NULL };
JMF 0:9d5134074d84 377 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
JMF 0:9d5134074d84 378 if (rc == 0)
fkellermavnet 14:0c353e212296 379 return true; //timer.read();
JMF 0:9d5134074d84 380 wait_ms(1000 - (timer.read_ms() % 1000));
JMF 0:9d5134074d84 381 pc.printf("\r%d",timer.read_ms()/1000);
JMF 0:9d5134074d84 382 }
JMF 0:9d5134074d84 383 return false;
JMF 0:9d5134074d84 384 }
JMF 0:9d5134074d84 385
JMF 2:0e2ef866af95 386 int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) {
JMF 2:0e2ef866af95 387 static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars)
fkellermavnet 6:713b4cbf1a7d 388 size_t n = strlen(cmd);
fkellermavnet 6:713b4cbf1a7d 389 if (cmd && n > 0) {
JMF 2:0e2ef866af95 390 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 391 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 2:0e2ef866af95 392 }
fkellermavnet 6:713b4cbf1a7d 393 while (n--) {
fkellermavnet 6:713b4cbf1a7d 394 mdm.putc(*cmd++);
fkellermavnet 6:713b4cbf1a7d 395 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 396 };
fkellermavnet 6:713b4cbf1a7d 397 mdm.putc('\r');
fkellermavnet 6:713b4cbf1a7d 398 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 399 mdm.putc('\n');
fkellermavnet 6:713b4cbf1a7d 400 wait_ms(1);
JMF 2:0e2ef866af95 401 }
JMF 2:0e2ef866af95 402
JMF 2:0e2ef866af95 403 if (rsp_list) {
JMF 2:0e2ef866af95 404 rsp->erase(); // Clean up from prior cmd response
JMF 2:0e2ef866af95 405 *len = 0;
JMF 2:0e2ef866af95 406 Timer timer;
JMF 2:0e2ef866af95 407 timer.start();
JMF 2:0e2ef866af95 408 while (timer.read_ms() < timeout_ms) {
JMF 2:0e2ef866af95 409 int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms());
JMF 2:0e2ef866af95 410
JMF 2:0e2ef866af95 411 if (lenCmd == 0)
JMF 2:0e2ef866af95 412 continue;
JMF 2:0e2ef866af95 413
JMF 2:0e2ef866af95 414 if (lenCmd < 0)
JMF 2:0e2ef866af95 415 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 416 else {
JMF 2:0e2ef866af95 417 *len += lenCmd;
JMF 2:0e2ef866af95 418 *rsp += cmd_buf;
JMF 2:0e2ef866af95 419 }
JMF 2:0e2ef866af95 420
JMF 2:0e2ef866af95 421 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 422 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf);
JMF 2:0e2ef866af95 423 }
JMF 2:0e2ef866af95 424
JMF 2:0e2ef866af95 425 int rsp_idx = 0;
JMF 2:0e2ef866af95 426 while (rsp_list[rsp_idx]) {
JMF 2:0e2ef866af95 427 if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) {
JMF 2:0e2ef866af95 428 return rsp_idx;
JMF 2:0e2ef866af95 429 }
JMF 2:0e2ef866af95 430 rsp_idx++;
JMF 2:0e2ef866af95 431 }
JMF 2:0e2ef866af95 432 }
JMF 2:0e2ef866af95 433 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 434 }
JMF 2:0e2ef866af95 435 pc.printf("D %s",rsp);
JMF 2:0e2ef866af95 436 return MDM_OK;
JMF 2:0e2ef866af95 437 }
JMF 2:0e2ef866af95 438
JMF 2:0e2ef866af95 439 void reinitialize_mdm(void)
JMF 2:0e2ef866af95 440 {
JMF 2:0e2ef866af95 441 // Initialize the modem
JMF 2:0e2ef866af95 442 printf(GRN "Modem RE-initializing..." DEF "\r\n");
JMF 2:0e2ef866af95 443 if (!mdm_init()) {
JMF 2:0e2ef866af95 444 printf(RED "\n\rModem RE-initialization failed!" DEF "\n");
JMF 2:0e2ef866af95 445 }
JMF 2:0e2ef866af95 446 printf("\r\n");
JMF 2:0e2ef866af95 447 }
JMF 2:0e2ef866af95 448 // These are built on the fly
JMF 2:0e2ef866af95 449 string MyServerIpAddress;
JMF 2:0e2ef866af95 450 string MySocketData;
JMF 2:0e2ef866af95 451
JMF 2:0e2ef866af95 452 // These are to be built on the fly
JMF 2:0e2ef866af95 453 string my_temp;
JMF 2:0e2ef866af95 454 string my_humidity;
JMF 2:0e2ef866af95 455
JMF 0:9d5134074d84 456 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 457
stefanrousseau 3:26b3cc155f39 458 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 459 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 460 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 461 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 462 {
stefanrousseau 12:7c94ec5069dc 463 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 464 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 465 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 466 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 467 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 468 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 469 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 470 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 471 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 472 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 473 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 474 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 475 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 476 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 477 };
stefanrousseau 12:7c94ec5069dc 478
stefanrousseau 3:26b3cc155f39 479 void GenerateModemString(char * modem_string)
stefanrousseau 3:26b3cc155f39 480 {
stefanrousseau 12:7c94ec5069dc 481 switch(iSensorsToReport)
stefanrousseau 12:7c94ec5069dc 482 {
elmkom 35:2e864bae3af0 483 case PROXIMITY:
elmkom 35:2e864bae3af0 484 {
elmkom 35:2e864bae3af0 485 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&proximity=%d %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity,proximity, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 35:2e864bae3af0 486 break;
elmkom 35:2e864bae3af0 487 }
stefanrousseau 12:7c94ec5069dc 488 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 489 {
stefanrousseau 12:7c94ec5069dc 490 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 491 break;
stefanrousseau 12:7c94ec5069dc 492 }
stefanrousseau 12:7c94ec5069dc 493 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 494 {
stefanrousseau 12:7c94ec5069dc 495 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 496 break;
stefanrousseau 12:7c94ec5069dc 497 }
stefanrousseau 12:7c94ec5069dc 498 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 499 {
stefanrousseau 12:7c94ec5069dc 500 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 501 break;
stefanrousseau 12:7c94ec5069dc 502 }
stefanrousseau 12:7c94ec5069dc 503 default:
stefanrousseau 12:7c94ec5069dc 504 {
stefanrousseau 12:7c94ec5069dc 505 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 506 break;
stefanrousseau 12:7c94ec5069dc 507 }
stefanrousseau 16:17c5916f2d12 508 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 509 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 510
stefanrousseau 3:26b3cc155f39 511
stefanrousseau 3:26b3cc155f39 512 //Periodic timer
stefanrousseau 3:26b3cc155f39 513 Ticker OneMsTicker;
stefanrousseau 3:26b3cc155f39 514 volatile bool bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 515 int OneMsTicks = 0;
stefanrousseau 3:26b3cc155f39 516 int iTimer1Interval_ms = 1000;
stefanrousseau 3:26b3cc155f39 517 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 518 //* Periodic 1ms timer tick
stefanrousseau 3:26b3cc155f39 519 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 520 void OneMsFunction()
stefanrousseau 3:26b3cc155f39 521 {
stefanrousseau 3:26b3cc155f39 522 OneMsTicks++;
stefanrousseau 3:26b3cc155f39 523 if ((OneMsTicks % iTimer1Interval_ms) == 0)
stefanrousseau 3:26b3cc155f39 524 {
stefanrousseau 3:26b3cc155f39 525 bTimerExpiredFlag = true;
stefanrousseau 3:26b3cc155f39 526 }
stefanrousseau 3:26b3cc155f39 527 } //OneMsFunction()
stefanrousseau 3:26b3cc155f39 528
stefanrousseau 16:17c5916f2d12 529 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 530 //* Set the RGB LED's Color
stefanrousseau 16:17c5916f2d12 531 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
stefanrousseau 16:17c5916f2d12 532 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 533 void SetLedColor(unsigned char ucColor)
stefanrousseau 16:17c5916f2d12 534 {
stefanrousseau 16:17c5916f2d12 535 //Note that when an LED is on, you write a 0 to it:
stefanrousseau 16:17c5916f2d12 536 led_red = !(ucColor & 0x1); //bit 0
stefanrousseau 16:17c5916f2d12 537 led_green = !(ucColor & 0x2); //bit 1
stefanrousseau 16:17c5916f2d12 538 led_blue = !(ucColor & 0x4); //bit 2
stefanrousseau 16:17c5916f2d12 539 } //SetLedColor()
stefanrousseau 16:17c5916f2d12 540
stefanrousseau 16:17c5916f2d12 541 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 542 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 543 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 544 bool extract_JSON(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 545 {
stefanrousseau 16:17c5916f2d12 546 char* beginquote;
stefanrousseau 16:17c5916f2d12 547 char* endquote;
stefanrousseau 16:17c5916f2d12 548 beginquote = strchr(search_field, '{'); //start of JSON
stefanrousseau 16:17c5916f2d12 549 endquote = strchr(search_field, '}'); //end of JSON
stefanrousseau 16:17c5916f2d12 550 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 551 {
stefanrousseau 16:17c5916f2d12 552 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 553 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 554 {
stefanrousseau 16:17c5916f2d12 555 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 556 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 557 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 558 return true;
stefanrousseau 16:17c5916f2d12 559 }
stefanrousseau 16:17c5916f2d12 560 else
stefanrousseau 16:17c5916f2d12 561 {
stefanrousseau 16:17c5916f2d12 562 endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing
stefanrousseau 16:17c5916f2d12 563 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 564 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 565 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 566 return false;
stefanrousseau 16:17c5916f2d12 567 }
stefanrousseau 16:17c5916f2d12 568 }
stefanrousseau 16:17c5916f2d12 569 else
stefanrousseau 16:17c5916f2d12 570 {
stefanrousseau 16:17c5916f2d12 571 return false;
stefanrousseau 16:17c5916f2d12 572 }
stefanrousseau 16:17c5916f2d12 573 } //extract_JSON
stefanrousseau 16:17c5916f2d12 574
stefanrousseau 16:17c5916f2d12 575 bool parse_JSON(char* json_string)
stefanrousseau 16:17c5916f2d12 576 {
stefanrousseau 16:17c5916f2d12 577 char* beginquote;
stefanrousseau 16:17c5916f2d12 578 char token[] = "\"LED\":\"";
stefanrousseau 16:17c5916f2d12 579 beginquote = strstr(json_string, token );
stefanrousseau 16:17c5916f2d12 580 if ((beginquote != 0))
stefanrousseau 16:17c5916f2d12 581 {
stefanrousseau 16:17c5916f2d12 582 char cLedColor = beginquote[strlen(token)];
stefanrousseau 16:17c5916f2d12 583 printf(GRN "LED Found : %c" DEF "\r\n", cLedColor);
stefanrousseau 16:17c5916f2d12 584 switch(cLedColor)
stefanrousseau 16:17c5916f2d12 585 {
stefanrousseau 16:17c5916f2d12 586 case 'O':
stefanrousseau 16:17c5916f2d12 587 { //Off
stefanrousseau 16:17c5916f2d12 588 SetLedColor(0);
stefanrousseau 16:17c5916f2d12 589 break;
stefanrousseau 16:17c5916f2d12 590 }
stefanrousseau 16:17c5916f2d12 591 case 'R':
stefanrousseau 16:17c5916f2d12 592 { //Red
stefanrousseau 16:17c5916f2d12 593 SetLedColor(1);
stefanrousseau 16:17c5916f2d12 594 break;
stefanrousseau 16:17c5916f2d12 595 }
stefanrousseau 16:17c5916f2d12 596 case 'G':
stefanrousseau 16:17c5916f2d12 597 { //Green
stefanrousseau 16:17c5916f2d12 598 SetLedColor(2);
stefanrousseau 16:17c5916f2d12 599 break;
stefanrousseau 16:17c5916f2d12 600 }
stefanrousseau 16:17c5916f2d12 601 case 'Y':
stefanrousseau 16:17c5916f2d12 602 { //Yellow
stefanrousseau 16:17c5916f2d12 603 SetLedColor(3);
stefanrousseau 16:17c5916f2d12 604 break;
stefanrousseau 16:17c5916f2d12 605 }
stefanrousseau 16:17c5916f2d12 606 case 'B':
stefanrousseau 16:17c5916f2d12 607 { //Blue
stefanrousseau 16:17c5916f2d12 608 SetLedColor(4);
stefanrousseau 16:17c5916f2d12 609 break;
stefanrousseau 16:17c5916f2d12 610 }
stefanrousseau 16:17c5916f2d12 611 case 'M':
stefanrousseau 16:17c5916f2d12 612 { //Magenta
stefanrousseau 16:17c5916f2d12 613 SetLedColor(5);
stefanrousseau 16:17c5916f2d12 614 break;
stefanrousseau 16:17c5916f2d12 615 }
stefanrousseau 16:17c5916f2d12 616 case 'T':
stefanrousseau 16:17c5916f2d12 617 { //Turquoise
stefanrousseau 16:17c5916f2d12 618 SetLedColor(6);
stefanrousseau 16:17c5916f2d12 619 break;
stefanrousseau 16:17c5916f2d12 620 }
stefanrousseau 16:17c5916f2d12 621 case 'W':
stefanrousseau 16:17c5916f2d12 622 { //White
stefanrousseau 16:17c5916f2d12 623 SetLedColor(7);
stefanrousseau 16:17c5916f2d12 624 break;
stefanrousseau 16:17c5916f2d12 625 }
stefanrousseau 16:17c5916f2d12 626 default:
stefanrousseau 16:17c5916f2d12 627 {
stefanrousseau 16:17c5916f2d12 628 break;
stefanrousseau 16:17c5916f2d12 629 }
stefanrousseau 16:17c5916f2d12 630 } //switch(cLedColor)
stefanrousseau 16:17c5916f2d12 631 return true;
stefanrousseau 16:17c5916f2d12 632 }
stefanrousseau 16:17c5916f2d12 633 else
stefanrousseau 16:17c5916f2d12 634 {
stefanrousseau 16:17c5916f2d12 635 return false;
stefanrousseau 16:17c5916f2d12 636 }
stefanrousseau 16:17c5916f2d12 637 } //parse_JSON
stefanrousseau 16:17c5916f2d12 638
JMF 0:9d5134074d84 639 int main() {
JMF 2:0e2ef866af95 640 int i;
JMF 0:9d5134074d84 641 HTS221 hts221;
JMF 0:9d5134074d84 642 pc.baud(115200);
elmkom 35:2e864bae3af0 643 proximityi2c.frequency(400000);
elmkom 35:2e864bae3af0 644 for(int i = 0;i<8;i++)
elmkom 35:2e864bae3af0 645 init_proximity_sensor(i);
JMF 0:9d5134074d84 646
JMF 0:9d5134074d84 647 void hts221_init(void);
JMF 0:9d5134074d84 648
fkellermavnet 20:27a4f27254d0 649 // Set LED to RED until init finishes
fkellermavnet 20:27a4f27254d0 650 SetLedColor(0x1);
fkellermavnet 20:27a4f27254d0 651
JMF 1:af7a42f7d465 652 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 653 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 654
JMF 0:9d5134074d84 655 i = hts221.begin();
JMF 0:9d5134074d84 656 if( i )
JMF 0:9d5134074d84 657 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 658 else
JMF 0:9d5134074d84 659 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 660
JMF 0:9d5134074d84 661 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 662 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
JMF 0:9d5134074d84 663
elmkom 35:2e864bae3af0 664
stefanrousseau 11:e6602513730f 665 sensors_init();
stefanrousseau 12:7c94ec5069dc 666 read_sensors();
stefanrousseau 11:e6602513730f 667
JMF 0:9d5134074d84 668 // Initialize the modem
JMF 0:9d5134074d84 669 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 670 do {
fkellermavnet 14:0c353e212296 671 i=mdm_init();
fkellermavnet 14:0c353e212296 672 if (!i) {
fkellermavnet 14:0c353e212296 673 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 674 }
fkellermavnet 14:0c353e212296 675 } while (!i);
JMF 0:9d5134074d84 676
JMF 2:0e2ef866af95 677 //Software init
JMF 2:0e2ef866af95 678 software_init_mdm();
fkellermavnet 19:f89baed3bd6f 679
JMF 2:0e2ef866af95 680 // Resolve URL to IP address to connect to
JMF 2:0e2ef866af95 681 resolve_mdm();
JMF 0:9d5134074d84 682
stefanrousseau 3:26b3cc155f39 683 //Create a 1ms timer tick function:
stefanrousseau 3:26b3cc155f39 684 OneMsTicker.attach(OneMsFunction, 0.001f) ;
fkellermavnet 26:8d6e7e7cdcae 685
stefanrousseau 24:bd480d2aade4 686 iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
stefanrousseau 3:26b3cc155f39 687
fkellermavnet 26:8d6e7e7cdcae 688 // Open the socket (connect to the server)
fkellermavnet 25:e7996d22a7e6 689 sockopen_mdm();
stefanrousseau 3:26b3cc155f39 690
fkellermavnet 20:27a4f27254d0 691 // Set LED BLUE for partial init
fkellermavnet 20:27a4f27254d0 692 SetLedColor(0x4);
fkellermavnet 20:27a4f27254d0 693
JMF 2:0e2ef866af95 694 // Send and receive data perpetually
JMF 2:0e2ef866af95 695 while(1) {
fkellermavnet 20:27a4f27254d0 696 static unsigned ledOnce = 0;
stefanrousseau 3:26b3cc155f39 697 if (bTimerExpiredFlag)
stefanrousseau 3:26b3cc155f39 698 {
stefanrousseau 3:26b3cc155f39 699 bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 700 sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
stefanrousseau 3:26b3cc155f39 701 sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
stefanrousseau 4:f83bedd9cab4 702 read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
elmkom 35:2e864bae3af0 703 for(int i = 0;i<8;i++)
elmkom 35:2e864bae3af0 704 proximity = read_proximity(i);
stefanrousseau 3:26b3cc155f39 705 char modem_string[512];
stefanrousseau 3:26b3cc155f39 706 GenerateModemString(&modem_string[0]);
stefanrousseau 16:17c5916f2d12 707 printf(BLU "Sending to modem : %s" DEF "\n", modem_string);
stefanrousseau 3:26b3cc155f39 708 sockwrite_mdm(modem_string);
stefanrousseau 3:26b3cc155f39 709 sockread_mdm(&MySocketData, 1024, 20);
fkellermavnet 20:27a4f27254d0 710
fkellermavnet 20:27a4f27254d0 711 // If any non-zero response from server, make it GREEN one-time
fkellermavnet 20:27a4f27254d0 712 // then the actual FLOW responses will set the color.
fkellermavnet 20:27a4f27254d0 713 if ((!ledOnce) && (MySocketData.length() > 0))
fkellermavnet 20:27a4f27254d0 714 {
fkellermavnet 20:27a4f27254d0 715 ledOnce = 1;
fkellermavnet 20:27a4f27254d0 716 SetLedColor(0x2);
fkellermavnet 20:27a4f27254d0 717 }
fkellermavnet 20:27a4f27254d0 718
stefanrousseau 16:17c5916f2d12 719 printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]);
stefanrousseau 34:029e07b67a41 720 char myJsonResponse[512];
stefanrousseau 16:17c5916f2d12 721 if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
stefanrousseau 16:17c5916f2d12 722 {
stefanrousseau 16:17c5916f2d12 723 printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]);
stefanrousseau 16:17c5916f2d12 724 parse_JSON(&myJsonResponse[0]);
stefanrousseau 16:17c5916f2d12 725 }
stefanrousseau 16:17c5916f2d12 726 else
stefanrousseau 16:17c5916f2d12 727 {
stefanrousseau 16:17c5916f2d12 728 printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string
stefanrousseau 16:17c5916f2d12 729 parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
stefanrousseau 16:17c5916f2d12 730 }
stefanrousseau 3:26b3cc155f39 731 } //bTimerExpiredFlag
stefanrousseau 3:26b3cc155f39 732 } //forever loop
JMF 0:9d5134074d84 733 }