![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.
Dependencies: FXOS8700CQ MODSERIAL mbed
Pubnub demo for AT&T IoT Starter Kit
This demo is functionally similar to the Flow demo, so you can find general information here: https://developer.mbed.org/users/JMF/code/Avnet_ATT_Cellular_IOT/.
The only difference is that we use Pubnub to publish the measurements and subscribe to receiving the instructions to set the LED.
Settings
Pubnub related settings are:
Pubnub settings in `config_me.h`
PUBNUB_SUBSCRIBE_KEY PUBNUB_PUBLISH_KEY PUBNUB_CHANNEL
All are documented in their respective comments.
Pubnub context class
Similar to Pubnub SDKs, we provide a Pubnub context class. It is defined in pubnub.h
header file and implemented in pubnub.cpp
.
It provides only the fundamental "publish" and "subscribe" methods. They are documented in the header file.
This class is reusable in other code (it is not specific to this demo), it has a very narrow interface to the AT&T IoT cellular modem code. For example of use, you can look at the main()
(in main.c
).
Sample of published data
Published message w/measurement data
{"serial":"vstarterkit001","temp":89.61,"humidity":35,"accelX":0.97,"accelY":0.013,"accelZ":-0.038}
Don't worry, nobody got burnt, the temperature is in degrees Fahrenheit. :)
Publish a message (from, say, the Pubnub console http://pubnub.com/console) of the form {"LED":<name-of-the-color>}
on the channel that this demo listens to (default is hello_world
) to turn the LED to that color on the Starter Kit:
Turn LED to red
{"LED":"Red"}
Turn LED to green
{"LED":"Green"}
Turn LED to blue
{"LED":"Blue"}
cell_modem.cpp@61:f6b93129f954, 2016-08-01 (annotated)
- Committer:
- stefanrousseau
- Date:
- Mon Aug 01 18:29:04 2016 +0000
- Revision:
- 61:f6b93129f954
- Child:
- 63:90d7c69993cd
Moved cellular modem routines from main.cpp to cell_modem.cpp; Moved HTS221 routines from main.cpp to sensors.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefanrousseau | 61:f6b93129f954 | 1 | #include "mbed.h" |
stefanrousseau | 61:f6b93129f954 | 2 | #include <cctype> |
stefanrousseau | 61:f6b93129f954 | 3 | #include <string> |
stefanrousseau | 61:f6b93129f954 | 4 | #include "SerialBuffered.h" |
stefanrousseau | 61:f6b93129f954 | 5 | |
stefanrousseau | 61:f6b93129f954 | 6 | #include "config_me.h" |
stefanrousseau | 61:f6b93129f954 | 7 | #include "wnc_control.h" |
stefanrousseau | 61:f6b93129f954 | 8 | #include "hardware.h" |
stefanrousseau | 61:f6b93129f954 | 9 | |
stefanrousseau | 61:f6b93129f954 | 10 | #define MDM_DBG_OFF 0 |
stefanrousseau | 61:f6b93129f954 | 11 | #define MDM_DBG_AT_CMDS (1 << 0) |
stefanrousseau | 61:f6b93129f954 | 12 | int mdm_dbgmask = MDM_DBG_OFF; |
stefanrousseau | 61:f6b93129f954 | 13 | |
stefanrousseau | 61:f6b93129f954 | 14 | #define WNC_WAIT_FOR_AT_CMD_MS 40 |
stefanrousseau | 61:f6b93129f954 | 15 | |
stefanrousseau | 61:f6b93129f954 | 16 | DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot |
stefanrousseau | 61:f6b93129f954 | 17 | DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem) |
stefanrousseau | 61:f6b93129f954 | 18 | DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield |
stefanrousseau | 61:f6b93129f954 | 19 | |
stefanrousseau | 61:f6b93129f954 | 20 | DigitalOut mdm_reset(PTC12); // active high |
stefanrousseau | 61:f6b93129f954 | 21 | |
stefanrousseau | 61:f6b93129f954 | 22 | DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active |
stefanrousseau | 61:f6b93129f954 | 23 | DigitalOut mdm_uart1_cts(PTD0); |
stefanrousseau | 61:f6b93129f954 | 24 | |
stefanrousseau | 61:f6b93129f954 | 25 | #define TOUPPER(a) (a) //toupper(a) |
stefanrousseau | 61:f6b93129f954 | 26 | |
stefanrousseau | 61:f6b93129f954 | 27 | const char ok_str[] = "OK"; |
stefanrousseau | 61:f6b93129f954 | 28 | const char error_str[] = "ERROR"; |
stefanrousseau | 61:f6b93129f954 | 29 | |
stefanrousseau | 61:f6b93129f954 | 30 | #define MDM_OK 0 |
stefanrousseau | 61:f6b93129f954 | 31 | #define MDM_ERR_TIMEOUT -1 |
stefanrousseau | 61:f6b93129f954 | 32 | |
stefanrousseau | 61:f6b93129f954 | 33 | #define MAX_AT_RSP_LEN 255 |
stefanrousseau | 61:f6b93129f954 | 34 | |
stefanrousseau | 61:f6b93129f954 | 35 | ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) { |
stefanrousseau | 61:f6b93129f954 | 36 | int cin = -1; |
stefanrousseau | 61:f6b93129f954 | 37 | int cin_last; |
stefanrousseau | 61:f6b93129f954 | 38 | |
stefanrousseau | 61:f6b93129f954 | 39 | if (NULL == buff || size == 0) { |
stefanrousseau | 61:f6b93129f954 | 40 | return -1; |
stefanrousseau | 61:f6b93129f954 | 41 | } |
stefanrousseau | 61:f6b93129f954 | 42 | |
stefanrousseau | 61:f6b93129f954 | 43 | size_t len = 0; |
stefanrousseau | 61:f6b93129f954 | 44 | Timer timer; |
stefanrousseau | 61:f6b93129f954 | 45 | timer.start(); |
stefanrousseau | 61:f6b93129f954 | 46 | while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) { |
stefanrousseau | 61:f6b93129f954 | 47 | if (mdm.readable()) { |
stefanrousseau | 61:f6b93129f954 | 48 | cin_last = cin; |
stefanrousseau | 61:f6b93129f954 | 49 | cin = mdm.getc(); |
stefanrousseau | 61:f6b93129f954 | 50 | if (isprint(cin)) { |
stefanrousseau | 61:f6b93129f954 | 51 | buff[len++] = (char)cin; |
stefanrousseau | 61:f6b93129f954 | 52 | continue; |
stefanrousseau | 61:f6b93129f954 | 53 | } else if (('\r' == cin_last) && ('\n' == cin)) { |
stefanrousseau | 61:f6b93129f954 | 54 | break; |
stefanrousseau | 61:f6b93129f954 | 55 | } |
stefanrousseau | 61:f6b93129f954 | 56 | } |
stefanrousseau | 61:f6b93129f954 | 57 | // wait_ms(1); |
stefanrousseau | 61:f6b93129f954 | 58 | } |
stefanrousseau | 61:f6b93129f954 | 59 | buff[len] = (char)NULL; |
stefanrousseau | 61:f6b93129f954 | 60 | |
stefanrousseau | 61:f6b93129f954 | 61 | return len; |
stefanrousseau | 61:f6b93129f954 | 62 | } |
stefanrousseau | 61:f6b93129f954 | 63 | |
stefanrousseau | 61:f6b93129f954 | 64 | int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) { |
stefanrousseau | 61:f6b93129f954 | 65 | // Per WNC wait: |
stefanrousseau | 61:f6b93129f954 | 66 | wait_ms(WNC_WAIT_FOR_AT_CMD_MS); |
stefanrousseau | 61:f6b93129f954 | 67 | |
stefanrousseau | 61:f6b93129f954 | 68 | if (cmd && strlen(cmd) > 0) { |
stefanrousseau | 61:f6b93129f954 | 69 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
stefanrousseau | 61:f6b93129f954 | 70 | printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); |
stefanrousseau | 61:f6b93129f954 | 71 | } |
stefanrousseau | 61:f6b93129f954 | 72 | mdm.puts(cmd); |
stefanrousseau | 61:f6b93129f954 | 73 | mdm.puts("\r\n"); |
stefanrousseau | 61:f6b93129f954 | 74 | } |
stefanrousseau | 61:f6b93129f954 | 75 | |
stefanrousseau | 61:f6b93129f954 | 76 | if (rsp_list) { |
stefanrousseau | 61:f6b93129f954 | 77 | Timer timer; |
stefanrousseau | 61:f6b93129f954 | 78 | char rsp[MAX_AT_RSP_LEN+1]; |
stefanrousseau | 61:f6b93129f954 | 79 | int len; |
stefanrousseau | 61:f6b93129f954 | 80 | |
stefanrousseau | 61:f6b93129f954 | 81 | timer.start(); |
stefanrousseau | 61:f6b93129f954 | 82 | while (timer.read_ms() < timeout_ms) { |
stefanrousseau | 61:f6b93129f954 | 83 | len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms()); |
stefanrousseau | 61:f6b93129f954 | 84 | |
stefanrousseau | 61:f6b93129f954 | 85 | if (len < 0) |
stefanrousseau | 61:f6b93129f954 | 86 | return MDM_ERR_TIMEOUT; |
stefanrousseau | 61:f6b93129f954 | 87 | |
stefanrousseau | 61:f6b93129f954 | 88 | if (len == 0) |
stefanrousseau | 61:f6b93129f954 | 89 | continue; |
stefanrousseau | 61:f6b93129f954 | 90 | |
stefanrousseau | 61:f6b93129f954 | 91 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
stefanrousseau | 61:f6b93129f954 | 92 | printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp); |
stefanrousseau | 61:f6b93129f954 | 93 | } |
stefanrousseau | 61:f6b93129f954 | 94 | |
stefanrousseau | 61:f6b93129f954 | 95 | if (rsp_list) { |
stefanrousseau | 61:f6b93129f954 | 96 | int rsp_idx = 0; |
stefanrousseau | 61:f6b93129f954 | 97 | while (rsp_list[rsp_idx]) { |
stefanrousseau | 61:f6b93129f954 | 98 | if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) { |
stefanrousseau | 61:f6b93129f954 | 99 | return rsp_idx; |
stefanrousseau | 61:f6b93129f954 | 100 | } |
stefanrousseau | 61:f6b93129f954 | 101 | rsp_idx++; |
stefanrousseau | 61:f6b93129f954 | 102 | } |
stefanrousseau | 61:f6b93129f954 | 103 | } |
stefanrousseau | 61:f6b93129f954 | 104 | } |
stefanrousseau | 61:f6b93129f954 | 105 | return MDM_ERR_TIMEOUT; |
stefanrousseau | 61:f6b93129f954 | 106 | } |
stefanrousseau | 61:f6b93129f954 | 107 | return MDM_OK; |
stefanrousseau | 61:f6b93129f954 | 108 | } |
stefanrousseau | 61:f6b93129f954 | 109 | |
stefanrousseau | 61:f6b93129f954 | 110 | int mdm_init(void) { |
stefanrousseau | 61:f6b93129f954 | 111 | // Hard reset the modem (doesn't go through |
stefanrousseau | 61:f6b93129f954 | 112 | // the signal level translator) |
stefanrousseau | 61:f6b93129f954 | 113 | mdm_reset = 0; |
stefanrousseau | 61:f6b93129f954 | 114 | |
stefanrousseau | 61:f6b93129f954 | 115 | // disable signal level translator (necessary |
stefanrousseau | 61:f6b93129f954 | 116 | // for the modem to boot properly). All signals |
stefanrousseau | 61:f6b93129f954 | 117 | // except mdm_reset go through the level translator |
stefanrousseau | 61:f6b93129f954 | 118 | // and have internal pull-up/down in the module. While |
stefanrousseau | 61:f6b93129f954 | 119 | // the level translator is disabled, these pins will |
stefanrousseau | 61:f6b93129f954 | 120 | // be in the correct state. |
stefanrousseau | 61:f6b93129f954 | 121 | shield_3v3_1v8_sig_trans_ena = 0; |
stefanrousseau | 61:f6b93129f954 | 122 | |
stefanrousseau | 61:f6b93129f954 | 123 | // While the level translator is disabled and ouptut pins |
stefanrousseau | 61:f6b93129f954 | 124 | // are tristated, make sure the inputs are in the same state |
stefanrousseau | 61:f6b93129f954 | 125 | // as the WNC Module pins so that when the level translator is |
stefanrousseau | 61:f6b93129f954 | 126 | // enabled, there are no differences. |
stefanrousseau | 61:f6b93129f954 | 127 | mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high |
stefanrousseau | 61:f6b93129f954 | 128 | mdm_power_on = 0; // powr_on should be low |
stefanrousseau | 61:f6b93129f954 | 129 | mdm_wakeup_in = 1; // wake-up should be high |
stefanrousseau | 61:f6b93129f954 | 130 | mdm_uart1_cts = 0; // indicate that it is ok to send |
stefanrousseau | 61:f6b93129f954 | 131 | |
stefanrousseau | 61:f6b93129f954 | 132 | // Now, wait for the WNC Module to perform its initial boot correctly |
stefanrousseau | 61:f6b93129f954 | 133 | wait(1.0); |
stefanrousseau | 61:f6b93129f954 | 134 | |
stefanrousseau | 61:f6b93129f954 | 135 | // The WNC module initializes comms at 115200 8N1 so set it up |
stefanrousseau | 61:f6b93129f954 | 136 | mdm.baud(115200); |
stefanrousseau | 61:f6b93129f954 | 137 | |
stefanrousseau | 61:f6b93129f954 | 138 | //Now, enable the level translator, the input pins should now be the |
stefanrousseau | 61:f6b93129f954 | 139 | //same as how the M14A module is driving them with internal pull ups/downs. |
stefanrousseau | 61:f6b93129f954 | 140 | //When enabled, there will be no changes in these 4 pins... |
stefanrousseau | 61:f6b93129f954 | 141 | shield_3v3_1v8_sig_trans_ena = 1; |
stefanrousseau | 61:f6b93129f954 | 142 | |
stefanrousseau | 61:f6b93129f954 | 143 | // Now, give the modem 60 seconds to start responding by |
stefanrousseau | 61:f6b93129f954 | 144 | // sending simple 'AT' commands to modem once per second. |
stefanrousseau | 61:f6b93129f954 | 145 | Timer timer; |
stefanrousseau | 61:f6b93129f954 | 146 | timer.start(); |
stefanrousseau | 61:f6b93129f954 | 147 | while (timer.read() < 60) { |
stefanrousseau | 61:f6b93129f954 | 148 | const char * rsp_lst[] = { ok_str, error_str, NULL }; |
stefanrousseau | 61:f6b93129f954 | 149 | int rc = mdm_sendAtCmd("AT", rsp_lst, 500); |
stefanrousseau | 61:f6b93129f954 | 150 | if (rc == 0) |
stefanrousseau | 61:f6b93129f954 | 151 | return true; //timer.read(); |
stefanrousseau | 61:f6b93129f954 | 152 | wait_ms(1000 - (timer.read_ms() % 1000)); |
stefanrousseau | 61:f6b93129f954 | 153 | pc.printf("\r%d",timer.read_ms()/1000); |
stefanrousseau | 61:f6b93129f954 | 154 | } |
stefanrousseau | 61:f6b93129f954 | 155 | return false; |
stefanrousseau | 61:f6b93129f954 | 156 | } |
stefanrousseau | 61:f6b93129f954 | 157 | |
stefanrousseau | 61:f6b93129f954 | 158 | int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) { |
stefanrousseau | 61:f6b93129f954 | 159 | static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars) |
stefanrousseau | 61:f6b93129f954 | 160 | size_t n = strlen(cmd); |
stefanrousseau | 61:f6b93129f954 | 161 | |
stefanrousseau | 61:f6b93129f954 | 162 | // Per WNC wait: |
stefanrousseau | 61:f6b93129f954 | 163 | wait_ms(WNC_WAIT_FOR_AT_CMD_MS); |
stefanrousseau | 61:f6b93129f954 | 164 | |
stefanrousseau | 61:f6b93129f954 | 165 | if (cmd && n > 0) { |
stefanrousseau | 61:f6b93129f954 | 166 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
stefanrousseau | 61:f6b93129f954 | 167 | printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); |
stefanrousseau | 61:f6b93129f954 | 168 | } |
stefanrousseau | 61:f6b93129f954 | 169 | // mdm.puts(cmd); |
stefanrousseau | 61:f6b93129f954 | 170 | // mdm.puts("\r\n"); |
stefanrousseau | 61:f6b93129f954 | 171 | while (n--) { |
stefanrousseau | 61:f6b93129f954 | 172 | mdm.putc(*cmd++); |
stefanrousseau | 61:f6b93129f954 | 173 | wait_us(1000); |
stefanrousseau | 61:f6b93129f954 | 174 | }; |
stefanrousseau | 61:f6b93129f954 | 175 | mdm.putc('\r'); |
stefanrousseau | 61:f6b93129f954 | 176 | wait_us(1000); |
stefanrousseau | 61:f6b93129f954 | 177 | mdm.putc('\n'); |
stefanrousseau | 61:f6b93129f954 | 178 | wait_us(1000); |
stefanrousseau | 61:f6b93129f954 | 179 | } |
stefanrousseau | 61:f6b93129f954 | 180 | |
stefanrousseau | 61:f6b93129f954 | 181 | if (rsp_list) { |
stefanrousseau | 61:f6b93129f954 | 182 | rsp->erase(); // Clean up from prior cmd response |
stefanrousseau | 61:f6b93129f954 | 183 | *len = 0; |
stefanrousseau | 61:f6b93129f954 | 184 | Timer timer; |
stefanrousseau | 61:f6b93129f954 | 185 | timer.start(); |
stefanrousseau | 61:f6b93129f954 | 186 | while (timer.read_ms() < timeout_ms) { |
stefanrousseau | 61:f6b93129f954 | 187 | int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms()); |
stefanrousseau | 61:f6b93129f954 | 188 | |
stefanrousseau | 61:f6b93129f954 | 189 | if (lenCmd == 0) |
stefanrousseau | 61:f6b93129f954 | 190 | continue; |
stefanrousseau | 61:f6b93129f954 | 191 | |
stefanrousseau | 61:f6b93129f954 | 192 | if (lenCmd < 0) |
stefanrousseau | 61:f6b93129f954 | 193 | return MDM_ERR_TIMEOUT; |
stefanrousseau | 61:f6b93129f954 | 194 | else { |
stefanrousseau | 61:f6b93129f954 | 195 | *len += lenCmd; |
stefanrousseau | 61:f6b93129f954 | 196 | *rsp += cmd_buf; |
stefanrousseau | 61:f6b93129f954 | 197 | } |
stefanrousseau | 61:f6b93129f954 | 198 | |
stefanrousseau | 61:f6b93129f954 | 199 | if (mdm_dbgmask & MDM_DBG_AT_CMDS) { |
stefanrousseau | 61:f6b93129f954 | 200 | printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf); |
stefanrousseau | 61:f6b93129f954 | 201 | } |
stefanrousseau | 61:f6b93129f954 | 202 | |
stefanrousseau | 61:f6b93129f954 | 203 | int rsp_idx = 0; |
stefanrousseau | 61:f6b93129f954 | 204 | while (rsp_list[rsp_idx]) { |
stefanrousseau | 61:f6b93129f954 | 205 | if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) { |
stefanrousseau | 61:f6b93129f954 | 206 | return rsp_idx; |
stefanrousseau | 61:f6b93129f954 | 207 | } |
stefanrousseau | 61:f6b93129f954 | 208 | rsp_idx++; |
stefanrousseau | 61:f6b93129f954 | 209 | } |
stefanrousseau | 61:f6b93129f954 | 210 | } |
stefanrousseau | 61:f6b93129f954 | 211 | return MDM_ERR_TIMEOUT; |
stefanrousseau | 61:f6b93129f954 | 212 | } |
stefanrousseau | 61:f6b93129f954 | 213 | |
stefanrousseau | 61:f6b93129f954 | 214 | return MDM_OK; |
stefanrousseau | 61:f6b93129f954 | 215 | } |
stefanrousseau | 61:f6b93129f954 | 216 | |
stefanrousseau | 61:f6b93129f954 | 217 | void reinitialize_mdm(void) |
stefanrousseau | 61:f6b93129f954 | 218 | { |
stefanrousseau | 61:f6b93129f954 | 219 | // Initialize the modem |
stefanrousseau | 61:f6b93129f954 | 220 | printf(GRN "Modem RE-initializing..." DEF "\r\n"); |
stefanrousseau | 61:f6b93129f954 | 221 | if (!mdm_init()) { |
stefanrousseau | 61:f6b93129f954 | 222 | printf(RED "\n\rModem RE-initialization failed!" DEF "\n"); |
stefanrousseau | 61:f6b93129f954 | 223 | } |
stefanrousseau | 61:f6b93129f954 | 224 | printf("\r\n"); |
stefanrousseau | 61:f6b93129f954 | 225 | } |
stefanrousseau | 61:f6b93129f954 | 226 | // These are built on the fly |
stefanrousseau | 61:f6b93129f954 | 227 | string MyServerIpAddress; |
stefanrousseau | 61:f6b93129f954 | 228 | string MySocketData; |
stefanrousseau | 61:f6b93129f954 | 229 | |
stefanrousseau | 61:f6b93129f954 | 230 | //******************************************************************************************************************************************** |
stefanrousseau | 61:f6b93129f954 | 231 | //* Process JSON response messages |
stefanrousseau | 61:f6b93129f954 | 232 | //******************************************************************************************************************************************** |
stefanrousseau | 61:f6b93129f954 | 233 | bool extract_JSON(char* search_field, char* found_string) |
stefanrousseau | 61:f6b93129f954 | 234 | { |
stefanrousseau | 61:f6b93129f954 | 235 | char* beginquote; |
stefanrousseau | 61:f6b93129f954 | 236 | char* endquote; |
stefanrousseau | 61:f6b93129f954 | 237 | beginquote = strchr(search_field, '{'); //start of JSON |
stefanrousseau | 61:f6b93129f954 | 238 | endquote = strchr(search_field, '}'); //end of JSON |
stefanrousseau | 61:f6b93129f954 | 239 | if (beginquote) |
stefanrousseau | 61:f6b93129f954 | 240 | { |
stefanrousseau | 61:f6b93129f954 | 241 | uint16_t ifoundlen; |
stefanrousseau | 61:f6b93129f954 | 242 | if (endquote) |
stefanrousseau | 61:f6b93129f954 | 243 | { |
stefanrousseau | 61:f6b93129f954 | 244 | ifoundlen = (uint16_t) (endquote - beginquote) + 1; |
stefanrousseau | 61:f6b93129f954 | 245 | strncpy(found_string, beginquote, ifoundlen ); |
stefanrousseau | 61:f6b93129f954 | 246 | found_string[ifoundlen] = 0; //null terminate |
stefanrousseau | 61:f6b93129f954 | 247 | return true; |
stefanrousseau | 61:f6b93129f954 | 248 | } |
stefanrousseau | 61:f6b93129f954 | 249 | else |
stefanrousseau | 61:f6b93129f954 | 250 | { |
stefanrousseau | 61:f6b93129f954 | 251 | endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing |
stefanrousseau | 61:f6b93129f954 | 252 | ifoundlen = (uint16_t) (endquote - beginquote) + 1; |
stefanrousseau | 61:f6b93129f954 | 253 | strncpy(found_string, beginquote, ifoundlen ); |
stefanrousseau | 61:f6b93129f954 | 254 | found_string[ifoundlen] = 0; //null terminate |
stefanrousseau | 61:f6b93129f954 | 255 | return false; |
stefanrousseau | 61:f6b93129f954 | 256 | } |
stefanrousseau | 61:f6b93129f954 | 257 | } |
stefanrousseau | 61:f6b93129f954 | 258 | else |
stefanrousseau | 61:f6b93129f954 | 259 | { |
stefanrousseau | 61:f6b93129f954 | 260 | return false; |
stefanrousseau | 61:f6b93129f954 | 261 | } |
stefanrousseau | 61:f6b93129f954 | 262 | } //extract_JSON |
stefanrousseau | 61:f6b93129f954 | 263 | |
stefanrousseau | 61:f6b93129f954 | 264 | int cell_modem_init() |
stefanrousseau | 61:f6b93129f954 | 265 | { |
stefanrousseau | 61:f6b93129f954 | 266 | int i; |
stefanrousseau | 61:f6b93129f954 | 267 | |
stefanrousseau | 61:f6b93129f954 | 268 | pc.baud(115200); |
stefanrousseau | 61:f6b93129f954 | 269 | // Initialize the modem |
stefanrousseau | 61:f6b93129f954 | 270 | printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n"); |
stefanrousseau | 61:f6b93129f954 | 271 | do { |
stefanrousseau | 61:f6b93129f954 | 272 | i=mdm_init(); |
stefanrousseau | 61:f6b93129f954 | 273 | if (!i) { |
stefanrousseau | 61:f6b93129f954 | 274 | pc.printf(RED "Modem initialization failed!" DEF "\n"); |
stefanrousseau | 61:f6b93129f954 | 275 | } |
stefanrousseau | 61:f6b93129f954 | 276 | } while (!i); |
stefanrousseau | 61:f6b93129f954 | 277 | |
stefanrousseau | 61:f6b93129f954 | 278 | //Software init |
stefanrousseau | 61:f6b93129f954 | 279 | software_init_mdm(); |
stefanrousseau | 61:f6b93129f954 | 280 | |
stefanrousseau | 61:f6b93129f954 | 281 | // Resolve URL to IP address to connect to |
stefanrousseau | 61:f6b93129f954 | 282 | resolve_mdm(); |
stefanrousseau | 61:f6b93129f954 | 283 | // Open the socket (connect to the server) |
stefanrousseau | 61:f6b93129f954 | 284 | sockopen_mdm(); |
stefanrousseau | 61:f6b93129f954 | 285 | return (0); |
stefanrousseau | 61:f6b93129f954 | 286 | } |
stefanrousseau | 61:f6b93129f954 | 287 | |
stefanrousseau | 61:f6b93129f954 | 288 | int cell_modem_Sendreceive(char* tx_string, char* rx_string) |
stefanrousseau | 61:f6b93129f954 | 289 | { |
stefanrousseau | 61:f6b93129f954 | 290 | int iStatus = 0; //error by default |
stefanrousseau | 61:f6b93129f954 | 291 | printf(DEF "\r\n"); |
stefanrousseau | 61:f6b93129f954 | 292 | printf(BLU "Sending to modem : %s" DEF "\r\n", &tx_string[0]); |
stefanrousseau | 61:f6b93129f954 | 293 | sockwrite_mdm(&tx_string[0]); |
stefanrousseau | 61:f6b93129f954 | 294 | if (sockread_mdm(&MySocketData, 1024, 20)) |
stefanrousseau | 61:f6b93129f954 | 295 | { |
stefanrousseau | 61:f6b93129f954 | 296 | printf(DEF "\r\n"); |
stefanrousseau | 61:f6b93129f954 | 297 | printf(YEL "Read back : %s" DEF "\r\n", &MySocketData[0]); |
stefanrousseau | 61:f6b93129f954 | 298 | char stringToCharBuf[BUF_SIZE_FOR_N_MAX_SOCKREAD*MAX_WNC_SOCKREAD_PAYLOAD+1]; // WNC can return max of 1500 (per sockread) |
stefanrousseau | 61:f6b93129f954 | 299 | if ((MySocketData.length() + 1) < sizeof(stringToCharBuf)) |
stefanrousseau | 61:f6b93129f954 | 300 | { |
stefanrousseau | 61:f6b93129f954 | 301 | strcpy(stringToCharBuf, MySocketData.c_str()); |
stefanrousseau | 61:f6b93129f954 | 302 | if (extract_JSON(stringToCharBuf, &rx_string[0])) |
stefanrousseau | 61:f6b93129f954 | 303 | { |
stefanrousseau | 61:f6b93129f954 | 304 | printf(GRN "JSON : %s" DEF "\n", &rx_string[0]); |
stefanrousseau | 61:f6b93129f954 | 305 | iStatus = 1; //all good |
stefanrousseau | 61:f6b93129f954 | 306 | } |
stefanrousseau | 61:f6b93129f954 | 307 | } |
stefanrousseau | 61:f6b93129f954 | 308 | else |
stefanrousseau | 61:f6b93129f954 | 309 | { |
stefanrousseau | 61:f6b93129f954 | 310 | printf(RED "BUFFER not big enough for sock data!" DEF "\r\n"); |
stefanrousseau | 61:f6b93129f954 | 311 | } |
stefanrousseau | 61:f6b93129f954 | 312 | } |
stefanrousseau | 61:f6b93129f954 | 313 | else |
stefanrousseau | 61:f6b93129f954 | 314 | { |
stefanrousseau | 61:f6b93129f954 | 315 | printf(RED "No response..." DEF "\r\n"); |
stefanrousseau | 61:f6b93129f954 | 316 | } |
stefanrousseau | 61:f6b93129f954 | 317 | return iStatus; |
stefanrousseau | 61:f6b93129f954 | 318 | } |
stefanrousseau | 61:f6b93129f954 | 319 |