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