![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork with SeeedStudio Xadow GPS v2 enabled
Dependencies: FXOS8700CQ MODSERIAL XadowGPS mbed
Fork of Avnet_ATT_Cellular_IOT by
main.cpp@12:7c94ec5069dc, 2016-07-12 (annotated)
- Committer:
- stefanrousseau
- Date:
- Tue Jul 12 21:56:51 2016 +0000
- Revision:
- 12:7c94ec5069dc
- Parent:
- 11:e6602513730f
- Child:
- 14:0c353e212296
- Child:
- 15:61df4a452d38
Fixed GET string format and added 3 options for the sensors to be read.
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 |
stefanrousseau | 11:e6602513730f | 12 | |
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); |
JMF | 0:9d5134074d84 | 43 | DigitalOut led_red(LED_RED); |
JMF | 0:9d5134074d84 | 44 | DigitalOut led_green(LED_GREEN); |
JMF | 0:9d5134074d84 | 45 | |
JMF | 0:9d5134074d84 | 46 | DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot |
JMF | 0:9d5134074d84 | 47 | 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 | 48 | DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield |
JMF | 0:9d5134074d84 | 49 | |
JMF | 0:9d5134074d84 | 50 | DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active |
JMF | 0:9d5134074d84 | 51 | DigitalOut mdm_uart1_cts(PTD0); |
JMF | 0:9d5134074d84 | 52 | |
JMF | 0:9d5134074d84 | 53 | #define TOUPPER(a) (a) //toupper(a) |
JMF | 0:9d5134074d84 | 54 | |
JMF | 0:9d5134074d84 | 55 | const char ok_str[] = "OK"; |
JMF | 0:9d5134074d84 | 56 | const char error_str[] = "ERROR"; |
JMF | 0:9d5134074d84 | 57 | |
JMF | 0:9d5134074d84 | 58 | #define MDM_OK 0 |
JMF | 0:9d5134074d84 | 59 | #define MDM_ERR_TIMEOUT -1 |
JMF | 0:9d5134074d84 | 60 | |
JMF | 0:9d5134074d84 | 61 | #define MAX_AT_RSP_LEN 255 |
JMF | 0:9d5134074d84 | 62 | |
JMF | 0:9d5134074d84 | 63 | ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) { |
JMF | 0:9d5134074d84 | 64 | int cin = -1; |
JMF | 0:9d5134074d84 | 65 | int cin_last; |
JMF | 0:9d5134074d84 | 66 | |
JMF | 0:9d5134074d84 | 67 | if (NULL == buff || size == 0) { |
JMF | 0:9d5134074d84 | 68 | return -1; |
JMF | 0:9d5134074d84 | 69 | } |
JMF | 0:9d5134074d84 | 70 | |
JMF | 0:9d5134074d84 | 71 | size_t len = 0; |
JMF | 0:9d5134074d84 | 72 | Timer timer; |
JMF | 0:9d5134074d84 | 73 | timer.start(); |
JMF | 0:9d5134074d84 | 74 | while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) { |
JMF | 0:9d5134074d84 | 75 | if (mdm.readable()) { |
JMF | 0:9d5134074d84 | 76 | cin_last = cin; |
JMF | 0:9d5134074d84 | 77 | cin = mdm.getc(); |
JMF | 0:9d5134074d84 | 78 | if (isprint(cin)) { |
JMF | 0:9d5134074d84 | 79 | buff[len++] = (char)cin; |
JMF | 0:9d5134074d84 | 80 | continue; |
JMF | 0:9d5134074d84 | 81 | } else if (('\r' == cin_last) && ('\n' == cin)) { |
JMF | 0:9d5134074d84 | 82 | break; |
JMF | 0:9d5134074d84 | 83 | } |
JMF | 0:9d5134074d84 | 84 | } |
JMF | 0:9d5134074d84 | 85 | wait_ms(1); |
JMF | 0:9d5134074d84 | 86 | } |
JMF | 2:0e2ef866af95 | 87 | buff[len] = (char)NULL; |
JMF | 0:9d5134074d84 | 88 | |
JMF | 0:9d5134074d84 | 89 | return len; |
JMF | 0:9d5134074d84 | 90 | } |
JMF | 0:9d5134074d84 | 91 | |
JMF | 0:9d5134074d84 | 92 | int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) { |
JMF | 0:9d5134074d84 | 93 | if (cmd && strlen(cmd) > 0) { |
JMF | 0:9d5134074d84 | 94 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
JMF | 0:9d5134074d84 | 95 | printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); |
JMF | 0:9d5134074d84 | 96 | } |
JMF | 0:9d5134074d84 | 97 | mdm.printf("%s\r\n", cmd); |
JMF | 0:9d5134074d84 | 98 | } |
JMF | 0:9d5134074d84 | 99 | |
JMF | 0:9d5134074d84 | 100 | if (rsp_list) { |
JMF | 0:9d5134074d84 | 101 | Timer timer; |
JMF | 0:9d5134074d84 | 102 | char rsp[MAX_AT_RSP_LEN+1]; |
JMF | 0:9d5134074d84 | 103 | int len; |
JMF | 0:9d5134074d84 | 104 | |
JMF | 0:9d5134074d84 | 105 | timer.start(); |
JMF | 0:9d5134074d84 | 106 | while (timer.read_ms() < timeout_ms) { |
JMF | 0:9d5134074d84 | 107 | len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms()); |
JMF | 0:9d5134074d84 | 108 | |
JMF | 0:9d5134074d84 | 109 | if (len < 0) |
JMF | 0:9d5134074d84 | 110 | return MDM_ERR_TIMEOUT; |
JMF | 0:9d5134074d84 | 111 | |
JMF | 0:9d5134074d84 | 112 | if (len == 0) |
JMF | 0:9d5134074d84 | 113 | continue; |
JMF | 0:9d5134074d84 | 114 | |
JMF | 0:9d5134074d84 | 115 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
JMF | 0:9d5134074d84 | 116 | printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp); |
JMF | 0:9d5134074d84 | 117 | } |
JMF | 0:9d5134074d84 | 118 | |
JMF | 0:9d5134074d84 | 119 | if (rsp_list) { |
JMF | 0:9d5134074d84 | 120 | int rsp_idx = 0; |
JMF | 0:9d5134074d84 | 121 | while (rsp_list[rsp_idx]) { |
JMF | 0:9d5134074d84 | 122 | if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) { |
JMF | 0:9d5134074d84 | 123 | return rsp_idx; |
JMF | 0:9d5134074d84 | 124 | } |
JMF | 0:9d5134074d84 | 125 | rsp_idx++; |
JMF | 0:9d5134074d84 | 126 | } |
JMF | 0:9d5134074d84 | 127 | } |
JMF | 0:9d5134074d84 | 128 | } |
JMF | 0:9d5134074d84 | 129 | return MDM_ERR_TIMEOUT; |
JMF | 0:9d5134074d84 | 130 | } |
JMF | 0:9d5134074d84 | 131 | return MDM_OK; |
JMF | 0:9d5134074d84 | 132 | } |
JMF | 0:9d5134074d84 | 133 | |
JMF | 0:9d5134074d84 | 134 | int mdm_init(void) { |
JMF | 0:9d5134074d84 | 135 | // disable signal level translator |
JMF | 0:9d5134074d84 | 136 | shield_3v3_1v8_sig_trans_ena = 0; |
JMF | 0:9d5134074d84 | 137 | |
JMF | 0:9d5134074d84 | 138 | // power modem on //off |
JMF | 0:9d5134074d84 | 139 | mdm_power_on = 0; //1; |
JMF | 0:9d5134074d84 | 140 | |
JMF | 0:9d5134074d84 | 141 | // insure modem boots into normal operating mode |
JMF | 0:9d5134074d84 | 142 | // and does not go to sleep when powered on |
JMF | 0:9d5134074d84 | 143 | mdm_uart2_rx_boot_mode_sel = 1; |
JMF | 0:9d5134074d84 | 144 | mdm_wakeup_in = 1; |
JMF | 0:9d5134074d84 | 145 | |
JMF | 0:9d5134074d84 | 146 | // initialze comm with the modem |
JMF | 0:9d5134074d84 | 147 | mdm.baud(115200); |
JMF | 2:0e2ef866af95 | 148 | // clear out potential garbage |
JMF | 2:0e2ef866af95 | 149 | while (mdm.readable()) |
JMF | 2:0e2ef866af95 | 150 | mdm.getc(); |
JMF | 2:0e2ef866af95 | 151 | |
JMF | 0:9d5134074d84 | 152 | mdm_uart1_cts = 0; |
JMF | 0:9d5134074d84 | 153 | |
JMF | 0:9d5134074d84 | 154 | // enable the signal level translator to start |
JMF | 0:9d5134074d84 | 155 | // modem reset process (modem will be powered down) |
JMF | 0:9d5134074d84 | 156 | shield_3v3_1v8_sig_trans_ena = 1; |
JMF | 0:9d5134074d84 | 157 | |
JMF | 0:9d5134074d84 | 158 | // Give the modem 60 secons to start responding by |
JMF | 0:9d5134074d84 | 159 | // sending simple 'AT' commands to modem once per second. |
JMF | 0:9d5134074d84 | 160 | Timer timer; |
JMF | 0:9d5134074d84 | 161 | timer.start(); |
JMF | 0:9d5134074d84 | 162 | while (timer.read() < 60) { |
JMF | 0:9d5134074d84 | 163 | const char * rsp_lst[] = { ok_str, error_str, NULL }; |
JMF | 0:9d5134074d84 | 164 | int rc = mdm_sendAtCmd("AT", rsp_lst, 500); |
JMF | 0:9d5134074d84 | 165 | if (rc == 0) |
JMF | 0:9d5134074d84 | 166 | return timer.read(); |
JMF | 0:9d5134074d84 | 167 | wait_ms(1000 - (timer.read_ms() % 1000)); |
JMF | 0:9d5134074d84 | 168 | pc.printf("\r%d",timer.read_ms()/1000); |
JMF | 0:9d5134074d84 | 169 | } |
JMF | 0:9d5134074d84 | 170 | return false; |
JMF | 0:9d5134074d84 | 171 | } |
JMF | 0:9d5134074d84 | 172 | |
JMF | 2:0e2ef866af95 | 173 | int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) { |
JMF | 2:0e2ef866af95 | 174 | static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars) |
fkellermavnet | 6:713b4cbf1a7d | 175 | size_t n = strlen(cmd); |
fkellermavnet | 6:713b4cbf1a7d | 176 | if (cmd && n > 0) { |
JMF | 2:0e2ef866af95 | 177 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
JMF | 2:0e2ef866af95 | 178 | printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); |
JMF | 2:0e2ef866af95 | 179 | } |
fkellermavnet | 6:713b4cbf1a7d | 180 | while (n--) { |
fkellermavnet | 6:713b4cbf1a7d | 181 | mdm.putc(*cmd++); |
fkellermavnet | 6:713b4cbf1a7d | 182 | wait_ms(1); |
fkellermavnet | 6:713b4cbf1a7d | 183 | }; |
fkellermavnet | 6:713b4cbf1a7d | 184 | mdm.putc('\r'); |
fkellermavnet | 6:713b4cbf1a7d | 185 | wait_ms(1); |
fkellermavnet | 6:713b4cbf1a7d | 186 | mdm.putc('\n'); |
fkellermavnet | 6:713b4cbf1a7d | 187 | wait_ms(1); |
JMF | 2:0e2ef866af95 | 188 | } |
JMF | 2:0e2ef866af95 | 189 | |
JMF | 2:0e2ef866af95 | 190 | if (rsp_list) { |
JMF | 2:0e2ef866af95 | 191 | rsp->erase(); // Clean up from prior cmd response |
JMF | 2:0e2ef866af95 | 192 | *len = 0; |
JMF | 2:0e2ef866af95 | 193 | Timer timer; |
JMF | 2:0e2ef866af95 | 194 | timer.start(); |
JMF | 2:0e2ef866af95 | 195 | while (timer.read_ms() < timeout_ms) { |
JMF | 2:0e2ef866af95 | 196 | int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms()); |
JMF | 2:0e2ef866af95 | 197 | |
JMF | 2:0e2ef866af95 | 198 | if (lenCmd == 0) |
JMF | 2:0e2ef866af95 | 199 | continue; |
JMF | 2:0e2ef866af95 | 200 | |
JMF | 2:0e2ef866af95 | 201 | if (lenCmd < 0) |
JMF | 2:0e2ef866af95 | 202 | return MDM_ERR_TIMEOUT; |
JMF | 2:0e2ef866af95 | 203 | else { |
JMF | 2:0e2ef866af95 | 204 | *len += lenCmd; |
JMF | 2:0e2ef866af95 | 205 | *rsp += cmd_buf; |
JMF | 2:0e2ef866af95 | 206 | } |
JMF | 2:0e2ef866af95 | 207 | |
JMF | 2:0e2ef866af95 | 208 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
JMF | 2:0e2ef866af95 | 209 | printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf); |
JMF | 2:0e2ef866af95 | 210 | } |
JMF | 2:0e2ef866af95 | 211 | |
JMF | 2:0e2ef866af95 | 212 | int rsp_idx = 0; |
JMF | 2:0e2ef866af95 | 213 | while (rsp_list[rsp_idx]) { |
JMF | 2:0e2ef866af95 | 214 | if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) { |
JMF | 2:0e2ef866af95 | 215 | return rsp_idx; |
JMF | 2:0e2ef866af95 | 216 | } |
JMF | 2:0e2ef866af95 | 217 | rsp_idx++; |
JMF | 2:0e2ef866af95 | 218 | } |
JMF | 2:0e2ef866af95 | 219 | } |
JMF | 2:0e2ef866af95 | 220 | return MDM_ERR_TIMEOUT; |
JMF | 2:0e2ef866af95 | 221 | } |
JMF | 2:0e2ef866af95 | 222 | pc.printf("D %s",rsp); |
JMF | 2:0e2ef866af95 | 223 | return MDM_OK; |
JMF | 2:0e2ef866af95 | 224 | } |
JMF | 2:0e2ef866af95 | 225 | |
JMF | 2:0e2ef866af95 | 226 | void reinitialize_mdm(void) |
JMF | 2:0e2ef866af95 | 227 | { |
JMF | 2:0e2ef866af95 | 228 | // Initialize the modem |
JMF | 2:0e2ef866af95 | 229 | printf(GRN "Modem RE-initializing..." DEF "\r\n"); |
JMF | 2:0e2ef866af95 | 230 | if (!mdm_init()) { |
JMF | 2:0e2ef866af95 | 231 | printf(RED "\n\rModem RE-initialization failed!" DEF "\n"); |
JMF | 2:0e2ef866af95 | 232 | } |
JMF | 2:0e2ef866af95 | 233 | printf("\r\n"); |
JMF | 2:0e2ef866af95 | 234 | } |
JMF | 2:0e2ef866af95 | 235 | // These are built on the fly |
JMF | 2:0e2ef866af95 | 236 | string MyServerIpAddress; |
JMF | 2:0e2ef866af95 | 237 | string MySocketData; |
JMF | 2:0e2ef866af95 | 238 | |
JMF | 2:0e2ef866af95 | 239 | // These are to be built on the fly |
JMF | 2:0e2ef866af95 | 240 | string my_temp; |
JMF | 2:0e2ef866af95 | 241 | string my_humidity; |
JMF | 2:0e2ef866af95 | 242 | |
JMF | 0:9d5134074d84 | 243 | #define CTOF(x) ((x)*1.8+32) |
JMF | 0:9d5134074d84 | 244 | |
stefanrousseau | 3:26b3cc155f39 | 245 | //******************************************************************************************************************************************** |
stefanrousseau | 12:7c94ec5069dc | 246 | //* Create string with sensor readings that can be sent to flow as an HTTP get |
stefanrousseau | 3:26b3cc155f39 | 247 | //******************************************************************************************************************************************** |
stefanrousseau | 12:7c94ec5069dc | 248 | K64F_Sensors_t SENSOR_DATA = |
stefanrousseau | 3:26b3cc155f39 | 249 | { |
stefanrousseau | 12:7c94ec5069dc | 250 | .Temperature = "0", |
stefanrousseau | 12:7c94ec5069dc | 251 | .Humidity = "0", |
stefanrousseau | 12:7c94ec5069dc | 252 | .AccelX = "0", |
stefanrousseau | 12:7c94ec5069dc | 253 | .AccelY = "0", |
stefanrousseau | 12:7c94ec5069dc | 254 | .AccelZ = "0", |
stefanrousseau | 12:7c94ec5069dc | 255 | .MagnetometerX = "0", |
stefanrousseau | 12:7c94ec5069dc | 256 | .MagnetometerY = "0", |
stefanrousseau | 12:7c94ec5069dc | 257 | .MagnetometerZ = "0", |
stefanrousseau | 12:7c94ec5069dc | 258 | .AmbientLightVis = "0", |
stefanrousseau | 12:7c94ec5069dc | 259 | .AmbientLightIr = "0", |
stefanrousseau | 12:7c94ec5069dc | 260 | .UVindex = "0", |
stefanrousseau | 12:7c94ec5069dc | 261 | .Proximity = "0", |
stefanrousseau | 12:7c94ec5069dc | 262 | .Temperature_Si7020 = "0", |
stefanrousseau | 12:7c94ec5069dc | 263 | .Humidity_Si7020 = "0" |
stefanrousseau | 3:26b3cc155f39 | 264 | }; |
stefanrousseau | 12:7c94ec5069dc | 265 | |
stefanrousseau | 3:26b3cc155f39 | 266 | void GenerateModemString(char * modem_string) |
stefanrousseau | 3:26b3cc155f39 | 267 | { |
stefanrousseau | 12:7c94ec5069dc | 268 | switch(iSensorsToReport) |
stefanrousseau | 12:7c94ec5069dc | 269 | { |
stefanrousseau | 12:7c94ec5069dc | 270 | case TEMP_HUMIDITY_ONLY: |
stefanrousseau | 12:7c94ec5069dc | 271 | { |
stefanrousseau | 12:7c94ec5069dc | 272 | 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 | 273 | break; |
stefanrousseau | 12:7c94ec5069dc | 274 | } |
stefanrousseau | 12:7c94ec5069dc | 275 | case TEMP_HUMIDITY_ACCELEROMETER: |
stefanrousseau | 12:7c94ec5069dc | 276 | { |
stefanrousseau | 12:7c94ec5069dc | 277 | 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 | 278 | break; |
stefanrousseau | 12:7c94ec5069dc | 279 | } |
stefanrousseau | 12:7c94ec5069dc | 280 | case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS: |
stefanrousseau | 12:7c94ec5069dc | 281 | { |
stefanrousseau | 12:7c94ec5069dc | 282 | 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 | 283 | break; |
stefanrousseau | 12:7c94ec5069dc | 284 | } |
stefanrousseau | 12:7c94ec5069dc | 285 | default: |
stefanrousseau | 12:7c94ec5069dc | 286 | { |
stefanrousseau | 12:7c94ec5069dc | 287 | sprintf(modem_string, "Invalid sensor selected\r\n\r\n"); |
stefanrousseau | 12:7c94ec5069dc | 288 | break; |
stefanrousseau | 12:7c94ec5069dc | 289 | } |
stefanrousseau | 12:7c94ec5069dc | 290 | } //switch(*ucCommandIndex) |
stefanrousseau | 3:26b3cc155f39 | 291 | } //GenerateModemString |
stefanrousseau | 3:26b3cc155f39 | 292 | |
stefanrousseau | 3:26b3cc155f39 | 293 | |
stefanrousseau | 3:26b3cc155f39 | 294 | //Periodic timer |
stefanrousseau | 3:26b3cc155f39 | 295 | Ticker OneMsTicker; |
stefanrousseau | 3:26b3cc155f39 | 296 | volatile bool bTimerExpiredFlag = false; |
stefanrousseau | 3:26b3cc155f39 | 297 | int OneMsTicks = 0; |
stefanrousseau | 3:26b3cc155f39 | 298 | int iTimer1Interval_ms = 1000; |
stefanrousseau | 3:26b3cc155f39 | 299 | //******************************************************************************************************************************************** |
stefanrousseau | 3:26b3cc155f39 | 300 | //* Periodic 1ms timer tick |
stefanrousseau | 3:26b3cc155f39 | 301 | //******************************************************************************************************************************************** |
stefanrousseau | 3:26b3cc155f39 | 302 | void OneMsFunction() |
stefanrousseau | 3:26b3cc155f39 | 303 | { |
stefanrousseau | 3:26b3cc155f39 | 304 | OneMsTicks++; |
stefanrousseau | 3:26b3cc155f39 | 305 | if ((OneMsTicks % iTimer1Interval_ms) == 0) |
stefanrousseau | 3:26b3cc155f39 | 306 | { |
stefanrousseau | 3:26b3cc155f39 | 307 | bTimerExpiredFlag = true; |
stefanrousseau | 3:26b3cc155f39 | 308 | } |
stefanrousseau | 3:26b3cc155f39 | 309 | } //OneMsFunction() |
stefanrousseau | 3:26b3cc155f39 | 310 | |
JMF | 0:9d5134074d84 | 311 | int main() { |
JMF | 2:0e2ef866af95 | 312 | int i; |
JMF | 0:9d5134074d84 | 313 | HTS221 hts221; |
JMF | 0:9d5134074d84 | 314 | pc.baud(115200); |
JMF | 0:9d5134074d84 | 315 | |
JMF | 0:9d5134074d84 | 316 | void hts221_init(void); |
JMF | 0:9d5134074d84 | 317 | |
JMF | 1:af7a42f7d465 | 318 | pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r"); |
JMF | 0:9d5134074d84 | 319 | pc.printf(GRN "Initialize the HTS221\n\r"); |
JMF | 0:9d5134074d84 | 320 | |
JMF | 0:9d5134074d84 | 321 | i = hts221.begin(); |
JMF | 0:9d5134074d84 | 322 | if( i ) |
JMF | 0:9d5134074d84 | 323 | pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i); |
JMF | 0:9d5134074d84 | 324 | else |
JMF | 0:9d5134074d84 | 325 | pc.printf(RED "HTS221 NOT DETECTED!!\n\r"); |
JMF | 0:9d5134074d84 | 326 | |
JMF | 0:9d5134074d84 | 327 | printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature())); |
JMF | 0:9d5134074d84 | 328 | printf("Humid is: %02d %%\n\r",hts221.readHumidity()); |
JMF | 0:9d5134074d84 | 329 | |
stefanrousseau | 11:e6602513730f | 330 | sensors_init(); |
stefanrousseau | 12:7c94ec5069dc | 331 | read_sensors(); |
stefanrousseau | 11:e6602513730f | 332 | |
JMF | 0:9d5134074d84 | 333 | // Initialize the modem |
JMF | 0:9d5134074d84 | 334 | printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n"); |
JMF | 0:9d5134074d84 | 335 | i=mdm_init(); |
JMF | 0:9d5134074d84 | 336 | if (!i) { |
JMF | 0:9d5134074d84 | 337 | pc.printf(RED "Modem initialization failed!" DEF "\n"); |
JMF | 0:9d5134074d84 | 338 | while (1); |
JMF | 0:9d5134074d84 | 339 | } |
JMF | 0:9d5134074d84 | 340 | |
JMF | 2:0e2ef866af95 | 341 | //Software init |
JMF | 2:0e2ef866af95 | 342 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 343 | |
JMF | 2:0e2ef866af95 | 344 | // Resolve URL to IP address to connect to |
JMF | 2:0e2ef866af95 | 345 | resolve_mdm(); |
JMF | 0:9d5134074d84 | 346 | |
stefanrousseau | 3:26b3cc155f39 | 347 | //Create a 1ms timer tick function: |
stefanrousseau | 3:26b3cc155f39 | 348 | OneMsTicker.attach(OneMsFunction, 0.001f) ; |
stefanrousseau | 3:26b3cc155f39 | 349 | iTimer1Interval_ms = 5000; //5 seconds |
stefanrousseau | 3:26b3cc155f39 | 350 | |
JMF | 2:0e2ef866af95 | 351 | // Send and receive data perpetually |
JMF | 2:0e2ef866af95 | 352 | while(1) { |
stefanrousseau | 3:26b3cc155f39 | 353 | if (bTimerExpiredFlag) |
stefanrousseau | 3:26b3cc155f39 | 354 | { |
stefanrousseau | 3:26b3cc155f39 | 355 | bTimerExpiredFlag = false; |
stefanrousseau | 3:26b3cc155f39 | 356 | sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature())); |
stefanrousseau | 3:26b3cc155f39 | 357 | sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity()); |
stefanrousseau | 4:f83bedd9cab4 | 358 | read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor |
stefanrousseau | 3:26b3cc155f39 | 359 | sockopen_mdm(); |
stefanrousseau | 3:26b3cc155f39 | 360 | char modem_string[512]; |
stefanrousseau | 3:26b3cc155f39 | 361 | GenerateModemString(&modem_string[0]); |
stefanrousseau | 3:26b3cc155f39 | 362 | printf(DEF "Sending to modem : %s\n", modem_string); |
stefanrousseau | 3:26b3cc155f39 | 363 | sockwrite_mdm(modem_string); |
stefanrousseau | 3:26b3cc155f39 | 364 | sockread_mdm(&MySocketData, 1024, 20); |
stefanrousseau | 3:26b3cc155f39 | 365 | sockclose_mdm(); |
stefanrousseau | 3:26b3cc155f39 | 366 | } //bTimerExpiredFlag |
stefanrousseau | 3:26b3cc155f39 | 367 | } //forever loop |
JMF | 2:0e2ef866af95 | 368 | |
stefanrousseau | 12:7c94ec5069dc | 369 | #if (0) |
JMF | 2:0e2ef866af95 | 370 | string * pStr; |
JMF | 2:0e2ef866af95 | 371 | while (1) |
JMF | 2:0e2ef866af95 | 372 | { |
JMF | 2:0e2ef866af95 | 373 | send_wnc_cmd("AT", &pStr, WNC_TIMEOUT_MS); |
JMF | 0:9d5134074d84 | 374 | } |
stefanrousseau | 12:7c94ec5069dc | 375 | #endif |
JMF | 0:9d5134074d84 | 376 | } |