This program connects to a few sensors via I2C and sends the data collected to a WNC Cellular Module which is located on an Avnet WNC-Shield card.

Dependencies:   FXOS8700CQ MODSERIAL mbed

/media/uploads/kevinkeryk/avnet_logo_tagline_rgb.png

Avnet Cellular IoT Instructions

  • One problematic area is setting the MY_SERVER_URL. When you copy the URL from the flow, you must make sure the MY_SERVER_URL is also set to the appropriate server. It can be either "run-east.att.io" or "run-west.att.io".

Useful Links

Adding Additional Sensors

The FLOW_DEVICE_NAME field must contain the name of the instance of the Virtual Starter Kit in FLOW you will be communicating with. Usually this is "vstarterkit001", but if you have problems communicating you can verify this is correct. Note: This device will not be created until you click the “Initialize” input on the Virtual Device tab of the Starter Kit project in FLOW. At that point, it becomes available in M2X and you can see it as the DEVICE SERIAL field under Devices as in the image below. /media/uploads/JMF/vstarterkit.png

Sensors: When executing, the FRDM-K64F board uploads sensor measurements to AT&T’s Flow environment every 5 seconds, using the Cellular shield board. You can adjust how often you want to do this by editing the SENSOR_UPDATE_INTERVAL_MS value in the header file.

Temperature and humidity: By default, the board reports readings from the HTS221 temperature and humidity sensor. These two values are sent to the HTTP IN /climate port in FLOW with field names “temp” and “humidity”. Temperature is in degrees Fahrenheit and humidity is a %. This default assignment is: iSensorsToReport = TEMP_HUMIDITY_ONLY;

Accelerometer: If you want to expand and use the onboard motion sensor, you can also send 3-axis accelerometer information from the board as “accelX”, “accelY”, and “accelZ”. This is useful if you want to know the stationary position of the board with regards to gravity, or whether it is in motion. These readings are in g’s. To send these values, change the assignment to: iSensorsToReport = TEMP_HUMIDITY_ACCELEROMETER;

PMOD Sensors: If you have a Silicon Labs sensor module that can plug into the PMOD connector on the Cellular shield, you are able to measure proximity, UV light, ambient visible and infrared light from the Si1145 sensor. This PMOD also has a temperature and humidity sensor, but in this case it is redundant. When enabled, the fields “proximity”, “light_uv”, “light_vis” and “light_ir” are also sent. To enable all these sensors, change the assignment to: iSensorsToReport = TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS;

Connecting the PMOD sensors: Because the pinouts do not align, the SiLabs PMOD sensor board cannot be plugged into the J10 PMOD receptacle on the shield directly. The following wiring instructions must be followed:

SignalJ10ShieldPMOD Color in the image below
VCCPin 6Pin 6Red
GNDPin 5Pin 5Black
SDAPin4Pin 3Green
SCLPin3Pin 2Yellow

/media/uploads/JMF/xyz.jpg

AT&T M2X and FLOW Instructions

M2X & FLOW Instructions

Link to AT&T M2X

M2X

Link to AT&T Flow

FLOW

Avnet WNC-Shield Information

Getting Started with the Avnet WNC-Shield Software

  • This project uses Revision 119 of the MBED library because of I2C implementation differences with the tip (Revision 121).
  • This project uses Revision 4 of the FXOS8700CQ library for sensors.

Easily Modifiable Parameters

