Driver for the ESP8266 WiFi module using ATParser library. Espressif Firmware.

Dependencies:   ATParser

Dependents:   ESP8266Interface

Fork of ESP8266 by NetworkSocketAPI

Note

This library assumes your ESP8266 is running the Espressif Firmware. For instructions on how to update your ESP8266 to use the correct firmware see the Firmware Update Wiki Page.

Committer:
sam_grove
Date:
Mon Dec 28 05:38:00 2015 +0000
Revision:
14:13cef05d740c
Parent:
13:4c014a7f0c63
Child:
15:640a262a93e1
expose debug through the constructor

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 {
sarahmarshy 8:80048194de79 85 return (atParser.send("AT+CIPSTA?") && atParser.recv("+CIPSTA:\"%[^\"]\"", ip));
sarahmarshy 8:80048194de79 86 }
sam_grove 9:dcf3aa250bc1 87
sam_grove 9:dcf3aa250bc1 88 bool ESP8266::isConnected(void)
sarahmarshy 8:80048194de79 89 {
sam_grove 9:dcf3aa250bc1 90 char* ip = "";
sarahmarshy 8:80048194de79 91 return getIPAddress(ip);
sarahmarshy 8:80048194de79 92 }
sam_grove 9:dcf3aa250bc1 93
sam_grove 9:dcf3aa250bc1 94 bool ESP8266::openSocket(string sockType, int id, int port, const char* addr)
sarahmarshy 8:80048194de79 95 {
sarahmarshy 8:80048194de79 96 //IDs only 0-4
sam_grove 9:dcf3aa250bc1 97 if(id > 4) {
sarahmarshy 8:80048194de79 98 return false;
sam_grove 9:dcf3aa250bc1 99 }
sarahmarshy 8:80048194de79 100 char portstr[5];
sarahmarshy 8:80048194de79 101 char idstr[2];
sarahmarshy 8:80048194de79 102 sprintf(idstr,"%d",id);
sarahmarshy 8:80048194de79 103 sprintf(portstr, "%d", port);
sarahmarshy 8:80048194de79 104
sarahmarshy 8:80048194de79 105 string start_command = "AT+CIPSTART="+(string)idstr+",\""+sockType+"\",\""+(string)addr+"\","+(string)portstr;
sam_grove 9:dcf3aa250bc1 106 if (!(atParser.send(start_command.c_str()) && atParser.recv("OK"))) {
sarahmarshy 8:80048194de79 107 return false;//opening socket not succesful
sarahmarshy 8:80048194de79 108 }
sarahmarshy 8:80048194de79 109 return true;
sarahmarshy 8:80048194de79 110 }
sarahmarshy 8:80048194de79 111
sarahmarshy 8:80048194de79 112 bool ESP8266::sendData(int id, const void *data, uint32_t amount)
sarahmarshy 8:80048194de79 113 {
sarahmarshy 8:80048194de79 114 char idstr[2];
sarahmarshy 8:80048194de79 115 sprintf(idstr,"%d",id);
sarahmarshy 8:80048194de79 116 char lenstr[5];
sarahmarshy 8:80048194de79 117 sprintf(lenstr,"%d",(int)amount);
sam_grove 9:dcf3aa250bc1 118
sarahmarshy 8:80048194de79 119 string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr;
sarahmarshy 13:4c014a7f0c63 120 if(!atParser.send(send_command.c_str(),">")) {
sarahmarshy 8:80048194de79 121 return false;
sam_grove 9:dcf3aa250bc1 122 }
sarahmarshy 13:4c014a7f0c63 123 wait_ms(10);
sarahmarshy 8:80048194de79 124 atParser.write((char*)data, (int)amount);
sarahmarshy 13:4c014a7f0c63 125 wait_ms(10);
sarahmarshy 8:80048194de79 126 return true;
sarahmarshy 8:80048194de79 127 }
sarahmarshy 8:80048194de79 128
sarahmarshy 8:80048194de79 129 uint32_t ESP8266::recv(void *data, uint32_t amount)
sarahmarshy 8:80048194de79 130 {
sarahmarshy 8:80048194de79 131 int length;
sarahmarshy 8:80048194de79 132 int id;
sam_grove 9:dcf3aa250bc1 133 if (!(atParser.recv("+IPD,%d,%d:", &id, &length) && atParser.read((char*)data, length) && atParser.recv("OK"))) {
sarahmarshy 8:80048194de79 134 return 0;
sarahmarshy 8:80048194de79 135 }
sam_grove 9:dcf3aa250bc1 136 return length;
sarahmarshy 8:80048194de79 137 }
sam_grove 9:dcf3aa250bc1 138
sam_grove 9:dcf3aa250bc1 139 bool ESP8266::close(int id)
sarahmarshy 8:80048194de79 140 {
sarahmarshy 8:80048194de79 141 //IDs only 0-4
sam_grove 9:dcf3aa250bc1 142 if(id > 4) {
sarahmarshy 8:80048194de79 143 return false;
sam_grove 9:dcf3aa250bc1 144 }
sarahmarshy 8:80048194de79 145 char idstr[2];
sarahmarshy 8:80048194de79 146 sprintf(idstr,"%d",id);
sam_grove 9:dcf3aa250bc1 147 string close_command = "AT+CIPCLOSE="+(string)idstr;
sam_grove 9:dcf3aa250bc1 148
sarahmarshy 8:80048194de79 149 return (atParser.send(close_command.c_str()) && atParser.recv("OK"));
sarahmarshy 8:80048194de79 150 }
sam_grove 9:dcf3aa250bc1 151
sarahmarshy 8:80048194de79 152 void ESP8266::setTimeout(uint32_t timeout_ms)
sarahmarshy 8:80048194de79 153 {
sarahmarshy 8:80048194de79 154 atParser.setTimeout(timeout_ms);
sam_grove 9:dcf3aa250bc1 155 }