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