Inside the mbed Avnet_ATT_Cellular_IOT project, the parameters needed to customize your board are in the config_me.h file.

  • FLOW parameters: This project assumes you are using a fork of the Starter Kit Base project, which is a reference design created using AT&T’s FLOW (https://flow.att.com) that allows the creation of online virtualization and other IoT functionality. The default parameters in the config_me.h file are done for a specific instance of this project. When you fork the original project, you get your own instance and it will have its own base address. At the bottom of the FLOW environment, when you click on the Endpoints tab, URL information that is specific to your instance is displayed. Of note is the Base URL. In the example below (as in the default mbed project), the Base URL is: https://run-west.att.io/1e464b19cdcde/774c88d68202/86694923d5bf28a/in/flow You have to take note of two parts of this address. The run-west.att.io part is the server URL, and you have to make sure the
  • MY_SERVER_URL field in config_me.h matches this. The rest of the base URL, in green above, needs to be pasted into the FLOW_BASE_URL field.

There is also a FLOW_INPUT_NAME field. This should match the name of the HTTP IN port in the FLOW project that you want to send sensor data to. The default is "/climate", as in the FLOW image below.

/media/uploads/JMF/sf.png

Where is the Binary I compiled

When the COMPILE button is pressed, it compiles your project and links it. The result is placed in the DOWNLOAD folder you use when downloading files from the Internet. It will be called AvnetATT_shape_hackathon_K64F.bin.

Additional Information on Compiling/Configuring

Comprehensive instructions can be found at: Quick Start Instructions

Committer:
fkellermavnet
Date:
Mon Jul 11 23:54:24 2016 +0000
Revision:
10:df54436ecd38
Parent:
2:0e2ef866af95
Child:
19:f89baed3bd6f
Changed wnc control code so that it can't do a read or a write to a socket if the socket is not opened successfully.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 2:0e2ef866af95 1 #include "mbed.h"
JMF 2:0e2ef866af95 2 #include <cctype>
JMF 2:0e2ef866af95 3 #include <string>
JMF 2:0e2ef866af95 4 #include "config_me.h"
JMF 2:0e2ef866af95 5 #include "SerialBuffered.h"
JMF 2:0e2ef866af95 6 #include "wnc_control.h"
JMF 2:0e2ef866af95 7
JMF 2:0e2ef866af95 8 extern Serial pc;
JMF 2:0e2ef866af95 9 extern Serial mdm;
JMF 2:0e2ef866af95 10 extern string MyServerIpAddress;
JMF 2:0e2ef866af95 11 extern string MySocketData;
JMF 2:0e2ef866af95 12
JMF 2:0e2ef866af95 13 int reinitialize_mdm(void);
JMF 2:0e2ef866af95 14
JMF 2:0e2ef866af95 15 enum WNC_ERR_e {
JMF 2:0e2ef866af95 16 WNC_OK =0,
JMF 2:0e2ef866af95 17 WNC_CMD_ERR = -1,
JMF 2:0e2ef866af95 18 WNC_NO_RESPONSE = -2
JMF 2:0e2ef866af95 19 };
JMF 2:0e2ef866af95 20
JMF 2:0e2ef866af95 21 // Contains result of last call to send_wnc_cmd(..)
JMF 2:0e2ef866af95 22 WNC_ERR_e WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 23
JMF 2:0e2ef866af95 24 // Contains the RAW WNC UART responses
JMF 2:0e2ef866af95 25 static string wncStr;
fkellermavnet 10:df54436ecd38 26 static int socketOpen = 0;
JMF 2:0e2ef866af95 27
JMF 2:0e2ef866af95 28 void software_init_mdm(void)
JMF 2:0e2ef866af95 29 {
JMF 2:0e2ef866af95 30 do
JMF 2:0e2ef866af95 31 {
JMF 2:0e2ef866af95 32 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 33 at_init_wnc();
JMF 2:0e2ef866af95 34 if (WNC_MDM_ERR != WNC_OK)
JMF 2:0e2ef866af95 35 reinitialize_mdm();
JMF 2:0e2ef866af95 36 } while (WNC_MDM_ERR != WNC_OK);
JMF 2:0e2ef866af95 37 }
JMF 2:0e2ef866af95 38
JMF 2:0e2ef866af95 39 void resolve_mdm(void)
JMF 2:0e2ef866af95 40 {
JMF 2:0e2ef866af95 41 do
JMF 2:0e2ef866af95 42 {
JMF 2:0e2ef866af95 43 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 44 at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress);
JMF 2:0e2ef866af95 45 if (WNC_MDM_ERR == WNC_NO_RESPONSE)
JMF 2:0e2ef866af95 46 {
JMF 2:0e2ef866af95 47 reinitialize_mdm();
JMF 2:0e2ef866af95 48 software_init_mdm();
JMF 2:0e2ef866af95 49 }
JMF 2:0e2ef866af95 50 else if (WNC_MDM_ERR == WNC_CMD_ERR)
JMF 2:0e2ef866af95 51 {
JMF 2:0e2ef866af95 52 pc.puts("Bad URL!!!!!!\r\n");
JMF 2:0e2ef866af95 53 MyServerIpAddress = "192.168.0.1";
JMF 2:0e2ef866af95 54 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 55 }
JMF 2:0e2ef866af95 56 } while (WNC_MDM_ERR != WNC_OK);
JMF 2:0e2ef866af95 57
JMF 2:0e2ef866af95 58 pc.printf("My Server IP: %s\r\n", MyServerIpAddress.data());
JMF 2:0e2ef866af95 59 }
JMF 2:0e2ef866af95 60
JMF 2:0e2ef866af95 61 void sockopen_mdm(void)
JMF 2:0e2ef866af95 62 {
JMF 2:0e2ef866af95 63 do
JMF 2:0e2ef866af95 64 {
JMF 2:0e2ef866af95 65 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 66 at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR);
JMF 2:0e2ef866af95 67 if (WNC_MDM_ERR == WNC_NO_RESPONSE)
JMF 2:0e2ef866af95 68 {
JMF 2:0e2ef866af95 69 reinitialize_mdm();
JMF 2:0e2ef866af95 70 software_init_mdm();
JMF 2:0e2ef866af95 71 }
JMF 2:0e2ef866af95 72 else if (WNC_MDM_ERR == WNC_CMD_ERR)
JMF 2:0e2ef866af95 73 pc.puts("Socket open fail!!!!\r\n");
fkellermavnet 10:df54436ecd38 74 else
fkellermavnet 10:df54436ecd38 75 socketOpen = 1;
JMF 2:0e2ef866af95 76 } while (WNC_MDM_ERR != WNC_OK);
JMF 2:0e2ef866af95 77 }
JMF 2:0e2ef866af95 78
JMF 2:0e2ef866af95 79 void sockwrite_mdm(const char * s)
JMF 2:0e2ef866af95 80 {
fkellermavnet 10:df54436ecd38 81 if (socketOpen == 1)
fkellermavnet 10:df54436ecd38 82 {
JMF 2:0e2ef866af95 83 do
JMF 2:0e2ef866af95 84 {
JMF 2:0e2ef866af95 85 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 86 at_sockwrite_wnc(s);
JMF 2:0e2ef866af95 87 if (WNC_MDM_ERR == WNC_NO_RESPONSE)
JMF 2:0e2ef866af95 88 {
JMF 2:0e2ef866af95 89 reinitialize_mdm();
JMF 2:0e2ef866af95 90 software_init_mdm();
JMF 2:0e2ef866af95 91 }
JMF 2:0e2ef866af95 92 else if (WNC_MDM_ERR == WNC_CMD_ERR)
JMF 2:0e2ef866af95 93 {
JMF 2:0e2ef866af95 94 pc.puts("Socket Write fail!!!\r\n");
JMF 2:0e2ef866af95 95 // Have seen when write fails modem gets stuck in bad state, try to recover
JMF 2:0e2ef866af95 96 reinitialize_mdm();
JMF 2:0e2ef866af95 97 software_init_mdm();
JMF 2:0e2ef866af95 98 }
JMF 2:0e2ef866af95 99 } while (WNC_MDM_ERR != WNC_OK);
fkellermavnet 10:df54436ecd38 100 }
fkellermavnet 10:df54436ecd38 101 else
fkellermavnet 10:df54436ecd38 102 puts("Socket is closed for write!\r\n");
JMF 2:0e2ef866af95 103 }
JMF 2:0e2ef866af95 104
JMF 2:0e2ef866af95 105 void sockread_mdm(string * sockData, int len, int retries)
JMF 2:0e2ef866af95 106 {
fkellermavnet 10:df54436ecd38 107 if (socketOpen == 1)
fkellermavnet 10:df54436ecd38 108 {
JMF 2:0e2ef866af95 109 do
JMF 2:0e2ef866af95 110 {
JMF 2:0e2ef866af95 111 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 112 at_sockread_wnc(sockData, len, retries);
JMF 2:0e2ef866af95 113 if (WNC_MDM_ERR == WNC_NO_RESPONSE)
JMF 2:0e2ef866af95 114 {
JMF 2:0e2ef866af95 115 reinitialize_mdm();
JMF 2:0e2ef866af95 116 software_init_mdm();
JMF 2:0e2ef866af95 117 }
JMF 2:0e2ef866af95 118 else if (WNC_MDM_ERR == WNC_CMD_ERR)
JMF 2:0e2ef866af95 119 puts("Sock read fail!!!!\r\n");
JMF 2:0e2ef866af95 120 } while (WNC_MDM_ERR != WNC_OK);
fkellermavnet 10:df54436ecd38 121 }
fkellermavnet 10:df54436ecd38 122 else
fkellermavnet 10:df54436ecd38 123 puts("Socket is closed for read\r\n");
JMF 2:0e2ef866af95 124 }
JMF 2:0e2ef866af95 125
JMF 2:0e2ef866af95 126 void sockclose_mdm(void)
JMF 2:0e2ef866af95 127 {
JMF 2:0e2ef866af95 128 do
JMF 2:0e2ef866af95 129 {
JMF 2:0e2ef866af95 130 WNC_MDM_ERR = WNC_OK;
JMF 2:0e2ef866af95 131 at_sockclose_wnc();
fkellermavnet 10:df54436ecd38 132 // Assume close happened even if it went bad
fkellermavnet 10:df54436ecd38 133 // going bad will result in a re-init anyways and if close
fkellermavnet 10:df54436ecd38 134 // fails we're pretty much in bad state and not much can do
fkellermavnet 10:df54436ecd38 135 socketOpen = 0;
JMF 2:0e2ef866af95 136 if (WNC_MDM_ERR == WNC_NO_RESPONSE)
JMF 2:0e2ef866af95 137 {
JMF 2:0e2ef866af95 138 reinitialize_mdm();
JMF 2:0e2ef866af95 139 software_init_mdm();
JMF 2:0e2ef866af95 140 }
JMF 2:0e2ef866af95 141 else if (WNC_MDM_ERR == WNC_CMD_ERR)
JMF 2:0e2ef866af95 142 puts("Sock close fail!!!\r\n");
JMF 2:0e2ef866af95 143 } while (WNC_MDM_ERR != WNC_OK);
JMF 2:0e2ef866af95 144 }
JMF 2:0e2ef866af95 145
JMF 2:0e2ef866af95 146 /**
JMF 2:0e2ef866af95 147 * C++ version 0.4 char* style "itoa":
JMF 2:0e2ef866af95 148 * Written by Lukás Chmela
JMF 2:0e2ef866af95 149 * Released under GPLv3.
JMF 2:0e2ef866af95 150 */
JMF 2:0e2ef866af95 151
JMF 2:0e2ef866af95 152 char* itoa(int value, char* result, int base)
JMF 2:0e2ef866af95 153 {
JMF 2:0e2ef866af95 154 // check that the base if valid
JMF 2:0e2ef866af95 155 if ( base < 2 || base > 36 ) {
JMF 2:0e2ef866af95 156 *result = '\0';
JMF 2:0e2ef866af95 157 return result;
JMF 2:0e2ef866af95 158 }
JMF 2:0e2ef866af95 159
JMF 2:0e2ef866af95 160 char* ptr = result, *ptr1 = result, tmp_char;
JMF 2:0e2ef866af95 161 int tmp_value;
JMF 2:0e2ef866af95 162
JMF 2:0e2ef866af95 163 do {
JMF 2:0e2ef866af95 164 tmp_value = value;
JMF 2:0e2ef866af95 165 value /= base;
JMF 2:0e2ef866af95 166 *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
JMF 2:0e2ef866af95 167 } while ( value );
JMF 2:0e2ef866af95 168
JMF 2:0e2ef866af95 169 // Apply negative sign
JMF 2:0e2ef866af95 170 if ( tmp_value < 0 )
JMF 2:0e2ef866af95 171 *ptr++ = '-';
JMF 2:0e2ef866af95 172 *ptr-- = '\0';
JMF 2:0e2ef866af95 173
JMF 2:0e2ef866af95 174 while ( ptr1 < ptr ) {
JMF 2:0e2ef866af95 175 tmp_char = *ptr;
JMF 2:0e2ef866af95 176 *ptr-- = *ptr1;
JMF 2:0e2ef866af95 177 *ptr1++ = tmp_char;
JMF 2:0e2ef866af95 178 }
JMF 2:0e2ef866af95 179
JMF 2:0e2ef866af95 180 return result;
JMF 2:0e2ef866af95 181 }
JMF 2:0e2ef866af95 182
JMF 2:0e2ef866af95 183 extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len);
JMF 2:0e2ef866af95 184
JMF 2:0e2ef866af95 185 // Sets a global with failure or success, assumes 1 thread all the time
JMF 2:0e2ef866af95 186 int send_wnc_cmd(const char * s, string ** r, int ms_timeout)
JMF 2:0e2ef866af95 187 {
JMF 2:0e2ef866af95 188 static const char * rsp_lst[] = { "OK", "ERROR", NULL };
JMF 2:0e2ef866af95 189 int len;
JMF 2:0e2ef866af95 190
JMF 2:0e2ef866af95 191 pc.printf("Send: %s\r\n",s);
JMF 2:0e2ef866af95 192 int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len);
JMF 2:0e2ef866af95 193 *r = &wncStr; // Return a pointer to the static string
JMF 2:0e2ef866af95 194
JMF 2:0e2ef866af95 195 if (res >= 0)
JMF 2:0e2ef866af95 196 {
JMF 2:0e2ef866af95 197 pc.puts("[");
JMF 2:0e2ef866af95 198 pc.puts(wncStr.data());
JMF 2:0e2ef866af95 199 pc.puts("]\n\r");
JMF 2:0e2ef866af95 200 if (res > 0)
JMF 2:0e2ef866af95 201 {
JMF 2:0e2ef866af95 202 if (WNC_MDM_ERR != WNC_NO_RESPONSE)
JMF 2:0e2ef866af95 203 WNC_MDM_ERR = WNC_CMD_ERR;
JMF 2:0e2ef866af95 204 return -1;
JMF 2:0e2ef866af95 205 }
JMF 2:0e2ef866af95 206 else
JMF 2:0e2ef866af95 207 return 0;
JMF 2:0e2ef866af95 208 }
JMF 2:0e2ef866af95 209 else
JMF 2:0e2ef866af95 210 {
JMF 2:0e2ef866af95 211 WNC_MDM_ERR = WNC_NO_RESPONSE;
JMF 2:0e2ef866af95 212 pc.puts("No response from WNC!\n\r");
JMF 2:0e2ef866af95 213 return -2;
JMF 2:0e2ef866af95 214 }
JMF 2:0e2ef866af95 215 }
JMF 2:0e2ef866af95 216
JMF 2:0e2ef866af95 217 void at_init_wnc(void)
JMF 2:0e2ef866af95 218 {
JMF 2:0e2ef866af95 219 string * pRespStr;
JMF 2:0e2ef866af95 220 send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat?
JMF 2:0e2ef866af95 221 send_wnc_cmd("ATE1", &pRespStr, WNC_TIMEOUT_MS); // Echo ON
JMF 2:0e2ef866af95 222 string cmd_str("AT%PDNSET=1,");
JMF 2:0e2ef866af95 223 cmd_str += MY_APN_STR;
JMF 2:0e2ef866af95 224 cmd_str += ",IP";
JMF 2:0e2ef866af95 225 send_wnc_cmd(cmd_str.data(), &pRespStr, 2*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
JMF 2:0e2ef866af95 226 send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); // Internet services enabled
JMF 2:0e2ef866af95 227 send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
JMF 2:0e2ef866af95 228 }
JMF 2:0e2ef866af95 229
JMF 2:0e2ef866af95 230 void at_sockopen_wnc(const string & ipStr, const char * port )
JMF 2:0e2ef866af95 231 {
JMF 2:0e2ef866af95 232 string * pRespStr;
JMF 2:0e2ef866af95 233 send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS);
JMF 2:0e2ef866af95 234 string cmd_str("AT@SOCKCONN=1,\"");
JMF 2:0e2ef866af95 235 cmd_str += ipStr;
JMF 2:0e2ef866af95 236 cmd_str += "\",";
JMF 2:0e2ef866af95 237 cmd_str += port;
JMF 2:0e2ef866af95 238 send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS);
JMF 2:0e2ef866af95 239 }
JMF 2:0e2ef866af95 240
JMF 2:0e2ef866af95 241 void at_sockclose_wnc(void)
JMF 2:0e2ef866af95 242 {
JMF 2:0e2ef866af95 243 string * pRespStr;
JMF 2:0e2ef866af95 244 send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS);
JMF 2:0e2ef866af95 245 }
JMF 2:0e2ef866af95 246
JMF 2:0e2ef866af95 247 int at_dnsresolve_wnc(const char * s, string * ipStr)
JMF 2:0e2ef866af95 248 {
JMF 2:0e2ef866af95 249 string * pRespStr;
JMF 2:0e2ef866af95 250 string str(s);
JMF 2:0e2ef866af95 251 str = "AT@DNSRESVDON=\"" + str;
JMF 2:0e2ef866af95 252 str += "\"\r\n";
JMF 2:0e2ef866af95 253 if (send_wnc_cmd(str.data(), &pRespStr, WNC_TIMEOUT_MS) == 0)
JMF 2:0e2ef866af95 254 {
JMF 2:0e2ef866af95 255 size_t pos_start = pRespStr->find(":\"") + 2;
JMF 2:0e2ef866af95 256 if (pos_start != string::npos)
JMF 2:0e2ef866af95 257 {
JMF 2:0e2ef866af95 258 size_t pos_end = pRespStr->rfind("\"") - 1;
JMF 2:0e2ef866af95 259 if (pos_end != string::npos)
JMF 2:0e2ef866af95 260 {
JMF 2:0e2ef866af95 261 if (pos_end > pos_start)
JMF 2:0e2ef866af95 262 {
JMF 2:0e2ef866af95 263 // Make a copy for use later (the source string is re-used)
JMF 2:0e2ef866af95 264 *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1);
JMF 2:0e2ef866af95 265 return 1;
JMF 2:0e2ef866af95 266 }
JMF 2:0e2ef866af95 267 else
JMF 2:0e2ef866af95 268 pc.puts("URL Resolve fail, substr Err\r\n");
JMF 2:0e2ef866af95 269 }
JMF 2:0e2ef866af95 270 else
JMF 2:0e2ef866af95 271 pc.puts("URL Resolve fail, no 2nd quote\r\n");
JMF 2:0e2ef866af95 272 }
JMF 2:0e2ef866af95 273 else
JMF 2:0e2ef866af95 274 pc.puts("URL Resolve fail, no quotes\r\n");
JMF 2:0e2ef866af95 275 }
JMF 2:0e2ef866af95 276 else
JMF 2:0e2ef866af95 277 pc.puts("URL Resolve fail, WNC cmd fail\r\n");
JMF 2:0e2ef866af95 278
JMF 2:0e2ef866af95 279 return -1;
JMF 2:0e2ef866af95 280 }
JMF 2:0e2ef866af95 281
JMF 2:0e2ef866af95 282 void at_sockwrite_wnc(const char * s)
JMF 2:0e2ef866af95 283 {
JMF 2:0e2ef866af95 284 string * pRespStr;
JMF 2:0e2ef866af95 285 char num2str[6];
JMF 2:0e2ef866af95 286 size_t sLen = strlen(s);
JMF 2:0e2ef866af95 287 if (sLen <= 99999)
JMF 2:0e2ef866af95 288 {
JMF 2:0e2ef866af95 289 string cmd_str("AT@SOCKWRITE=1,");
JMF 2:0e2ef866af95 290 itoa(sLen, num2str, 10);
JMF 2:0e2ef866af95 291 cmd_str += num2str;
JMF 2:0e2ef866af95 292 cmd_str += ",\"";
JMF 2:0e2ef866af95 293 while(*s != '\0')
JMF 2:0e2ef866af95 294 {
JMF 2:0e2ef866af95 295 itoa((int)*s++, num2str, 16);
JMF 2:0e2ef866af95 296 // Always 2-digit ascii hex:
JMF 2:0e2ef866af95 297 if (strlen(num2str) == 1)
JMF 2:0e2ef866af95 298 {
JMF 2:0e2ef866af95 299 num2str[2] = '\0';
JMF 2:0e2ef866af95 300 num2str[1] = num2str[0];
JMF 2:0e2ef866af95 301 num2str[0] = '0';
JMF 2:0e2ef866af95 302 }
JMF 2:0e2ef866af95 303 cmd_str += num2str;
JMF 2:0e2ef866af95 304 }
JMF 2:0e2ef866af95 305 cmd_str += "\"";
JMF 2:0e2ef866af95 306 send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS);
JMF 2:0e2ef866af95 307 }
JMF 2:0e2ef866af95 308 else
JMF 2:0e2ef866af95 309 pc.puts("sockwrite Err, string to long\r\n");
JMF 2:0e2ef866af95 310 }
JMF 2:0e2ef866af95 311
JMF 2:0e2ef866af95 312 unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0)
JMF 2:0e2ef866af95 313 {
JMF 2:0e2ef866af95 314 unsigned i;
JMF 2:0e2ef866af95 315 string * pRespStr;
JMF 2:0e2ef866af95 316 string cmd_str("AT@SOCKREAD=1,");
JMF 2:0e2ef866af95 317 if (n <= 1500)
JMF 2:0e2ef866af95 318 {
JMF 2:0e2ef866af95 319 char num2str[6];
JMF 2:0e2ef866af95 320 itoa(n, num2str, 10);
JMF 2:0e2ef866af95 321 cmd_str += num2str;
JMF 2:0e2ef866af95 322 retries += 1;
JMF 2:0e2ef866af95 323 while (retries--)
JMF 2:0e2ef866af95 324 {
JMF 2:0e2ef866af95 325 send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS);
JMF 2:0e2ef866af95 326 size_t pos_start = pRespStr->find("\"") + 1;
JMF 2:0e2ef866af95 327 size_t pos_end = pRespStr->rfind("\"") - 1;
JMF 2:0e2ef866af95 328 i = pos_end - pos_start + 1;
JMF 2:0e2ef866af95 329 if (i > 0)
JMF 2:0e2ef866af95 330 {
JMF 2:0e2ef866af95 331 retries = 0; // If any data found stop retrying
JMF 2:0e2ef866af95 332 string byte;
JMF 2:0e2ef866af95 333 pS->erase();
JMF 2:0e2ef866af95 334 while (pos_start < pos_end)
JMF 2:0e2ef866af95 335 {
JMF 2:0e2ef866af95 336 byte = pRespStr->substr(pos_start, 2);
JMF 2:0e2ef866af95 337 *pS += (char)strtol(byte.data(), NULL, 16);
JMF 2:0e2ef866af95 338 pos_start += 2;
JMF 2:0e2ef866af95 339 }
JMF 2:0e2ef866af95 340 return i;
JMF 2:0e2ef866af95 341 }
JMF 2:0e2ef866af95 342 }
JMF 2:0e2ef866af95 343 }
JMF 2:0e2ef866af95 344 else
JMF 2:0e2ef866af95 345 pc.puts("sockread Err, to many to read\r\n");
JMF 2:0e2ef866af95 346
JMF 2:0e2ef866af95 347 return 0;
JMF 2:0e2ef866af95 348 }