This is library for using WizFi250

Dependents:   WebSocket_WizFi250_HelloWorld IFTTT_WizFi250 AxedaGo-WizFi250 FANARM_AP_udp_server ... more

Committer:
kaizen
Date:
Wed Jun 24 06:25:56 2015 +0000
Revision:
11:e52b91d1a5d5
Parent:
9:12ecb377f63f
Child:
14:1f5121c5efcd
Child:
17:2a20f54f2929
Modified problem when MCU call the send all function as revision history.; Modified receive function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kaizen 0:f2039204c8f6 1 /*
DongEun Koak 4:085e7efeae47 2 * Copyright (C) 2013 gsfan, MIT License
kaizen 0:f2039204c8f6 3 *
kaizen 0:f2039204c8f6 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kaizen 0:f2039204c8f6 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kaizen 0:f2039204c8f6 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kaizen 0:f2039204c8f6 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kaizen 0:f2039204c8f6 8 * furnished to do so, subject to the following conditions:
kaizen 0:f2039204c8f6 9 *
kaizen 0:f2039204c8f6 10 * The above copyright notice and this permission notice shall be included in all copies or
kaizen 0:f2039204c8f6 11 * substantial portions of the Software.
kaizen 0:f2039204c8f6 12 *
kaizen 0:f2039204c8f6 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kaizen 0:f2039204c8f6 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kaizen 0:f2039204c8f6 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kaizen 0:f2039204c8f6 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kaizen 0:f2039204c8f6 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kaizen 0:f2039204c8f6 18 */
kaizen 0:f2039204c8f6 19 /* Copyright (C) 2014 Wiznet, MIT License
kaizen 0:f2039204c8f6 20 * port to the Wiznet Module WizFi250
kaizen 0:f2039204c8f6 21 */
kaizen 0:f2039204c8f6 22
kaizen 0:f2039204c8f6 23
kaizen 0:f2039204c8f6 24 #include "WizFi250.h"
kaizen 0:f2039204c8f6 25
kaizen 0:f2039204c8f6 26
kaizen 0:f2039204c8f6 27
kaizen 0:f2039204c8f6 28 void WizFi250::clearFlags()
kaizen 0:f2039204c8f6 29 {
kaizen 0:f2039204c8f6 30 _state.ok = false;
kaizen 0:f2039204c8f6 31 _state.failure = false;
kaizen 0:f2039204c8f6 32 _state.res = RES_NULL;
kaizen 0:f2039204c8f6 33 _state.n = 0;
kaizen 0:f2039204c8f6 34 }
kaizen 0:f2039204c8f6 35
kaizen 0:f2039204c8f6 36
kaizen 0:f2039204c8f6 37 int WizFi250::sendCommand(const char * cmd, Response res, int timeout, int opt)
kaizen 0:f2039204c8f6 38 {
DongEun Koak 7:ba28fe711055 39 unsigned int i;
kaizen 0:f2039204c8f6 40 Timer t;
kaizen 0:f2039204c8f6 41
kaizen 0:f2039204c8f6 42 if (lockUart(timeout)) return -1;
kaizen 0:f2039204c8f6 43
kaizen 0:f2039204c8f6 44 clearFlags();
kaizen 0:f2039204c8f6 45 _state.res = res;
kaizen 0:f2039204c8f6 46
DongEun Koak 5:8a0702aa91e3 47 WIZ_INFO("%s", cmd)
kaizen 0:f2039204c8f6 48 for (i=0; i< strlen(cmd); i++)
kaizen 0:f2039204c8f6 49 {
kaizen 0:f2039204c8f6 50 putUart(cmd[i]);
kaizen 0:f2039204c8f6 51 }
kaizen 0:f2039204c8f6 52
DongEun Koak 7:ba28fe711055 53 if(opt == 1)
kaizen 0:f2039204c8f6 54 {
kaizen 0:f2039204c8f6 55 putUart('\r');
kaizen 0:f2039204c8f6 56 }
DongEun Koak 7:ba28fe711055 57 else if(opt == 2)
kaizen 0:f2039204c8f6 58 {
kaizen 0:f2039204c8f6 59 putUart('\r');
kaizen 0:f2039204c8f6 60 putUart('\n');
kaizen 0:f2039204c8f6 61 }
kaizen 0:f2039204c8f6 62 unlockUart();
kaizen 0:f2039204c8f6 63
kaizen 0:f2039204c8f6 64 if(timeout)
kaizen 0:f2039204c8f6 65 {
kaizen 0:f2039204c8f6 66 t.start();
kaizen 0:f2039204c8f6 67 for(;;)
kaizen 0:f2039204c8f6 68 {
kaizen 0:f2039204c8f6 69 if (_state.ok && _state.res == RES_NULL){
kaizen 0:f2039204c8f6 70 break;
kaizen 0:f2039204c8f6 71 }
kaizen 0:f2039204c8f6 72 if (_state.failure || t.read_ms() > timeout)
kaizen 0:f2039204c8f6 73 {
DongEun Koak 5:8a0702aa91e3 74 WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms());
kaizen 0:f2039204c8f6 75 _state.res = RES_NULL;
DongEun Koak 7:ba28fe711055 76 t.stop();
kaizen 0:f2039204c8f6 77 return -1;
kaizen 0:f2039204c8f6 78 }
kaizen 0:f2039204c8f6 79 }
kaizen 0:f2039204c8f6 80
kaizen 0:f2039204c8f6 81 t.stop();
kaizen 0:f2039204c8f6 82 }
kaizen 0:f2039204c8f6 83
DongEun Koak 5:8a0702aa91e3 84 WIZ_INFO("[OK]\r\n");
kaizen 0:f2039204c8f6 85 _state.res = RES_NULL;
kaizen 0:f2039204c8f6 86
kaizen 0:f2039204c8f6 87 return 0;
kaizen 0:f2039204c8f6 88 }
kaizen 0:f2039204c8f6 89
kaizen 0:f2039204c8f6 90 int WizFi250::cmdAT()
kaizen 0:f2039204c8f6 91 {
kaizen 0:f2039204c8f6 92 int resp;
kaizen 0:f2039204c8f6 93
kaizen 0:f2039204c8f6 94 resp = sendCommand("AT");
kaizen 0:f2039204c8f6 95
kaizen 0:f2039204c8f6 96 return resp;
kaizen 0:f2039204c8f6 97 }
kaizen 0:f2039204c8f6 98
kaizen 0:f2039204c8f6 99 int WizFi250::cmdMECHO(bool flg)
kaizen 0:f2039204c8f6 100 {
kaizen 0:f2039204c8f6 101 int status;
kaizen 0:f2039204c8f6 102 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 103
kaizen 0:f2039204c8f6 104 sprintf(cmd,"AT+MECHO=%d",flg);
kaizen 0:f2039204c8f6 105 status = sendCommand(cmd);
kaizen 0:f2039204c8f6 106
kaizen 0:f2039204c8f6 107 return status;
kaizen 0:f2039204c8f6 108 }
kaizen 0:f2039204c8f6 109
kaizen 0:f2039204c8f6 110 int WizFi250::cmdUSET(int baud, char *flow)
kaizen 0:f2039204c8f6 111 {
kaizen 0:f2039204c8f6 112 int status;
kaizen 0:f2039204c8f6 113 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 114
kaizen 0:f2039204c8f6 115 sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow);
kaizen 0:f2039204c8f6 116 status = sendCommand(cmd);
kaizen 0:f2039204c8f6 117
kaizen 0:f2039204c8f6 118 if(status == 0)
kaizen 0:f2039204c8f6 119 {
kaizen 0:f2039204c8f6 120 wait(1);
kaizen 0:f2039204c8f6 121 _state.buf->flush();
kaizen 0:f2039204c8f6 122 }
kaizen 0:f2039204c8f6 123
kaizen 0:f2039204c8f6 124 return status;
kaizen 0:f2039204c8f6 125 }
kaizen 0:f2039204c8f6 126
kaizen 0:f2039204c8f6 127 int WizFi250::cmdMMAC(const char *mac)
kaizen 0:f2039204c8f6 128 {
kaizen 0:f2039204c8f6 129 int resp;
kaizen 0:f2039204c8f6 130 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 131
kaizen 0:f2039204c8f6 132 if (mac)
kaizen 0:f2039204c8f6 133 {
kaizen 0:f2039204c8f6 134 sprintf(cmd, "AT+MMAC=%s",mac);
kaizen 0:f2039204c8f6 135 resp = sendCommand(cmd);
kaizen 0:f2039204c8f6 136 }
kaizen 0:f2039204c8f6 137 else
kaizen 0:f2039204c8f6 138 {
kaizen 0:f2039204c8f6 139 sprintf(cmd, "AT+MMAC=?");
kaizen 0:f2039204c8f6 140 resp = sendCommand(cmd, RES_MACADDRESS);
kaizen 0:f2039204c8f6 141 }
kaizen 0:f2039204c8f6 142
kaizen 0:f2039204c8f6 143 return resp;
kaizen 0:f2039204c8f6 144 }
kaizen 0:f2039204c8f6 145
kaizen 0:f2039204c8f6 146 int WizFi250::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel)
kaizen 0:f2039204c8f6 147 {
kaizen 0:f2039204c8f6 148 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 149
kaizen 0:f2039204c8f6 150 if(*bssid == NULL)
kaizen 0:f2039204c8f6 151 {
kaizen 0:f2039204c8f6 152 sprintf(cmd, "AT+WSET=%d,%s",mode, ssid);
kaizen 0:f2039204c8f6 153 }
kaizen 0:f2039204c8f6 154 else
kaizen 0:f2039204c8f6 155 {
kaizen 0:f2039204c8f6 156 sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel);
kaizen 0:f2039204c8f6 157 }
kaizen 0:f2039204c8f6 158
kaizen 0:f2039204c8f6 159 return sendCommand(cmd);
kaizen 0:f2039204c8f6 160 }
kaizen 0:f2039204c8f6 161
kaizen 0:f2039204c8f6 162 int WizFi250::cmdWSEC(WiFiMode mode, const char *key, const char *sec)
kaizen 0:f2039204c8f6 163 {
kaizen 0:f2039204c8f6 164 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 165
kaizen 0:f2039204c8f6 166 if(*sec == NULL)
kaizen 0:f2039204c8f6 167 {
kaizen 0:f2039204c8f6 168 sprintf(cmd, "AT+WSEC=%d,,%s",mode, key);
kaizen 0:f2039204c8f6 169 }
kaizen 0:f2039204c8f6 170 else
kaizen 0:f2039204c8f6 171 {
kaizen 0:f2039204c8f6 172 sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key);
kaizen 0:f2039204c8f6 173 }
kaizen 0:f2039204c8f6 174
kaizen 0:f2039204c8f6 175 return sendCommand(cmd);
kaizen 0:f2039204c8f6 176 }
kaizen 0:f2039204c8f6 177
kaizen 0:f2039204c8f6 178 int WizFi250::cmdWJOIN()
kaizen 0:f2039204c8f6 179 {
kaizen8501 9:12ecb377f63f 180 //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) )
kaizen8501 9:12ecb377f63f 181 if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) )
kaizen8501 9:12ecb377f63f 182 {
kaizen8501 9:12ecb377f63f 183 WIZ_ERR("cmdWJOIN");
kaizen8501 9:12ecb377f63f 184 return -1;
kaizen8501 9:12ecb377f63f 185 }
kaizen 0:f2039204c8f6 186
kaizen8501 9:12ecb377f63f 187 if( cmdWSTATUS() )
kaizen8501 9:12ecb377f63f 188 return -1;
kaizen8501 9:12ecb377f63f 189
kaizen8501 9:12ecb377f63f 190 WIZ_INFO("WizFi250 is successfully join to AP");
DongEun Koak 5:8a0702aa91e3 191
kaizen8501 9:12ecb377f63f 192 return 0;
kaizen8501 9:12ecb377f63f 193 }
kaizen8501 9:12ecb377f63f 194
kaizen8501 9:12ecb377f63f 195 int WizFi250::cmdWSTATUS()
kaizen8501 9:12ecb377f63f 196 {
kaizen8501 9:12ecb377f63f 197 if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) )
kaizen8501 9:12ecb377f63f 198 {
kaizen8501 9:12ecb377f63f 199 WIZ_ERR("cmdWSTATUS");
kaizen8501 9:12ecb377f63f 200 return -1;
kaizen8501 9:12ecb377f63f 201 }
kaizen8501 9:12ecb377f63f 202
kaizen8501 9:12ecb377f63f 203 WIZ_INFO("IP : %s", _state.ip);
kaizen8501 9:12ecb377f63f 204 WIZ_INFO("Gateway : %s", _state.gateway);
kaizen 0:f2039204c8f6 205
kaizen 0:f2039204c8f6 206 return 0;
kaizen 0:f2039204c8f6 207 }
kaizen 0:f2039204c8f6 208
kaizen 0:f2039204c8f6 209 int WizFi250::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode)
kaizen 0:f2039204c8f6 210 {
kaizen 0:f2039204c8f6 211 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 212
kaizen 0:f2039204c8f6 213 sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode);
kaizen 0:f2039204c8f6 214 return sendCommand(cmd);
kaizen 0:f2039204c8f6 215 }
kaizen 0:f2039204c8f6 216
kaizen 0:f2039204c8f6 217 int WizFi250::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode)
kaizen 0:f2039204c8f6 218 {
kaizen 0:f2039204c8f6 219 int resp;
kaizen 0:f2039204c8f6 220 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 221
kaizen8501 9:12ecb377f63f 222 if(localPort == 0)
kaizen 0:f2039204c8f6 223 sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode);
kaizen 0:f2039204c8f6 224 else
kaizen 0:f2039204c8f6 225 sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode);
kaizen 0:f2039204c8f6 226
kaizen8501 9:12ecb377f63f 227 resp = sendCommand(cmd, RES_CONNECT, 15000 );
kaizen 0:f2039204c8f6 228
kaizen 0:f2039204c8f6 229 return resp;
kaizen 0:f2039204c8f6 230 }
kaizen 0:f2039204c8f6 231
kaizen 0:f2039204c8f6 232 int WizFi250::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout )
kaizen 0:f2039204c8f6 233 {
kaizen 0:f2039204c8f6 234 int i, resp;
kaizen 0:f2039204c8f6 235 Timer t;
kaizen 0:f2039204c8f6 236 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 237
kaizen 0:f2039204c8f6 238 if (lockUart(Timeout)) return -1;
kaizen 0:f2039204c8f6 239
kaizen 0:f2039204c8f6 240 clearFlags();
kaizen8501 9:12ecb377f63f 241 if(remoteIp == NULL)
kaizen 0:f2039204c8f6 242 {
kaizen8501 9:12ecb377f63f 243 sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize);
kaizen 0:f2039204c8f6 244 }
kaizen 0:f2039204c8f6 245 else
kaizen 0:f2039204c8f6 246 {
kaizen 0:f2039204c8f6 247 sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize);
kaizen 0:f2039204c8f6 248 }
kaizen 0:f2039204c8f6 249
kaizen 0:f2039204c8f6 250 _con[cid].send_length = sendSize;
kaizen 0:f2039204c8f6 251
kaizen 0:f2039204c8f6 252 resp = sendCommand(cmd, RES_SSEND, 2000, 1);
kaizen8501 9:12ecb377f63f 253
kaizen 0:f2039204c8f6 254 unlockUart();
kaizen 0:f2039204c8f6 255 if(resp){
DongEun Koak 5:8a0702aa91e3 256 WIZ_DBG("Fail cmdSSEND")
DongEun Koak 7:ba28fe711055 257 return -1;
kaizen 0:f2039204c8f6 258 }
kaizen 0:f2039204c8f6 259
kaizen 0:f2039204c8f6 260 for(i=0; i<sendSize; i++)
kaizen 0:f2039204c8f6 261 {
kaizen 0:f2039204c8f6 262 putUart(data[i]);
kaizen 0:f2039204c8f6 263 }
kaizen 0:f2039204c8f6 264 unlockUart();
kaizen 0:f2039204c8f6 265
kaizen 0:f2039204c8f6 266 if(Timeout)
kaizen 0:f2039204c8f6 267 {
kaizen 0:f2039204c8f6 268 t.start();
kaizen 0:f2039204c8f6 269 for(;;)
kaizen 0:f2039204c8f6 270 {
kaizen 0:f2039204c8f6 271 if (_state.ok) break;
kaizen 0:f2039204c8f6 272 if (_state.failure || t.read_ms() > Timeout)
kaizen 0:f2039204c8f6 273 {
DongEun Koak 5:8a0702aa91e3 274 WIZ_WARN("failure or timeout\r\n");
kaizen 0:f2039204c8f6 275 return -1;
kaizen 0:f2039204c8f6 276 }
kaizen 0:f2039204c8f6 277 }
kaizen 0:f2039204c8f6 278 t.stop();
kaizen 0:f2039204c8f6 279 }
kaizen 0:f2039204c8f6 280
kaizen 11:e52b91d1a5d5 281 wait(0.05);
kaizen 11:e52b91d1a5d5 282 WIZ_INFO("%s\r\n",data);
kaizen 0:f2039204c8f6 283
kaizen 0:f2039204c8f6 284 return i;
kaizen 0:f2039204c8f6 285 }
kaizen 0:f2039204c8f6 286
kaizen 0:f2039204c8f6 287
kaizen 0:f2039204c8f6 288 int WizFi250::cmdCLOSE ( int cid )
kaizen 0:f2039204c8f6 289 {
kaizen 0:f2039204c8f6 290 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 291
kaizen 0:f2039204c8f6 292 sprintf(cmd,"AT+SMGMT=%d",cid);
kaizen 0:f2039204c8f6 293 return sendCommand(cmd);
kaizen 0:f2039204c8f6 294 }
kaizen 0:f2039204c8f6 295
kaizen 0:f2039204c8f6 296
kaizen 0:f2039204c8f6 297 int WizFi250::cmdFDNS (const char *host)
kaizen 0:f2039204c8f6 298 {
kaizen 0:f2039204c8f6 299 char cmd[CFG_CMD_SIZE];
kaizen 0:f2039204c8f6 300 int resp;
kaizen 0:f2039204c8f6 301
kaizen 0:f2039204c8f6 302 sprintf(cmd,"AT+FDNS=%s,1000",host);
DongEun Koak 7:ba28fe711055 303 resp = sendCommand(cmd, RES_FDNS);
kaizen 0:f2039204c8f6 304
DongEun Koak 5:8a0702aa91e3 305 WIZ_DBG("%s",_state.resolv);
kaizen 0:f2039204c8f6 306 return resp;
kaizen 0:f2039204c8f6 307 }
kaizen 0:f2039204c8f6 308
kaizen 0:f2039204c8f6 309 int WizFi250::cmdSMGMT ( int cid )
kaizen 0:f2039204c8f6 310 {
kaizen 0:f2039204c8f6 311 int resp;
kaizen 0:f2039204c8f6 312
kaizen 0:f2039204c8f6 313 resp = sendCommand("AT+SMGMT=?", RES_SMGMT);
kaizen 0:f2039204c8f6 314 return resp;
kaizen 0:f2039204c8f6 315 }