test

Dependencies:   ATParser

Fork of ESP8266 by NetworkSocketAPI

Committer:
geky
Date:
Mon Feb 01 22:17:08 2016 +0000
Revision:
15:640a262a93e1
Parent:
14:13cef05d740c
Child:
16:f886ef3f2297
Updated IP AT command for new firmware

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 14:13cef05d740c 19 ESP8266::ESP8266(PinName tx, PinName rx, uint8_t debug) : serial(tx, rx), atParser(serial)
sarahmarshy 8:80048194de79 20 {
sarahmarshy 8:80048194de79 21 serial.baud(115200);
sam_grove 14:13cef05d740c 22 atParser.debugOn(debug);
sarahmarshy 8:80048194de79 23 }
sarahmarshy 8:80048194de79 24
sarahmarshy 8:80048194de79 25 bool ESP8266::startup(void)
sarahmarshy 8:80048194de79 26 {
sarahmarshy 8:80048194de79 27 return (atParser.send("AT") && atParser.recv("OK"));
sarahmarshy 8:80048194de79 28 }
sam_grove 9:dcf3aa250bc1 29
sarahmarshy 8:80048194de79 30 bool ESP8266::reset(void)
sarahmarshy 8:80048194de79 31 {
sarahmarshy 8:80048194de79 32 return (atParser.send("AT+RST") && atParser.recv("OK\r\nready"));
sarahmarshy 8:80048194de79 33 }
sam_grove 9:dcf3aa250bc1 34
sarahmarshy 8:80048194de79 35 bool ESP8266::wifiMode(int mode)
sarahmarshy 8:80048194de79 36 {
sarahmarshy 8:80048194de79 37 //only 3 valid modes
sam_grove 9:dcf3aa250bc1 38 if(mode < 1 || mode > 3) {
sarahmarshy 8:80048194de79 39 return false;
sam_grove 9:dcf3aa250bc1 40 }
sam_grove 9:dcf3aa250bc1 41
sarahmarshy 8:80048194de79 42 char modestr[1];
sarahmarshy 8:80048194de79 43 sprintf(modestr,"%d",mode);
sarahmarshy 8:80048194de79 44 string mode_command = "AT+CWMODE="+string(modestr);
sam_grove 9:dcf3aa250bc1 45 return (atParser.send(mode_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 46 }
sam_grove 9:dcf3aa250bc1 47
sarahmarshy 8:80048194de79 48 bool ESP8266::multipleConnections(bool enabled)
sarahmarshy 8:80048194de79 49 {
sarahmarshy 8:80048194de79 50 int on = (int)enabled;
sarahmarshy 8:80048194de79 51 char enable[1];
sarahmarshy 8:80048194de79 52 sprintf(enable,"%d",on);
sarahmarshy 8:80048194de79 53 string mux_command = "AT+CIPMUX="+string(enable);
sam_grove 9:dcf3aa250bc1 54 return (atParser.send(mux_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 55 }
sam_grove 9:dcf3aa250bc1 56
sarahmarshy 8:80048194de79 57 bool ESP8266::dhcp(int mode, bool enabled)
sarahmarshy 8:80048194de79 58 {
sarahmarshy 8:80048194de79 59 //only 3 valid modes
sam_grove 9:dcf3aa250bc1 60 if(mode < 0 || mode > 2) {
sarahmarshy 8:80048194de79 61 return false;
sam_grove 9:dcf3aa250bc1 62 }
sarahmarshy 8:80048194de79 63 int on = (int)enabled;
sarahmarshy 8:80048194de79 64 char enable[1];
sarahmarshy 8:80048194de79 65 sprintf(enable,"%d",on);
sarahmarshy 8:80048194de79 66 char modestr[1];
sarahmarshy 8:80048194de79 67 sprintf(modestr,"%d",mode);
sarahmarshy 8:80048194de79 68 string mode_command = "AT+CWDHCP="+string(modestr)+","+string(enable);
sarahmarshy 8:80048194de79 69 return (atParser.send(mode_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 70 }
sarahmarshy 8:80048194de79 71
sarahmarshy 8:80048194de79 72 bool ESP8266::connect(const char *ap, const char *passPhrase)
sarahmarshy 8:80048194de79 73 {
sarahmarshy 8:80048194de79 74 string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)passPhrase+"\"";
sam_grove 9:dcf3aa250bc1 75 return (atParser.send(connect_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 76 }
sarahmarshy 8:80048194de79 77
sarahmarshy 8:80048194de79 78 bool ESP8266::disconnect(void)
sarahmarshy 8:80048194de79 79 {
sarahmarshy 8:80048194de79 80 return (atParser.send("AT+CWQAP") && atParser.recv("OK"));
sarahmarshy 8:80048194de79 81 }
sarahmarshy 8:80048194de79 82
sarahmarshy 8:80048194de79 83 bool ESP8266::getIPAddress(char* ip)
sarahmarshy 8:80048194de79 84 {
geky 15:640a262a93e1 85 return (atParser.send("AT+CIPSTA?")
geky 15:640a262a93e1 86 && atParser.recv("+CIPSTA:ip:\"%[^\"]\"", ip)
geky 15:640a262a93e1 87 && atParser.recv("OK"));
sarahmarshy 8:80048194de79 88 }
sam_grove 9:dcf3aa250bc1 89
sam_grove 9:dcf3aa250bc1 90 bool ESP8266::isConnected(void)
sarahmarshy 8:80048194de79 91 {
sam_grove 9:dcf3aa250bc1 92 char* ip = "";
sarahmarshy 8:80048194de79 93 return getIPAddress(ip);
sarahmarshy 8:80048194de79 94 }
sam_grove 9:dcf3aa250bc1 95
sam_grove 9:dcf3aa250bc1 96 bool ESP8266::openSocket(string sockType, int id, int port, const char* addr)
sarahmarshy 8:80048194de79 97 {
sarahmarshy 8:80048194de79 98 //IDs only 0-4
sam_grove 9:dcf3aa250bc1 99 if(id > 4) {
sarahmarshy 8:80048194de79 100 return false;
sam_grove 9:dcf3aa250bc1 101 }
sarahmarshy 8:80048194de79 102 char portstr[5];
sarahmarshy 8:80048194de79 103 char idstr[2];
sarahmarshy 8:80048194de79 104 sprintf(idstr,"%d",id);
sarahmarshy 8:80048194de79 105 sprintf(portstr, "%d", port);
sarahmarshy 8:80048194de79 106
sarahmarshy 8:80048194de79 107 string start_command = "AT+CIPSTART="+(string)idstr+",\""+sockType+"\",\""+(string)addr+"\","+(string)portstr;
sam_grove 9:dcf3aa250bc1 108 if (!(atParser.send(start_command.c_str()) && atParser.recv("OK"))) {
sarahmarshy 8:80048194de79 109 return false;//opening socket not succesful
sarahmarshy 8:80048194de79 110 }
sarahmarshy 8:80048194de79 111 return true;
sarahmarshy 8:80048194de79 112 }
sarahmarshy 8:80048194de79 113
sarahmarshy 8:80048194de79 114 bool ESP8266::sendData(int id, const void *data, uint32_t amount)
sarahmarshy 8:80048194de79 115 {
sarahmarshy 8:80048194de79 116 char idstr[2];
sarahmarshy 8:80048194de79 117 sprintf(idstr,"%d",id);
sarahmarshy 8:80048194de79 118 char lenstr[5];
sarahmarshy 8:80048194de79 119 sprintf(lenstr,"%d",(int)amount);
sam_grove 9:dcf3aa250bc1 120
sarahmarshy 8:80048194de79 121 string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr;
sarahmarshy 13:4c014a7f0c63 122 if(!atParser.send(send_command.c_str(),">")) {
sarahmarshy 8:80048194de79 123 return false;
sam_grove 9:dcf3aa250bc1 124 }
sarahmarshy 13:4c014a7f0c63 125 wait_ms(10);
sarahmarshy 8:80048194de79 126 atParser.write((char*)data, (int)amount);
sarahmarshy 13:4c014a7f0c63 127 wait_ms(10);
sarahmarshy 8:80048194de79 128 return true;
sarahmarshy 8:80048194de79 129 }
sarahmarshy 8:80048194de79 130
sarahmarshy 8:80048194de79 131 uint32_t ESP8266::recv(void *data, uint32_t amount)
sarahmarshy 8:80048194de79 132 {
sarahmarshy 8:80048194de79 133 int length;
sarahmarshy 8:80048194de79 134 int id;
sam_grove 9:dcf3aa250bc1 135 if (!(atParser.recv("+IPD,%d,%d:", &id, &length) && atParser.read((char*)data, length) && atParser.recv("OK"))) {
sarahmarshy 8:80048194de79 136 return 0;
sarahmarshy 8:80048194de79 137 }
sam_grove 9:dcf3aa250bc1 138 return length;
sarahmarshy 8:80048194de79 139 }
sam_grove 9:dcf3aa250bc1 140
sam_grove 9:dcf3aa250bc1 141 bool ESP8266::close(int id)
sarahmarshy 8:80048194de79 142 {
sarahmarshy 8:80048194de79 143 //IDs only 0-4
sam_grove 9:dcf3aa250bc1 144 if(id > 4) {
sarahmarshy 8:80048194de79 145 return false;
sam_grove 9:dcf3aa250bc1 146 }
sarahmarshy 8:80048194de79 147 char idstr[2];
sarahmarshy 8:80048194de79 148 sprintf(idstr,"%d",id);
sam_grove 9:dcf3aa250bc1 149 string close_command = "AT+CIPCLOSE="+(string)idstr;
sam_grove 9:dcf3aa250bc1 150
sarahmarshy 8:80048194de79 151 return (atParser.send(close_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 152 }
sam_grove 9:dcf3aa250bc1 153
sarahmarshy 8:80048194de79 154 void ESP8266::setTimeout(uint32_t timeout_ms)
sarahmarshy 8:80048194de79 155 {
sarahmarshy 8:80048194de79 156 atParser.setTimeout(timeout_ms);
sam_grove 9:dcf3aa250bc1 157 }