modified by ohneta

Dependencies:   ATParser

Dependents:   ESP8266Interface

Fork of ESP8266 by NetworkSocketAPI

Committer:
sarahmarshy
Date:
Wed Aug 05 21:57:41 2015 +0000
Revision:
13:4c014a7f0c63
Parent:
12:2c5afb36bc8c
Child:
14:71474261a9d7
Waits added for send command, and actual data sent

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 8:80048194de79 1 /* ESP8266 Example
sarahmarshy 8:80048194de79 2 * Copyright (c) 2015 ARM Limited
sarahmarshy 8:80048194de79 3 *
sarahmarshy 8:80048194de79 4 * Licensed under the Apache License, Version 2.0 (the "License");
sarahmarshy 8:80048194de79 5 * you may not use this file except in compliance with the License.
sarahmarshy 8:80048194de79 6 * You may obtain a copy of the License at
sarahmarshy 8:80048194de79 7 *
sarahmarshy 8:80048194de79 8 * http://www.apache.org/licenses/LICENSE-2.0
sarahmarshy 8:80048194de79 9 *
sarahmarshy 8:80048194de79 10 * Unless required by applicable law or agreed to in writing, software
sarahmarshy 8:80048194de79 11 * distributed under the License is distributed on an "AS IS" BASIS,
sarahmarshy 8:80048194de79 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sarahmarshy 8:80048194de79 13 * See the License for the specific language governing permissions and
sarahmarshy 8:80048194de79 14 * limitations under the License.
sarahmarshy 8:80048194de79 15 */
sam_grove 9:dcf3aa250bc1 16
sarahmarshy 8:80048194de79 17 #include "ESP8266.h"
sarahmarshy 8:80048194de79 18
sam_grove 9:dcf3aa250bc1 19 ESP8266::ESP8266(PinName tx, PinName rx) : serial(tx, rx), atParser(serial)
sarahmarshy 8:80048194de79 20 {
sarahmarshy 8:80048194de79 21 serial.baud(115200);
sarahmarshy 8:80048194de79 22 }
sarahmarshy 8:80048194de79 23
sarahmarshy 8:80048194de79 24 bool ESP8266::startup(void)
sarahmarshy 8:80048194de79 25 {
sarahmarshy 8:80048194de79 26 return (atParser.send("AT") && atParser.recv("OK"));
sarahmarshy 8:80048194de79 27 }
sam_grove 9:dcf3aa250bc1 28
sarahmarshy 8:80048194de79 29 bool ESP8266::reset(void)
sarahmarshy 8:80048194de79 30 {
sarahmarshy 8:80048194de79 31 return (atParser.send("AT+RST") && atParser.recv("OK\r\nready"));
sarahmarshy 8:80048194de79 32 }
sam_grove 9:dcf3aa250bc1 33
sarahmarshy 8:80048194de79 34 bool ESP8266::wifiMode(int mode)
sarahmarshy 8:80048194de79 35 {
sarahmarshy 8:80048194de79 36 //only 3 valid modes
sam_grove 9:dcf3aa250bc1 37 if(mode < 1 || mode > 3) {
sarahmarshy 8:80048194de79 38 return false;
sam_grove 9:dcf3aa250bc1 39 }
sam_grove 9:dcf3aa250bc1 40
sarahmarshy 8:80048194de79 41 char modestr[1];
sarahmarshy 8:80048194de79 42 sprintf(modestr,"%d",mode);
sarahmarshy 8:80048194de79 43 string mode_command = "AT+CWMODE="+string(modestr);
sam_grove 9:dcf3aa250bc1 44 return (atParser.send(mode_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 45 }
sam_grove 9:dcf3aa250bc1 46
sarahmarshy 8:80048194de79 47 bool ESP8266::multipleConnections(bool enabled)
sarahmarshy 8:80048194de79 48 {
sarahmarshy 8:80048194de79 49 int on = (int)enabled;
sarahmarshy 8:80048194de79 50 char enable[1];
sarahmarshy 8:80048194de79 51 sprintf(enable,"%d",on);
sarahmarshy 8:80048194de79 52 string mux_command = "AT+CIPMUX="+string(enable);
sam_grove 9:dcf3aa250bc1 53 return (atParser.send(mux_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 54 }
sam_grove 9:dcf3aa250bc1 55
sarahmarshy 8:80048194de79 56 bool ESP8266::dhcp(int mode, bool enabled)
sarahmarshy 8:80048194de79 57 {
sarahmarshy 8:80048194de79 58 //only 3 valid modes
sam_grove 9:dcf3aa250bc1 59 if(mode < 0 || mode > 2) {
sarahmarshy 8:80048194de79 60 return false;
sam_grove 9:dcf3aa250bc1 61 }
sarahmarshy 8:80048194de79 62 int on = (int)enabled;
sarahmarshy 8:80048194de79 63 char enable[1];
sarahmarshy 8:80048194de79 64 sprintf(enable,"%d",on);
sarahmarshy 8:80048194de79 65 char modestr[1];
sarahmarshy 8:80048194de79 66 sprintf(modestr,"%d",mode);
sarahmarshy 8:80048194de79 67 string mode_command = "AT+CWDHCP="+string(modestr)+","+string(enable);
sarahmarshy 8:80048194de79 68 return (atParser.send(mode_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 69 }
sarahmarshy 8:80048194de79 70
sarahmarshy 8:80048194de79 71 bool ESP8266::connect(const char *ap, const char *passPhrase)
sarahmarshy 8:80048194de79 72 {
sarahmarshy 8:80048194de79 73 string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)passPhrase+"\"";
sam_grove 9:dcf3aa250bc1 74 return (atParser.send(connect_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 75 }
sarahmarshy 8:80048194de79 76
sarahmarshy 8:80048194de79 77 bool ESP8266::disconnect(void)
sarahmarshy 8:80048194de79 78 {
sarahmarshy 8:80048194de79 79 return (atParser.send("AT+CWQAP") && atParser.recv("OK"));
sarahmarshy 8:80048194de79 80 }
sarahmarshy 8:80048194de79 81
sarahmarshy 8:80048194de79 82 bool ESP8266::getIPAddress(char* ip)
sarahmarshy 8:80048194de79 83 {
sarahmarshy 8:80048194de79 84 return (atParser.send("AT+CIPSTA?") && atParser.recv("+CIPSTA:\"%[^\"]\"", ip));
sarahmarshy 8:80048194de79 85 }
sam_grove 9:dcf3aa250bc1 86
sam_grove 9:dcf3aa250bc1 87 bool ESP8266::isConnected(void)
sarahmarshy 8:80048194de79 88 {
sam_grove 9:dcf3aa250bc1 89 char* ip = "";
sarahmarshy 8:80048194de79 90 return getIPAddress(ip);
sarahmarshy 8:80048194de79 91 }
sam_grove 9:dcf3aa250bc1 92
sam_grove 9:dcf3aa250bc1 93 bool ESP8266::openSocket(string sockType, int id, int port, const char* addr)
sarahmarshy 8:80048194de79 94 {
sarahmarshy 8:80048194de79 95 //IDs only 0-4
sam_grove 9:dcf3aa250bc1 96 if(id > 4) {
sarahmarshy 8:80048194de79 97 return false;
sam_grove 9:dcf3aa250bc1 98 }
sarahmarshy 8:80048194de79 99 char portstr[5];
sarahmarshy 8:80048194de79 100 char idstr[2];
sarahmarshy 8:80048194de79 101 sprintf(idstr,"%d",id);
sarahmarshy 8:80048194de79 102 sprintf(portstr, "%d", port);
sarahmarshy 8:80048194de79 103
sarahmarshy 8:80048194de79 104 string start_command = "AT+CIPSTART="+(string)idstr+",\""+sockType+"\",\""+(string)addr+"\","+(string)portstr;
sam_grove 9:dcf3aa250bc1 105 if (!(atParser.send(start_command.c_str()) && atParser.recv("OK"))) {
sarahmarshy 8:80048194de79 106 return false;//opening socket not succesful
sarahmarshy 8:80048194de79 107 }
sarahmarshy 8:80048194de79 108 return true;
sarahmarshy 8:80048194de79 109 }
sarahmarshy 8:80048194de79 110
sarahmarshy 8:80048194de79 111 bool ESP8266::sendData(int id, const void *data, uint32_t amount)
sarahmarshy 8:80048194de79 112 {
sarahmarshy 8:80048194de79 113 char idstr[2];
sarahmarshy 8:80048194de79 114 sprintf(idstr,"%d",id);
sarahmarshy 8:80048194de79 115 char lenstr[5];
sarahmarshy 8:80048194de79 116 sprintf(lenstr,"%d",(int)amount);
sam_grove 9:dcf3aa250bc1 117
sarahmarshy 8:80048194de79 118 string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr;
sarahmarshy 13:4c014a7f0c63 119 if(!atParser.send(send_command.c_str(),">")) {
sarahmarshy 8:80048194de79 120 return false;
sam_grove 9:dcf3aa250bc1 121 }
sarahmarshy 13:4c014a7f0c63 122 wait_ms(10);
sarahmarshy 8:80048194de79 123 atParser.write((char*)data, (int)amount);
sarahmarshy 13:4c014a7f0c63 124 wait_ms(10);
sarahmarshy 8:80048194de79 125 return true;
sarahmarshy 8:80048194de79 126 }
sarahmarshy 8:80048194de79 127
sarahmarshy 8:80048194de79 128 uint32_t ESP8266::recv(void *data, uint32_t amount)
sarahmarshy 8:80048194de79 129 {
sarahmarshy 8:80048194de79 130 int length;
sarahmarshy 8:80048194de79 131 int id;
sam_grove 9:dcf3aa250bc1 132 if (!(atParser.recv("+IPD,%d,%d:", &id, &length) && atParser.read((char*)data, length) && atParser.recv("OK"))) {
sarahmarshy 8:80048194de79 133 return 0;
sarahmarshy 8:80048194de79 134 }
sam_grove 9:dcf3aa250bc1 135 return length;
sarahmarshy 8:80048194de79 136 }
sam_grove 9:dcf3aa250bc1 137
sam_grove 9:dcf3aa250bc1 138 bool ESP8266::close(int id)
sarahmarshy 8:80048194de79 139 {
sarahmarshy 8:80048194de79 140 //IDs only 0-4
sam_grove 9:dcf3aa250bc1 141 if(id > 4) {
sarahmarshy 8:80048194de79 142 return false;
sam_grove 9:dcf3aa250bc1 143 }
sarahmarshy 8:80048194de79 144 char idstr[2];
sarahmarshy 8:80048194de79 145 sprintf(idstr,"%d",id);
sam_grove 9:dcf3aa250bc1 146 string close_command = "AT+CIPCLOSE="+(string)idstr;
sam_grove 9:dcf3aa250bc1 147
sarahmarshy 8:80048194de79 148 return (atParser.send(close_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 149 }
sam_grove 9:dcf3aa250bc1 150
sarahmarshy 8:80048194de79 151 void ESP8266::setTimeout(uint32_t timeout_ms)
sarahmarshy 8:80048194de79 152 {
sarahmarshy 8:80048194de79 153 atParser.setTimeout(timeout_ms);
sam_grove 9:dcf3aa250bc1 154 }