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